-
Notifications
You must be signed in to change notification settings - Fork 1
/
goe_test.go
126 lines (93 loc) · 2.61 KB
/
goe_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
package goe_test
import (
"os/exec"
"testing"
"time"
"github.com/ysmood/goe"
"github.com/ysmood/got"
)
func TestExample(t *testing.T) {
g := got.T(t)
cmd := exec.Command("go", "run", "./example")
out, err := cmd.CombinedOutput()
g.Desc("%s", string(out)).E(err)
g.Has(string(out), `.env
1 hello true [1 2] map[1:2 3:4] dev hello true 2023`)
}
func TestGet(t *testing.T) {
g := got.T(t)
t.Setenv("ENV", "dev")
t.Setenv("BOOL", "true")
t.Setenv("NUM", "2")
t.Setenv("STR", "ok")
t.Setenv("FLOAT", "1.2")
t.Setenv("DURATION", "1m")
t.Setenv("TIME", "2023-12-21T15:41:51+08:00")
type MyInt int
g.Eq(goe.Get("NUM", MyInt(0)), 2)
g.Eq(goe.Get("BOOL", false), true)
g.Eq(goe.Get("BOOL_DEFAULT", true), true)
g.Eq(goe.Get("NUM", 0), 2)
g.Eq(goe.Get("NUM", uint(0)), 2)
g.Eq(goe.Get("NUM_DEFAULT", uint(1)), 1)
g.Eq(goe.Get("STR", ""), "ok")
g.Eq(goe.Get("STR_DEFAULT", "yes"), "yes")
g.Eq(goe.Get("FLOAT", 0.0), 1.2)
g.Eq(goe.Get("FLOAT_DEFAULT", 1.1), 1.1)
g.Eq(goe.Get("DURATION", time.Second), time.Minute)
g.Eq(goe.Get("DURATION_DEFAULT", time.Hour), time.Hour)
g.Eq(goe.GetWithParser("TIME", goe.Time, time.Time{}).Format("2006"), "2023")
tm, err := time.Parse("2006", "2023")
g.E(err)
g.Eq(goe.GetWithParser("TIME_DEFAULT", goe.Time, tm).Format("2006"), "2023")
g.Eq(goe.Require[int]("NUM"), 2)
g.Eq(g.Panic(func() {
goe.Require[int]("KEY")
}), "required env variable not found: KEY")
g.Eq(g.Panic(func() {
t.Setenv("WRONG_INT", "xxx")
goe.Get("WRONG_INT", 0)
}).(error).Error(), `failed to parse int: strconv.ParseInt: parsing "xxx": invalid syntax`)
}
func TestIs(t *testing.T) {
g := got.T(t)
t.Setenv("ENV", "dev")
g.True(goe.Is("ENV", "dev"))
g.False(goe.Is("ENV", "stg"))
g.False(goe.Is("NOT_EXISTS", "dev"))
g.True(goe.Is("NOT_EXISTS", ""))
}
func TestHas(t *testing.T) {
g := got.T(t)
t.Setenv("ENV", "dev")
g.True(goe.Has("ENV"))
g.False(goe.Has("NOT_EXISTS"))
}
func TestLoad(t *testing.T) {
g := got.T(t)
g.Chdir("example")
g.E(goe.Load(false, true, ".env"))
g.Eq(goe.Get("SECRET", ""), "hello")
g.Has(goe.Get("EXPANDED", ""), "dev")
g.Eq(goe.Get("BIN", []byte{}), []byte("hello"))
}
func TestGetList(t *testing.T) {
g := got.T(t)
t.Setenv("LIST", "1,2")
g.Eq(goe.GetList("LIST", []int{}), []int{1, 2})
g.Eq(goe.GetList("DEFAULT", []int{1}), []int{1})
}
func TestGetMap(t *testing.T) {
g := got.T(t)
t.Setenv("MAP", "a:1,b:2")
g.Eq(
goe.GetMap("MAP", map[string]int{"a": 2, "c": 3}),
map[string]int{"a": 1, "b": 2, "c": 3},
)
}
func TestUnset(t *testing.T) {
g := got.T(t)
t.Setenv("ENV", "dev")
goe.Unset("ENV")
g.Eq(goe.Get("ENV", "stg"), "stg")
}