-
Notifications
You must be signed in to change notification settings - Fork 1
/
snek_test.go
86 lines (71 loc) · 1.99 KB
/
snek_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
package snek
import (
"os"
"testing"
)
func TestSnekDefault(t *testing.T) {
envVar := "SOME_VAR"
value := "testvalue"
// call Default()
Default(envVar, value)
// Get value, make sure default is returned
v := Get(envVar)
if v != value {
t.Error("Default() failed to store and retrieve default value")
}
}
func TestSnekGet(t *testing.T) {
envVar := "ANOTHER_VAR"
value := "testvalue"
// Get nonexistent value, make sure it returns ""
v := Get(envVar)
if v != "" {
t.Error("Get() failed to return empty string when value not present")
}
// Set default
Default(envVar, value)
// Get value again, make sure default is returned
v1 := Get(envVar)
if v1 != value {
t.Error("Get() failed to return default value when set")
}
// Set ENV_VAR
os.Setenv(envVar, "newvalue")
// get value again, make sure env var value is returned'
v2 := Get(envVar)
if v2 != "newvalue" {
t.Error("Get() failed to override default value with env var when set")
}
}
func TestSnekGetOrError(t *testing.T) {
envVar := "YET_ANOTHER_VAR"
value := "testvalue"
// Get nonexistent value, make sure error is returned
v, err := GetOrError(envVar)
if v != "" {
t.Error("GetOrError() failed to return empty string when value not present")
}
if err == nil {
t.Error("GetOrError() failed to return error when value not present")
}
// Set default
Default(envVar, value)
// Get value again, make sure default is returned
v1, err := GetOrError(envVar)
if v1 != value {
t.Error("GetOrError() failed to return default value when set")
}
if err != nil {
t.Error("GetOrError() returned an error when default value was set and returned successfully")
}
// Set ENV_VAR
os.Setenv(envVar, "newvalue")
// get value again, make sure env var value is returned
v2, err := GetOrError(envVar)
if v2 != "newvalue" {
t.Error("GetOrError() failed to override default value with env var when set")
}
if err != nil {
t.Error("GetOrError() returned an error when env var value was set and returned successfully")
}
}