-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoenv_test.go
200 lines (173 loc) · 4.1 KB
/
goenv_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package goenv
import (
"net/url"
"reflect"
"testing"
"time"
)
type ts struct {
TestBool bool
TestInt int
TestInt64 int64
TestUint uint
TestUint64 uint64
TestString string
TestFloat64 float64
TestDuration time.Duration
TestURL url.URL
TestStrings []string
}
func TestDefault(t *testing.T) {
d, _ := time.ParseDuration("2h45m")
url := url.URL{Host: "test.com"}
ts := &ts{}
BoolVar(&ts.TestBool, "TEST_BOOL", false)
IntVar(&ts.TestInt, "TEST_INT", 0)
Int64Var(&ts.TestInt64, "TEST_INT64", 1)
UintVar(&ts.TestUint, "TEST_UINT", 2)
Uint64Var(&ts.TestUint64, "TEST_UINT64", 3)
StringVar(&ts.TestString, "TEST_STRING", "0")
Float64Var(&ts.TestFloat64, "TEST_FLOAT64", 4.25)
DurationVar(&ts.TestDuration, "TEST_DURATION", d)
URLVar(&ts.TestURL, "TEST_URL", url)
StringsVar(&ts.TestStrings, "TEST_STRINGS", nil)
Parse()
if ts.TestBool != false {
t.Error("TestBool should be false")
}
if ts.TestInt != 0 {
t.Error("TestInt should be 0")
}
if ts.TestInt64 != 1 {
t.Error("TestInt64 should be 1")
}
if ts.TestUint64 != 3 {
t.Error("TestBool should be false")
}
if ts.TestString != "0" {
t.Error("TestBool should be false")
}
if ts.TestFloat64 != 4.25 {
t.Error("TestBool should be false")
}
if ts.TestDuration != d {
t.Error("TestBool should be false")
}
if ts.TestURL != url {
t.Error("TestBool should be ", url.String())
}
if ts.TestStrings != nil {
t.Error("TestStrings should be nil")
}
}
func TestNewBoolValue(t *testing.T) {
var p bool
v := newBoolValue(true, &p)
if v.Get().(bool) != true {
t.Error("Get newBoolValue should be true")
}
v.Set("false")
if p != false {
t.Error("Get newBoolValue should be false")
}
}
func TestNewIntValue(t *testing.T) {
var p int
v := newIntValue(100, &p)
if v.Get().(int) != 100 {
t.Error("Get newIntValue should be 100")
}
v.Set("50")
if p != 50 {
t.Error("Get newIntValue should be 50")
}
}
func TestNewInt64Value(t *testing.T) {
var p int64
v := newInt64Value(100, &p)
if v.Get().(int64) != 100 {
t.Error("Get newInt64Value should be 100")
}
v.Set("50")
if p != 50 {
t.Error("Get newInt64Value should be 50")
}
}
func TestNewUintValue(t *testing.T) {
var p uint
v := newUintValue(100, &p)
if v.Get().(uint) != 100 {
t.Error("Get newUintValue should be 100")
}
v.Set("50")
if p != 50 {
t.Error("Get newUintValue should be 50")
}
}
func TestNewUint64Value(t *testing.T) {
var p uint64
v := newUint64Value(100, &p)
if v.Get().(uint64) != 100 {
t.Error("Get newUint64Value should be 100")
}
v.Set("50")
if p != 50 {
t.Error("Get newUint64Value should be 50")
}
}
func TestNewFloat64Value(t *testing.T) {
var p float64
v := newFloat64Value(100.999, &p)
if v.Get().(float64) != 100.999 {
t.Error("Get newFloat64Value should be 100.999")
}
v.Set("50.999")
if p != 50.999 {
t.Error("Get newFloat64Value should be 50.999")
}
}
func TestNewDurationValue(t *testing.T) {
var p time.Duration
v := newDurationValue(time.Hour, &p)
if v.Get().(time.Duration) != time.Hour {
t.Error("Get newDurationValue should be 1h")
}
v.Set("2h")
if p != time.Hour*2 {
t.Error("Get newDurationValue should be 2h")
}
}
func TestNewStringValue(t *testing.T) {
var p string
v := newStringValue("hello", &p)
if v.Get().(string) != "hello" {
t.Error("Get newFloat64Value should be hello")
}
v.Set("hello world")
if p != "hello world" {
t.Error("Get newStringValue should be hello world")
}
}
func TestNewStringsValue(t *testing.T) {
var p []string
v := newStringsValue([]string{"hello"}, &p)
if !reflect.DeepEqual(v.Get().([]string), []string{"hello"}) {
t.Error("Get newStringsValue should be hello")
}
v.Set("hello,world")
if !reflect.DeepEqual(p, []string{"hello", "world"}) {
t.Error("Get newStringsValue should be []string{\"hello\", \"world\"")
}
}
func TestNewURLValue(t *testing.T) {
var p url.URL
u := url.URL{Host: "test.com"}
v := newURLValue(u, &p)
if v.Get().(url.URL) != u {
t.Error("Get newURLValue should be ", u.String())
}
v.Set("http://new.test.com")
if p.Host != "new.test.com" || p.Scheme != "http" {
t.Error("Get newURLValue should be ", p.String())
}
}