-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgenv_z_unit_test.go
145 lines (129 loc) · 3.26 KB
/
genv_z_unit_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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package genv_test
import (
"os"
"testing"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/genv"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
)
func Test_GEnv_All(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(os.Environ(), genv.All())
})
}
func Test_GEnv_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
t.AssertNil(err)
t.Assert(genv.Map()[key], "TEST")
})
}
func Test_GEnv_Get(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
t.AssertNil(err)
t.AssertEQ(genv.Get(key).String(), "TEST")
})
}
func Test_GEnv_GetVar(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
t.AssertNil(err)
t.AssertEQ(genv.Get(key).String(), "TEST")
})
}
func Test_GEnv_Contains(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
t.AssertNil(err)
t.AssertEQ(genv.Contains(key), true)
t.AssertEQ(genv.Contains("none"), false)
})
}
func Test_GEnv_Set(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := genv.Set(key, "TEST")
t.AssertNil(err)
t.AssertEQ(os.Getenv(key), "TEST")
})
}
func Test_GEnv_SetMap(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := genv.SetMap(g.MapStrStr{
"K1": "TEST1",
"K2": "TEST2",
})
t.AssertNil(err)
t.AssertEQ(os.Getenv("K1"), "TEST1")
t.AssertEQ(os.Getenv("K2"), "TEST2")
})
}
func Test_GEnv_Build(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := genv.Build(map[string]string{
"k1": "v1",
"k2": "v2",
})
t.AssertIN("k1=v1", s)
t.AssertIN("k2=v2", s)
})
}
func Test_GEnv_Remove(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := gconv.String(gtime.TimestampNano())
key := "TEST_ENV_" + value
err := os.Setenv(key, "TEST")
t.AssertNil(err)
err = genv.Remove(key)
t.AssertNil(err)
t.AssertEQ(os.Getenv(key), "")
})
}
func Test_GetWithCmd(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gcmd.Init("-test", "2")
t.Assert(genv.GetWithCmd("TEST"), 2)
})
gtest.C(t, func(t *gtest.T) {
genv.Set("TEST", "1")
defer genv.Remove("TEST")
gcmd.Init("-test", "2")
t.Assert(genv.GetWithCmd("test"), 1)
})
}
func Test_MapFromEnv(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := genv.MapFromEnv([]string{"a=1", "b=2"})
t.Assert(m, g.Map{"a": 1, "b": 2})
})
}
func Test_MapToEnv(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := genv.MapToEnv(g.MapStrStr{"a": "1"})
t.Assert(s, []string{"a=1"})
})
}
func Test_Filter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := genv.Filter([]string{"a=1", "a=3"})
t.Assert(s, []string{"a=3"})
})
}