-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_test.go
91 lines (82 loc) · 2.45 KB
/
decode_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
package message_test
import (
"testing"
message "github.com/veselj/encoding-message"
. "github.com/onsi/gomega"
)
func Test_ShouldFailWrongOutParam(t *testing.T) {
g := NewGomegaWithT(t)
// nil out param
err := message.Unmarshal([]byte("0"), nil)
g.Expect(err).NotTo(BeNil())
g.Expect(err.Error()).To(Equal("input not a pointer or nil"))
// out param not a pointer
var i int
err = message.Unmarshal([]byte("03"), i)
g.Expect(err).NotTo(BeNil())
g.Expect(err.Error()).To(Equal("input not a pointer or nil"))
// out param not a struct
err = message.Unmarshal([]byte("03"), &i)
g.Expect(err).NotTo(BeNil())
g.Expect(err.Error()).To(Equal("input must point to a struct"))
}
func Test_SingleItem_IntStruct(t *testing.T) {
g := NewGomegaWithT(t)
type intS struct {
IntField int `type:"int" len:"3" padding:"0"`
}
var ints intS
err := message.Unmarshal([]byte(`001`), &ints)
g.Expect(err).To(BeNil())
g.Expect(ints.IntField).To(Equal(1))
}
func Test_IntAndStringStruct(t *testing.T) {
g := NewGomegaWithT(t)
type Message struct {
IntField int `len:"2" padding:"0"`
StrField string `len:"5" padding:" "`
}
var msg Message
err := message.Unmarshal([]byte(`03hello`), &msg)
g.Expect(err).To(BeNil())
g.Expect(msg.IntField).To(Equal(3))
g.Expect(msg.StrField).To(Equal("hello"))
}
func Test_SeparatedStruct(t *testing.T) {
g := NewGomegaWithT(t)
type Message struct {
IntField int `sep:"\x1c" padding:"0"`
StrField string `sep:"\x1c" padding:" "`
}
var msg Message
err := message.Unmarshal([]byte(`045greetings`), &msg)
g.Expect(err).To(BeNil())
g.Expect(msg.IntField).To(Equal(45))
g.Expect(msg.StrField).To(Equal("greetings"))
}
func Test_StructSlice_String(t *testing.T) {
g := NewGomegaWithT(t)
type Message struct {
StrFields []string `sep:"\x1c" padding:" "`
}
var msg Message
err := message.Unmarshal([]byte(`123hi`), &msg)
g.Expect(err).To(BeNil())
g.Expect(len(msg.StrFields)).To(Equal(2))
g.Expect(msg.StrFields[0]).To(Equal("123"))
g.Expect(msg.StrFields[1]).To(Equal("hi"))
}
func Test_StructSlice_Int(t *testing.T) {
g := NewGomegaWithT(t)
type Message struct {
IntFields []int `sep:"\x1c" padding:" "`
}
var msg Message
err := message.Unmarshal([]byte(`04040-24`), &msg)
g.Expect(err).To(BeNil())
g.Expect(len(msg.IntFields)).To(Equal(4))
g.Expect(msg.IntFields[0]).To(Equal(0))
g.Expect(msg.IntFields[1]).To(Equal(4))
g.Expect(msg.IntFields[2]).To(Equal(40))
g.Expect(msg.IntFields[3]).To(Equal(-24))
}