This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal_test.go
162 lines (135 loc) · 3.22 KB
/
marshal_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ion
import (
"bytes"
"math"
"testing"
"time"
)
func TestMarshalText(t *testing.T) {
test := func(v interface{}, eval string) {
t.Run(eval, func(t *testing.T) {
val, err := MarshalText(v)
if err != nil {
t.Fatal(err)
}
if string(val) != eval {
t.Errorf("expected '%v', got '%v'", eval, string(val))
}
})
}
test(nil, "null")
test(true, "true")
test(false, "false")
test(byte(42), "42")
test(-42, "-42")
test(uint64(math.MaxUint64), "18446744073709551615")
test(math.MinInt64, "-9223372036854775808")
test(42.0, "4.2e+1")
test(math.Inf(1), "+inf")
test(math.Inf(-1), "-inf")
test(math.NaN(), "nan")
test(MustParseDecimal("1.20"), "1.20")
test(time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC), "2010-01-01T00:00:00Z")
test("hello\tworld", "\"hello\\tworld\"")
test(struct{ A, B int }{42, 0}, "{A:42,B:0}")
test(struct {
A int `json:"val,ignoreme"`
B int `json:"-"`
C int `json:",omitempty"`
d int
}{42, 0, 0, 0}, "{val:42}")
test(struct{ V interface{} }{}, "{V:null}")
test(struct{ V interface{} }{"42"}, "{V:\"42\"}")
fourtytwo := 42
test(struct{ V *int }{}, "{V:null}")
test(struct{ V *int }{&fourtytwo}, "{V:42}")
test(map[string]int{"b": 2, "a": 1}, "{a:1,b:2}")
test(struct{ V []int }{}, "{V:null}")
test(struct{ V []int }{[]int{4, 2}}, "{V:[4,2]}")
test(struct{ V []byte }{}, "{V:null}")
test(struct{ V []byte }{[]byte{4, 2}}, "{V:{{BAI=}}}")
test(struct{ V [2]byte }{[2]byte{4, 2}}, "{V:[4,2]}")
}
func TestMarshalBinary(t *testing.T) {
test := func(v interface{}, name string, eval []byte) {
t.Run(name, func(t *testing.T) {
val, err := MarshalBinary(v)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, eval) {
t.Errorf("expected '%v', got '%v'", fmtbytes(eval), fmtbytes(val))
}
})
}
test(nil, "null", []byte{0xE0, 0x01, 0x00, 0xEA, 0x0F})
test(struct{ A, B int }{42, 0}, "{A:42,B:0}", []byte{
0xE0, 0x01, 0x00, 0xEA,
0xE9, 0x81, 0x83, 0xD6, 0x87, 0xB4, 0x81, 'A', 0x81, 'B',
0xD5,
0x8A, 0x21, 0x2A,
0x8B, 0x20,
})
}
func TestMarshalBinaryLST(t *testing.T) {
lsta := NewLocalSymbolTable(nil, nil)
lstb := NewLocalSymbolTable(nil, []string{
"A", "B",
})
test := func(v interface{}, name string, lst SymbolTable, eval []byte) {
t.Run(name, func(t *testing.T) {
val, err := MarshalBinaryLST(v, lst)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, eval) {
t.Errorf("expected '%v', got '%v'", fmtbytes(eval), fmtbytes(val))
}
})
}
test(nil, "null", lsta, []byte{0xE0, 0x01, 0x00, 0xEA, 0x0F})
test(struct{ A, B int }{42, 0}, "{A:42,B:0}", lstb, []byte{
0xE0, 0x01, 0x00, 0xEA,
0xE9, 0x81, 0x83, 0xD6, 0x87, 0xB4, 0x81, 'A', 0x81, 'B',
0xD5,
0x8A, 0x21, 0x2A,
0x8B, 0x20,
})
}
func TestMarshalNestedStructs(t *testing.T) {
type gp struct {
A int `json:"a"`
}
type gp2 struct {
B int `json:"b"`
}
type parent struct {
gp
*gp2
C int `json:"c"`
}
type root struct {
parent
D int `json:"d"`
}
v := root{
parent: parent{
gp: gp{
A: 1,
},
gp2: &gp2{
B: 2,
},
C: 3,
},
D: 4,
}
val, err := MarshalText(v)
if err != nil {
t.Fatal(err)
}
eval := "{a:1,b:2,c:3,d:4}"
if string(val) != eval {
t.Errorf("expected %v, got %v", eval, string(val))
}
}