-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
184 lines (162 loc) · 5.01 KB
/
json_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
package simplejson
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var sampleJSON string = `{
"Actors": [
{
"name": "Tom Cruise",
"age": 56,
"Born At": "Syracuse, NY",
"Birthdate": "July 3, 1962",
"hasChildren":true,
"hasTwitterAccount":true,
"hasGreyHair":false,
"photo": "https://jsonformatter.org/img/tom-cruise.jpg"
},
{
"name": "Robert Downey Jr.",
"age": 53,
"Born At": "New York City, NY",
"Birthdate": "April 4, 1965",
"photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg"
}
]
}
`
// TestLoads parses a valid json and gets keys.
func TestLoads(t *testing.T) {
d, err := Loads([]byte(sampleJSON))
assert.Nil(t, err)
data := d.Get("Actors").Get("", 0).Get("name")
assert.Equal(t, data.String(), "\"Tom Cruise\"", "Should equals Tom Cruise")
}
// TestLoadsEmptyString tests the String method on an empty type. Should return "" for a non-existing key.
func TestLoadsEmptyString(t *testing.T) {
d, err := Loads([]byte(sampleJSON))
assert.Nil(t, err)
data := d.Get("Actors").Get("", 0).Get("names").Get("Foo").Get("Bar")
assert.Equal(t, data.String(), "", "Should equals \"\"")
}
// TestEmpty tests Get on an invalid key for the return type empty. This will prevent the nil pointer dereference error when the key doesn't exist.
func TestEmpty(t *testing.T) {
d, err := Loads([]byte(sampleJSON))
assert.Nil(t, err)
d1 := d.Get("Actors").Get("", 0).Get("names").Get("foo")
e := new(empty)
if d1 != nil {
assert.IsType(t, e, d1)
assert.Nil(t, d1.Bytes())
assert.Equal(t, d1.String(), "")
} else {
assert.Fail(t, "This shouldn't be nil")
}
}
// TestLoadsFail parses an invalid json to test the failure to parse.
func TestLoadsFail(t *testing.T) {
_, err := Loads([]byte(fmt.Sprintf("%s%s", sampleJSON, "invalidJson")))
assert.EqualError(t, err, "invalid character 'i' after top-level value")
}
// TestLoad does simplejson.Load() to load json from disk.
func TestLoad(t *testing.T) {
fd, err := os.Open("samplejson.json")
assert.Nil(t, err)
d1, err := Load(fd)
assert.Nil(t, err)
assert.Equal(t, d1.Get("", 2).Get("tags").Get("", 3).String(), "\"incididunt\"")
fd.Close()
}
// TestLoadFail tests the ioutil.ReadAll failure after trying to read from a closed fd.
func TestLoadFail(t *testing.T) {
fd, err := os.Open("samplejson.json")
fd.Close()
assert.Nil(t, err)
_, err = Load(fd)
assert.NotNil(t, err)
assert.EqualError(t, err, "read samplejson.json: file already closed")
}
// TestLoadKeyFail tests the key error.
func TestLoadsKeyFail(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("Expected recovery from Key Error. Error is: %s", r)
assert.EqualError(t, r.(error), "key error: names not found")
}
}()
d, err := Loads([]byte(sampleJSON))
if err != nil {
panic(err)
}
d.Get("Actors").Get("", 0).Get("names")
}
// TestStringFail tests the json Marshal error in the String method.
func TestStringFail(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("Expected recovery from Json Marshal in String() method. Error is: %s", r)
assert.EqualError(t, r.(error), "json: unsupported type: chan int")
}
}()
d := new(data)
d.jsonData = make(chan int)
t.Logf("%s", d.String())
}
// TestDumpAsBytesFail tests the json Marshal error in the String method.
func TestDumpAsBytesFail(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("Expected recovery from Json Marshal in DumpAsBytes() method. Error is: %s", r)
assert.EqualError(t, r.(error), "json: unsupported type: chan int")
}
}()
d := new(data)
d.jsonData = make(chan int)
t.Logf("%s", d.Bytes())
}
// TestEmptyByte tests the Bytes() on the empty type.
func TestEmptyByte(t *testing.T) {
d, err := Loads([]byte(sampleJSON))
assert.Nil(t, err)
data := d.Get("Actors").Get("", 0).Get("names").Get("Foo").Bytes()
assert.Nil(t, data)
}
// TestInvalidIndex tests for out of range index number.
func TestInvalidIndex(t *testing.T) {
d, err := Loads([]byte(sampleJSON))
assert.Nil(t, err)
data := d.Get("Actors").Get("", 200).Get("names").Get("Foo").Bytes()
assert.Nil(t, data)
}
// TestGetFail tests "Not Implemented" part of the Get method.
func TestGetFail(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Logf("Expected recovery from String() method. Error is: %s", r)
assert.EqualError(t, r.(error), "not implemented for chan int")
}
}()
d := new(data)
d.jsonData = make(chan int)
t.Logf("Output is %s", d.Get("Invalid"))
}
func TestJsonTypes(t *testing.T) {
assert := assert.New(t)
val, err := Dumps(10000)
assert.Nil(err)
data, err := Loads(val)
assert.Nil(err)
assert.EqualValuesf(data.Bytes(), []byte{0x31, 0x30, 0x30, 0x30, 0x30}, "10000 in byte slice")
val, err = Dumps(true)
assert.Nil(err)
data, err = Loads(val)
assert.Nil(err)
assert.EqualValuesf(data.Bytes(), []byte{0x74, 0x72, 0x75, 0x65}, "true in byte slice")
val, err = Dumps(nil)
assert.Nil(err)
data, err = Loads(val)
assert.Nil(err)
assert.EqualValuesf(data.Bytes(), []byte{0x6e, 0x75, 0x6c, 0x6c}, "null in byte slice")
}