-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_test.go
120 lines (112 loc) · 2.37 KB
/
env_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package env
import (
"os"
"testing"
)
const (
strKey = "KEY_STRING"
intKey = "KEY_INT"
boolKey = "KEY_BOOL"
)
var environments = map[string]string{
strKey: "magic",
intKey: "31337",
boolKey: "true",
}
func TestStr(t *testing.T) {
setEnvs()
defer unsetEnvs()
type args struct {
key string
choices []string
}
tests := []struct {
args args
wantValue string
wantErr bool
}{
{args{strKey, []string{}}, "magic", false},
{args{"some_key", []string{}}, "", true},
{args{strKey, []string{"magic"}}, "magic", false},
{args{strKey, []string{"some_value"}}, "", true},
}
for _, test := range tests {
value, err := Str(test.args.key, test.args.choices...)
if err != nil {
if !test.wantErr {
t.Fatalf("Str(%s) error = '%v', wantErr = %v", test.args.key, err, test.wantErr)
}
continue
}
if value != test.wantValue {
t.Fatalf("Str(%s) = %v, wantValue = %v", test.args.key, value, test.wantValue)
}
}
}
func TestInt(t *testing.T) {
setEnvs()
defer unsetEnvs()
type args struct {
key string
choices []int
}
tests := []struct {
args args
wantValue int
wantErr bool
}{
{args{intKey, []int{}}, 31337, false},
{args{"some_key", []int{}}, 0, true},
{args{intKey, []int{31337}}, 31337, false},
{args{intKey, []int{0}}, 0, true},
}
for _, test := range tests {
value, err := Int(test.args.key, test.args.choices...)
if err != nil {
if !test.wantErr {
t.Fatalf("Int(%s) error = '%v', wantErr = %v", test.args.key, err, test.wantErr)
}
continue
}
if value != test.wantValue {
t.Fatalf("Int(%s) = %v, wantValue = %v", test.args.key, value, test.wantValue)
}
}
}
func TestBool(t *testing.T) {
setEnvs()
defer unsetEnvs()
type args struct {
key string
}
tests := []struct {
args args
wantValue bool
wantErr bool
}{
{args{boolKey}, true, false},
{args{"some_key"}, false, true},
}
for _, test := range tests {
value, err := Bool(test.args.key)
if err != nil {
if !test.wantErr {
t.Fatalf("Bool(%s) error = '%v', wantErr = %v", test.args.key, err, test.wantErr)
}
continue
}
if value != test.wantValue {
t.Fatalf("Bool(%s) = %v, wantValue = %v", test.args.key, value, test.wantValue)
}
}
}
func setEnvs() {
for key, value := range environments {
os.Setenv(key, value)
}
}
func unsetEnvs() {
for key := range environments {
os.Unsetenv(key)
}
}