github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_native_array_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"testing"
     7  )
     8  
     9  func TestValEncoder_Array_Error(t *testing.T) {
    10  	t.Run("error", func(t *testing.T) {
    11  		e := errors.New("test")
    12  		arr := [...]json.Marshaler{testJSONMarshaler{
    13  			data: `"test"`,
    14  			err:  e,
    15  		}}
    16  		checkEncodeValueWithStandard(t, arr, e)
    17  	})
    18  }
    19  
    20  func TestValEncoder_Array_Empty(t *testing.T) {
    21  	t.Run("empty", func(t *testing.T) {
    22  		arr := [...]int{}
    23  		checkEncodeValueWithStandard(t, arr, nil)
    24  	})
    25  	t.Run("pointer", func(t *testing.T) {
    26  		f := func(t *testing.T, ptr *[0]int, err error) {
    27  			checkEncodeValueWithStandard(t, ptr, err)
    28  		}
    29  		t.Run("nil", func(t *testing.T) {
    30  			f(t, nil, nil)
    31  		})
    32  		t.Run("pointer", func(t *testing.T) {
    33  			arr := [...]int{}
    34  			f(t, &arr, nil)
    35  		})
    36  	})
    37  	t.Run("pointer of pointer", func(t *testing.T) {
    38  		f := func(t *testing.T, ptr **[0]int, err error) {
    39  			checkEncodeValueWithStandard(t, ptr, err)
    40  		}
    41  		t.Run("nil", func(t *testing.T) {
    42  			f(t, nil, nil)
    43  		})
    44  		t.Run("pointer of nil", func(t *testing.T) {
    45  			ptr := (*[0]int)(nil)
    46  			f(t, &ptr, nil)
    47  		})
    48  		t.Run("non nil", func(t *testing.T) {
    49  			arr := [...]int{}
    50  			ptr := &arr
    51  			f(t, &ptr, nil)
    52  		})
    53  	})
    54  }
    55  
    56  func TestValEncoder_Array_Indirect(t *testing.T) {
    57  	// len != 1
    58  	t.Run("len<>1", func(t *testing.T) {
    59  		t.Run("pointer", func(t *testing.T) {
    60  			arr := [...]int{1, 2, 3}
    61  			checkEncodeValueWithStandard(t, &arr, nil)
    62  		})
    63  		t.Run("non pointer", func(t *testing.T) {
    64  			arr := [...]int{1, 2, 3}
    65  			checkEncodeValueWithStandard(t, arr, nil)
    66  		})
    67  		t.Run("array of pointer", func(t *testing.T) {
    68  			i := 1
    69  			arr := [...]*int{(*int)(nil), &i}
    70  			checkEncodeValueWithStandard(t, arr, nil)
    71  		})
    72  	})
    73  	// element is indirect
    74  	t.Run("len==1", func(t *testing.T) {
    75  		t.Run("one element array", func(t *testing.T) {
    76  			arr := [...]int{1}
    77  			checkEncodeValueWithStandard(t, arr, nil)
    78  		})
    79  	})
    80  }
    81  
    82  func TestValEncoder_Array_Direct(t *testing.T) {
    83  	t.Run("nil element", func(t *testing.T) {
    84  		arr := [...]*int{(*int)(nil)}
    85  		checkEncodeValueWithStandard(t, arr, nil)
    86  	})
    87  	t.Run("non nil element", func(t *testing.T) {
    88  		i := 1
    89  		arr := [...]*int{&i}
    90  		checkEncodeValueWithStandard(t, arr, nil)
    91  	})
    92  	t.Run("pointer", func(t *testing.T) {
    93  		checkEncodeValueWithStandard(t, (*[1]*int)(nil), nil)
    94  	})
    95  }
    96  
    97  func TestValEncoder_Array_Marshaler(t *testing.T) {
    98  	t.Run("json marshaler", func(t *testing.T) {
    99  		t.Run("arr value", func(t *testing.T) {
   100  			t.Run("value", func(t *testing.T) {
   101  				checkEncodeValueWithStandard(t, [...]testMarshalByte{'t', 'e', 's', 't'}, nil)
   102  			})
   103  			t.Run("pointer", func(t *testing.T) {
   104  				skipTest(t, "array with pointer json marshaler")
   105  				checkEncodeValueWithStandard(t, [...]testMarshalByte2{'t', 'e', 's', 't'}, nil)
   106  			})
   107  		})
   108  		t.Run("arr ptr", func(t *testing.T) {
   109  			t.Run("value", func(t *testing.T) {
   110  				arr := [...]testMarshalByte{'t', 'e', 's', 't'}
   111  				checkEncodeValueWithStandard(t, &arr, nil)
   112  			})
   113  			t.Run("pointer", func(t *testing.T) {
   114  				arr := [...]testMarshalByte2{'t', 'e', 's', 't'}
   115  				checkEncodeValueWithStandard(t, &arr, nil)
   116  			})
   117  		})
   118  	})
   119  	t.Run("text marshaler", func(t *testing.T) {
   120  		t.Run("arr value", func(t *testing.T) {
   121  			t.Run("value", func(t *testing.T) {
   122  				checkEncodeValueWithStandard(t, [...]testMarshalByte3{'t', 'e', 's', 't'}, nil)
   123  			})
   124  			t.Run("pointer", func(t *testing.T) {
   125  				skipTest(t, "array with pointer text marshaler")
   126  				checkEncodeValueWithStandard(t, [...]testMarshalByte4{'t', 'e', 's', 't'}, nil)
   127  			})
   128  		})
   129  		t.Run("arr ptr", func(t *testing.T) {
   130  			t.Run("value", func(t *testing.T) {
   131  				arr := [...]testMarshalByte3{'t', 'e', 's', 't'}
   132  				checkEncodeValueWithStandard(t, &arr, nil)
   133  			})
   134  			t.Run("pointer", func(t *testing.T) {
   135  				arr := [...]testMarshalByte4{'t', 'e', 's', 't'}
   136  				checkEncodeValueWithStandard(t, &arr, nil)
   137  			})
   138  		})
   139  	})
   140  }
   141  
   142  func TestValEncoder_Array_OmitEmpty(t *testing.T) {
   143  	t.Run("empty", func(t *testing.T) {
   144  		type st struct {
   145  			A [0]int `json:",omitempty"`
   146  		}
   147  		checkEncodeValueWithStandard(t, &st{}, nil)
   148  	})
   149  	t.Run("direct", func(t *testing.T) {
   150  		type st struct {
   151  			A [1]*int `json:",omitempty"`
   152  		}
   153  		checkEncodeValueWithStandard(t, &st{}, nil)
   154  	})
   155  	t.Run("indirect", func(t *testing.T) {
   156  		type st struct {
   157  			A [1]int `json:",omitempty"`
   158  		}
   159  		checkEncodeValueWithStandard(t, &st{}, nil)
   160  	})
   161  }