-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_bool_var_test.go
99 lines (81 loc) · 1.69 KB
/
get_bool_var_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
package env
import (
"os"
"testing"
)
type boolFlagTest struct {
valueIn string
valueOut bool
fallback bool
}
var getBoolVarFlagTests = []boolFlagTest{
{"true", true, false},
{"1", true, false},
{"t", true, false},
{"T", true, false},
{"TRUE", true, false},
{"True", true, false},
{"0", false, true},
{"f", false, true},
{"F", false, true},
{"false", false, true},
{"FALSE", false, true},
{"False", false, true},
}
var getBoolVarFlagTestsInvalid = []string{
"test",
"!!!",
"+false",
"0true",
"true1",
"+",
"-",
"o",
}
// Test option with preset value
func TestGetBoolVar(t *testing.T) {
for _, tt := range getBoolVarFlagTests {
t.Run(tt.valueIn, func(t *testing.T) {
os.Clearenv()
if err := os.Setenv(variableName, tt.valueIn); err != nil {
t.Error(err)
}
v, err := GetBoolVar(variableName, tt.fallback)
if err != nil {
t.Error(err)
}
if v != tt.valueOut {
t.Errorf("Variable %s not equal to value '%v'", variableName, tt.valueOut)
}
})
}
}
// Test option when variable was not found
func TestGetBoolVarDefault(t *testing.T) {
os.Clearenv()
v, err := GetBoolVar(variableName, false)
if err != nil {
t.Error(err)
}
if v == true {
t.Error("Variable must contain false value")
}
}
// Test option when variable contain invalid value
func TestGetBoolVarInvalidValue(t *testing.T) {
for _, v := range getBoolVarFlagTestsInvalid {
t.Run(v, func(t *testing.T) {
os.Clearenv()
if err := os.Setenv(variableName, v); err != nil {
t.Error(err)
}
v, err := GetBoolVar(variableName, false)
if err == nil {
t.Error("Must be error")
}
if v != false {
t.Error("Variable must contain false value")
}
})
}
}