-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
100 lines (93 loc) · 2.04 KB
/
options_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
package cfgenv
import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"reflect"
"testing"
"time"
)
func TestDateTimeSetterOption(t *testing.T) {
type Config struct {
Test time.Time
}
_ = os.Setenv("TEST", "2024-01-01T18:00:00Z")
cfg := &Config{}
err := Load(cfg, NewDatetimeSetter(""))
assert.NoError(t, err)
assert.Equal(t, "2024-01-01T18:00:00Z", cfg.Test.Format(time.RFC3339))
_ = os.Setenv("TEST", "2024-01-01")
err = Load(cfg, NewDatetimeSetter(""))
assert.Error(t, err)
err = Load(cfg, NewDatetimeSetter("2006-01-01"))
assert.NoError(t, err)
assert.Equal(t, "2024-01-01T00:00:00Z", cfg.Test.Format(time.RFC3339))
}
func TestDefaultNamingOption(t *testing.T) {
type Config struct {
TestMe string
}
ct := reflect.TypeOf(Config{})
fld := ct.Field(0)
testCases := []struct {
prefix string
separator string
overrideName string
expect string
}{
{
expect: "TEST_ME",
},
{
overrideName: "FOO",
expect: "FOO",
},
{
prefix: "MY",
expect: "MYTEST_ME",
},
{
prefix: "MY",
separator: "_",
expect: "MY_TEST_ME",
},
{
separator: "_",
expect: "TEST_ME",
},
{
prefix: "MY",
separator: "_",
overrideName: "FOO",
expect: "MY_FOO",
},
{
prefix: "",
separator: "_",
overrideName: "FOO",
expect: "FOO",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("[%d]", i+1), func(t *testing.T) {
n := defaultNamingOption.BuildName(tc.prefix, tc.separator, fld, tc.overrideName)
assert.Equal(t, tc.expect, n)
})
}
}
func TestExpand(t *testing.T) {
ex := Expand()
v := ex.Expand("${FOO}-${BAR}")
assert.Equal(t, "-", v)
_ = os.Setenv("FOO", "a")
_ = os.Setenv("BAR", "b")
v = ex.Expand("${FOO}-${BAR}")
assert.Equal(t, "a-b", v)
_ = os.Setenv("FOO", "${BAZ}")
_ = os.Setenv("BAZ", "baz!")
v = ex.Expand("${FOO}-${BAR}")
assert.Equal(t, "baz!-b", v)
ex = Expand(map[string]string{"FOO": "foo!"}, map[string]string{"BAR": "bar!"})
v = ex.Expand("${FOO}-${BAR}")
assert.Equal(t, "foo!-bar!", v)
}