github.com/wI2L/jettison@v0.7.5-0.20230106001914-c70014c6417a/json_1.13_test.go (about)

     1  //go:build !go1.14
     2  
     3  package jettison
     4  
     5  import (
     6  	"encoding"
     7  	"encoding/json"
     8  	"fmt"
     9  	"net"
    10  	"strconv"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  type (
    16  	bvtm int
    17  	brtm int
    18  	bvjm string
    19  	brjm string
    20  	cvtm struct{ L, R string }
    21  	crtm struct{ L, R string }
    22  )
    23  
    24  func (m bvtm) MarshalText() ([]byte, error)  { return []byte(strconv.Itoa(int(m))), nil }
    25  func (m *brtm) MarshalText() ([]byte, error) { return []byte(strconv.Itoa(int(*m))), nil }
    26  func (m bvjm) MarshalJSON() ([]byte, error)  { return []byte(strconv.Quote(string(m))), nil }
    27  func (m *brjm) MarshalJSON() ([]byte, error) { return []byte(strconv.Quote(string(*m))), nil }
    28  func (m cvtm) MarshalText() ([]byte, error)  { return []byte(fmt.Sprintf("%s:%s", m.L, m.R)), nil }
    29  func (m *crtm) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf("%s:%s", m.L, m.R)), nil }
    30  
    31  // TestTextMarshalerMapKey tests the marshaling
    32  // of maps with key types that implements the
    33  // encoding.TextMarshaler interface
    34  func TestTextMarshalerMapKey(t *testing.T) {
    35  	var (
    36  		bval = bvtm(42)
    37  		bref = brtm(84)
    38  		cval = cvtm{L: "A", R: "B"}
    39  		cref = crtm{L: "A", R: "B"}
    40  		ip   = &net.IP{127, 0, 0, 1}
    41  	)
    42  	// The nil key cases, although supported by this library,
    43  	// isn't tested with verions of Go prior to 1.14, because
    44  	// the standard library panics on it, and thus, the results
    45  	// cannot be compared.
    46  	valid := []interface{}{
    47  		map[time.Time]string{
    48  			time.Now(): "now",
    49  			{}:         "",
    50  		},
    51  		map[*net.IP]string{
    52  			ip: "localhost",
    53  		},
    54  		map[cvtm]string{cval: "ab"},
    55  		map[*cvtm]string{
    56  			&cval: "ab",
    57  		},
    58  		map[*crtm]string{
    59  			&cref: "ab",
    60  		},
    61  		map[bvtm]string{bval: "42"},
    62  		map[*bvtm]string{
    63  			&bval: "42",
    64  		},
    65  		map[brtm]string{bref: "42"},
    66  		map[*brtm]string{
    67  			&bref: "42",
    68  		},
    69  	}
    70  	for _, v := range valid {
    71  		marshalCompare(t, v, "valid")
    72  	}
    73  	invalid := []interface{}{
    74  		// Non-pointer value of a pointer-receiver
    75  		// type isn't a valid map key type.
    76  		map[crtm]string{
    77  			{L: "A", R: "B"}: "ab",
    78  		},
    79  	}
    80  	for _, v := range invalid {
    81  		marshalCompareError(t, v, "invalid")
    82  	}
    83  }
    84  
    85  //nolint:godox
    86  func TestNilMarshaler(t *testing.T) {
    87  	testdata := []struct {
    88  		v interface{}
    89  	}{
    90  		// json.Marshaler
    91  		{struct{ M json.Marshaler }{M: nil}},
    92  		{struct{ M json.Marshaler }{(*niljsonm)(nil)}},
    93  		{struct{ M interface{} }{(*niljsonm)(nil)}},
    94  		{struct{ M *niljsonm }{M: nil}},
    95  		{json.Marshaler((*niljsonm)(nil))},
    96  		{(*niljsonm)(nil)},
    97  
    98  		// encoding.TextMarshaler
    99  		{struct{ M encoding.TextMarshaler }{(*niltextm)(nil)}},
   100  		{struct{ M interface{} }{(*niltextm)(nil)}},
   101  		{struct{ M *niltextm }{M: nil}},
   102  		{encoding.TextMarshaler((*niltextm)(nil))},
   103  		{(*niltextm)(nil)},
   104  
   105  		// The following case panics with versions of Go
   106  		// prior to 1.14. See this issue for reference:
   107  		// https://github.com/golang/go/issues/34235
   108  		// {struct{ M encoding.TextMarshaler }{M: nil}},
   109  
   110  		// jettison.Marshaler
   111  		{struct{ M comboMarshaler }{M: nil}},
   112  		{struct{ M comboMarshaler }{(*niljetim)(nil)}},
   113  		{struct{ M interface{} }{(*niljetim)(nil)}},
   114  		{struct{ M *niljetim }{M: nil}},
   115  		{comboMarshaler((*niljetim)(nil))},
   116  		{(*niljetim)(nil)},
   117  
   118  		// jettison.MarshalerCtx
   119  		{struct{ M comboMarshalerCtx }{M: nil}},
   120  		{struct{ M comboMarshalerCtx }{(*nilmjctx)(nil)}},
   121  		{struct{ M interface{} }{(*nilmjctx)(nil)}},
   122  		{struct{ M *nilmjctx }{M: nil}},
   123  		{comboMarshalerCtx((*nilmjctx)(nil))},
   124  		{(*nilmjctx)(nil)},
   125  	}
   126  	for _, e := range testdata {
   127  		marshalCompare(t, e.v, "nil-marshaler")
   128  	}
   129  }