-
Notifications
You must be signed in to change notification settings - Fork 24
/
simplejson_test.go
82 lines (70 loc) · 2.08 KB
/
simplejson_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
// this is a learning test
package turbo
import (
"testing"
sjson "github.com/bitly/go-simplejson"
"github.com/stretchr/testify/assert"
)
func TestLen(t *testing.T) {
json, _ := sjson.NewJson([]byte("{\"number\": \"123\"}"))
jmap, _ := json.Map()
assert.Equal(t, 1, len(jmap))
}
func TestGet(t *testing.T) {
json, _ := sjson.NewJson([]byte("{\"number\": \"123\"}"))
s, _ := json.Get("number").String()
assert.Equal(t, "123", s)
j, ok := json.CheckGet("number")
jStr, _ := j.String()
assert.Equal(t, true, ok)
assert.Equal(t, "123", jStr)
}
func TestGetUnknown(t *testing.T) {
json, _ := sjson.NewJson([]byte("{\"number\": \"123\"}"))
_, ok := json.CheckGet("a")
assert.Equal(t, false, ok)
}
func TestDel(t *testing.T) {
json, _ := sjson.NewJson([]byte("{\"number\": \"123\"}"))
jmap, _ := json.Map()
assert.Equal(t, 1, len(jmap))
json.Del("number")
assert.Equal(t, 0, len(jmap))
}
func TestSet(t *testing.T) {
json, _ := sjson.NewJson([]byte("{\"number\": \"123\"}"))
json.Set("number", int64(123))
n, _ := json.Get("number").Int64()
assert.Equal(t, int64(123), n)
s, _ := json.MarshalJSON()
assert.Equal(t, "{\"number\":123}", string(s))
}
func TestSetNull(t *testing.T) {
json, _ := sjson.NewJson([]byte("{\"number\": \"123\"}"))
json.Set("nullptr", nil)
s, _ := json.MarshalJSON()
assert.Equal(t, "{\"nullptr\":null,\"number\":\"123\"}", string(s))
}
func TestArray(t *testing.T) {
json, _ := sjson.NewJson([]byte("[\"123\",\"456\"]"))
arr, _ := json.Array()
assert.Equal(t, 2, len(arr))
assert.Equal(t, "123", arr[0].(string))
assert.Equal(t, "456", arr[1].(string))
}
func TestChangeArray(t *testing.T) {
json, _ := sjson.NewJson([]byte("[\"123\",\"456\"]"))
s, _ := json.MarshalJSON()
assert.Equal(t, "[\"123\",\"456\"]", string(s))
arr, _ := json.Array()
arr[0] = int64(111)
arr[1] = int64(222)
s, _ = json.MarshalJSON()
assert.Equal(t, "[111,222]", string(s))
}
func TestStructArray(t *testing.T) {
json, _ := sjson.NewJson([]byte("[{\"name\":\"apple\"}]"))
jo := json.GetIndex(0)
s, _ := jo.MarshalJSON()
assert.Equal(t, "{\"name\":\"apple\"}", string(s))
}