-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig_test.go
132 lines (116 loc) · 2.65 KB
/
config_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
126
127
128
129
130
131
132
package exec
import (
"github.com/stretchr/testify/require"
"reflect"
"testing"
"time"
)
func TestConfig_Value(t *testing.T) {
type testCase struct {
test string
cfg *config
expectedPanic interface{}
}
testCases := []testCase{
{
test: "valid simple value",
cfg: &config{
value: "",
},
},
{
test: "valid func value",
cfg: &config{
value: func() interface{} {
return "value"
},
},
},
{
test: "panics on invalid func with input param",
cfg: &config{
value: func(s string) interface{} {
return "value"
},
},
expectedPanic: "Function type must have no input parameters",
},
{
test: "panics on invalid func with more than one return param",
cfg: &config{
value: func() (interface{}, string) {
return "value", "one"
},
},
expectedPanic: "Function type must have a single return value",
},
{
test: "panics on invalid func with no input param",
cfg: &config{
value: func() string {
return "value"
},
},
expectedPanic: "Function return value must be an interface{}",
},
}
for _, testCase := range testCases {
t.Run(testCase.test, func(t *testing.T) {
if testCase.expectedPanic != nil {
require.PanicsWithValue(t, testCase.expectedPanic, func() {
testCase.cfg.Value()
})
} else {
require.Equal(t, testCase.cfg.value, testCase.cfg.Value())
}
})
}
}
func TestConfig_String(t *testing.T) {
cfg := &config{
Name: "name",
value: "string",
}
require.Equal(t, cfg.value, cfg.String())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.String()))
}
func TestConfig_Int(t *testing.T) {
cfg := &config{
Name: "name",
value: 1,
}
require.Equal(t, cfg.value, cfg.Int())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Int()))
}
func TestConfig_Int64(t *testing.T) {
cfg := &config{
Name: "name",
value: int64(1),
}
require.Equal(t, cfg.value, cfg.Int64())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Int64()))
}
func TestConfig_Bool(t *testing.T) {
cfg := &config{
Name: "name",
value: true,
}
require.Equal(t, cfg.value, cfg.Bool())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Bool()))
}
func TestConfig_Slice(t *testing.T) {
cfg := &config{
Name: "name",
value: []string{"a", "b"},
}
require.Equal(t, cfg.value, cfg.Slice())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Slice()))
}
func TestConfig_Time(t *testing.T) {
cfg := &config{
Name: "name",
value: time.Now(),
}
require.Equal(t, cfg.value, cfg.Time())
require.IsType(t, reflect.TypeOf(cfg.value), reflect.TypeOf(cfg.Time()))
}