github.com/sandwich-go/boost@v1.3.29/xencoding/codec_test.go (about)

     1  package xencoding
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  )
     9  
    10  type MockCodec struct {
    11  	name string
    12  }
    13  
    14  func (mc *MockCodec) Marshal(context.Context, interface{}) ([]byte, error) { return nil, nil }
    15  func (mc *MockCodec) Unmarshal(context.Context, []byte, interface{}) error { return nil }
    16  func (mc *MockCodec) Name() string                                         { return mc.name }
    17  
    18  func TestCodec(t *testing.T) {
    19  	mc := &MockCodec{name: "mock_test"}
    20  
    21  	Convey("register codec will panic if codec nil or name empty", t, func() {
    22  		RegisterCodec(mc)
    23  		So(GetCodec(mc.Name()), ShouldEqual, mc)
    24  		So(func() { RegisterCodec(nil) }, ShouldPanic)
    25  		So(func() { RegisterCodec(&MockCodec{}) }, ShouldPanic)
    26  	})
    27  	Convey("get nil codec", t, func() {
    28  		So(FromContext(context.Background()), ShouldEqual, nil)
    29  	})
    30  
    31  	Convey("with golang standard context", t, func() {
    32  		ctx := WithContext(context.Background(), mc)
    33  		So(FromContext(ctx), ShouldEqual, mc)
    34  	})
    35  }