-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
125 lines (93 loc) · 2.32 KB
/
util_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
package goset
import (
"fmt"
"testing"
)
func testValue(t *testing.T, value interface{}, expected interface{}) {
if value != expected {
t.Error(fmt.Sprintf("%s != %s", value, expected))
}
}
func testData() map[string]interface{} {
return map[string]interface{}{
"value": "some-value",
"another value": map[string]interface{}{
"data": "something",
},
}
}
func TestParse(t *testing.T) {
data, err := parse([]byte(`{"str":"rts","num":8.4}`))
if err != nil {
t.Error(err)
}
if data["str"] != "rts" || data["num"] != 8.4 {
t.Error("Parsed data don't match")
}
}
func TestParseInvalidJson(t *testing.T) {
data, err := parse([]byte(`{"sd:333"},`))
if data != nil {
t.Error("Data should be nil")
}
if err == nil {
t.Error("No error on invalid json")
}
}
func TestMerge(t *testing.T) {
current := testData()
next := map[string]interface{}{
"value": "value",
"some other": map[string]interface{}{
"ok": "yes",
},
}
merged := merge(¤t, &next)
testValue(t, merged["value"], next["value"])
testValue(t, merged["another value"].(map[string]interface{})["data"], "something")
testValue(t, merged["some other"].(map[string]interface{})["ok"], "yes")
}
func TestValidSingleExtract(t *testing.T) {
data := testData()
val, err := extract(data, "value")
if err != nil {
t.Error(err)
}
testValue(t, val, "some-value")
}
func TestValidNestedExtract(t *testing.T) {
data := testData()
val, err := extract(data, "another value.data")
if err != nil {
t.Error(err)
}
testValue(t, val, "something")
}
func TestInvalidSingleExtract(t *testing.T) {
data := map[string]interface{}{}
val, err := extract(data, "nonexisting")
if val != nil {
t.Error("Value shoud be nil")
}
testValue(t, fmt.Sprint(err), "nonexisting not found")
}
func TestInvalidNestedExtract(t *testing.T) {
data := map[string]interface{}{
"another value": map[string]interface{}{},
}
val, err := extract(data, "another value.nonexisting")
if val != nil {
t.Error("Value shoud be nil")
}
testValue(t, fmt.Sprint(err), "nonexisting not found")
}
func TestInvalidTypeInNestedExtract(t *testing.T) {
data := map[string]interface{}{
"another value": 3,
}
val, err := extract(data, "another value.key")
if val != nil {
t.Error("Value shoud be nil")
}
testValue(t, fmt.Sprint(err), "another value is not a map")
}