forked from bitly/go-simplejson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplejson_go11_test.go
97 lines (82 loc) · 2.37 KB
/
simplejson_go11_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
// +build go1.1
package simplejson
import (
"bytes"
"encoding/json"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewFromReader(t *testing.T) {
//Use New Constructor
buf := bytes.NewBuffer([]byte(`{
"test": {
"array": [1, "2", 3],
"arraywithsubs": [
{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}
],
"bignum": 9223372036854775807,
"uint64": 18446744073709551615
}
}`))
js, err := NewFromReader(buf)
//Standard Test Case
assert.NotEqual(t, nil, js)
assert.Equal(t, nil, err)
arr, _ := js.Get("test").Get("array").Array()
assert.NotEqual(t, nil, arr)
for i, v := range arr {
var iv int
switch v.(type) {
case json.Number:
i64, err := v.(json.Number).Int64()
assert.Equal(t, nil, err)
iv = int(i64)
case string:
iv, _ = strconv.Atoi(v.(string))
}
assert.Equal(t, i+1, iv)
}
ma := js.Get("test").Get("array").MustArray()
assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")})
mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")})
assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807))
assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615))
}
func TestSimplejsonGo11(t *testing.T) {
js, err := NewJson([]byte(`{
"test": {
"array": [1, "2", 3],
"arraywithsubs": [
{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}
],
"bignum": 9223372036854775807,
"uint64": 18446744073709551615
}
}`))
assert.NotEqual(t, nil, js)
assert.Equal(t, nil, err)
arr, _ := js.Get("test").Get("array").Array()
assert.NotEqual(t, nil, arr)
for i, v := range arr {
var iv int
switch v.(type) {
case json.Number:
i64, err := v.(json.Number).Int64()
assert.Equal(t, nil, err)
iv = int(i64)
case string:
iv, _ = strconv.Atoi(v.(string))
}
assert.Equal(t, i+1, iv)
}
ma := js.Get("test").Get("array").MustArray()
assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")})
mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")})
assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807))
assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615))
}