-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructtag_test.go
63 lines (56 loc) · 1.66 KB
/
structtag_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
package structtag_test
import (
"testing"
"github.com/execjosh/structtag"
"github.com/stretchr/testify/assert"
)
func TestKeys(t *testing.T) {
type kvs map[string]string
testcases := map[string]struct {
orig kvs
want []string
}{
"empty": {kvs{}, []string{}},
"one pair": {kvs{"json": "-"}, []string{"json"}},
"two pair": {kvs{"valid": "-", "json": "-"}, []string{"json", "valid"}},
"ordering": {kvs{"valid": "-", "json": "-", "xxx": "x", "abc": "a"}, []string{"abc", "json", "valid", "xxx"}},
}
for _, tc := range testcases {
tag := structtag.FromMap(tc.orig)
assert.Equal(t, tc.want, tag.Keys())
}
}
func TestString(t *testing.T) {
type kvs map[string]string
testcases := map[string]struct {
orig kvs
want string
}{
"empty": {kvs{}, "``"},
"one pair": {kvs{"json": "-"}, "`json:\"-\"`"},
"two pair": {kvs{"valid": "-", "json": "-"}, "`json:\"-\" valid:\"-\"`"},
"ordering": {kvs{"valid": "-", "json": "-", "xxx": "x", "abc": "a"}, "`abc:\"a\" json:\"-\" valid:\"-\" xxx:\"x\"`"},
}
for _, tc := range testcases {
tag := structtag.FromMap(tc.orig)
assert.Equal(t, tc.want, tag.String())
}
}
func TestUnion(t *testing.T) {
testcases := map[string]struct {
a string
b string
want string
}{
"second tag empty": {`json:"-"`, "", "`json:\"-\"`"},
"first tag empty": {"", `json:"json"`, "`json:\"json\"`"},
"duplicate keys, second wins": {`json:"-"`, `json:"json"`, "`json:\"json\"`"},
}
for _, tc := range testcases {
a, err := structtag.Parse(tc.a)
assert.NoError(t, err)
b, err := structtag.Parse(tc.b)
assert.NoError(t, err)
assert.Equal(t, tc.want, structtag.Union(a, b).String())
}
}