forked from adjust/goenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoenv_test.go
117 lines (97 loc) · 2.49 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
package goenv
import (
"testing"
"time"
)
func TestSetEnvironment(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "custom", "nil")
if goenv.GetPort() != "6711" {
t.Error("port != 6711")
}
}
func TestSetEnvironmentNotFound(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "nonexistent", "nil")
if goenv.GetPort() != "8080" {
t.Error("port != 8080")
}
}
func TestSetConfigFile(t *testing.T) {
goenv := NewGoenv("./test_config2.yml", "config", "nil")
if goenv.GetPort() != "4388" {
t.Error("port != 4388")
}
}
func TestSetConfigFileNotFound(t *testing.T) {
defer func() { recover() }()
NewGoenv("nonexistent", "", "nil")
t.Error("SetConfigFile didn't panic")
}
func TestGet(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "config", "nil")
if goenv.Get("custom", "") != "aoeu" {
t.Error("custom != aoeu")
}
}
func TestGetInt(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "config", "nil")
if goenv.GetInt("number", 1245) != 1234 {
t.Error("number != 1234")
}
}
func TestGetDuration(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "config", "nil")
if goenv.GetDuration("duration", "30s") != time.Duration(10*time.Second) {
t.Error("duration != 10s")
}
}
func TestRequire(t *testing.T) {
defer func() { recover() }()
goenv := NewGoenv("./test_config.yml", "config", "nil")
goenv.Require("dingdong")
t.Error("Require didn't panic")
}
func TestGetNotFound(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "config", "nil")
if goenv.Get("nonexistent", "snth") != "snth" {
t.Error("nonexistent != snth")
}
}
func TestGetEnvName(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "", "nil")
if goenv.GetEnvName() != "development" {
t.Error("wrong environment name returned")
}
}
func TestCount(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "list", "nil")
if goenv.Count("entries") != 3 {
t.Error("list count wrong")
}
if goenv.Require("entries[1]") != "b" {
t.Error("list entry wrong")
}
if goenv.Count("nested") != 2 {
t.Error("nested list count wrong")
}
if goenv.Require("nested[1].value") != "v2" {
t.Error("nested list entry wrong")
}
}
func TestCountOk(t *testing.T) {
goenv := NewGoenv("./test_config.yml", "list", "nil")
count, ok := goenv.CountOk("entries")
if !ok {
t.Error("list count not ok")
}
if count != 3 {
t.Error("list count wrong")
}
count, ok = goenv.CountOk("nonexistent")
if ok {
t.Error("list count ok")
}
if count != 0 {
t.Error("list count wrong")
}
}
// TODO: test new functions