forked from facebookarchive/structtag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structtag_test.go
78 lines (71 loc) · 1.33 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package structtag_test
import (
"testing"
"github.com/bemobi/structtag"
)
func TestExtract(t *testing.T) {
cases := []struct {
Name string // Input name
Tag string // Input tag
Found bool // Expected found status
Value string // Expected value
Error bool // Indicates if an error is expected
}{
{
Name: "inject",
Tag: `inject:`,
Error: true,
},
{
Name: "inject",
Tag: `inject:"`,
Error: true,
},
{
Name: "inject",
Tag: `inject:""`,
Found: true,
},
{
Name: "inject",
Tag: `inject:"a"`,
Found: true,
Value: "a",
},
{
Name: "inject",
Tag: ` inject:"a"`,
Found: true,
Value: "a",
},
{
Name: "inject",
Tag: ` `,
},
{
Name: "inject",
Tag: `inject:"\"a"`,
Found: true,
Value: `"a`,
},
}
for _, e := range cases {
found, value, err := structtag.Extract(e.Name, e.Tag)
if !e.Error && err != nil {
t.Fatalf("unexpected error %s for case %+v", err, e)
}
if e.Error && err == nil {
t.Fatalf("did not get expected error for case %+v", e)
}
if found != e.Found {
if e.Found {
t.Fatalf("did not find value when expecting to %+v", e)
} else {
t.Fatalf("found value when not expecting to %+v", e)
}
}
if value != e.Value {
t.Fatalf(`found unexpected value "%s" for %+v`, value, e)
}
}
}