-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
105 lines (92 loc) · 2.33 KB
/
parser_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
package envconf_test
import (
"fmt"
"os"
"testing"
"github.com/antonmashko/envconf"
"github.com/antonmashko/envconf/option"
)
func TestParse_EmptyStruct_OK(t *testing.T) {
if err := envconf.Parse(&struct{}{}); err != nil {
t.Errorf("failed to parse empty struct. err=%#v", err)
}
}
func TestParse_NilData_NilDataErr(t *testing.T) {
if err := envconf.Parse(nil); err == nil || err != envconf.ErrNilData {
t.Errorf("err doesn't equals to ErrNilData. err=%#v", err)
}
}
func TestParse_NilValue_NilDataErr(t *testing.T) {
var d *struct{}
if err := envconf.Parse(d); err == nil || err != envconf.ErrNilData {
t.Errorf("err doesn't equals to ErrNilData. err=%#v", err)
}
}
func TestParse_PassDataByValue_Err(t *testing.T) {
data := struct {
Field string `default:"123"`
}{}
if err := envconf.Parse(data); err == nil {
t.Fatal("expected error but got nil")
}
}
func TestParse_FlagParsedCallback_OK(t *testing.T) {
x := 0
fpo := option.WithFlagParsed(func() error {
x = 1
return nil
})
tc := struct{}{}
if err := envconf.Parse(&tc, fpo); err != nil {
t.Errorf("failed to parse. err=%s", err)
}
if x != 1 {
t.Errorf("incorrect value was set. %#v", x)
}
}
func TestParse_InvalidData_Err(t *testing.T) {
var cfg string
if err := envconf.Parse(&cfg); err == nil {
t.Fail()
}
}
func TestParse_AutoGeneratedEnvNames_Ok(t *testing.T) {
t.Run("EnvVar", func(t *testing.T) {
var cfg struct {
Inner1 struct {
Field1 string `env:"*"`
}
}
var expectedResult = "ok"
os.Setenv("INNER1_FIELD1", expectedResult)
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if cfg.Inner1.Field1 != expectedResult {
t.Fatalf("incorrect result. expected=%s actual=%s", expectedResult, cfg.Inner1.Field1)
}
})
t.Run("EnvVarChangedParentName", func(t *testing.T) {
var cfg struct {
Inner1 struct {
Field1 string `env:"*"`
} `envconf:"INNER"`
}
var expectedResult = "ok"
os.Setenv("INNER_FIELD1", expectedResult)
if err := envconf.Parse(&cfg); err != nil {
t.Fatal(err)
}
if cfg.Inner1.Field1 != expectedResult {
t.Fatalf("incorrect result. expected=%s actual=%s", expectedResult, cfg.Inner1.Field1)
}
})
}
func TestParse_NotAssignableInterface_Err(t *testing.T) {
cfg := struct {
Field1 fmt.Stringer `default:"10"`
}{}
if err := envconf.Parse(&cfg); err == nil {
t.Fail()
}
}