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
|
package json_test
import (
"reflect"
"testing"
"go.neonxp.ru/json"
"go.neonxp.ru/json/std"
)
func TestMustQuery(t *testing.T) {
jsonString := `{
"string key": "string value",
"number key": 123.321,
"bool key": true,
"object": {
"one": "two",
"object 2": {
"three": "four"
}
},
"array": [
"one",
2,
true,
null,
{
"five": "six"
}
]
}`
type args struct {
parent json.Node
path []string
}
tests := []struct {
name string
args args
want json.Node
}{
{
name: "find in object",
args: args{
parent: json.New(std.Factory).MustUnmarshal(jsonString),
path: []string{"object", "object 2", "three"},
},
want: &std.StringNode{Value: "four"},
},
{
name: "find in array",
args: args{
parent: json.New(std.Factory).MustUnmarshal(jsonString),
path: []string{"array", "[4]", "five"},
},
want: &std.StringNode{Value: "six"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := json.MustQuery(tt.args.parent, tt.args.path); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MustQuery() = %v, want %v", got, tt.want)
}
})
}
}
|