github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/level_1/pointer_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 "github.com/v2pro/plz/msgfmt/jsonfmt" 6 "github.com/v2pro/plz/test" 7 "github.com/v2pro/plz/countlog" 8 "github.com/v2pro/plz/test/must" 9 "encoding/json" 10 "time" 11 "errors" 12 "io" 13 ) 14 15 func Test_pointer(t *testing.T) { 16 t.Run("ptr int", test.Case(func(ctx *countlog.Context) { 17 one := 1 18 must.Equal("1", jsonfmt.MarshalToString(&one)) 19 })) 20 t.Run("nil", test.Case(func(ctx *countlog.Context) { 21 var ptr *int 22 must.Equal("null", jsonfmt.MarshalToString(ptr)) 23 })) 24 t.Run("ptr eface", test.Case(func(ctx *countlog.Context) { 25 one := interface{}(1) 26 must.Equal("1", jsonfmt.MarshalToString(&one)) 27 })) 28 t.Run("ptr marshaler", test.Case(func(ctx *countlog.Context) { 29 marshaler := json.Marshaler(time.Time{}) 30 must.Equal(`"0001-01-01T00:00:00Z"`, jsonfmt.MarshalToString(&marshaler)) 31 })) 32 t.Run("ptr error", test.Case(func(ctx *countlog.Context) { 33 err := errors.New("hello") 34 must.Equal(`"hello"`, jsonfmt.MarshalToString(&err)) 35 })) 36 t.Run("ptr iface", test.Case(func(ctx *countlog.Context) { 37 closer := io.Closer(TestCloser(100)) 38 must.Equal(`100`, jsonfmt.MarshalToString(&closer)) 39 })) 40 }