-
Notifications
You must be signed in to change notification settings - Fork 13
/
structure_test.go
103 lines (94 loc) · 2.05 KB
/
structure_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2017 Steven Roose <[email protected]>.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gonfig
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestOptionFromField(t *testing.T) {
testCases := []struct {
desc string
fieldName string
fieldTag string
parent option
expected option
}{
{
"empty",
"Empty",
``,
option{},
option{
fullIDParts: []string{"empty"},
defaultSet: false,
isParent: false,
id: "empty",
short: "",
defaul: "",
desc: "",
},
},
{
"normal",
"name",
`id:"realname" short:"s" default:"defaultvalue" desc:"testing.."`,
option{},
option{
fullIDParts: []string{"realname"},
defaultSet: true,
isParent: false,
id: "realname",
short: "s",
defaul: "defaultvalue",
desc: "testing..",
},
},
{
"with parent",
"child",
`short:"S" default:"defaultvalue" desc:"testing.."`,
option{
isParent: true,
fullIDParts: []string{"mother", "father"},
id: "father",
},
option{
fullIDParts: []string{"mother", "father", "child"},
defaultSet: true,
isParent: false,
id: "child",
short: "S",
defaul: "defaultvalue",
desc: "testing..",
},
},
{
"with option",
"name",
`id:"realname" opts:"hidden" short:"s" default:"defaultvalue" desc:"testing.."`,
option{},
option{
fullIDParts: []string{"realname"},
defaultSet: true,
isParent: false,
id: "realname",
short: "s",
defaul: "defaultvalue",
desc: "testing..",
opts: []string{"hidden"},
},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
field := reflect.StructField{
Name: tc.fieldName,
Tag: reflect.StructTag(tc.fieldTag),
}
result := optionFromField(field, &tc.parent)
assert.Equal(t, &tc.expected, result)
})
}
}