-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_uint_var_test.go
90 lines (73 loc) · 1.48 KB
/
get_uint_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
package env
import (
"os"
"testing"
)
type uintFlagTest struct {
valueIn string
valueOut uint
fallback uint
}
var getUintVarFlagTests = []uintFlagTest{
{"0", 0, 7},
{"42", 42, 7},
{"13", 13, 7},
{"432", 432, 7},
{"1024", 1024, 7},
{"1989", 1989, 7},
{"20001", 20001, 7},
}
var getUintVarFlagTestsInvalid = []string{
"test",
"!!!",
"+",
"-",
"10.5",
}
// Test option with preset value
func TestGetUintVar(t *testing.T) {
for _, tt := range getUintVarFlagTests {
t.Run(tt.valueIn, func(t *testing.T) {
os.Clearenv()
if err := os.Setenv(variableName, tt.valueIn); err != nil {
t.Error(err)
}
v, err := GetUintVar(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 TestGetUintVarDefault(t *testing.T) {
os.Clearenv()
v, err := GetUintVar(variableName, 12)
if err != nil {
t.Error(err)
}
if v != 12 {
t.Error("Variable must contain 12")
}
}
// Test option when variable contain invalid value
func TestGetUintVarInvalidValue(t *testing.T) {
for _, v := range getUintVarFlagTestsInvalid {
t.Run(v, func(t *testing.T) {
os.Clearenv()
if err := os.Setenv(variableName, v); err != nil {
t.Error(err)
}
v, err := GetUintVar(variableName, 0)
if err == nil {
t.Error("Must be error")
}
if v != 0 {
t.Error("Variable must contain 0")
}
})
}
}