forked from phR0ze/n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_test.go
271 lines (230 loc) · 6.33 KB
/
n_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package n
import (
"fmt"
"testing"
"github.com/phR0ze/n/pkg/sys"
yaml "github.com/phR0ze/yaml/v2"
"github.com/stretchr/testify/assert"
)
const nines7 = 9999999
const nines6 = 999999
const nines5 = 99999
const nines4 = 9999
const nines3 = 999
type bob struct {
o interface{}
}
var tmpDir = "test/temp"
var tmpFile = "test/temp/.tmp"
func ExampleEitherStr() {
fmt.Println(EitherStr("", "default"))
// Output: default
}
func TestEitherStr(t *testing.T) {
// Test default set
{
assert.Equal(t, "test", EitherStr("", "test"))
}
// Test value set
{
assert.Equal(t, "foo", EitherStr("foo", "test"))
}
}
func ExampleSetOnEmpty() {
result := ""
fmt.Println(SetOnEmpty(&result, "foo"))
// Output: foo
}
func TestSetOnEmpty(t *testing.T) {
result := ""
assert.Equal(t, "foo", SetOnEmpty(&result, "foo"))
assert.Equal(t, "foo", SetOnEmpty(&result, "bar"))
assert.Equal(t, "bar", SetOnEmpty(nil, "bar"))
}
func TestSetOnTrueA(t *testing.T) {
result := ""
assert.Equal(t, "foo", SetOnTrueA(&result, "foo", true))
assert.Equal(t, "foo", SetOnTrueA(&result, "bar", true))
assert.Equal(t, "bar", SetOnTrueA(nil, "bar", true))
assert.Equal(t, "foo", SetOnTrueA(&result, "bar", false))
}
func ExampleSetOnFalseB() {
result := false
fmt.Println(SetOnFalseB(&result, true, false))
// Output: true
}
func TestSetOnFalseB(t *testing.T) {
result := false
assert.Equal(t, true, SetOnFalseB(&result, true, false))
assert.Equal(t, true, SetOnFalseB(&result, false, true))
assert.Equal(t, true, SetOnFalseB(&result, true, false))
}
func ExampleSetOnTrueB() {
result := false
fmt.Println(SetOnTrueB(&result, true, true))
// Output: true
}
func TestSetOnTrueB(t *testing.T) {
result := false
assert.Equal(t, true, SetOnTrueB(&result, true, true))
assert.Equal(t, false, SetOnTrueB(&result, false, true))
assert.Equal(t, false, SetOnTrueB(&result, true, false))
}
func TestRange(t *testing.T) {
assert.Equal(t, []int{0}, Range(0, 0))
assert.Equal(t, []int{0, 1}, Range(0, 1))
assert.Equal(t, []int{3, 4, 5, 6, 7, 8}, Range(3, 8))
}
func TestLoadJSON(t *testing.T) {
clearTmpDir()
// Load simple file
{
// Write out the yaml to read in
data := "{\n \"foo\": \"bar\"\n}"
sys.WriteBytes(tmpFile, []byte(data))
// Load the json and validate
m := LoadJSON(tmpFile)
assert.Equal(t, M().Add("foo", "bar"), m)
}
// More complicated file
{
// Write out the yaml to read in
data := "{\n \"foo\": {\n \"value\": 1\n }\n}"
sys.WriteBytes(tmpFile, []byte(data))
// Load the json and validate
m := LoadJSON(tmpFile)
assert.Equal(t, M().Add("foo", map[string]interface{}{"value": float64(1)}), m)
}
}
func TestLoadJSONE(t *testing.T) {
clearTmpDir()
// Load simple file
{
// Write out the yaml to read in
data := "{\n \"foo\": \"bar\"\n}"
sys.WriteBytes(tmpFile, []byte(data))
// Load the json and validate
m, err := LoadJSONE(tmpFile)
assert.Nil(t, err)
assert.Equal(t, M().Add("foo", "bar"), m)
}
// More complicated file
{
// Write out the yaml to read in
data := "{\n \"foo\": {\n \"value\": 1\n }\n}"
sys.WriteBytes(tmpFile, []byte(data))
// Load the json and validate
m, err := LoadJSONE(tmpFile)
assert.Nil(t, err)
assert.Equal(t, M().Add("foo", map[string]interface{}{"value": float64(1)}), m)
}
}
func TestLoadYAML(t *testing.T) {
clearTmpDir()
// Load yaml file
{
// Write out the yaml to read in
data1 := "b: b1\na: a1\n"
assert.NoError(t, sys.WriteBytes(tmpFile, []byte(data1)))
// Load yaml and modify target
m := LoadYAML(tmpFile).Update("a", "a2")
assert.NoError(t, sys.Remove(tmpFile))
assert.False(t, sys.Exists(tmpFile))
// Write YAML out to disk and read back in and assert order
assert.NoError(t, m.WriteYAML(tmpFile))
data2, err := sys.ReadBytes(tmpFile)
assert.NoError(t, err)
assert.Equal(t, "b: b1\na: a2\n", string(data2))
}
}
func TestLoadYAMLE(t *testing.T) {
clearTmpDir()
// Anchors and aliases are handled correctly
{
m := map[string]interface{}{}
data1 := `
foo:
<<: &foobar1
bar1: val1
<<: &foobar2
bar2: val2
<<: &foobar3
bar3: val3
foo1:
- <<: *foobar2
blah1:
<<: *foobar1
foo2:
- <<: *foobar2
<<: *foobar3
blah2: true
`
err := yaml.Unmarshal([]byte(data1), &m)
assert.NoError(t, err)
// Validate complete structure
assert.Equal(t, 1, len(m))
foo := m["foo"].(map[interface{}]interface{})
assert.Equal(t, 5, len(foo))
assert.Equal(t, "val1", foo["bar1"])
assert.Equal(t, "val2", foo["bar2"])
assert.Equal(t, "val3", foo["bar3"])
foo1slice := foo["foo1"].([]interface{})
assert.Equal(t, 1, len(foo1slice))
foo1 := foo1slice[0].(map[interface{}]interface{})
assert.Equal(t, 2, len(foo1))
assert.Equal(t, "val2", foo1["bar2"])
assert.Equal(t, "val1", foo1["blah1"].(map[interface{}]interface{})["bar1"])
foo2slice := foo["foo2"].([]interface{})
assert.Equal(t, 1, len(foo2slice))
foo2 := foo2slice[0].(map[interface{}]interface{})
assert.Equal(t, 3, len(foo2))
assert.Equal(t, "val2", foo2["bar2"])
assert.Equal(t, "val3", foo2["bar3"])
assert.Equal(t, true, foo2["blah2"])
// Validate complete structure
fn := func(x *StringMap) {
assert.Equal(t, 1, x.Len())
assert.Equal(t, "val1", x.Query("foo.bar1").A())
assert.Equal(t, "val2", x.Query("foo.bar2").A())
assert.Equal(t, "val3", x.Query("foo.bar3").A())
assert.Equal(t, "val2", x.Query("foo.foo1.[0].bar2").A())
assert.Equal(t, "val1", x.Query("foo.foo1.[0].blah1.bar1").A())
assert.Equal(t, "val2", x.Query("foo.foo2.[0].bar2").A())
assert.Equal(t, "val3", x.Query("foo.foo2.[0].bar3").A())
assert.Equal(t, true, x.Query("foo.foo2.[0].blah2").O())
}
fn(MV(m))
// Now repeat with yaml.StringMap load
x, err := ToStringMapE(data1)
assert.NoError(t, err)
fn(x)
}
}
func rangeObject(min, max int) []Object {
result := make([]Object, max-min+1)
for i := range result {
result[i] = Object{min + i}
}
return result
}
func rangeInterObject(min, max int) []interface{} {
result := make([]interface{}, max-min+1)
for i := range result {
result[i] = Object{min + i}
}
return result
}
// rangeO creates slice of the given range of numbers inclusive
func rangeO(min, max int) []interface{} {
result := make([]interface{}, max-min+1)
for i := range result {
result[i] = min + i
}
return result
}
func clearTmpDir() {
if sys.Exists(tmpDir) {
sys.RemoveAll(tmpDir)
}
sys.MkdirP(tmpDir)
}