/
util_test.go
56 lines (45 loc) · 1.18 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
package kstatus
import (
"testing"
"github.com/stretchr/testify/assert"
)
var testObj = map[string]interface{}{
"f1": map[string]interface{}{
"f2": map[string]interface{}{
"i32": int32(32),
"i64": int64(64),
"float": 64.02,
"ms": []interface{}{
map[string]interface{}{"f1f2ms0f1": 22},
map[string]interface{}{"f1f2ms1f1": "index1"},
},
"msbad": []interface{}{
map[string]interface{}{"f1f2ms0f1": 22},
32,
},
},
},
"ride": "dragon",
"status": map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{"f1f2ms0f1": 22},
map[string]interface{}{"f1f2ms1f1": "index1"},
},
},
}
func TestGetIntField(t *testing.T) {
v := GetIntField(testObj, ".f1.f2.i32", -1)
assert.Equal(t, int(32), v)
v = GetIntField(testObj, ".f1.f2.wrongname", -1)
assert.Equal(t, int(-1), v)
v = GetIntField(testObj, ".f1.f2.i64", -1)
assert.Equal(t, int(64), v)
v = GetIntField(testObj, ".f1.f2.float", -1)
assert.Equal(t, int(-1), v)
}
func TestGetStringField(t *testing.T) {
v := GetStringField(testObj, ".ride", "horse")
assert.Equal(t, v, "dragon")
v = GetStringField(testObj, ".destination", "north")
assert.Equal(t, v, "north")
}