同步操作将从 邢楠/msgpack 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package msgpack_testimport ("bufio""bytes""reflect""testing""time". "gopkg.in/check.v1""github.com/vmihailenco/msgpack")type nameStruct struct {Name string}func TestGocheck(t *testing.T) { TestingT(t) }type MsgpackTest struct {buf *bytes.Bufferenc *msgpack.Encoderdec *msgpack.Decoder}var _ = Suite(&MsgpackTest{})func (t *MsgpackTest) SetUpTest(c *C) {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) TestTime(c *C) {in := time.Now()var out time.Timec.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out.Equal(in), Equals, true)var zero time.Timec.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)}func (t *MsgpackTest) TestLargeBytes(c *C) {N := int(1e6)src := bytes.Repeat([]byte{'1'}, N)c.Assert(t.enc.Encode(src), IsNil)var dst []bytec.Assert(t.dec.Decode(&dst), IsNil)c.Assert(dst, DeepEquals, src)}func (t *MsgpackTest) TestLargeString(c *C) {N := int(1e6)src := string(bytes.Repeat([]byte{'1'}, N))c.Assert(t.enc.Encode(src), IsNil)var dst stringc.Assert(t.dec.Decode(&dst), IsNil)c.Assert(dst, Equals, src)}func (t *MsgpackTest) TestSliceOfStructs(c *C) {in := []*nameStruct{&nameStruct{"hello"}}var out []*nameStructc.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out, DeepEquals, in)}func (t *MsgpackTest) TestMap(c *C) {for _, i := range []struct {m map[string]stringb []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))var m map[string]stringc.Assert(t.dec.Decode(&m), IsNil)c.Assert(m, DeepEquals, i.m)}}func (t *MsgpackTest) TestStructNil(c *C) {var dst *nameStructc.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")}func (t *MsgpackTest) TestStructUnknownField(c *C) {in := struct {Field1 stringField2 stringField3 string}{Field1: "value1",Field2: "value2",Field3: "value3",}c.Assert(t.enc.Encode(in), IsNil)out := struct {Field2 string}{}c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out.Field2, Equals, "value2")}//------------------------------------------------------------------------------type coderStruct struct {name string}type wrapperStruct struct {coderStruct}var (_ msgpack.CustomEncoder = (*coderStruct)(nil)_ msgpack.CustomDecoder = (*coderStruct)(nil))func (s *coderStruct) Name() string {return s.name}func (s *coderStruct) EncodeMsgpack(enc *msgpack.Encoder) error {return enc.Encode(s.name)}func (s *coderStruct) DecodeMsgpack(dec *msgpack.Decoder) error {return dec.Decode(&s.name)}func (t *MsgpackTest) TestCoder(c *C) {in := &coderStruct{name: "hello"}var out coderStructc.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out.Name(), Equals, "hello")}func (t *MsgpackTest) TestNilCoder(c *C) {in := &coderStruct{name: "hello"}var out *coderStructc.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out.Name(), Equals, "hello")}func (t *MsgpackTest) TestNilCoderValue(c *C) {in := &coderStruct{name: "hello"}var out *coderStructc.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.DecodeValue(reflect.ValueOf(&out)), IsNil)c.Assert(out.Name(), Equals, "hello")}func (t *MsgpackTest) TestPtrToCoder(c *C) {in := &coderStruct{name: "hello"}var out coderStructout2 := &outc.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out2), IsNil)c.Assert(out.Name(), Equals, "hello")}func (t *MsgpackTest) TestWrappedCoder(c *C) {in := &wrapperStruct{coderStruct: coderStruct{name: "hello"}}var out wrapperStructc.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out.Name(), Equals, "hello")}//------------------------------------------------------------------------------type struct2 struct {Name string}type struct1 struct {Name stringStruct2 struct2}func (t *MsgpackTest) TestNestedStructs(c *C) {in := &struct1{Name: "hello", Struct2: struct2{Name: "world"}}var out struct1c.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)}type Struct4 struct {Name2 string}type Struct3 struct {Struct4Name1 string}func TestEmbedding(t *testing.T) {in := &Struct3{Name1: "hello",Struct4: Struct4{Name2: "world",},}var out Struct3b, err := msgpack.Marshal(in)if err != nil {t.Fatal(err)}err = msgpack.Unmarshal(b, &out)if err != nil {t.Fatal(err)}if out.Name1 != in.Name1 {t.Fatalf("")}if out.Name2 != in.Name2 {t.Fatalf("")}}func (t *MsgpackTest) TestSliceNil(c *C) {in := [][]*int{nil}var out [][]*intc.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out, DeepEquals, in)}//------------------------------------------------------------------------------func (t *MsgpackTest) TestMapStringInterface(c *C) {in := map[string]interface{}{"foo": "bar","hello": map[string]interface{}{"foo": "bar",},}var out map[string]interface{}c.Assert(t.enc.Encode(in), IsNil)c.Assert(t.dec.Decode(&out), IsNil)c.Assert(out["foo"], Equals, "bar")mm := out["hello"].(map[string]interface{})c.Assert(mm["foo"], Equals, "bar")}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。