-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
179 lines (145 loc) · 4.11 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package env_test
import (
"fmt"
"os"
"testing"
"time"
"github.com/jelmersnoeck/env"
"github.com/stretchr/testify/assert"
)
var ev = "ENV_TEST_VALUE"
func TestString(t *testing.T) {
not := envTest(ev, "", func() {
assert.Equal(t, "default", env.String(ev, "default"))
})
assert.Len(t, not.notifications, 1)
not = envTest(ev, "value", func() {
assert.Equal(t, "value", env.String(ev, "default"))
})
assert.Len(t, not.notifications, 0)
}
func TestBool(t *testing.T) {
not := envTest(ev, "", func() {
assert.True(t, env.Bool(ev, true))
assert.False(t, env.Bool(ev, false))
})
assert.Len(t, not.notifications, 2)
not = envTest(ev, "true", func() {
assert.True(t, env.Bool(ev, true))
assert.True(t, env.Bool(ev, false))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "1", func() {
assert.True(t, env.Bool(ev, true))
assert.True(t, env.Bool(ev, false))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "non-true", func() {
assert.True(t, env.Bool(ev, true))
assert.False(t, env.Bool(ev, false))
})
assert.Len(t, not.notifications, 2)
not = envTest(ev, "false", func() {
assert.False(t, env.Bool(ev, true))
assert.False(t, env.Bool(ev, false))
})
assert.Len(t, not.notifications, 0)
}
func TestDuration(t *testing.T) {
not := envTest(ev, "", func() {
assert.Equal(t, time.Minute, env.Duration(ev, time.Minute))
})
assert.Len(t, not.notifications, 1)
not = envTest(ev, "5m", func() {
assert.Equal(t, 5*time.Minute, env.Duration(ev, time.Minute))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "invalid duration", func() {
assert.Equal(t, time.Minute, env.Duration(ev, time.Minute))
})
assert.Len(t, not.notifications, 1)
}
func TestInt64(t *testing.T) {
not := envTest(ev, "", func() {
assert.Equal(t, int64(5), env.Int64(ev, int64(5)))
})
assert.Len(t, not.notifications, 1)
not = envTest(ev, "3", func() {
assert.Equal(t, int64(3), env.Int64(ev, int64(5)))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "invalid", func() {
assert.Equal(t, int64(5), env.Int64(ev, int64(5)))
})
assert.Len(t, not.notifications, 1)
}
func TestInt32(t *testing.T) {
not := envTest(ev, "", func() {
assert.Equal(t, int32(5), env.Int32(ev, int32(5)))
})
assert.Len(t, not.notifications, 1)
not = envTest(ev, "3", func() {
assert.Equal(t, int32(3), env.Int32(ev, int32(5)))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "invalid", func() {
assert.Equal(t, int32(5), env.Int32(ev, int32(5)))
})
assert.Len(t, not.notifications, 1)
}
func TestInt(t *testing.T) {
not := envTest(ev, "", func() {
assert.Equal(t, int(5), env.Int(ev, int(5)))
})
assert.Len(t, not.notifications, 1)
not = envTest(ev, "3", func() {
assert.Equal(t, int(3), env.Int(ev, int(5)))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "invalid", func() {
assert.Equal(t, int(5), env.Int(ev, int(5)))
})
assert.Len(t, not.notifications, 1)
}
func envTest(ev, val string, f func()) *mockNotifier {
not := &mockNotifier{}
env.UseNotifier(not)
os.Setenv(ev, val)
defer os.Setenv(ev, "")
f()
return not
}
func TestFloat64(t *testing.T) {
not := envTest(ev, "", func() {
assert.Equal(t, float64(5), env.Float64(ev, float64(5)))
})
assert.Len(t, not.notifications, 1)
not = envTest(ev, "3", func() {
assert.Equal(t, float64(3), env.Float64(ev, float64(5)))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "invalid", func() {
assert.Equal(t, float64(5), env.Float64(ev, float64(5)))
})
assert.Len(t, not.notifications, 1)
}
func TestFloat32(t *testing.T) {
not := envTest(ev, "", func() {
assert.Equal(t, float32(5), env.Float32(ev, float32(5)))
})
assert.Len(t, not.notifications, 1)
not = envTest(ev, "3", func() {
assert.Equal(t, float32(3), env.Float32(ev, float32(5)))
})
assert.Len(t, not.notifications, 0)
not = envTest(ev, "invalid", func() {
assert.Equal(t, float32(5), env.Float32(ev, float32(5)))
})
assert.Len(t, not.notifications, 1)
}
type mockNotifier struct {
notifications []string
}
func (n *mockNotifier) Notify(msg string, i ...interface{}) {
n.notifications = append(n.notifications, fmt.Sprintf(msg, i...))
}