github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_json_unmarshaler_test.go (about) 1 package jzon 2 3 import ( 4 "errors" 5 "io" 6 "testing" 7 ) 8 9 type testJSONUnmarshaler struct { 10 data string 11 err error 12 } 13 14 func (t *testJSONUnmarshaler) UnmarshalJSON(data []byte) error { 15 t.data = string(data) 16 return t.err 17 } 18 19 func TestValDecoder_JsonUnmarshaler(t *testing.T) { 20 f := func(t *testing.T, data string, ex error, initValue string, initErr error) { 21 var p1 *testJSONUnmarshaler 22 var p2 *testJSONUnmarshaler 23 if initValue != "" { 24 b1 := testJSONUnmarshaler{ 25 data: initValue, 26 err: initErr, 27 } 28 p1 = &b1 29 b2 := testJSONUnmarshaler{ 30 data: initValue, 31 err: initErr, 32 } 33 p2 = &b2 34 } 35 checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2) 36 } 37 f2 := func(t *testing.T, data string, ex error, initErr error) { 38 f(t, data, ex, "dummy", initErr) 39 } 40 t.Run("nil pointer", func(t *testing.T) { 41 f(t, "null", ErrNilPointerReceiver, "", nil) 42 }) 43 t.Run("eof", func(t *testing.T) { 44 f2(t, " ", io.EOF, nil) 45 }) 46 t.Run("custom error", func(t *testing.T) { 47 f2(t, ` null `, errors.New("test"), errors.New("test")) 48 }) 49 t.Run("no error", func(t *testing.T) { 50 f2(t, ` null `, nil, nil) 51 }) 52 }