-
Notifications
You must be signed in to change notification settings - Fork 10
/
jsonw_test.go
187 lines (155 loc) · 4.84 KB
/
jsonw_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package jsonw
import (
"bytes"
"testing"
"encoding/json"
)
func TestInt(t *testing.T) {
const x = 100
w := NewInt(x)
if v, _ := w.GetInt(); v != x {
t.Errorf("%d != %d in GetInt() test", v, x)
}
}
func TestBigInt(t *testing.T) {
const x = 1<<62 + 55555
w := NewInt64(x)
if v, _ := w.GetInt64(); v != x {
t.Errorf("Big int test failed")
}
if v, _ := w.GetUint64(); v != x {
t.Errorf("Big uint test failed")
}
}
func TestFloat (t *testing.T) {
e := 2.71828183;
f := NewFloat64(e)
e2, err := f.GetFloat();
if err != nil {
t.Errorf("Getting a float failed: %s\n", err);
} else if (e - e2)*(e - e2) > .1 {
t.Errorf("Weird mismatch: %f v %f\n", e, e2);
}
jsonStream := []byte("{ \"e\" : 2.71828183 }");
var res interface{}
err = json.Unmarshal(jsonStream, &res);
if err != nil {
t.Errorf("cannot unmarshall: %s\n", err);
}
w := NewWrapper(res)
e2, err = w.AtKey("e").GetFloat()
if err != nil {
t.Errorf("Pass 2: Getting a float failed: %s\n", err);
} else if (e - e2)*(e - e2) > .1 {
t.Errorf("Pass 2: Weird mismatch: %f v %f\n", e, e2);
}
}
func TestBytes(t *testing.T) {
s := "hello world"
buf := bytes.NewBufferString(s)
bv := buf.Bytes()
w := NewWrapper(bv)
if out, err := w.GetString(); err != nil || out != s {
t.Errorf("failed to get %s back out", s)
}
}
func TestVoid(t *testing.T) {
w := NewDictionary()
/*
* { "uno" : "un",
* "dos" : "deux",
* "tres" : "trois",
* "quatro" : 4,
* "others" : [ 100, 101, 102 ]
* }
*/
w.SetKey("uno", NewString("un"))
w.SetKey("dos", NewString("deux"))
w.SetKey("tres", NewString("trois"))
w.SetKey("quatro", NewInt(4))
w.SetKey("others", NewArray(3))
w.AtKey("others").SetIndex(0, NewInt(100))
w.AtKey("others").SetIndex(1, NewInt(101))
w.AtKey("others").SetIndex(2, NewInt(102))
var e, e2 error
var s string
var i int
w.AtKey("dos").GetStringVoid(&s, &e)
if e != nil || s != "deux" {
t.Errorf("Failure for dos/deux")
}
w.AtKey("tres").GetIntVoid(&i, &e)
if e == nil {
t.Errorf("Expected an error on tres!")
}
expected := "<root>.tres: type error: wanted int, got string"
if e.Error() != expected {
t.Errorf("Wanted error '%s', but got '%s'", expected, e.Error())
}
w.AtKey("quatro").GetStringVoid(&s, &e)
if e.Error() != expected {
t.Errorf("Wanted error '%s' to stick around, but got '%s'",
expected, e.Error())
}
w.AtKey("others").AtIndex(2).GetStringVoid(&s, &e2)
expected = "<root>.others[2]: type error: wanted string, got int"
if e2 == nil || e2.Error() != expected {
t.Errorf("others[2]: Wanted error '%s', got '%s'",
expected, e2)
}
}
func TestDict(t *testing.T) {
w := NewDictionary()
const dog = 3333
var cat string = "meow"
w.SetKey("dog", NewInt(dog))
w.SetKey("cat", NewString(cat))
if v, _ := w.AtKey("dog").GetInt(); v != dog {
t.Errorf("Dictionary fail for 'dog': %d != %d", v, dog)
}
if v, _ := w.AtKey("cat").GetString(); v != cat {
t.Errorf("Dictionary fail for 'dog': %s != %s", v, cat)
}
const parrot = 3318
var sparrow string = "tweet"
w.SetKey("birds", NewDictionary())
w.AtKey("birds").SetKey("parrot", NewInt(parrot))
w.AtKey("birds").SetKey("sparrow", NewString(sparrow))
if v, _ := w.AtKey("birds").AtKey("sparrow").GetString(); v != sparrow {
t.Errorf("Dictionary fail for birds.sparrow: %s != %s", v, sparrow)
}
if v, _ := w.AtKey("birds").AtKey("parrot").GetInt(); v != parrot {
t.Errorf("Dictionary fail for birds.sparrow: %d != %d", v, parrot)
}
w.AtKey("birds").SetKey("waterfowl", NewArray(2))
w.AtKey("birds").AtKey("waterfowl").SetIndex(0, NewString("duck"))
w.AtKey("birds").AtKey("waterfowl").SetIndex(1, NewString("swan"))
if v, _ := w.AtKey("birds").AtKey("waterfowl").Len(); v != 2 {
t.Errorf("Wrong length for birds.waterfowl: %d v %d", v, 2)
}
if v, _ := w.AtKey("birds").AtKey("waterfowl").AtIndex(1).GetString(); v != "swan" {
t.Errorf("Wrong waterfowl in array: %s v swan (%s)", v)
}
}
func TestPath(t *testing.T) {
w := NewDictionary()
w.SetKey("dogs", NewArray(2))
w.AtKey("dogs").SetIndex(0, NewDictionary())
w.AtKey("dogs").SetIndex(1, NewDictionary())
w.AtKey("dogs").AtIndex(0).SetKey("age", NewInt(7))
w.AtKey("dogs").AtIndex(0).SetKey("name", NewDictionary())
w.AtKey("dogs").AtIndex(0).AtKey("name").SetKey("first", NewString("Fido"))
w.AtKey("dogs").AtIndex(1).SetKey("age", NewInt(3))
w.AtKey("dogs").AtIndex(1).SetKey("name", NewDictionary())
w.AtKey("dogs").AtIndex(1).AtKey("name").SetKey("first", NewString("Peanut"))
if v, e := w.AtPath("dogs.0.age").GetInt(); e != nil {
t.Errorf("Expected 7 for dogs.0.age, got Error: %v", e)
} else if int(v) != 7 {
t.Errorf("Expected 7 for dogs.0.age, got: %v", v)
}
if v, e := w.AtPath("dogs.1.name.first").GetString(); e != nil {
t.Errorf("Expected Peanut for dogs.1.name.first, got Error: %v", e)
} else if v != "Peanut" {
t.Errorf("Expected Peanut for dogs.1.name.first, got: %v", v)
}
}