This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
forked from dozen/ruby-marshal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal_test.go
268 lines (240 loc) · 5.58 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package rbmarshal
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"testing"
)
const (
// null
Null = "040830"
// "hoge"
String = "0408492209686f6765063a064554"
// :name
SymName = "04083a096e616d65"
// 0
Int0 = "04086900"
// 1
Int1 = "04086906"
// 2
Int2 = "04086907"
// -5
IntM5 = "040869f6"
// 777
Int777 = "040869020903"
// -777
IntM777 = "040869fef7fc"
// 65537
Int65537 = "04086903010001"
// -65537
IntM65537 = "040869fdfffffe"
// { host: "localhost", db: 1 }
Hash1 = "04087b073a09686f737449220e6c6f63616c686f7374063a0645543a0764626906"
// { "name" => "taro", "age" => 21 }
Hash2 = "04087b074922096e616d65063a0645544922097461726f063b0054492208616765063b0054691a"
// { user: { name: "matsumoto-yasunori", age: 57 }, job: "voice-actor" }
Hash3 = "04087b073a09757365727b073a096e616d654922176d617473756d6f746f2d796173756e6f7269063a0645543a08616765693e3a086a6f62492210766f6963652d6163746f72063b0754"
)
var (
// 1612874507
PosBignum = []byte{0x04, 0x08, 0x6c, 0x2b, 0x07, 0x0b, 0x83, 0x22, 0x60}
// -15241578750190521
NegBignum = []byte{0x04, 0x08, 0x6c, 0x2d, 0x09, 0xb9, 0xa3, 0x38, 0x97, 0x22, 0x26, 0x36, 0x00}
)
func TestDecodeNull(t *testing.T) {
b, err := hex.DecodeString(Null)
if err != nil {
t.Skip(err.Error())
}
var v interface{}
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != nil {
t.Errorf("not nil. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeString(t *testing.T) {
b, err := hex.DecodeString(String)
if err != nil {
t.Skip(err.Error())
}
var v string
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != "hoge" {
t.Errorf("not \"hoge\". Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeSymName(t *testing.T) {
b, err := hex.DecodeString(SymName)
if err != nil {
t.Skip(err.Error())
}
var v string
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != "name" {
t.Errorf("not \"name\". Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeInt0(t *testing.T) {
b, err := hex.DecodeString(Int0)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != 0 {
t.Errorf("not 0. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeInt1(t *testing.T) {
b, err := hex.DecodeString(Int1)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != 1 {
t.Errorf("not 1. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeInt2(t *testing.T) {
b, err := hex.DecodeString(Int2)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != 2 {
t.Errorf("not 2. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeIntM5(t *testing.T) {
b, err := hex.DecodeString(IntM5)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != -5 {
t.Errorf("not 5. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeInt777(t *testing.T) {
b, err := hex.DecodeString(Int777)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != 777 {
t.Errorf("not 777. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeIntM777(t *testing.T) {
b, err := hex.DecodeString(IntM777)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != -777 {
t.Errorf("not -777. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeInt65537(t *testing.T) {
b, err := hex.DecodeString(Int65537)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != 65537 {
t.Errorf("not 0. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeIntM65537(t *testing.T) {
b, err := hex.DecodeString(IntM65537)
if err != nil {
t.Skip(err.Error())
}
var v int
NewDecoder(bytes.NewReader(b)).Decode(&v)
if v != -65537 {
t.Errorf("not -65537. Type: %T\tValue: %#v", v, v)
}
}
func TestDecodeHash1(t *testing.T) {
b, err := hex.DecodeString(Hash1)
if err != nil {
t.Skip(err.Error())
}
var v RedisConf
NewDecoder(bytes.NewReader(b)).Decode(&v)
if !(v.Host == "localhost" && v.DB == 1) {
t.Errorf("not matched. Value: %#v", v)
}
}
type RedisConf struct {
Host string `ruby:"host"`
DB int `ruby:"db"`
}
func TestDecodeHash2(t *testing.T) {
b, err := hex.DecodeString(Hash2)
if err != nil {
t.Skip(err.Error())
}
var v User
NewDecoder(bytes.NewReader(b)).Decode(&v)
if !(v.Name == "taro" && v.Age == 21) {
t.Errorf("not matched. Value: %#v", v)
}
}
func TestDecodeHash3(t *testing.T) {
b, err := hex.DecodeString(Hash3)
if err != nil {
t.Skip(err.Error())
}
var v Profile
NewDecoder(bytes.NewReader(b)).Decode(&v)
if !(v.Job == "voice-actor" && v.User.Name == "matsumoto-yasunori" && v.User.Age == 57) {
t.Errorf("not matched. Value: %#v", v)
}
}
func TestDecodeBignum(t *testing.T) {
var v *big.Int
NewDecoder(bytes.NewReader(PosBignum)).Decode(&v)
want := big.NewInt(1612874507)
if v.Cmp(want) != 0 {
t.Errorf("not matched. Value: %#v", v)
}
}
func TestDecodeNegativeBignum(t *testing.T) {
var v *big.Int
NewDecoder(bytes.NewReader(NegBignum)).Decode(&v)
want := big.NewInt(-15241578750190521)
if v.Cmp(want) != 0 {
t.Errorf("not matched. Value: %#v", v)
}
}
type Profile struct {
User User `ruby:"user"`
Job string `ruby:"job"`
}
type User struct {
Name string `ruby:"name"`
Age int `ruby:"age"`
}
func TestNewEncoder(t *testing.T) {
w := bytes.NewBuffer([]byte{})
e := NewEncoder(w)
v := "めっちゃ日本語"
if err := e.Encode(&v); err != nil {
t.Error(err.Error())
}
encoded := w.Bytes()
fmt.Printf("encoded:\t%#v\n", encoded)
fmt.Printf("encoded:\t%x\n", encoded)
var str string
NewDecoder(bytes.NewReader(encoded)).Decode(&str)
fmt.Printf("%#x\n%#v\n", str, str)
}