-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfield_describer_test.go
125 lines (96 loc) · 2.97 KB
/
field_describer_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package fangs
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_fieldDescriber(t *testing.T) {
f1 := &fdTest1{
Ptr: &fdTest3{},
}
d := NewFieldDescriber(f1)
require.Equal(t, 1, f1.called)
require.Equal(t, 1, f1.FdTest2.called)
require.Equal(t, 1, f1.Ptr.called)
dd := d.(*directDescriber)
var values []string
for _, d := range dd.flagRefs {
values = append(values, d.Usage)
}
require.Contains(t, values, "string description")
require.Contains(t, values, "int description")
require.Contains(t, values, "fd test 3 value description")
}
func Test_FieldDescriberDoesNotPanicOnEmbeddedUnexportedStruct(t *testing.T) {
type moduleConfig struct {
ModuleBool bool `yaml:"module-bool" mapstructure:"module-bool"`
}
type specialModuleConfig struct {
moduleConfig `yaml:",inline" mapstructure:",squash"`
SpecialModuleBool bool `yaml:"special-module-bool" mapstructure:"special-module-bool"`
}
type TopLevelConfig struct {
Module1 moduleConfig `yaml:"module-1" mapstructure:"module-1"`
Module2 specialModuleConfig `yaml:"module-2" mapstructure:"module-2"`
}
cfgPtr := &TopLevelConfig{}
_ = NewFieldDescriber(cfgPtr)
}
func Test_FieldDescriberDoesNotPanicOnEmbeddedExportedStructPointer(t *testing.T) {
type ModuleConfig struct {
ModuleBool bool `yaml:"module-bool" mapstructure:"module-bool"`
}
type specialModuleConfig struct {
*ModuleConfig `yaml:",inline" mapstructure:",squash"`
SpecialModuleBool bool `yaml:"special-module-bool" mapstructure:"special-module-bool"`
}
type TopLevelConfig struct {
Module1 ModuleConfig `yaml:"module-1" mapstructure:"module-1"`
Module2 specialModuleConfig `yaml:"module-2" mapstructure:"module-2"`
}
cfgPtr := &TopLevelConfig{}
_ = NewFieldDescriber(cfgPtr)
}
func Test_FieldDescriberDoesNotPanicOnEmbeddedUnexportedStructPointer(t *testing.T) {
type moduleConfig struct {
ModuleBool bool `yaml:"module-bool" mapstructure:"module-bool"`
}
type specialModuleConfig struct {
*moduleConfig `yaml:",inline" mapstructure:",squash"`
SpecialModuleBool bool `yaml:"special-module-bool" mapstructure:"special-module-bool"`
}
type TopLevelConfig struct {
Module1 moduleConfig `yaml:"module-1" mapstructure:"module-1"`
Module2 specialModuleConfig `yaml:"module-2" mapstructure:"module-2"`
}
cfgPtr := &TopLevelConfig{}
_ = NewFieldDescriber(cfgPtr)
}
type fdTest1 struct {
called int
String string
FdTest2 fdTest2
Ptr *fdTest3
}
func (f *fdTest1) DescribeFields(d FieldDescriptionSet) {
f.called++
d.Add(&f.String, "string description")
}
var _ FieldDescriber = &fdTest1{}
type fdTest2 struct {
called int
Int int
}
func (f *fdTest2) DescribeFields(d FieldDescriptionSet) {
f.called++
d.Add(&f.Int, "int description")
}
var _ FieldDescriber = &fdTest2{}
type fdTest3 struct {
called int
Value string
}
func (f *fdTest3) DescribeFields(d FieldDescriptionSet) {
f.called++
d.Add(&f.Value, "fd test 3 value description")
}
var _ FieldDescriber = &fdTest3{}