-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
encode_test.go
196 lines (189 loc) · 4.01 KB
/
encode_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package ajson
import (
"fmt"
"testing"
)
func ExampleMarshal() {
data := []byte(`[{"latitude":1,"longitude":2},{"other":"value"},null,{"internal":{"name": "unknown", "longitude":22, "latitude":11}}]`)
root := Must(Unmarshal(data))
locations, _ := root.JSONPath("$..[?(@.latitude && @.longitude)]")
for _, location := range locations {
name := fmt.Sprintf("At [%v, %v]", location.MustKey("latitude").MustNumeric(), location.MustKey("longitude").MustNumeric())
_ = location.AppendObject("name", StringNode("", name))
}
result, _ := Marshal(root)
fmt.Printf("%s", result)
// JSON Output:
// [
// {
// "latitude":1,
// "longitude":2,
// "name":"At [1, 2]"
// },
// {
// "other":"value"
// },
// null,
// {
// "internal":{
// "name":"At [11, 22]",
// "longitude":22,
// "latitude":11
// }
// }
// ]
}
func TestMarshal_Primitive(t *testing.T) {
tests := []struct {
name string
node *Node
}{
{
name: "null",
node: NullNode(""),
},
{
name: "true",
node: BoolNode("", true),
},
{
name: "false",
node: BoolNode("", false),
},
{
name: `"string"`,
node: StringNode("", "string"),
},
{
name: `"one \"encoded\" string"`,
node: StringNode("", `one "encoded" string`),
},
{
name: `"spec.symbols: \r\n\t; UTF-8: 😹; \u2028 \u0000"`,
node: StringNode("", "spec.symbols: \r\n\t; UTF-8: 😹; \u2028 \000"),
},
{
name: "100500",
node: NumericNode("", 100500),
},
{
name: "100.5",
node: NumericNode("", 100.5),
},
{
name: "[1,2,3]",
node: ArrayNode("", []*Node{
NumericNode("0", 1),
NumericNode("2", 2),
NumericNode("3", 3),
}),
},
{
name: `{"foo":"bar"}`,
node: ObjectNode("", map[string]*Node{
"foo": StringNode("foo", "bar"),
}),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
value, err := Marshal(test.node)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else if string(value) != test.name {
t.Errorf("wrong result: '%s', expected '%s'", value, test.name)
}
})
}
}
func TestMarshal_Unparsed(t *testing.T) {
node := Must(Unmarshal([]byte(`{"foo":"bar"}`)))
node.borders[1] = 0 // broken borders
_, err := Marshal(node)
if err == nil {
t.Errorf("expected error")
} else if current, ok := err.(Error); !ok {
t.Errorf("unexpected error type: %T %s", err, err)
} else if current.Error() != "not parsed yet" {
t.Errorf("unexpected error: %s", err)
}
}
func TestMarshal_Encoded(t *testing.T) {
base := `"one \"encoded\" string"`
node := Must(Unmarshal([]byte(base)))
value, err := Marshal(node)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else if string(value) != base {
t.Errorf("wrong result: '%s', expected '%s'", value, base)
}
}
func TestMarshal_Errors(t *testing.T) {
tests := []struct {
name string
node func() (node *Node)
}{
{
name: "nil",
node: func() (node *Node) {
return
},
},
{
name: "broken",
node: func() (node *Node) {
node = Must(Unmarshal([]byte(`{}`)))
node.borders[1] = 0
return
},
},
{
name: "Numeric",
node: func() (node *Node) {
return valueNode(nil, "", Numeric, false)
},
},
{
name: "String",
node: func() (node *Node) {
return valueNode(nil, "", String, false)
},
},
{
name: "Bool",
node: func() (node *Node) {
return valueNode(nil, "", Bool, 1)
},
},
{
name: "Array_1",
node: func() (node *Node) {
node = ArrayNode("", nil)
node.children["1"] = NullNode("1")
return
},
},
{
name: "Array_2",
node: func() (node *Node) {
return ArrayNode("", []*Node{valueNode(nil, "", Bool, 1)})
},
},
{
name: "Object",
node: func() (node *Node) {
return ObjectNode("", map[string]*Node{"key": valueNode(nil, "key", Bool, 1)})
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
value, err := Marshal(test.node())
if err == nil {
t.Errorf("expected error")
} else if len(value) != 0 {
t.Errorf("wrong result")
}
})
}
}