forked from gocraft/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
54 lines (45 loc) · 914 Bytes
/
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
package meta
import (
"encoding/json"
"testing"
)
type jsonObj struct {
Str String
I Int64
UI Uint64
F Float64
B Bool
}
func TestJSONUnmarshal(t *testing.T) {
obj := jsonObj{
Str: NewString("hello world"),
I: NewInt64(11),
UI: NewUint64(12),
F: NewFloat64(13),
B: NewBool(true),
}
bs, err := json.Marshal(obj)
if err != nil {
t.Fatal(err)
}
var decode jsonObj
err = json.Unmarshal(bs, &decode)
if err != nil {
t.Fatal(err)
}
assertEqual(t, decode.Str.Val, "hello world")
assertEqual(t, decode.I.Val, int64(11))
assertEqual(t, decode.UI.Val, uint64(12))
assertEqual(t, decode.F.Val, float64(13))
assertEqual(t, decode.B.Val, true)
}
func TestJSONNulls(t *testing.T) {
str := []byte(`{"str": null}`)
var obj jsonObj
err := json.Unmarshal(str, &obj)
if err != nil {
t.Fatal(err)
}
assertEqual(t, obj.Str.Null, true)
assertEqual(t, obj.Str.Val, "")
}