| 1 | import x.json2 as json |
| 2 | |
| 3 | // Tests for strict mode: quoted strings are NOT decoded as numbers (JSON spec compliance). |
| 4 | // Tests for default mode: quoted strings ARE accepted as numbers for compatibility. |
| 5 | |
| 6 | // ===== STRICT MODE TESTS ===== |
| 7 | |
| 8 | fn test_strict_string_not_decoded_as_int() { |
| 9 | json.decode[int]('"0"', strict: true) or { |
| 10 | assert err is json.JsonDecodeError |
| 11 | if err is json.JsonDecodeError { |
| 12 | assert err.message == 'Data: Expected number, but got string' |
| 13 | } |
| 14 | return |
| 15 | } |
| 16 | assert false, 'Expected JsonDecodeError for quoted number string in strict mode' |
| 17 | } |
| 18 | |
| 19 | fn test_strict_string_not_decoded_as_int_positive() { |
| 20 | json.decode[int]('"100"', strict: true) or { |
| 21 | assert err is json.JsonDecodeError |
| 22 | if err is json.JsonDecodeError { |
| 23 | assert err.message == 'Data: Expected number, but got string' |
| 24 | } |
| 25 | return |
| 26 | } |
| 27 | assert false, 'Expected JsonDecodeError for quoted number string in strict mode' |
| 28 | } |
| 29 | |
| 30 | fn test_strict_string_not_decoded_as_float() { |
| 31 | json.decode[f64]('"-23.6e1"', strict: true) or { |
| 32 | assert err is json.JsonDecodeError |
| 33 | if err is json.JsonDecodeError { |
| 34 | assert err.message == 'Data: Expected number, but got string' |
| 35 | } |
| 36 | return |
| 37 | } |
| 38 | assert false, 'Expected JsonDecodeError for quoted number string in strict mode' |
| 39 | } |
| 40 | |
| 41 | fn test_strict_string_in_array_not_decoded_as_int() { |
| 42 | json.decode[[]int]('["100", 99, "98", 97]', strict: true) or { |
| 43 | assert err is json.JsonDecodeError |
| 44 | if err is json.JsonDecodeError { |
| 45 | assert err.message == 'Data: Expected number, but got string' |
| 46 | } |
| 47 | return |
| 48 | } |
| 49 | assert false, 'Expected JsonDecodeError for quoted number string in array in strict mode' |
| 50 | } |
| 51 | |
| 52 | // ===== DEFAULT MODE TESTS ===== |
| 53 | |
| 54 | fn test_default_string_decoded_as_int() { |
| 55 | assert json.decode[int]('"0"')! == 0 |
| 56 | assert json.decode[int]('"100"')! == 100 |
| 57 | assert json.decode[int]('"-50"')! == -50 |
| 58 | } |
| 59 | |
| 60 | fn test_default_string_decoded_as_float() { |
| 61 | assert json.decode[f64]('"-23.6e1"')! == -236.0 |
| 62 | assert json.decode[f64]('"3.14"')! == 3.14 |
| 63 | } |
| 64 | |
| 65 | fn test_default_string_in_array_decoded_as_int() { |
| 66 | assert json.decode[[]int]('["100", "99", "98", "97"]')! == [100, 99, 98, 97] |
| 67 | assert json.decode[[]int]('["100", 99, "98", 97]')! == [100, 99, 98, 97] |
| 68 | } |
| 69 | |
| 70 | // ===== COMMON TESTS (both modes) ===== |
| 71 | |
| 72 | fn test_valid_unquoted_numbers() { |
| 73 | assert json.decode[int]('0')! == 0 |
| 74 | assert json.decode[int]('100')! == 100 |
| 75 | assert json.decode[int]('-50')! == -50 |
| 76 | assert json.decode[f64]('-23.6e1')! == -236.0 |
| 77 | assert json.decode[[]int]('[100, 99, 98, 97]')! == [100, 99, 98, 97] |
| 78 | |
| 79 | // Strict mode also works for unquoted numbers |
| 80 | assert json.decode[int]('0', strict: true)! == 0 |
| 81 | assert json.decode[int]('100', strict: true)! == 100 |
| 82 | } |
| 83 | |
| 84 | fn test_invalid_number_string_fails() { |
| 85 | json.decode[int]('"not_a_number"') or { return } |
| 86 | assert false, 'Expected error for invalid number string' |
| 87 | } |
| 88 | |
| 89 | fn test_leading_plus_in_string() { |
| 90 | assert json.decode[int]('"+100"')! == 100 |
| 91 | |
| 92 | json.decode[int]('"+100"', strict: true) or { |
| 93 | assert err is json.JsonDecodeError |
| 94 | return |
| 95 | } |
| 96 | assert false, 'Expected error in strict mode' |
| 97 | } |
| 98 | |