github.com/jxskiss/gopkg@v0.17.3/errcode/code_test.go (about)

     1  package errcode
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestCode(t *testing.T) {
    11  	reg := New()
    12  	dummy1 := reg.Register(100001, "")
    13  	dummy2 := reg.RegisterReserved(100002, "dummy2")
    14  
    15  	assert.Equal(t, dummy1.Error(), "[100001] (no message)")
    16  	assert.Equal(t, dummy2.Error(), "[100002] dummy2")
    17  
    18  	got1 := func() error { return dummy1 }()
    19  	got2 := func() error { return dummy2 }()
    20  	assert.True(t, Is(got1, dummy1))
    21  	assert.True(t, Is(got2, dummy2))
    22  	assert.True(t, IsErrCode(got1))
    23  	assert.True(t, IsErrCode(got2))
    24  
    25  	assert.False(t, Is(nil, dummy1))
    26  	assert.False(t, Is(errors.New("dummy1"), dummy1))
    27  	assert.False(t, IsErrCode(nil))
    28  	assert.False(t, IsErrCode(errors.New("dummy1")))
    29  
    30  	json1, _ := dummy1.MarshalJSON()
    31  	json2, _ := dummy2.MarshalJSON()
    32  	assert.Equal(t, []byte(`{"code":100001}`), json1)
    33  	assert.Equal(t, []byte(`{"code":100002,"message":"dummy2"}`), json2)
    34  }