forked from vmihailenco/msgpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_test.go
73 lines (62 loc) · 1.28 KB
/
ext_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 msgpack_test
import (
"testing"
"gopkg.in/vmihailenco/msgpack.v2"
"gopkg.in/vmihailenco/msgpack.v2/codes"
)
func init() {
msgpack.RegisterExt(127, extTest{})
}
func TestRegisterExtPanic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatalf("panic expected")
}
got := r.(error).Error()
wanted := "ext with id 127 is already registered"
if got != wanted {
t.Fatalf("got %q, wanted %q", got, wanted)
}
}()
msgpack.RegisterExt(127, extTest{})
}
type extTest struct {
S string
}
type extTest2 struct {
S string
}
func TestExt(t *testing.T) {
for _, v := range []interface{}{extTest{"hello"}, &extTest{"hello"}} {
b, err := msgpack.Marshal(v)
if err != nil {
t.Fatal(err)
}
var dst interface{}
err = msgpack.Unmarshal(b, &dst)
if err != nil {
t.Fatal(err)
}
v, ok := dst.(extTest)
if !ok {
t.Fatalf("got %#v, wanted extTest", dst)
}
if v.S != "hello" {
t.Fatalf("got %q, wanted hello", v.S)
}
}
}
func TestUnknownExt(t *testing.T) {
b := []byte{codes.FixExt1, 1, 0}
var dst interface{}
err := msgpack.Unmarshal(b, &dst)
if err == nil {
t.Fatalf("got nil, wanted error")
}
got := err.Error()
wanted := "msgpack: unregistered ext id 1"
if got != wanted {
t.Fatalf("got %q, wanted %q", got, wanted)
}
}