-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuri_test.go
65 lines (52 loc) · 1.42 KB
/
uri_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package claxon
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type StringID string
func (x StringID) MarshalJSON() ([]byte, error) {
return MarshalURN(string(x))
}
type CustomID StringID
func (x CustomID) MarshalJSON() ([]byte, error) {
return MarshalURN(string(x))
}
type SampleDocument struct {
Id StringID `json:"id"`
Parent CustomID `json:"parent"`
}
func TestURIMarshal(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
s1 := SampleDocument{
Id: "urn:foobar:abc",
Parent: "urn:parent:xyz",
}
j1, err := json.Marshal(s1)
require.NoError(err)
assert.Equal(`{"id":"urn:foobar:abc","parent":"urn:parent:xyz"}`, string(j1))
UsingMap(map[string]string{
"urn:foobar:": "https://example.com/account/",
}, func() {
j1, err = json.Marshal(s1)
})
require.NoError(err)
assert.Equal(`{"id":"https://example.com/account/abc","parent":"urn:parent:xyz"}`, string(j1))
UsingMap(map[string]string{
"urn:parent:": "/link/",
}, func() {
j1, err = json.Marshal(s1)
})
require.NoError(err)
assert.Equal(`{"id":"urn:foobar:abc","parent":"/link/xyz"}`, string(j1))
UsingMap(map[string]string{
"urn:foobar:": "https://example.com/account/",
"urn:parent:": "/link/",
}, func() {
j1, err = json.Marshal(s1)
})
require.NoError(err)
assert.Equal(`{"id":"https://example.com/account/abc","parent":"/link/xyz"}`, string(j1))
}