-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_test.go
More file actions
executable file
·110 lines (90 loc) · 2.91 KB
/
Copy pathstruct_test.go
File metadata and controls
executable file
·110 lines (90 loc) · 2.91 KB
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
package introspect
import (
"encoding/json"
"log"
"reflect"
"testing"
)
type otherStruct struct {
OtherString string
OtherInt int
Interface map[string]interface{}
}
type otherStructPtr struct {
OtherStringPtr string
}
type testStruct struct {
FieldString string
FieldInt int
privateString string
Other otherStruct
OtherPtr *otherStructPtr
SliceString []string
SliceInt []int
}
func TestStruct(t *testing.T) {
numInt := 2
ni := NewStruct(1, "/")
nip := NewStruct(&numInt, "/")
if ni.Value("/nil/") != nil {
t.Errorf("Value should be 'nil', but got %s", ni.Value("/nil/"))
}
if ni.Value("/int/") != 1 {
t.Errorf("Value should be '2', but got %s", ni.Value("/int/"))
}
if nip.Value("/int/") != numInt {
t.Errorf("Value should be 'numInt', but got %s", nip.Value("/int/"))
}
test := &testStruct{
FieldString: "Hello world !",
FieldInt: 1337,
privateString: "private str",
SliceString: []string{"LE", "ET"},
SliceInt: []int{13, 37},
}
strJson := `{"a": 1,"vars": {"hello": "world","number": 2},"1.2": [{"str1a": "string 1 a","str1b": "string 1 b"},["a","b","c"],{"str2a": "string 2 a","str2b": null},"string 3"]}`
data := make(map[string]interface{})
json.Unmarshal([]byte(strJson), &data)
testOtherPtr := &otherStructPtr{OtherStringPtr: "other struct ptr"}
test.OtherPtr = testOtherPtr
test.Other.Interface = data
test.Other.OtherString = "test"
is := NewStruct(test, "/")
isd := NewStruct(test)
k := is.Keys()
n := len(k)
log.Println("Keys :")
for _, i := range k {
log.Println(i, "=", is.Value(i))
}
if n != 19 {
t.Errorf("Wrong number of keys got %d", n)
} else {
log.Println("keys", n)
}
if is.TypeOf("/NIL") != "nil" {
t.Errorf("TypeOf should be nil for unknown path, but got %s", is.TypeOf("NIL"))
}
if isd.TypeOf(".otherStruct.Interface.a") != "float64" {
t.Errorf("Value should be 'float64', but got %s", isd.TypeOf(".otherStruct.Interface.a"))
}
if is.TypeOf("/otherStruct/Interface/a") != "float64" {
t.Errorf("Value should be 'float64', but got %s", is.TypeOf("/otherStruct/Interface/a"))
}
if is.Value("/otherStruct/Interface/a").(float64) != 1 {
t.Errorf("Value should be '1', but got %s", is.Value("/otherStruct/Interface/a"))
}
if reflect.TypeOf(is.Get("/otherStruct/Interface/a")).Kind() != reflect.Ptr {
t.Errorf("Value should be 'reflect.Ptr', but got %s", reflect.TypeOf(is.Value("/otherStruct/Interface/a")).Kind())
}
is.Set("/otherStruct/Interface/a", 2)
if is.Value("/otherStruct/Interface/a") != 2 {
t.Errorf("Value should be '2', but got %s", is.Value("/otherStruct/Interface/a"))
}
if is.TypeOf("/otherStruct/Interface/vars/hello") != "string" {
t.Errorf("Value should be 'int', but got %s", is.TypeOf("/otherStruct/Interface/vars/hello"))
}
if is.Value("/otherStruct/Interface/vars/hello") != "world" {
t.Errorf("Value should be '1', but got %s", is.Value("/otherStruct/Interface/vars/hello"))
}
}