-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathijson_test.go
132 lines (122 loc) · 2.89 KB
/
ijson_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
package ijson
import (
"reflect"
"testing"
)
// func Example() {
// var data = []interface{}{
// map[string]interface{}{
// "index": 0,
// "friends": []interface{}{
// map[string]interface{}{
// "id": 0,
// "name": "Justine Bird",
// },
// map[string]interface{}{
// "id": 0,
// "name": "Justine Bird",
// },
// map[string]interface{}{
// "id": 1,
// "name": "Marianne Rutledge",
// },
// },
// },
// }
// r := New(data).
// GetP("#0.friends.#~name"). // list the friend names for 0th record -
// // []interface {}{"Justine Bird", "Justine Bird", "Marianne Rutledge"}
// Del("#0"). // delete 0th record
// // []interface {}{"Marianne Rutledge", "Justine Bird"}
// Set("tom", "#") // append "tom" in the list
// // // []interface {}{"Marianne Rutledge", "Justine Bird", "tom"}
// fmt.Printf("%#v\n", r.Value())
// // output: []interface {}{"Marianne Rutledge", "Justine Bird", "tom"}
// // // returns error if the data type differs than the type expected by query
// // fmt.Println(r.Set(1, "name").Error())
// }
func TestNew(t *testing.T) {
type args struct {
data interface{}
}
tests := []struct {
name string
args args
wantR Result
}{
{
name: "init result",
args: args{data: []interface{}{}},
wantR: Result{val: []interface{}{}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotR := New(tt.args.data); !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("New() = %v, want %v", gotR, tt.wantR)
}
})
}
}
func TestResult_Get(t *testing.T) {
r := Result{val: Object(), err: Err{o: errExpArr, a: "SET"}}
type args struct {
path []string
}
tests := []struct {
name string
r Result
args args
want Result
}{
{
name: "object/ valid path",
r: New(Object()),
args: args{path: []string{"id"}},
want: Result{val: Object()["id"]},
},
{
name: "object/ valid path/ existing error",
r: r,
args: args{path: []string{"id"}},
want: r,
},
// FIXME: failed tests because of error interface
// {
// name: "object/ invalid path",
// r: New(Object()),
// args: args{path: []string{"#0"}},
// want: Result{val: nil, err: Err{o: errExpObj, a: "GET"}},
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Get(tt.args.path...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Result.Get() = %v, want %v", got, tt.want)
}
})
}
}
func TestParse(t *testing.T) {
type args struct {
data string
}
tests := []struct {
name string
args args
want Result
}{
{
name: "valid data",
args: args{data: `{"id":"0"}`},
want: Result{val: map[string]interface{}{"id": "0"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Parse(tt.args.data); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Parse() = %v, want %v", got, tt.want)
}
})
}
}