Debian Codegraph @

jsonq_testdata_test.go

raw symbols SCIP
1 package gojsonq
2
3 import (
4 "bytes"
5 "encoding/json"
6 "io/ioutil"
7 "os"
8 "testing"
9 )
10
11 // ==================== Test Data===================
12 // ==================== DO NOT EDIT===================
13 var (
14 jsonStr = `
15 {
16 "name":"computers",
17 "description":"List of computer products",
18 "vendor":{
19 "name":"Star Trek",
20 "email":"[email protected]",
21 "website":"www.example.com",
22 "items":[
23 {
24 "id":1,
25 "name":"MacBook Pro 13 inch retina",
26 "price":1350
27 },
28 {
29 "id":2,
30 "name":"MacBook Pro 15 inch retina",
31 "price":1700
32 },
33 {
34 "id":3,
35 "name":"Sony VAIO",
36 "price":1200
37 },
38 {
39 "id":4,
40 "name":"Fujitsu",
41 "price":850
42 },
43 {
44 "id":5,
45 "name":"HP core i5",
46 "price":850,
47 "key": 2300
48 },
49 {
50 "id":6,
51 "name":"HP core i7",
52 "price":950
53 },
54 {
55 "id":null,
56 "name":"HP core i3 SSD",
57 "price":850
58 }
59 ],
60 "prices":[
61 2400,
62 2100,
63 1200,
64 400.87,
65 89.90,
66 150.10
67 ],
68 "names":[
69 "John Doe",
70 "Jane Doe",
71 "Tom",
72 "Jerry",
73 "Nicolas",
74 "Abby"
75 ]
76 }
77 }
78 `
79 jsonStrUsers = `{
80 "users":[
81 {
82 "id":1,
83 "name":{
84 "first":"John",
85 "last":"Ramboo"
86 }
87 },
88 {
89 "id":2,
90 "name":{
91 "first":"Ethan",
92 "last":"Hunt"
93 }
94 },
95 {
96 "id":3,
97 "name":{
98 "first":"John",
99 "last":"Doe"
100 }
101 }
102 ]
103 }`
104 )
105
106 // ================= Test Helpers===========================
107
108 func createTestFile(t *testing.T, filename string) (string, func()) {
109 file, err := ioutil.TempFile("", filename)
110 if err != nil {
111 t.Errorf("failed to create %s test file %v", filename, err)
112 }
113
114 // create data.json file from the jsonStr above
115 if _, err := file.Write([]byte(jsonStr)); err != nil {
116 t.Errorf("failed to create %s test file %v", filename, err)
117 }
118
119 return file.Name(), func() {
120 if err := os.Remove(file.Name()); err != nil {
121 t.Errorf("failed to remove %s test file %v", filename, err)
122 }
123 }
124 }
125
126 func assertJSON(t *testing.T, v interface{}, expJSON string, tag ...string) {
127 bb, err := json.Marshal(v)
128 if err != nil {
129 t.Errorf("failed to marshal: %v", err)
130 }
131 eb := []byte(expJSON)
132 if !bytes.Equal(bb, eb) {
133 if len(tag) > 0 {
134 t.Errorf("Tag: %s\nExpected: %v\nGot: %v", tag[0], expJSON, string(bb))
135 } else {
136 t.Errorf("Expected: %v\nGot: %v", expJSON, string(bb))
137 }
138 }
139 }
140
141 func assertInterface(t *testing.T, x, y interface{}, tag ...string) {
142 bbX, err := json.Marshal(x)
143 if err != nil {
144 t.Errorf("failed to marshal x: %v", err)
145 }
146
147 bbY, err := json.Marshal(y)
148 if err != nil {
149 t.Errorf("failed to marshal x: %v", err)
150 }
151
152 if !bytes.Equal(bbX, bbY) {
153 if len(tag) > 0 {
154 t.Errorf("Tag: %s\nExpected: %v\nGot: %v", tag[0], x, y)
155 } else {
156 t.Errorf("Expected: %v\nGot: %v", x, y)
157 }
158 }
159 }
160
161 // cDecoder will be used as a custom decoder for testing// though it use std lib
162 type cDecoder struct {
163 }
164
165 func (c *cDecoder) Decode(data []byte, v interface{}) error {
166 return json.Unmarshal(data, &v) // let's assume this is a custom unmarshaler
167 }