forked from bitly/go-simplejson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplejson_go10_test.go
87 lines (72 loc) · 1.89 KB
/
simplejson_go10_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
// +build !go1.1
package simplejson
import (
"bytes"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewFromReader(t *testing.T) {
buf := bytes.NewBuffer([]byte(`{
"test": {
"array": [1, "2", 3],
"arraywithsubs": [
{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}
],
"bignum": 8000000000
}
}`))
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 float64:
iv = int(v.(float64))
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{}{float64(1), "2", float64(3)})
mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)})
assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000))
}
func TestSimplejsonGo10(t *testing.T) {
js, err := NewJson([]byte(`{
"test": {
"array": [1, "2", 3],
"arraywithsubs": [
{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}
],
"bignum": 8000000000
}
}`))
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 float64:
iv = int(v.(float64))
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{}{float64(1), "2", float64(3)})
mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)})
assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000))
}