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

     1  package jzon
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type testIfaceImpl2 struct {
     8  	Field string
     9  }
    10  
    11  func (testIfaceImpl2) Foo() {}
    12  
    13  type testIfaceImpl3 struct {
    14  	Field string
    15  }
    16  
    17  func (*testIfaceImpl3) Foo() {}
    18  
    19  func TestValEncoder_Interface(t *testing.T) {
    20  	t.Run("builtin", func(t *testing.T) {
    21  		t.Run("nil", func(t *testing.T) {
    22  			var i interface{}
    23  			checkEncodeValueWithStandard(t, &i, nil)
    24  		})
    25  		t.Run("non nil", func(t *testing.T) {
    26  			var i interface{} = 1
    27  			checkEncodeValueWithStandard(t, &i, nil)
    28  		})
    29  	})
    30  	t.Run("eface", func(t *testing.T) {
    31  		t.Run("nil", func(t *testing.T) {
    32  			type I interface{}
    33  			var i I
    34  			checkEncodeValueWithStandard(t, &i, nil)
    35  		})
    36  		t.Run("non nil", func(t *testing.T) {
    37  			type I interface{}
    38  			var i I = 1
    39  			checkEncodeValueWithStandard(t, &i, nil)
    40  		})
    41  	})
    42  	t.Run("iface", func(t *testing.T) {
    43  		t.Run("nil", func(t *testing.T) {
    44  			var i testIface
    45  			checkEncodeValueWithStandard(t, &i, nil)
    46  		})
    47  		t.Run("non nil", func(t *testing.T) {
    48  			var i testIface = testIfaceImpl2{
    49  				Field: "test",
    50  			}
    51  			checkEncodeValueWithStandard(t, &i, nil)
    52  		})
    53  		t.Run("non nil 2", func(t *testing.T) {
    54  			var i testIface = &testIfaceImpl2{
    55  				Field: "test2",
    56  			}
    57  			checkEncodeValueWithStandard(t, &i, nil)
    58  		})
    59  		t.Run("pointer receiver", func(t *testing.T) {
    60  			var i testIface = &testIfaceImpl3{
    61  				Field: "test3",
    62  			}
    63  			checkEncodeValueWithStandard(t, &i, nil)
    64  		})
    65  	})
    66  }
    67  
    68  func TestValEncoder_Interface_OmitEmpty(t *testing.T) {
    69  	t.Run("eface", func(t *testing.T) {
    70  		type st struct {
    71  			I interface{} `json:",omitempty"`
    72  		}
    73  		t.Run("nil", func(t *testing.T) {
    74  			checkEncodeValueWithStandard(t, st{}, nil)
    75  		})
    76  		t.Run("non nil", func(t *testing.T) {
    77  			checkEncodeValueWithStandard(t, st{I: true}, nil)
    78  		})
    79  		t.Run("nil pointer", func(t *testing.T) {
    80  			checkEncodeValueWithStandard(t, st{
    81  				I: (*testJSONMarshaler2)(nil),
    82  			}, nil)
    83  		})
    84  	})
    85  	t.Run("iface", func(t *testing.T) {
    86  		type st struct {
    87  			I testIface `json:",omitempty"`
    88  		}
    89  		t.Run("nil", func(t *testing.T) {
    90  			checkEncodeValueWithStandard(t, st{}, nil)
    91  		})
    92  		t.Run("value receiver", func(t *testing.T) {
    93  			t.Run("non nil", func(t *testing.T) {
    94  				checkEncodeValueWithStandard(t, st{
    95  					I: testIfaceImpl{},
    96  				}, nil)
    97  			})
    98  			t.Run("empty struct", func(t *testing.T) {
    99  				checkEncodeValueWithStandard(t, st{
   100  					I: testIfaceImpl2{},
   101  				}, nil)
   102  			})
   103  			t.Run("non empty struct", func(t *testing.T) {
   104  				checkEncodeValueWithStandard(t, st{
   105  					I: testIfaceImpl2{
   106  						Field: "test2",
   107  					},
   108  				}, nil)
   109  			})
   110  		})
   111  		t.Run("pointer receiver", func(t *testing.T) {
   112  			t.Run("nil pointer", func(t *testing.T) {
   113  				checkEncodeValueWithStandard(t, st{
   114  					I: (*testIfaceImpl3)(nil),
   115  				}, nil)
   116  			})
   117  			t.Run("empty struct", func(t *testing.T) {
   118  				checkEncodeValueWithStandard(t, st{
   119  					I: &testIfaceImpl3{},
   120  				}, nil)
   121  			})
   122  			t.Run("non empty struct", func(t *testing.T) {
   123  				checkEncodeValueWithStandard(t, st{
   124  					I: &testIfaceImpl3{
   125  						Field: "test3",
   126  					},
   127  				}, nil)
   128  			})
   129  		})
   130  	})
   131  }