-
Notifications
You must be signed in to change notification settings - Fork 0
/
j_test.go
230 lines (205 loc) · 5.73 KB
/
j_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package fetch
import (
"fmt"
"testing"
)
func TestJ_Q(t *testing.T) {
type testCase struct {
In string
P string
E any
}
cases := []testCase{
{In: `{"name": "Lola"}`, P: ".name", E: "Lola"},
{In: `{"name": "Lola"}`, P: "name", E: "Lola"},
{In: `{"dog": {"name": "Lola"}}`, P: ".dog.name", E: "Lola"},
{In: `{"pet": {"dog": {"name": "Lola"}}}`, P: ".pet.dog.name", E: "Lola"},
{In: `{"num": 1}`, P: ".num", E: 1.0},
{In: `1`, P: ".", E: 1.0},
{In: `true`, P: ".", E: true},
{In: `"hello"`, P: ".", E: "hello"},
{In: `{"sold": false}`, P: ".sold", E: false},
{In: `[1, 2, 3]`, P: ".[0]", E: 1.0},
{In: `[{"name":"Lola"}, {"name":"Buster"}]`, P: ".[1].name", E: "Buster"},
{In: `{"nums":[1, 2, 57]}`, P: ".nums[2]", E: 57.0},
{In: `{"pets":[{"name":"Lola"}]}`, P: ".pets[0].name", E: "Lola"},
{In: `[{}, {"pets":[{},{}, {"name":"Lola", "tags":[{"id": 12}]}]}]`, P: ".[1].pets[2].tags[0].id", E: 12.0},
{In: `{"":"hello"}`, P: "..", E: "hello"},
{In: `{"":{"name":"Lola"}}`, P: "..name", E: "Lola"},
{In: `[[1, 2, 3]]`, P: ".[0][2]", E: 3.0},
{In: `[[1, 2, 3]]`, P: ".[0].[2]", E: 3.0},
{In: `{}`, P: ".name", E: nil},
{In: `{}`, P: ".name.category", E: nil},
{In: `{}`, P: ".name.tags[0]", E: nil},
{In: `{}`, P: ".[0]", E: nil},
{In: `[]`, P: ".name", E: nil},
{In: `{}`, P: "..", E: nil},
{In: `{"name":"Lola"}`, P: ".name.category", E: nil},
{In: `[1, 2]`, P: ".[3]", E: nil},
{In: `{"tags":[{"id":12}]}`, P: ".tags.id", E: nil},
{In: `{"category":{"name":"dog"}}`, P: ".category[0]", E: nil},
}
for i, c := range cases {
j, err := Unmarshal[J](c.In)
if err != nil {
t.Errorf("Unmarshal error: %s", err)
continue
}
//// debugging
//if i == 14 {
// fmt.Print()
//}
got := j.Q(c.P).Raw()
if got != c.E {
t.Errorf("case #%d: wrong value, expected=%v, got=%v", i, c.E, got)
}
}
errorCases := []testCase{
{In: `[1, 2, 3]`, P: ".[1", E: "expected ] for array index"},
{In: `[1, 2, 3]`, P: ".[hello]", E: "expected a number for array index, got: 'hello'"},
{In: `[1, 2, 3]`, P: ".[0]name", E: "expected . or [, got: 'name'"},
}
for i, c := range errorCases {
j, err := Unmarshal[J](c.In)
if err != nil {
t.Errorf("Unmarshal error: %s", err)
continue
}
got := j.Q(c.P)
errStr, ok := c.E.(string)
if !ok {
panic("E should be string")
}
jqErr, ok := got.(*JQError)
if !ok {
t.Errorf("error case #%d: expected error, got=%v", i, got)
continue
}
if jqErr.s != errStr {
t.Errorf("error case #%d: wrong value, expected=%v, got=%v", i, errStr, jqErr.s)
}
}
}
func TestJ_String(t *testing.T) {
type testCase struct {
I J
O string
}
var cases = []testCase{
{I: M{"name": "Lola"}, O: `{"name":"Lola"}`},
{I: A{1, 2, 3}, O: `[1,2,3]`},
{I: F(1), O: `1`},
{I: S("hello"), O: `hello`},
{I: A{M{"name": "Lola"}}, O: `[{"name":"Lola"}]`},
{I: M{"tags": A{1, 2, 3}}, O: `{"tags":[1,2,3]}`},
}
for _, c := range cases {
got := c.I.String()
if got != c.O {
t.Errorf("J.String() expected=%s, got=%s", c.O, got)
}
}
if (M{"name": "Lola"}).String() != `{"name":"Lola"}` {
t.Errorf("")
}
var j J = M{"name": "Lola"}
if fmt.Sprint(j) != `{"name":"Lola"}` {
t.Errorf("J.String j inteface wrong value")
}
}
func TestJ_Nil(t *testing.T) {
j, err := Unmarshal[J](`{"name":"Lola"}`)
if err != nil {
t.Fatal(err)
}
if j.Q(".id") == nil {
t.Errorf("didn't expect J value to be nil")
}
if !j.Q("id").IsNil() {
t.Errorf("expected J.IsNil to be true")
}
if j.Q(".id").Raw() != nil {
t.Errorf("expected id to be nil")
}
if j.Q(".id").String() != "nil" {
t.Errorf("expected id to print nil")
}
if j.Q(".id").Q(".yaid").Raw() != nil {
t.Errorf("expected id to be nil")
}
}
func TestJ_AsFirstValue(t *testing.T) {
m, _ := mustUnmarshal(`{"key":"value"}`).AsObject()
if m["key"] != "value" {
t.Errorf("object value is wrong")
}
a, _ := mustUnmarshal(`[1, 2, 3]`).AsArray()
if len(a) != 3 {
t.Errorf("array value is wrong")
}
n, _ := mustUnmarshal(`1`).AsNumber()
if n != 1 {
t.Errorf("number value is wrong")
}
s, _ := mustUnmarshal(`{"key":"value"}`).Q("key").AsString()
if s != "value" {
t.Errorf("string value is wrong")
}
b, _ := mustUnmarshal(`true`).AsBoolean()
if !b {
t.Errorf("boolean value is wrong")
}
}
type AsCheck struct {
Boolean bool
Number bool
String bool
Array bool
Object bool
IsNil bool
}
func TestJ_AsSecondValue(t *testing.T) {
type testCase struct {
I J
O AsCheck
}
var cases = []testCase{
{I: mustUnmarshal(`{"key":"value"}`), O: AsCheck{Object: true}},
{I: mustUnmarshal(`{"outer":{"key":"value"}}`).Q("outer"), O: AsCheck{Object: true}},
{I: mustUnmarshal(`{"key":[1, 2, 3]}`).Q("key"), O: AsCheck{Array: true}},
{I: mustUnmarshal(`[1, 2]`), O: AsCheck{Array: true}},
{I: mustUnmarshal(`0`), O: AsCheck{Number: true}},
{I: mustUnmarshal(`1`), O: AsCheck{Number: true}},
{I: mustUnmarshal(`false`), O: AsCheck{Boolean: true}},
{I: mustUnmarshal(`true`), O: AsCheck{Boolean: true}},
{I: mustUnmarshal(`{"key":"value"}`).Q("key"), O: AsCheck{String: true}},
{I: mustUnmarshal(`{}`).Q("key"), O: AsCheck{IsNil: true}},
}
for i, c := range cases {
if c.I.IsNil() != c.O.IsNil {
t.Errorf("%d nil mismatch", i)
}
if _, ok := c.I.AsBoolean(); ok != c.O.Boolean {
t.Errorf("%d boolean mismatch", i)
}
if _, ok := c.I.AsNumber(); ok != c.O.Number {
t.Errorf("%d number mismatch", i)
}
if _, ok := c.I.AsString(); ok != c.O.String {
t.Errorf("%d string mismatch", i)
}
if _, ok := c.I.AsArray(); ok != c.O.Array {
t.Errorf("%d array mismatch", i)
}
if _, ok := c.I.AsObject(); ok != c.O.Object {
t.Errorf("%d object mismatch", i)
}
}
}
func mustUnmarshal(s string) J {
j, err := Unmarshal[J](s)
if err != nil {
panic(err)
}
return j
}