-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson_test.go
More file actions
208 lines (181 loc) · 5.28 KB
/
json_test.go
File metadata and controls
208 lines (181 loc) · 5.28 KB
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
package simplejson
import (
"testing"
"strconv"
"fmt"
)
var jsonString = `{
"keyString":"stringValue",
"keyInt": 123,
"keyFloat64": 1.23,
"keyFloat32": 1.23,
"keyBool": true,
"keyJSONObject": {
"keyString":"stringValue",
"keyInt": 123
},
"keyArray":[
"string1",
"string2"
]
}`
var jsonArrayAllTypes = `{
"keyString":["stringValue"],
"keyInt": [123],
"keyFloat64": [1.23],
"keyFloat32": [1.23],
"keyBool": [true],
"keyJSONObject": [{
"keyString":"stringValue",
"keyInt": 123
}],
"keyArray":[[
"string1",
"string2"
]]
}`
var jsonArrayAsRoot = `[
{
"elemKeyString": "stringValue",
"elemKeyInt": 0
},
123,
"stringElement"
]
`
var jsonArrayAsRoot2 = `[
0,
1,
2
]
`
func TestJsonArrayWithInts(t *testing.T) {
jsonArray, err := NewJSONArrayFromString(jsonArrayAsRoot2);
if err != nil {
t.Error("Parsing failed with error: " + err.Error())
t.FailNow()
}
if jsonArray.Int(0) != 0 || jsonArray.Int(1) != 1 || jsonArray.Int(2) != 2 {
t.Error("Parsing failed with error: " + err.Error())
t.FailNow()
}
}
func TestParseInt(t *testing.T) {
var someFloat64 float64
var someInt int
var someInterface interface{}
someInt = 123
someFloat64 = 123
someInterface = 123
if parseInt(someInt) != 123 || parseInt(someFloat64) != 123 || parseInt(someInterface) != 123 {
t.Error("parseInt could not parse interface")
t.FailNow()
}
}
func TestJSONObjectAllTypesExceptArray(t *testing.T) {
jsonObject, err := NewJSONObjectFromString(jsonString);
if err != nil {
t.Error("Parsing failed with error: " + err.Error())
t.FailNow()
}
stringValue := jsonObject.String("keyString")
if (stringValue != "stringValue") {
t.Error("keyString was " + stringValue + " instead of \"stringValue\" ")
}
intValue := jsonObject.Int("keyInt")
if (intValue != 123) {
t.Errorf("keyInt was %d instead of 123 ", intValue)
}
float32Value := jsonObject.Float32("keyFloat32")
if (float32Value != 1.23) {
t.Errorf("keyFloat32 was %d instead of 1.23 ", float32Value)
}
float64Value := jsonObject.Float64("keyFloat64")
if (float64Value != 1.23) {
t.Errorf("keyFloat64 was %d instead of 1.23 ", float64Value)
}
boolValue := jsonObject.Bool("keyBool")
if (boolValue != true) {
t.Errorf("keyBool was %b instead of true ", boolValue)
}
jsonValue := jsonObject.JSONObject("keyJSONObject")
if (jsonValue.String("keyString") != "stringValue") {
t.Error("keyJSONObject didn't include the string strin")
}
}
func TestJSONArrayAllTypes(t *testing.T) {
jsonObject, err := NewJSONObjectFromString(jsonArrayAllTypes);
if err != nil {
t.Error("Parsing failed with error: " + err.Error())
t.FailNow()
}
stringValue := jsonObject.JSONArray("keyString").String(0)
if (stringValue != "stringValue") {
t.Error("keyString was " + stringValue + " instead of \"stringValue\" ")
}
intValue := jsonObject.JSONArray("keyInt").Int(0)
if (intValue != 123) {
t.Errorf("keyInt was %d instead of 123 ", intValue)
}
float32Value := jsonObject.JSONArray("keyFloat32").Float32(0)
if (float32Value != 1.23) {
t.Errorf("keyFloat32 was %d instead of 1.23 ", float32Value)
}
float64Value := jsonObject.JSONArray("keyFloat64").Float64(0)
if (float64Value != 1.23) {
t.Errorf("keyFloat64 was %d instead of 1.23 ", float64Value)
}
boolValue := jsonObject.JSONArray("keyBool").Bool(0)
if (boolValue != true) {
t.Errorf("keyBool was %b instead of true ", boolValue)
}
jsonValue := jsonObject.JSONArray("keyJSONObject").JSONObject(0)
if (jsonValue.String("keyString") != "stringValue") {
t.Error("keyJSONObject didn't include the string strin")
}
}
func TestParsingStringToJSONArray(t *testing.T) {
jsonArray, err := NewJSONArrayFromString(jsonArrayAsRoot);
if err != nil {
t.Error("Parsing failed with error: " + err.Error())
t.FailNow()
}
object0 := jsonArray.JSONObject(0)
if stringValue := object0.String("elemKeyString"); stringValue != "stringValue" {
t.Error("object[0]::elemKeyString was " + stringValue + " instead of \"stringValue\" ")
}
if intValue := object0.Int("elemKeyInt"); intValue != 0 {
t.Error("object[0]::elemKeyInt was " + strconv.Itoa(intValue) + " instead of 0 ")
}
object1 := jsonArray.Int(1)
if object1 != 123 {
t.Error("object1 was %d instead of 123 ", object1)
}
object2 := jsonArray.String(2)
if object2 != "stringElement" {
t.Error("object1 was " + object2 + " instead of \"stringElement\"")
}
}
func TestSet(t *testing.T) {
jsonobject := NewJSONObject();
jsonobject.Set("object1", "stringValue")
if value := jsonobject.String("object1"); value != "stringValue" {
t.Error("object1 was " + value + " instead of \"stringValue\"")
}
object2 := make([]float32, 5, 5)
object2[3] = 19.88
jsonobject.Set("object2", object2)
fmt.Println(jsonobject.AsString())
valueArray := jsonobject.JSONArray("object2")
if current := valueArray.Int(0); current != 0 {
t.Error("object2[0] was %d instead of 0", current)
}else if current := valueArray.Int(1); current != 0 {
t.Error("object2[1] was %d instead of 0", current)
}else if current := valueArray.Int(2); current != 0 {
t.Error("object2[2] was %d instead of 0", current)
}else if current := valueArray.Float32(3); current != 19.88 {
t.Error("object2[3] was %d instead of 1988", current)
}else if current := valueArray.Int(4); current != 0 {
t.Error("object2[4] was %d instead of 0", current)
}
}