github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/level_0/error_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/msgfmt/jsonfmt"
     6  	"errors"
     7  	"github.com/v2pro/plz/test"
     8  	"github.com/v2pro/plz/countlog"
     9  	"github.com/v2pro/plz/test/must"
    10  )
    11  
    12  type testError struct {
    13  	onePtr *int
    14  }
    15  
    16  func (err testError) Error() string {
    17  	return "hello"
    18  }
    19  
    20  func Test_error(t *testing.T) {
    21  	t.Run("ptr struct", test.Case(func(ctx *countlog.Context) {
    22  		must.Equal(`"hello"`, jsonfmt.MarshalToString(errors.New("hello")))
    23  	}))
    24  	t.Run("single ptr struct", test.Case(func(ctx *countlog.Context) {
    25  		type TestObject struct {
    26  			Err testError
    27  		}
    28  		must.Equal(`{"Err":"hello"}`, jsonfmt.MarshalToString(TestObject{}))
    29  	}))
    30  }