|
1 | 1 | package jsonschema |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "math" |
4 | 7 | "strings" |
5 | 8 | "testing" |
6 | 9 | ) |
@@ -353,3 +356,59 @@ func TestIsUUID(t *testing.T) { |
353 | 356 | } |
354 | 357 | } |
355 | 358 | } |
| 359 | + |
| 360 | +func TestIsInt32(t *testing.T) { |
| 361 | + tests := []struct { |
| 362 | + val interface{} |
| 363 | + valid bool |
| 364 | + }{ |
| 365 | + {1, true}, |
| 366 | + {1.0, true}, |
| 367 | + {1.2, false}, |
| 368 | + {-0, true}, |
| 369 | + {math.MinInt32, true}, |
| 370 | + {int32(math.MinInt32), true}, |
| 371 | + {int64(math.MinInt64), false}, |
| 372 | + {math.NaN(), false}, |
| 373 | + {math.Inf(1), false}, |
| 374 | + {json.Number("1"), true}, |
| 375 | + {json.Number("1.0"), true}, |
| 376 | + {json.Number("-1.01"), false}, |
| 377 | + {json.Number(fmt.Sprint(math.MaxInt32)), true}, |
| 378 | + {json.Number(fmt.Sprint(math.MaxInt64)), false}, |
| 379 | + {"string", true}, |
| 380 | + } |
| 381 | + for i, test := range tests { |
| 382 | + if test.valid != isInt32(test.val) { |
| 383 | + t.Errorf("#%d: %q, valid %t, got valid %t", i, test.val, test.valid, !test.valid) |
| 384 | + } |
| 385 | + } |
| 386 | +} |
| 387 | + |
| 388 | +func TestIsInt64(t *testing.T) { |
| 389 | + tests := []struct { |
| 390 | + val interface{} |
| 391 | + valid bool |
| 392 | + }{ |
| 393 | + {1, true}, |
| 394 | + {1.0, true}, |
| 395 | + {1.2, false}, |
| 396 | + {0, true}, |
| 397 | + {int32(math.MinInt32), true}, |
| 398 | + {int64(math.MinInt64), true}, |
| 399 | + {math.NaN(), false}, |
| 400 | + {math.Inf(-1), false}, |
| 401 | + {json.Number("1"), true}, |
| 402 | + {json.Number("1.0"), true}, |
| 403 | + {json.Number("-1.01"), false}, |
| 404 | + {json.Number(fmt.Sprint(math.MaxInt32)), true}, |
| 405 | + {json.Number(fmt.Sprint(math.MaxInt64)), true}, |
| 406 | + {json.Number("9223372036854775808"), false}, |
| 407 | + {"string", true}, |
| 408 | + } |
| 409 | + for i, test := range tests { |
| 410 | + if test.valid != isInt64(test.val) { |
| 411 | + t.Errorf("#%d: %q, valid %t, got valid %t", i, test.val, test.valid, !test.valid) |
| 412 | + } |
| 413 | + } |
| 414 | +} |
0 commit comments