encode_number_float_test.go (5442B)
1 package gojay 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestEncoderFloat64(t *testing.T) { 11 var testCasesBasic = []struct { 12 name string 13 v float64 14 expectedJSON string 15 }{ 16 { 17 name: "basic", 18 v: float64(1), 19 expectedJSON: "[1,1]", 20 }, 21 { 22 name: "big", 23 v: float64(0), 24 expectedJSON: "[0,0]", 25 }, 26 } 27 for _, testCase := range testCasesBasic { 28 t.Run(testCase.name, func(t *testing.T) { 29 var b = &strings.Builder{} 30 var enc = NewEncoder(b) 31 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 32 enc.Float64(testCase.v) 33 enc.AddFloat64(testCase.v) 34 })) 35 assert.Equal(t, testCase.expectedJSON, b.String()) 36 }) 37 } 38 var testCasesOmitEmpty = []struct { 39 name string 40 v float64 41 expectedJSON string 42 }{ 43 { 44 name: "basic", 45 v: float64(1), 46 expectedJSON: "[1,1]", 47 }, 48 { 49 name: "big", 50 v: float64(0), 51 expectedJSON: "[]", 52 }, 53 } 54 for _, testCase := range testCasesOmitEmpty { 55 t.Run(testCase.name, func(t *testing.T) { 56 var b = &strings.Builder{} 57 var enc = NewEncoder(b) 58 enc.Encode(EncodeArrayFunc(func(enc *Encoder) { 59 enc.Float64OmitEmpty(testCase.v) 60 enc.AddFloat64OmitEmpty(testCase.v) 61 })) 62 assert.Equal(t, testCase.expectedJSON, b.String()) 63 }) 64 } 65 var testCasesKeyBasic = []struct { 66 name string 67 v float64 68 expectedJSON string 69 }{ 70 { 71 name: "basic", 72 v: float64(1), 73 expectedJSON: `{"foo":1,"bar":1}`, 74 }, 75 { 76 name: "big", 77 v: float64(0), 78 expectedJSON: `{"foo":0,"bar":0}`, 79 }, 80 } 81 for _, testCase := range testCasesKeyBasic { 82 t.Run(testCase.name, func(t *testing.T) { 83 var b = &strings.Builder{} 84 var enc = NewEncoder(b) 85 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 86 enc.Float64Key("foo", testCase.v) 87 enc.AddFloat64Key("bar", testCase.v) 88 })) 89 assert.Equal(t, testCase.expectedJSON, b.String()) 90 }) 91 } 92 var testCasesKeyOmitEmpty = []struct { 93 name string 94 v float64 95 expectedJSON string 96 }{ 97 { 98 name: "basic", 99 v: float64(1), 100 expectedJSON: `{"foo":1,"bar":1}`, 101 }, 102 { 103 name: "big", 104 v: float64(0), 105 expectedJSON: "{}", 106 }, 107 } 108 for _, testCase := range testCasesKeyOmitEmpty { 109 t.Run(testCase.name, func(t *testing.T) { 110 var b = &strings.Builder{} 111 var enc = NewEncoder(b) 112 enc.Encode(EncodeObjectFunc(func(enc *Encoder) { 113 enc.Float64KeyOmitEmpty("foo", testCase.v) 114 enc.AddFloat64KeyOmitEmpty("bar", testCase.v) 115 })) 116 assert.Equal(t, testCase.expectedJSON, b.String()) 117 }) 118 } 119 } 120 121 func TestEncoderFloat64NullEmpty(t *testing.T) { 122 var testCases = []struct { 123 name string 124 baseJSON string 125 expectedJSON string 126 }{ 127 { 128 name: "basic 1st elem", 129 baseJSON: "[", 130 expectedJSON: `[null,1`, 131 }, 132 { 133 name: "basic 2nd elem", 134 baseJSON: `["test"`, 135 expectedJSON: `["test",null,1`, 136 }, 137 } 138 for _, testCase := range testCases { 139 t.Run("true", func(t *testing.T) { 140 var b strings.Builder 141 var enc = NewEncoder(&b) 142 enc.writeString(testCase.baseJSON) 143 enc.FloatNullEmpty(0) 144 enc.AddFloatNullEmpty(1) 145 enc.Write() 146 assert.Equal(t, testCase.expectedJSON, b.String()) 147 }) 148 } 149 } 150 151 func TestEncoderFloat64KeyNullEmpty(t *testing.T) { 152 var testCases = []struct { 153 name string 154 baseJSON string 155 expectedJSON string 156 }{ 157 { 158 name: "basic 1st elem", 159 baseJSON: "{", 160 expectedJSON: `{"foo":null,"bar":1`, 161 }, 162 { 163 name: "basic 2nd elem", 164 baseJSON: `{"test":"test"`, 165 expectedJSON: `{"test":"test","foo":null,"bar":1`, 166 }, 167 } 168 for _, testCase := range testCases { 169 t.Run(testCase.name, func(t *testing.T) { 170 var b strings.Builder 171 var enc = NewEncoder(&b) 172 enc.writeString(testCase.baseJSON) 173 enc.FloatKeyNullEmpty("foo", 0) 174 enc.AddFloatKeyNullEmpty("bar", 1) 175 enc.Write() 176 assert.Equal(t, testCase.expectedJSON, b.String()) 177 }) 178 } 179 } 180 181 func TestEncoderFloat32NullEmpty(t *testing.T) { 182 var testCases = []struct { 183 name string 184 baseJSON string 185 expectedJSON string 186 }{ 187 { 188 name: "basic 1st elem", 189 baseJSON: "[", 190 expectedJSON: `[null,1`, 191 }, 192 { 193 name: "basic 2nd elem", 194 baseJSON: `["test"`, 195 expectedJSON: `["test",null,1`, 196 }, 197 } 198 for _, testCase := range testCases { 199 t.Run("true", func(t *testing.T) { 200 var b strings.Builder 201 var enc = NewEncoder(&b) 202 enc.writeString(testCase.baseJSON) 203 enc.Float32NullEmpty(0) 204 enc.AddFloat32NullEmpty(1) 205 enc.Write() 206 assert.Equal(t, testCase.expectedJSON, b.String()) 207 }) 208 } 209 } 210 211 func TestEncoderFloat32KeyNullEmpty(t *testing.T) { 212 var testCases = []struct { 213 name string 214 baseJSON string 215 expectedJSON string 216 }{ 217 { 218 name: "basic 1st elem", 219 baseJSON: "{", 220 expectedJSON: `{"foo":null,"bar":1`, 221 }, 222 { 223 name: "basic 2nd elem", 224 baseJSON: `{"test":"test"`, 225 expectedJSON: `{"test":"test","foo":null,"bar":1`, 226 }, 227 } 228 for _, testCase := range testCases { 229 t.Run(testCase.name, func(t *testing.T) { 230 var b strings.Builder 231 var enc = NewEncoder(&b) 232 enc.writeString(testCase.baseJSON) 233 enc.Float32KeyNullEmpty("foo", 0) 234 enc.AddFloat32KeyNullEmpty("bar", 1) 235 enc.Write() 236 assert.Equal(t, testCase.expectedJSON, b.String()) 237 }) 238 } 239 }