github.com/wI2L/jettison@v0.7.5-0.20230106001914-c70014c6417a/json_1.14_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  	valid := []interface{}{
    43  		map[time.Time]string{
    44  			time.Now(): "now",
    45  			{}:         "",
    46  		},
    47  		map[*net.IP]string{
    48  			ip:  "localhost",
    49  			nil: "",
    50  		},
    51  		map[cvtm]string{cval: "ab"},
    52  		map[*cvtm]string{
    53  			&cval: "ab",
    54  			nil:   "ba",
    55  		},
    56  		map[*crtm]string{
    57  			&cref: "ab",
    58  			nil:   "",
    59  		},
    60  		map[bvtm]string{bval: "42"},
    61  		map[*bvtm]string{
    62  			&bval: "42",
    63  			nil:   "",
    64  		},
    65  		map[brtm]string{bref: "42"},
    66  		map[*brtm]string{
    67  			&bref: "42",
    68  			nil:   "",
    69  		},
    70  	}
    71  	for _, v := range valid {
    72  		marshalCompare(t, v, "valid")
    73  	}
    74  	invalid := []interface{}{
    75  		// Non-pointer value of a pointer-receiver
    76  		// type isn't a valid map key type.
    77  		map[crtm]string{
    78  			{L: "A", R: "B"}: "ab",
    79  		},
    80  	}
    81  	for _, v := range invalid {
    82  		marshalCompareError(t, v, "invalid")
    83  	}
    84  }
    85  
    86  //nolint:godox
    87  func TestNilMarshaler(t *testing.T) {
    88  	testdata := []struct {
    89  		v interface{}
    90  	}{
    91  		// json.Marshaler
    92  		{struct{ M json.Marshaler }{M: nil}},
    93  		{struct{ M json.Marshaler }{(*niljsonm)(nil)}},
    94  		{struct{ M interface{} }{(*niljsonm)(nil)}},
    95  		{struct{ M *niljsonm }{M: nil}},
    96  		{json.Marshaler((*niljsonm)(nil))},
    97  		{(*niljsonm)(nil)},
    98  
    99  		// encoding.TextMarshaler
   100  		{struct{ M encoding.TextMarshaler }{M: nil}},
   101  		{struct{ M encoding.TextMarshaler }{(*niltextm)(nil)}},
   102  		{struct{ M interface{} }{(*niltextm)(nil)}},
   103  		{struct{ M *niltextm }{M: nil}},
   104  		{encoding.TextMarshaler((*niltextm)(nil))},
   105  		{(*niltextm)(nil)},
   106  
   107  		// jettison.Marshaler
   108  		{struct{ M comboMarshaler }{M: nil}},
   109  		{struct{ M comboMarshaler }{(*niljetim)(nil)}},
   110  		{struct{ M interface{} }{(*niljetim)(nil)}},
   111  		{struct{ M *niljetim }{M: nil}},
   112  		{comboMarshaler((*niljetim)(nil))},
   113  		{(*niljetim)(nil)},
   114  
   115  		// jettison.MarshalerCtx
   116  		{struct{ M comboMarshalerCtx }{M: nil}},
   117  		{struct{ M comboMarshalerCtx }{(*nilmjctx)(nil)}},
   118  		{struct{ M interface{} }{(*nilmjctx)(nil)}},
   119  		{struct{ M *nilmjctx }{M: nil}},
   120  		{comboMarshalerCtx((*nilmjctx)(nil))},
   121  		{(*nilmjctx)(nil)},
   122  	}
   123  	for _, e := range testdata {
   124  		marshalCompare(t, e.v, "nil-marshaler")
   125  	}
   126  }