-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_test.go
137 lines (125 loc) · 2.69 KB
/
document_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
136
137
package njson
import (
"strings"
"testing"
)
func Test_parser(t *testing.T) {
d := Document{}
n, tail, err := d.Parse(smallJSON)
if err != nil {
t.Error(err)
return
}
if n.get() == nil {
t.Errorf("Nil root")
return
}
if strings.TrimSpace(tail) != "" {
t.Errorf("Non empty tail: %s", tail)
}
}
func Benchmark_parser(b *testing.B) {
d := Document{}
b.SetBytes(int64(len(mediumJSON)))
for i := 0; i < b.N; i++ {
d.Reset()
d.Parse(mediumJSON)
}
}
func TestDocument_Object(t *testing.T) {
d := Blank()
defer d.Close()
n := d.Object()
n.Set("foo", d.Text("bar"))
if data, err := n.AppendJSON(nil); err != nil {
t.Errorf("Unexpected error: %s", err)
return
} else if s := `{"foo":"bar"}`; string(data) != s {
t.Errorf("Invalid json: %s != %s", data, s)
}
n.Set("bar", n)
if data, err := n.AppendJSON(nil); err != nil {
t.Errorf("Unexpected error: %s", err)
return
} else if s := `{"foo":"bar","bar":{"foo":"bar"}}`; string(data) != s {
t.Errorf("Invalid json: %s != %s", data, s)
}
}
func TestDocument_Create(t *testing.T) {
d := Document{}
n := d.Null()
assertEqual(t, n.get(), &node{
info: vNull | infRoot,
raw: strNull,
values: nil,
})
n = d.Text("foo")
assertEqual(t, n.get(), &node{
info: vString | infRoot,
raw: "foo",
values: nil,
})
n = d.TextHTML("<p>Foo</p>")
assertEqual(t, n.get(), &node{
info: vString | infRoot,
raw: `\u003cp\u003eFoo\u003c\/p\u003e`,
values: nil,
})
n = d.Number(42)
assertEqual(t, n.get(), &node{
info: vNumber | infRoot,
raw: `42`,
values: nil,
})
n = d.Array()
assertEqual(t, n.get(), &node{
info: vArray | infRoot,
raw: ``,
values: nil,
})
n = d.Object()
assertEqual(t, n.get(), &node{
info: vObject | infRoot,
raw: ``,
values: nil,
})
n = d.True()
assertEqual(t, n.get(), &node{
info: vBoolean | infRoot,
raw: `true`,
values: nil,
})
n = d.False()
assertEqual(t, n.get(), &node{
info: vBoolean | infRoot,
raw: `false`,
values: nil,
})
}
func Test_DocumentReset(t *testing.T) {
d := new(Document)
n := d.Text("foo")
assertEqual(t, n.Document(), d)
d.Reset()
if n.Document() != nil {
t.Errorf("Node document not nil after reset")
}
}
func TestDocument_ncopy(t *testing.T) {
d := new(Document)
n := d.Object()
n.Set("foo", d.Text("bar"))
assertEqual(t, len(d.nodes), 2)
id := d.ncopy(d, d.get(0))
assertEqual(t, id, uint(2))
assertEqual(t, len(d.nodes), 4)
other := new(Document)
other.Object()
id = d.ncopy(other, other.get(0))
assertEqual(t, id, uint(4))
assertEqual(t, len(d.nodes), 5)
n.Set("bar", other.Root())
data, err := n.AppendJSON(nil)
assertNoError(t, err)
assertEqual(t, string(data), `{"foo":"bar","bar":{}}`)
}