-
Notifications
You must be signed in to change notification settings - Fork 31
/
node_test.go
135 lines (119 loc) · 2.77 KB
/
node_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
package jsonquery
import (
"reflect"
"strings"
"testing"
)
func parseString(s string) (*Node, error) {
return Parse(strings.NewReader(s))
}
func TestParseJsonNumberArray(t *testing.T) {
s := `[1,2,3,4,5,6]`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
var values []float64
for _, n := range doc.ChildNodes() {
values = append(values, n.Value().(float64))
}
expected := []float64{1, 2, 3, 4, 5, 6}
if p1, p2 := len(values), len(expected); p1 != p2 {
t.Fatalf("got %d elements but expected %d", p1, p2)
}
if !reflect.DeepEqual(values, expected) {
t.Fatalf("got %v but expected %v", values, expected)
}
}
func TestParseJsonObject(t *testing.T) {
s := `{
"name":"John",
"age":31,
"female":false
}`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
m := make(map[string]interface{})
for _, n := range doc.ChildNodes() {
m[n.Data] = n.Value()
}
expected := []struct {
name string
value interface{}
}{
{"name", "John"},
{"age", float64(31)},
{"female", false},
}
for _, v := range expected {
if e, g := v.value, m[v.name]; e != g {
t.Fatalf("expected %s = %v(%T),but %s = %v(%t)", v.name, e, e, v.name, g, g)
}
}
}
func TestParseJsonObjectArray(t *testing.T) {
s := `[
{"models":[ "Fiesta", "Focus", "Mustang" ] }
]`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
first := doc.FirstChild
models := first.SelectElement("models")
if expected := reflect.ValueOf(models.Value()).Kind(); expected != reflect.Slice {
t.Fatalf("expected models is slice(Array) but got %v", expected)
}
expected := []string{"Fiesta", "Focus", "Mustang"}
var values []string
for _, v := range models.Value().([]interface{}) {
values = append(values, v.(string))
}
if !reflect.DeepEqual(expected, values) {
t.Fatalf("expected %v but got %v", expected, values)
}
}
func TestParseJson(t *testing.T) {
s := `{
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
n := doc.SelectElement("name")
if n == nil {
t.Fatal("n is nil")
}
if n.NextSibling != nil {
t.Fatal("next sibling shoud be nil")
}
if e, g := "John", n.InnerText(); e != g {
t.Fatalf("expected %v but %v", e, g)
}
cars := doc.SelectElement("cars")
if e, g := 3, len(cars.ChildNodes()); e != g {
t.Fatalf("expected %v but %v", e, g)
}
}
func TestLargeFloat(t *testing.T) {
s := `{
"large_number": 365823929453
}`
doc, err := parseString(s)
if err != nil {
t.Fatal(err)
}
n := doc.SelectElement("large_number")
if n.Value() != float64(365823929453) {
t.Fatalf("expected %v but %v", "365823929453", n.InnerText())
}
}