-
Notifications
You must be signed in to change notification settings - Fork 139
/
jsonq_testdata_test.go
167 lines (155 loc) · 3.34 KB
/
jsonq_testdata_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
package gojsonq
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"testing"
)
// ==================== Test Data===================
// ==================== DO NOT EDIT===================
var (
jsonStr = `
{
"name":"computers",
"description":"List of computer products",
"vendor":{
"name":"Star Trek",
"email":"[email protected]",
"website":"www.example.com",
"items":[
{
"id":1,
"name":"MacBook Pro 13 inch retina",
"price":1350
},
{
"id":2,
"name":"MacBook Pro 15 inch retina",
"price":1700
},
{
"id":3,
"name":"Sony VAIO",
"price":1200
},
{
"id":4,
"name":"Fujitsu",
"price":850
},
{
"id":5,
"name":"HP core i5",
"price":850,
"key": 2300
},
{
"id":6,
"name":"HP core i7",
"price":950
},
{
"id":null,
"name":"HP core i3 SSD",
"price":850
}
],
"prices":[
2400,
2100,
1200,
400.87,
89.90,
150.10
],
"names":[
"John Doe",
"Jane Doe",
"Tom",
"Jerry",
"Nicolas",
"Abby"
]
}
}
`
jsonStrUsers = `{
"users":[
{
"id":1,
"name":{
"first":"John",
"last":"Ramboo"
}
},
{
"id":2,
"name":{
"first":"Ethan",
"last":"Hunt"
}
},
{
"id":3,
"name":{
"first":"John",
"last":"Doe"
}
}
]
}`
)
// ================= Test Helpers===========================
func createTestFile(t *testing.T, filename string) (string, func()) {
file, err := ioutil.TempFile("", filename)
if err != nil {
t.Errorf("failed to create %s test file %v", filename, err)
}
// create data.json file from the jsonStr above
if _, err := file.Write([]byte(jsonStr)); err != nil {
t.Errorf("failed to create %s test file %v", filename, err)
}
return file.Name(), func() {
if err := os.Remove(file.Name()); err != nil {
t.Errorf("failed to remove %s test file %v", filename, err)
}
}
}
func assertJSON(t *testing.T, v interface{}, expJSON string, tag ...string) {
bb, err := json.Marshal(v)
if err != nil {
t.Errorf("failed to marshal: %v", err)
}
eb := []byte(expJSON)
if !bytes.Equal(bb, eb) {
if len(tag) > 0 {
t.Errorf("Tag: %s\nExpected: %v\nGot: %v", tag[0], expJSON, string(bb))
} else {
t.Errorf("Expected: %v\nGot: %v", expJSON, string(bb))
}
}
}
func assertInterface(t *testing.T, x, y interface{}, tag ...string) {
bbX, err := json.Marshal(x)
if err != nil {
t.Errorf("failed to marshal x: %v", err)
}
bbY, err := json.Marshal(y)
if err != nil {
t.Errorf("failed to marshal x: %v", err)
}
if !bytes.Equal(bbX, bbY) {
if len(tag) > 0 {
t.Errorf("Tag: %s\nExpected: %v\nGot: %v", tag[0], x, y)
} else {
t.Errorf("Expected: %v\nGot: %v", x, y)
}
}
}
// cDecoder will be used as a custom decoder for testing// though it use std lib
type cDecoder struct {
}
func (c *cDecoder) Decode(data []byte, v interface{}) error {
return json.Unmarshal(data, &v) // let's assume this is a custom unmarshaler
}