...
1
2
3
4 package status
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 )
11
12 var testObj = map[string]interface{}{
13 "f1": map[string]interface{}{
14 "f2": map[string]interface{}{
15 "i32": int32(32),
16 "i64": int64(64),
17 "float": 64.02,
18 "ms": []interface{}{
19 map[string]interface{}{"f1f2ms0f1": 22},
20 map[string]interface{}{"f1f2ms1f1": "index1"},
21 },
22 "msbad": []interface{}{
23 map[string]interface{}{"f1f2ms0f1": 22},
24 32,
25 },
26 },
27 },
28
29 "ride": "dragon",
30
31 "status": map[string]interface{}{
32 "conditions": []interface{}{
33 map[string]interface{}{"f1f2ms0f1": 22},
34 map[string]interface{}{"f1f2ms1f1": "index1"},
35 },
36 },
37 }
38
39 func TestGetIntField(t *testing.T) {
40 v := GetIntField(testObj, ".f1.f2.i32", -1)
41 assert.Equal(t, int(32), v)
42
43 v = GetIntField(testObj, ".f1.f2.wrongname", -1)
44 assert.Equal(t, int(-1), v)
45
46 v = GetIntField(testObj, ".f1.f2.i64", -1)
47 assert.Equal(t, int(64), v)
48
49 v = GetIntField(testObj, ".f1.f2.float", -1)
50 assert.Equal(t, int(-1), v)
51 }
52
53 func TestGetStringField(t *testing.T) {
54 v := GetStringField(testObj, ".ride", "horse")
55 assert.Equal(t, v, "dragon")
56
57 v = GetStringField(testObj, ".destination", "north")
58 assert.Equal(t, v, "north")
59 }
60
View as plain text