decode_bool_test.go (15402B)
1 package gojay 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestDecoderBool(t *testing.T) { 11 testCases := []struct { 12 name string 13 json string 14 expectedResult bool 15 expectations func(t *testing.T, v bool, err error) 16 }{ 17 { 18 name: "true-basic", 19 json: "true", 20 expectations: func(t *testing.T, v bool, err error) { 21 assert.Nil(t, err, "err should be nil") 22 assert.True(t, v, "result should be true") 23 }, 24 }, 25 { 26 name: "false-basic", 27 json: "false", 28 expectations: func(t *testing.T, v bool, err error) { 29 assert.Nil(t, err, "err should be nil") 30 assert.False(t, v, "result should be false") 31 }, 32 }, 33 { 34 name: "null-basic", 35 json: "null", 36 expectations: func(t *testing.T, v bool, err error) { 37 assert.Nil(t, err, "err should be nil") 38 assert.False(t, v, "result should be false") 39 }, 40 }, 41 { 42 name: "true-error", 43 json: "taue", 44 expectations: func(t *testing.T, v bool, err error) { 45 assert.NotNil(t, err, "err should be nil") 46 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 47 assert.False(t, v, "result should be false") 48 }, 49 }, 50 { 51 name: "true-error2", 52 json: "trae", 53 expectations: func(t *testing.T, v bool, err error) { 54 assert.NotNil(t, err, "err should be nil") 55 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 56 assert.False(t, v, "result should be false") 57 }, 58 }, 59 { 60 name: "true-error3", 61 json: "trua", 62 expectations: func(t *testing.T, v bool, err error) { 63 assert.NotNil(t, err, "err should be nil") 64 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 65 assert.False(t, v, "result should be false") 66 }, 67 }, 68 { 69 name: "true-error4", 70 json: "truea", 71 expectations: func(t *testing.T, v bool, err error) { 72 assert.NotNil(t, err, "err should be nil") 73 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 74 assert.False(t, v, "result should be false") 75 }, 76 }, 77 { 78 name: "true-error5", 79 json: "t", 80 expectations: func(t *testing.T, v bool, err error) { 81 assert.NotNil(t, err, "err should be nil") 82 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 83 assert.False(t, v, "result should be false") 84 }, 85 }, 86 { 87 name: "true-error6", 88 json: "a", 89 expectations: func(t *testing.T, v bool, err error) { 90 assert.NotNil(t, err, "err should be nil") 91 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 92 assert.False(t, v, "result should be false") 93 }, 94 }, 95 { 96 name: "false-error", 97 json: "fulse", 98 expectations: func(t *testing.T, v bool, err error) { 99 assert.NotNil(t, err, "err should be nil") 100 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 101 assert.False(t, v, "result should be false") 102 }, 103 }, 104 { 105 name: "false-error2", 106 json: "fause", 107 expectations: func(t *testing.T, v bool, err error) { 108 assert.NotNil(t, err, "err should be nil") 109 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 110 assert.False(t, v, "result should be false") 111 }, 112 }, 113 { 114 name: "false-error3", 115 json: "falze", 116 expectations: func(t *testing.T, v bool, err error) { 117 assert.NotNil(t, err, "err should be nil") 118 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 119 assert.False(t, v, "result should be false") 120 }, 121 }, 122 { 123 name: "false-error4", 124 json: "falso", 125 expectations: func(t *testing.T, v bool, err error) { 126 assert.NotNil(t, err, "err should be nil") 127 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 128 assert.False(t, v, "result should be false") 129 }, 130 }, 131 { 132 name: "false-error5", 133 json: "falsea", 134 expectations: func(t *testing.T, v bool, err error) { 135 assert.NotNil(t, err, "err should be nil") 136 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 137 assert.False(t, v, "result should be false") 138 }, 139 }, 140 { 141 name: "false-error6", 142 json: "f", 143 expectations: func(t *testing.T, v bool, err error) { 144 assert.NotNil(t, err, "err should be nil") 145 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 146 assert.False(t, v, "result should be false") 147 }, 148 }, 149 { 150 name: "false-error7", 151 json: "a", 152 expectations: func(t *testing.T, v bool, err error) { 153 assert.NotNil(t, err, "err should be nil") 154 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 155 assert.False(t, v, "result should be false") 156 }, 157 }, 158 { 159 name: "null-error", 160 json: "nall", 161 expectations: func(t *testing.T, v bool, err error) { 162 assert.NotNil(t, err, "err should be nil") 163 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 164 assert.False(t, v, "result should be false") 165 }, 166 }, 167 { 168 name: "null-error2", 169 json: "nual", 170 expectations: func(t *testing.T, v bool, err error) { 171 assert.NotNil(t, err, "err should be nil") 172 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 173 assert.False(t, v, "result should be false") 174 }, 175 }, 176 { 177 name: "null-error3", 178 json: "nula", 179 expectations: func(t *testing.T, v bool, err error) { 180 assert.NotNil(t, err, "err should be nil") 181 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 182 assert.False(t, v, "result should be false") 183 }, 184 }, 185 { 186 name: "null-error4", 187 json: "nulle", 188 expectations: func(t *testing.T, v bool, err error) { 189 assert.NotNil(t, err, "err should be nil") 190 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 191 assert.False(t, v, "result should be false") 192 }, 193 }, 194 { 195 name: "null-error5", 196 json: "n", 197 expectations: func(t *testing.T, v bool, err error) { 198 assert.NotNil(t, err, "err should be nil") 199 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 200 assert.False(t, v, "result should be false") 201 }, 202 }, 203 { 204 name: "null-error6", 205 json: "a", 206 expectations: func(t *testing.T, v bool, err error) { 207 assert.NotNil(t, err, "err should be nil") 208 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 209 assert.False(t, v, "result should be false") 210 }, 211 }, 212 { 213 name: "null-skip", 214 json: "{}", 215 expectations: func(t *testing.T, v bool, err error) { 216 assert.NotNil(t, err, "err should not be nil") 217 assert.IsType(t, InvalidUnmarshalError(""), err, "err should be of type InvalidUnmarshalError") 218 assert.False(t, v, "result should be false") 219 }, 220 }, 221 { 222 name: "null-skip", 223 json: "", 224 expectations: func(t *testing.T, v bool, err error) { 225 assert.Nil(t, err, "err should not be nil") 226 assert.False(t, v, "result should be false") 227 }, 228 }, 229 } 230 231 for _, testCase := range testCases { 232 t.Run(testCase.name, func(t *testing.T) { 233 json := []byte(testCase.json) 234 var v bool 235 err := Unmarshal(json, &v) 236 testCase.expectations(t, v, err) 237 }) 238 } 239 } 240 241 func TestDecoderBoolNull(t *testing.T) { 242 testCases := []struct { 243 name string 244 json string 245 expectedResult bool 246 expectations func(t *testing.T, v *bool, err error) 247 }{ 248 { 249 name: "true-basic", 250 json: "true", 251 expectations: func(t *testing.T, v *bool, err error) { 252 assert.Nil(t, err, "err should be nil") 253 assert.True(t, *v, "result should be true") 254 }, 255 }, 256 { 257 name: "false-basic", 258 json: "false", 259 expectations: func(t *testing.T, v *bool, err error) { 260 assert.Nil(t, err, "err should be nil") 261 assert.False(t, *v, "result should be false") 262 }, 263 }, 264 { 265 name: "null-basic", 266 json: "null", 267 expectations: func(t *testing.T, v *bool, err error) { 268 assert.Nil(t, err, "err should be nil") 269 assert.Nil(t, v, "result should be nil") 270 }, 271 }, 272 { 273 name: "true-error", 274 json: "taue", 275 expectations: func(t *testing.T, v *bool, err error) { 276 assert.NotNil(t, err, "err should be nil") 277 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 278 assert.Nil(t, v, "result should be false") 279 }, 280 }, 281 { 282 name: "true-error2", 283 json: "trae", 284 expectations: func(t *testing.T, v *bool, err error) { 285 assert.NotNil(t, err, "err should be nil") 286 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 287 assert.Nil(t, v, "result should be nil") 288 }, 289 }, 290 { 291 name: "true-error3", 292 json: "trua", 293 expectations: func(t *testing.T, v *bool, err error) { 294 assert.NotNil(t, err, "err should be nil") 295 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 296 assert.Nil(t, v, "result should be nil") 297 }, 298 }, 299 { 300 name: "true-error4", 301 json: "truea", 302 expectations: func(t *testing.T, v *bool, err error) { 303 assert.NotNil(t, err, "err should be nil") 304 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 305 assert.Nil(t, v, "result should be nil") 306 }, 307 }, 308 { 309 name: "true-error5", 310 json: "t", 311 expectations: func(t *testing.T, v *bool, err error) { 312 assert.NotNil(t, err, "err should be nil") 313 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 314 assert.Nil(t, v, "result should be nil") 315 }, 316 }, 317 { 318 name: "true-error6", 319 json: "a", 320 expectations: func(t *testing.T, v *bool, err error) { 321 assert.NotNil(t, err, "err should be nil") 322 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 323 assert.Nil(t, v, "result should be nil") 324 }, 325 }, 326 { 327 name: "false-error", 328 json: "fulse", 329 expectations: func(t *testing.T, v *bool, err error) { 330 assert.NotNil(t, err, "err should be nil") 331 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 332 assert.Nil(t, v, "result should be nil") 333 }, 334 }, 335 { 336 name: "false-error2", 337 json: "fause", 338 expectations: func(t *testing.T, v *bool, err error) { 339 assert.NotNil(t, err, "err should be nil") 340 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 341 assert.Nil(t, v, "result should be nil") 342 }, 343 }, 344 { 345 name: "false-error3", 346 json: "falze", 347 expectations: func(t *testing.T, v *bool, err error) { 348 assert.NotNil(t, err, "err should be nil") 349 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 350 assert.Nil(t, v, "result should be nil") 351 }, 352 }, 353 { 354 name: "false-error4", 355 json: "falso", 356 expectations: func(t *testing.T, v *bool, err error) { 357 assert.NotNil(t, err, "err should be nil") 358 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 359 assert.Nil(t, v, "result should be nil") 360 }, 361 }, 362 { 363 name: "false-error5", 364 json: "falsea", 365 expectations: func(t *testing.T, v *bool, err error) { 366 assert.NotNil(t, err, "err should be nil") 367 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 368 assert.Nil(t, v, "result should be nil") 369 }, 370 }, 371 { 372 name: "false-error6", 373 json: "f", 374 expectations: func(t *testing.T, v *bool, err error) { 375 assert.NotNil(t, err, "err should be nil") 376 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 377 assert.Nil(t, v, "result should be nil") 378 }, 379 }, 380 { 381 name: "false-error7", 382 json: "a", 383 expectations: func(t *testing.T, v *bool, err error) { 384 assert.NotNil(t, err, "err should be nil") 385 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 386 assert.Nil(t, v, "result should be nil") 387 }, 388 }, 389 { 390 name: "null-error", 391 json: "nall", 392 expectations: func(t *testing.T, v *bool, err error) { 393 assert.NotNil(t, err, "err should be nil") 394 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 395 assert.Nil(t, v, "result should be nil") 396 }, 397 }, 398 { 399 name: "null-error2", 400 json: "nual", 401 expectations: func(t *testing.T, v *bool, err error) { 402 assert.NotNil(t, err, "err should be nil") 403 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 404 assert.Nil(t, v, "result should be nil") 405 }, 406 }, 407 { 408 name: "null-error3", 409 json: "nula", 410 expectations: func(t *testing.T, v *bool, err error) { 411 assert.NotNil(t, err, "err should be nil") 412 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 413 assert.Nil(t, v, "result should be nil") 414 }, 415 }, 416 { 417 name: "null-error4", 418 json: "nulle", 419 expectations: func(t *testing.T, v *bool, err error) { 420 assert.NotNil(t, err, "err should be nil") 421 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 422 assert.Nil(t, v, "result should be nil") 423 }, 424 }, 425 { 426 name: "null-error5", 427 json: "n", 428 expectations: func(t *testing.T, v *bool, err error) { 429 assert.NotNil(t, err, "err should be nil") 430 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 431 assert.Nil(t, v, "result should be nil") 432 }, 433 }, 434 { 435 name: "null-error6", 436 json: "a", 437 expectations: func(t *testing.T, v *bool, err error) { 438 assert.NotNil(t, err, "err should be nil") 439 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 440 assert.Nil(t, v, "result should be nil") 441 }, 442 }, 443 { 444 name: "null-skip", 445 json: "{}", 446 expectations: func(t *testing.T, v *bool, err error) { 447 assert.NotNil(t, err, "err should not be nil") 448 assert.IsType(t, InvalidUnmarshalError(""), err, "err should be of type InvalidUnmarshalError") 449 assert.Nil(t, v, "result should be nil") 450 }, 451 }, 452 { 453 name: "null-skip", 454 json: "", 455 expectations: func(t *testing.T, v *bool, err error) { 456 assert.Nil(t, err, "err should not be nil") 457 assert.Nil(t, v, "result should be nil") 458 }, 459 }, 460 } 461 462 for _, testCase := range testCases { 463 t.Run(testCase.name, func(t *testing.T) { 464 var v = struct { 465 b *bool 466 }{} 467 err := Unmarshal([]byte(testCase.json), &v.b) 468 testCase.expectations(t, v.b, err) 469 }) 470 } 471 t.Run("decoder-api-invalid-json2", func(t *testing.T) { 472 var v = new(bool) 473 var dec = NewDecoder(strings.NewReader(`folse`)) 474 err := dec.BoolNull(&v) 475 assert.NotNil(t, err, "Err must not be nil") 476 assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError") 477 }) 478 } 479 480 func TestDecoderBoolDecoderAPI(t *testing.T) { 481 var v bool 482 dec := BorrowDecoder(strings.NewReader("true")) 483 defer dec.Release() 484 err := dec.DecodeBool(&v) 485 assert.Nil(t, err, "Err must be nil") 486 assert.Equal(t, true, v, "v must be equal to true") 487 } 488 489 func TestDecoderBoolPoolError(t *testing.T) { 490 v := true 491 dec := NewDecoder(nil) 492 dec.Release() 493 defer func() { 494 err := recover() 495 assert.NotNil(t, err, "err shouldnt be nil") 496 assert.IsType(t, InvalidUsagePooledDecoderError(""), err, "err should be of type InvalidUsagePooledDecoderError") 497 }() 498 _ = dec.DecodeBool(&v) 499 assert.True(t, false, "should not be called as decoder should have panicked") 500 }