-
Notifications
You must be signed in to change notification settings - Fork 1
/
ge_int16_var_test.go
94 lines (77 loc) · 1.56 KB
/
ge_int16_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
package env
import (
"os"
"testing"
)
type int16FlagTest struct {
valueIn string
valueOut int16
fallback int16
}
var getInt16VarFlagTests = []int16FlagTest{
{"0", 0, 7},
{"-0", 0, 7},
{"1", 1, 7},
{"-1", -1, 7},
{"-50", -50, 7},
{"50", 50, 7},
{"-128", -128, 7},
{"127", 127, 7},
{"-32768", -32768, 7},
{"32767", 32767, 7},
}
var getInt16VarFlagTestsInvalid = []string{
"test",
"!!!",
"+",
"-",
"10.5",
"2147483649",
}
// Test option with preset value
func TestGetInt16Var(t *testing.T) {
for _, tt := range getInt16VarFlagTests {
t.Run(tt.valueIn, func(t *testing.T) {
os.Clearenv()
if err := os.Setenv(variableName, tt.valueIn); err != nil {
t.Error(err)
}
v, err := GetInt16Var(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 TestGetInt16VarDefault(t *testing.T) {
os.Clearenv()
v, err := GetInt16Var(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 TestGetInt16VarInvalidValue(t *testing.T) {
for _, v := range getInt16VarFlagTestsInvalid {
t.Run(v, func(t *testing.T) {
os.Clearenv()
if err := os.Setenv(variableName, v); err != nil {
t.Error(err)
}
v, err := GetInt16Var(variableName, 0)
if err == nil {
t.Error("Must be error")
}
if v != 0 {
t.Error("Variable must contain 0")
}
})
}
}