forked from xing4git/go-simplejson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplejson_test.go
73 lines (57 loc) · 1.44 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
package simplejson
import (
"github.com/bmizerany/assert"
"io/ioutil"
"log"
"strconv"
"testing"
)
func TestSimplejson(t *testing.T) {
var ok bool
var err error
log.SetOutput(ioutil.Discard)
js, err := NewJson([]byte(`{
"test": {
"array": [1, "2", 3],
"int": 10,
"float": 5.150,
"bignum": 9223372036854775807,
"string": "simplejson",
"bool": true
}
}`))
assert.NotEqual(t, nil, js)
assert.Equal(t, nil, err)
_, ok = js.CheckGet("test")
assert.Equal(t, true, ok)
_, ok = js.CheckGet("missing_key")
assert.Equal(t, false, ok)
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)
}
i, _ := js.Get("test").Get("int").Int()
assert.Equal(t, 10, i)
f, _ := js.Get("test").Get("float").Float64()
assert.Equal(t, 5.150, f)
s, _ := js.Get("test").Get("string").String()
assert.Equal(t, "simplejson", s)
b, _ := js.Get("test").Get("bool").Bool()
assert.Equal(t, true, b)
mi := js.Get("test").Get("int").MustInt()
assert.Equal(t, 10, mi)
mi2 := js.Get("test").Get("missing_int").MustInt(5150)
assert.Equal(t, 5150, mi2)
ms := js.Get("test").Get("string").MustString()
assert.Equal(t, "simplejson", ms)
ms2 := js.Get("test").Get("missing_string").MustString("fyea")
assert.Equal(t, "fyea", ms2)
}