You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
5.8 KiB

  1. package msgpack_test
  2. import (
  3. "bufio"
  4. "bytes"
  5. "reflect"
  6. "testing"
  7. "time"
  8. . "gopkg.in/check.v1"
  9. "github.com/vmihailenco/msgpack"
  10. )
  11. type nameStruct struct {
  12. Name string
  13. }
  14. func TestGocheck(t *testing.T) { TestingT(t) }
  15. type MsgpackTest struct {
  16. buf *bytes.Buffer
  17. enc *msgpack.Encoder
  18. dec *msgpack.Decoder
  19. }
  20. var _ = Suite(&MsgpackTest{})
  21. func (t *MsgpackTest) SetUpTest(c *C) {
  22. t.buf = &bytes.Buffer{}
  23. t.enc = msgpack.NewEncoder(t.buf)
  24. t.dec = msgpack.NewDecoder(bufio.NewReader(t.buf))
  25. }
  26. func (t *MsgpackTest) TestDecodeNil(c *C) {
  27. c.Assert(t.dec.Decode(nil), NotNil)
  28. }
  29. func (t *MsgpackTest) TestTime(c *C) {
  30. in := time.Now()
  31. var out time.Time
  32. c.Assert(t.enc.Encode(in), IsNil)
  33. c.Assert(t.dec.Decode(&out), IsNil)
  34. c.Assert(out.Equal(in), Equals, true)
  35. var zero time.Time
  36. c.Assert(t.enc.Encode(zero), IsNil)
  37. c.Assert(t.dec.Decode(&out), IsNil)
  38. c.Assert(out.Equal(zero), Equals, true)
  39. c.Assert(out.IsZero(), Equals, true)
  40. }
  41. func (t *MsgpackTest) TestLargeBytes(c *C) {
  42. N := int(1e6)
  43. src := bytes.Repeat([]byte{'1'}, N)
  44. c.Assert(t.enc.Encode(src), IsNil)
  45. var dst []byte
  46. c.Assert(t.dec.Decode(&dst), IsNil)
  47. c.Assert(dst, DeepEquals, src)
  48. }
  49. func (t *MsgpackTest) TestLargeString(c *C) {
  50. N := int(1e6)
  51. src := string(bytes.Repeat([]byte{'1'}, N))
  52. c.Assert(t.enc.Encode(src), IsNil)
  53. var dst string
  54. c.Assert(t.dec.Decode(&dst), IsNil)
  55. c.Assert(dst, Equals, src)
  56. }
  57. func (t *MsgpackTest) TestSliceOfStructs(c *C) {
  58. in := []*nameStruct{&nameStruct{"hello"}}
  59. var out []*nameStruct
  60. c.Assert(t.enc.Encode(in), IsNil)
  61. c.Assert(t.dec.Decode(&out), IsNil)
  62. c.Assert(out, DeepEquals, in)
  63. }
  64. func (t *MsgpackTest) TestMap(c *C) {
  65. for _, i := range []struct {
  66. m map[string]string
  67. b []byte
  68. }{
  69. {map[string]string{}, []byte{0x80}},
  70. {map[string]string{"hello": "world"}, []byte{0x81, 0xa5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa5, 0x77, 0x6f, 0x72, 0x6c, 0x64}},
  71. } {
  72. c.Assert(t.enc.Encode(i.m), IsNil)
  73. c.Assert(t.buf.Bytes(), DeepEquals, i.b, Commentf("err encoding %v", i.m))
  74. var m map[string]string
  75. c.Assert(t.dec.Decode(&m), IsNil)
  76. c.Assert(m, DeepEquals, i.m)
  77. }
  78. }
  79. func (t *MsgpackTest) TestStructNil(c *C) {
  80. var dst *nameStruct
  81. c.Assert(t.enc.Encode(nameStruct{Name: "foo"}), IsNil)
  82. c.Assert(t.dec.Decode(&dst), IsNil)
  83. c.Assert(dst, Not(IsNil))
  84. c.Assert(dst.Name, Equals, "foo")
  85. }
  86. func (t *MsgpackTest) TestStructUnknownField(c *C) {
  87. in := struct {
  88. Field1 string
  89. Field2 string
  90. Field3 string
  91. }{
  92. Field1: "value1",
  93. Field2: "value2",
  94. Field3: "value3",
  95. }
  96. c.Assert(t.enc.Encode(in), IsNil)
  97. out := struct {
  98. Field2 string
  99. }{}
  100. c.Assert(t.dec.Decode(&out), IsNil)
  101. c.Assert(out.Field2, Equals, "value2")
  102. }
  103. //------------------------------------------------------------------------------
  104. type coderStruct struct {
  105. name string
  106. }
  107. type wrapperStruct struct {
  108. coderStruct
  109. }
  110. var (
  111. _ msgpack.CustomEncoder = (*coderStruct)(nil)
  112. _ msgpack.CustomDecoder = (*coderStruct)(nil)
  113. )
  114. func (s *coderStruct) Name() string {
  115. return s.name
  116. }
  117. func (s *coderStruct) EncodeMsgpack(enc *msgpack.Encoder) error {
  118. return enc.Encode(s.name)
  119. }
  120. func (s *coderStruct) DecodeMsgpack(dec *msgpack.Decoder) error {
  121. return dec.Decode(&s.name)
  122. }
  123. func (t *MsgpackTest) TestCoder(c *C) {
  124. in := &coderStruct{name: "hello"}
  125. var out coderStruct
  126. c.Assert(t.enc.Encode(in), IsNil)
  127. c.Assert(t.dec.Decode(&out), IsNil)
  128. c.Assert(out.Name(), Equals, "hello")
  129. }
  130. func (t *MsgpackTest) TestNilCoder(c *C) {
  131. in := &coderStruct{name: "hello"}
  132. var out *coderStruct
  133. c.Assert(t.enc.Encode(in), IsNil)
  134. c.Assert(t.dec.Decode(&out), IsNil)
  135. c.Assert(out.Name(), Equals, "hello")
  136. }
  137. func (t *MsgpackTest) TestNilCoderValue(c *C) {
  138. in := &coderStruct{name: "hello"}
  139. var out *coderStruct
  140. c.Assert(t.enc.Encode(in), IsNil)
  141. c.Assert(t.dec.DecodeValue(reflect.ValueOf(&out)), IsNil)
  142. c.Assert(out.Name(), Equals, "hello")
  143. }
  144. func (t *MsgpackTest) TestPtrToCoder(c *C) {
  145. in := &coderStruct{name: "hello"}
  146. var out coderStruct
  147. out2 := &out
  148. c.Assert(t.enc.Encode(in), IsNil)
  149. c.Assert(t.dec.Decode(&out2), IsNil)
  150. c.Assert(out.Name(), Equals, "hello")
  151. }
  152. func (t *MsgpackTest) TestWrappedCoder(c *C) {
  153. in := &wrapperStruct{coderStruct: coderStruct{name: "hello"}}
  154. var out wrapperStruct
  155. c.Assert(t.enc.Encode(in), IsNil)
  156. c.Assert(t.dec.Decode(&out), IsNil)
  157. c.Assert(out.Name(), Equals, "hello")
  158. }
  159. //------------------------------------------------------------------------------
  160. type struct2 struct {
  161. Name string
  162. }
  163. type struct1 struct {
  164. Name string
  165. Struct2 struct2
  166. }
  167. func (t *MsgpackTest) TestNestedStructs(c *C) {
  168. in := &struct1{Name: "hello", Struct2: struct2{Name: "world"}}
  169. var out struct1
  170. c.Assert(t.enc.Encode(in), IsNil)
  171. c.Assert(t.dec.Decode(&out), IsNil)
  172. c.Assert(out.Name, Equals, in.Name)
  173. c.Assert(out.Struct2.Name, Equals, in.Struct2.Name)
  174. }
  175. type Struct4 struct {
  176. Name2 string
  177. }
  178. type Struct3 struct {
  179. Struct4
  180. Name1 string
  181. }
  182. func TestEmbedding(t *testing.T) {
  183. in := &Struct3{
  184. Name1: "hello",
  185. Struct4: Struct4{
  186. Name2: "world",
  187. },
  188. }
  189. var out Struct3
  190. b, err := msgpack.Marshal(in)
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. err = msgpack.Unmarshal(b, &out)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. if out.Name1 != in.Name1 {
  199. t.Fatalf("")
  200. }
  201. if out.Name2 != in.Name2 {
  202. t.Fatalf("")
  203. }
  204. }
  205. func (t *MsgpackTest) TestSliceNil(c *C) {
  206. in := [][]*int{nil}
  207. var out [][]*int
  208. c.Assert(t.enc.Encode(in), IsNil)
  209. c.Assert(t.dec.Decode(&out), IsNil)
  210. c.Assert(out, DeepEquals, in)
  211. }
  212. //------------------------------------------------------------------------------
  213. func (t *MsgpackTest) TestMapStringInterface(c *C) {
  214. in := map[string]interface{}{
  215. "foo": "bar",
  216. "hello": map[string]interface{}{
  217. "foo": "bar",
  218. },
  219. }
  220. var out map[string]interface{}
  221. c.Assert(t.enc.Encode(in), IsNil)
  222. c.Assert(t.dec.Decode(&out), IsNil)
  223. c.Assert(out["foo"], Equals, "bar")
  224. mm := out["hello"].(map[string]interface{})
  225. c.Assert(mm["foo"], Equals, "bar")
  226. }