github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_native_struct_complex_test.go (about) 1 package jzon 2 3 import ( 4 "encoding/json" 5 "testing" 6 ) 7 8 func TestValEncoder_Native_Struct_Complex_OneField(t *testing.T) { 9 t.Run("field indirect", func(t *testing.T) { 10 type st struct { 11 A int 12 } 13 checkEncodeValueWithStandard(t, st{ 14 A: 1, 15 }, nil) 16 }) 17 t.Run("field direct", func(t *testing.T) { 18 type st struct { 19 A *int 20 } 21 checkEncodeValueWithStandard(t, st{ 22 A: nil, 23 }, nil) 24 }) 25 t.Run("direct array", func(t *testing.T) { 26 type st struct { 27 A [1]*int 28 } 29 checkEncodeValueWithStandard(t, st{ 30 A: [1]*int{nil}, 31 }, nil) 32 i := 1 33 checkEncodeValueWithStandard(t, st{ 34 A: [1]*int{&i}, 35 }, nil) 36 }) 37 t.Run("direct map", func(t *testing.T) { 38 type st struct { 39 A map[int]int 40 } 41 checkEncodeValueWithStandard(t, st{ 42 A: nil, 43 }, nil) 44 checkEncodeValueWithStandard(t, st{ 45 A: map[int]int{1: 2}, 46 }, nil) 47 }) 48 } 49 50 func TestValEncoder_Native_Struct_Complex_MultipleField(t *testing.T) { 51 t.Run("nil pointer", func(t *testing.T) { 52 type st struct { 53 A int 54 B int 55 } 56 checkEncodeValueWithStandard(t, (*st)(nil), nil) 57 }) 58 t.Run("pointer", func(t *testing.T) { 59 type st struct { 60 A int 61 B int 62 } 63 checkEncodeValueWithStandard(t, &st{ 64 A: 1, B: 2, 65 }, nil) 66 }) 67 t.Run("non pointer", func(t *testing.T) { 68 type st struct { 69 A int 70 B int 71 } 72 checkEncodeValueWithStandard(t, st{ 73 A: 1, B: 2, 74 }, nil) 75 }) 76 t.Run("nested", func(t *testing.T) { 77 type inner struct { 78 A int 79 B int 80 } 81 type outer struct { 82 *inner 83 C int 84 } 85 type outer2 struct { 86 inner 87 C int 88 } 89 checkEncodeValueWithStandard(t, outer{ 90 inner: nil, 91 C: 1, 92 }, nil) 93 checkEncodeValueWithStandard(t, outer{ 94 inner: &inner{ 95 A: 1, 96 B: 2, 97 }, 98 C: 3, 99 }, nil) 100 checkEncodeValueWithStandard(t, outer2{}, nil) 101 }) 102 t.Run("pointer field", func(t *testing.T) { 103 type st struct { 104 A *int 105 B *int 106 } 107 checkEncodeValueWithStandard(t, st{}, nil) 108 }) 109 t.Run("pointer field 2", func(t *testing.T) { 110 type st struct { 111 A *json.Marshaler 112 B *interface{} 113 } 114 checkEncodeValueWithStandard(t, st{}, nil) 115 }) 116 t.Run("float field", func(t *testing.T) { 117 type st struct { 118 A float32 119 B float64 120 } 121 checkEncodeValueWithStandard(t, st{}, nil) 122 }) 123 t.Run("quoted", func(t *testing.T) { 124 type st struct { 125 S string `json:",string"` 126 I8 int8 `json:",string"` 127 I16 int16 `json:",string"` 128 I32 int32 `json:",string"` 129 I64 int64 `json:",string"` 130 U8 uint8 `json:",string"` 131 U16 uint16 `json:",string"` 132 U32 uint32 `json:",string"` 133 U64 uint64 `json:",string"` 134 Uptr uintptr `json:",string"` 135 PI8 *int8 `json:",string"` 136 O interface{} `json:",string"` 137 } 138 checkEncodeValueWithStandard(t, st{}, nil) 139 i8 := int8(1) 140 checkEncodeValueWithStandard(t, st{ 141 PI8: &i8, 142 O: &i8, 143 }, nil) 144 }) 145 } 146 147 func TestValEncoder_Native_Struct_Complex_Nested(t *testing.T) { 148 t.Run("nested 1", func(t *testing.T) { 149 type inner struct { 150 A int 151 } 152 type outer struct { 153 inner 154 A int 155 } 156 checkEncodeValueWithStandard(t, outer{ 157 inner: inner{A: 1}, 158 A: 2, 159 }, nil) 160 }) 161 t.Run("nested 2", func(t *testing.T) { 162 type inner struct { 163 A int 164 } 165 type aliasInner inner 166 type inner2inner struct { 167 A int 168 B int 169 } 170 type inner2 struct { 171 inner2inner 172 } 173 type outer struct { 174 inner 175 aliasInner 176 inner2 177 } 178 checkEncodeValueWithStandard(t, outer{ 179 inner: inner{ 180 A: 1, 181 }, 182 aliasInner: aliasInner{ 183 A: 2, 184 }, 185 inner2: inner2{ 186 inner2inner: inner2inner{ 187 A: 3, 188 B: 4, 189 }, 190 }, 191 }, nil) 192 }) 193 }