Skip to content

Commit

Permalink
Merge pull request #288 from yjhmelody/update-test
Browse files Browse the repository at this point in the history
chore: use testify to replace check.v1
  • Loading branch information
vmihailenco authored Nov 30, 2020
2 parents 35fa1c1 + 005932b commit 7e765f5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 85 deletions.
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module github.com/vmihailenco/msgpack/v5

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.6.1
github.com/vmihailenco/tagparser v0.1.2
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
)

go 1.11
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc=
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
146 changes: 73 additions & 73 deletions msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,109 @@ package msgpack_test
import (
"bufio"
"bytes"
"fmt"
"math"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
. "gopkg.in/check.v1"

"github.com/stretchr/testify/suite"
"github.com/vmihailenco/msgpack/v5"
)

type nameStruct struct {
Name string
}

func TestGocheck(t *testing.T) { TestingT(t) }

type MsgpackTest struct {
suite.Suite

buf *bytes.Buffer
enc *msgpack.Encoder
dec *msgpack.Decoder
}

var _ = Suite(&MsgpackTest{})

func (t *MsgpackTest) SetUpTest(c *C) {
func (t *MsgpackTest) SetUpTest() {
t.buf = &bytes.Buffer{}
t.enc = msgpack.NewEncoder(t.buf)
t.dec = msgpack.NewDecoder(bufio.NewReader(t.buf))
}

func (t *MsgpackTest) TestDecodeNil(c *C) {
c.Assert(t.dec.Decode(nil), NotNil)
func (t *MsgpackTest) TestDecodeNil() {
t.NotNil(t.dec.Decode(nil))
}

func (t *MsgpackTest) TestTime(c *C) {
func (t *MsgpackTest) TestTime() {
in := time.Now()
var out time.Time
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out.Equal(in), Equals, true)

t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out))
t.True(out.Equal(in))

var zero time.Time
c.Assert(t.enc.Encode(zero), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out.Equal(zero), Equals, true)
c.Assert(out.IsZero(), Equals, true)
t.Nil(t.enc.Encode(zero))
t.Nil(t.dec.Decode(&out))
t.True(out.Equal(zero))
t.True(out.IsZero())

}

func (t *MsgpackTest) TestLargeBytes(c *C) {
func (t *MsgpackTest) TestLargeBytes() {
N := int(1e6)

src := bytes.Repeat([]byte{'1'}, N)
c.Assert(t.enc.Encode(src), IsNil)
t.Nil(t.enc.Encode(src))
var dst []byte
c.Assert(t.dec.Decode(&dst), IsNil)
c.Assert(dst, DeepEquals, src)
t.Nil(t.dec.Decode(&dst))
t.Equal(dst, src)
}

func (t *MsgpackTest) TestLargeString(c *C) {
func (t *MsgpackTest) TestLargeString() {
N := int(1e6)

src := string(bytes.Repeat([]byte{'1'}, N))
c.Assert(t.enc.Encode(src), IsNil)
t.Nil(t.enc.Encode(src))
var dst string
c.Assert(t.dec.Decode(&dst), IsNil)
c.Assert(dst, Equals, src)
t.Nil(t.dec.Decode(&dst))
t.Equal(dst, src)
}

func (t *MsgpackTest) TestSliceOfStructs(c *C) {
in := []*nameStruct{&nameStruct{"hello"}}
func (t *MsgpackTest) TestSliceOfStructs() {
in := []*nameStruct{{"hello"}}
var out []*nameStruct
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out, DeepEquals, in)
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out))
t.Equal(out, in)
}

func (t *MsgpackTest) TestMap(c *C) {
func (t *MsgpackTest) TestMap() {
for _, i := range []struct {
m map[string]string
b []byte
}{
{map[string]string{}, []byte{0x80}},
{map[string]string{"hello": "world"}, []byte{0x81, 0xa5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa5, 0x77, 0x6f, 0x72, 0x6c, 0x64}},
} {
c.Assert(t.enc.Encode(i.m), IsNil)
c.Assert(t.buf.Bytes(), DeepEquals, i.b, Commentf("err encoding %v", i.m))
t.Nil(t.enc.Encode(i.m))
t.Equal(t.buf.Bytes(), i.b, fmt.Errorf("err encoding %v", i.m))
var m map[string]string
c.Assert(t.dec.Decode(&m), IsNil)
c.Assert(m, DeepEquals, i.m)
t.Nil(t.dec.Decode(&m))
t.Equal(m, i.m)
}
}

func (t *MsgpackTest) TestStructNil(c *C) {
func (t *MsgpackTest) TestStructNil() {
var dst *nameStruct

c.Assert(t.enc.Encode(nameStruct{Name: "foo"}), IsNil)
c.Assert(t.dec.Decode(&dst), IsNil)
c.Assert(dst, Not(IsNil))
c.Assert(dst.Name, Equals, "foo")
t.Nil(t.enc.Encode(nameStruct{Name: "foo"}))
t.Nil(t.dec.Decode(&dst))
t.NotNil(dst)
t.Equal(dst.Name, "foo")
}

func (t *MsgpackTest) TestStructUnknownField(c *C) {
func (t *MsgpackTest) TestStructUnknownField() {
in := struct {
Field1 string
Field2 string
Expand All @@ -115,13 +115,13 @@ func (t *MsgpackTest) TestStructUnknownField(c *C) {
Field2: "value2",
Field3: "value3",
}
c.Assert(t.enc.Encode(in), IsNil)
t.Nil(t.enc.Encode(in))

out := struct {
Field2 string
}{}
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out.Field2, Equals, "value2")
t.Nil(t.dec.Decode(&out))
t.Equal(out.Field2, "value2")
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -151,45 +151,45 @@ func (s *coderStruct) DecodeMsgpack(dec *msgpack.Decoder) error {
return dec.Decode(&s.name)
}

func (t *MsgpackTest) TestCoder(c *C) {
func (t *MsgpackTest) TestCoder() {
in := &coderStruct{name: "hello"}
var out coderStruct
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out.Name(), Equals, "hello")
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out))
t.Equal(out.Name(), "hello")
}

func (t *MsgpackTest) TestNilCoder(c *C) {
func (t *MsgpackTest) TestNilCoder() {
in := &coderStruct{name: "hello"}
var out *coderStruct
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out.Name(), Equals, "hello")
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out))
t.Equal(out.Name(), "hello")
}

func (t *MsgpackTest) TestNilCoderValue(c *C) {
func (t *MsgpackTest) TestNilCoderValue() {
in := &coderStruct{name: "hello"}
var out *coderStruct
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.DecodeValue(reflect.ValueOf(&out)), IsNil)
c.Assert(out.Name(), Equals, "hello")
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.DecodeValue(reflect.ValueOf(&out)))
t.Equal(out.Name(), "hello")
}

func (t *MsgpackTest) TestPtrToCoder(c *C) {
func (t *MsgpackTest) TestPtrToCoder() {
in := &coderStruct{name: "hello"}
var out coderStruct
out2 := &out
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out2), IsNil)
c.Assert(out.Name(), Equals, "hello")
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out2))
t.Equal(out.Name(), "hello")
}

func (t *MsgpackTest) TestWrappedCoder(c *C) {
func (t *MsgpackTest) TestWrappedCoder() {
in := &wrapperStruct{coderStruct: coderStruct{name: "hello"}}
var out wrapperStruct
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out.Name(), Equals, "hello")
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out))
t.Equal(out.Name(), "hello")
}

//------------------------------------------------------------------------------
Expand All @@ -203,13 +203,13 @@ type struct1 struct {
Struct2 struct2
}

func (t *MsgpackTest) TestNestedStructs(c *C) {
func (t *MsgpackTest) TestNestedStructs() {
in := &struct1{Name: "hello", Struct2: struct2{Name: "world"}}
var out struct1
c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out.Name, Equals, in.Name)
c.Assert(out.Struct2.Name, Equals, in.Struct2.Name)
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out))
t.Equal(out.Name, in.Name)
t.Equal(out.Struct2.Name, in.Struct2.Name)
}

type Struct4 struct {
Expand Down Expand Up @@ -247,13 +247,13 @@ func TestEmbedding(t *testing.T) {
}
}

func (t *MsgpackTest) TestSliceNil(c *C) {
func (t *MsgpackTest) TestSliceNil() {
in := [][]*int{nil}
var out [][]*int

c.Assert(t.enc.Encode(in), IsNil)
c.Assert(t.dec.Decode(&out), IsNil)
c.Assert(out, DeepEquals, in)
t.Nil(t.enc.Encode(in))
t.Nil(t.dec.Decode(&out))
t.Equal(out, in)
}

//------------------------------------------------------------------------------
Expand Down

0 comments on commit 7e765f5

Please sign in to comment.