|
1 |
| -#include <exception> |
2 | 1 | #include <gtest/gtest.h>
|
| 2 | + |
| 3 | +#include <exception> |
3 | 4 | #include <sstream>
|
4 | 5 |
|
5 | 6 | #include <sourcemeta/core/json.h>
|
6 | 7 |
|
7 |
| -#define EXPECT_PARSE_ERROR(input, expected_line, expected_column) \ |
| 8 | +#define __EXPECT_PARSE_ERROR(input, expected_line, expected_column, \ |
| 9 | + expected_error, expected_message) \ |
8 | 10 | try { \
|
9 | 11 | sourcemeta::core::parse_json((input)); \
|
10 | 12 | FAIL() << "The parse function was expected to throw"; \
|
11 |
| - } catch (const sourcemeta::core::JSONParseError &error) { \ |
| 13 | + } catch (const sourcemeta::core::expected_error &error) { \ |
12 | 14 | EXPECT_EQ(error.line(), expected_line); \
|
13 | 15 | EXPECT_EQ(error.column(), expected_column); \
|
| 16 | + EXPECT_STREQ(error.what(), expected_message); \ |
14 | 17 | SUCCEED(); \
|
15 | 18 | } catch (const std::exception &) { \
|
16 |
| - FAIL() << "The parse function was expected to throw a parse error"; \ |
| 19 | + FAIL() \ |
| 20 | + << "The parse function was expected to throw the desired parse error"; \ |
17 | 21 | }
|
18 | 22 |
|
| 23 | +#define EXPECT_PARSE_ERROR(input, expected_line, expected_column) \ |
| 24 | + __EXPECT_PARSE_ERROR(input, expected_line, expected_column, JSONParseError, \ |
| 25 | + "Failed to parse the JSON document"); |
| 26 | + |
| 27 | +#define EXPECT_PARSE_ERROR_INTEGER_LIMIT(input, expected_line, \ |
| 28 | + expected_column) \ |
| 29 | + __EXPECT_PARSE_ERROR(input, expected_line, expected_column, \ |
| 30 | + JSONParseIntegerLimitError, \ |
| 31 | + "The JSON value is not representable by the IETF RFC " \ |
| 32 | + "8259 interoperable signed integer range"); |
| 33 | + |
19 | 34 | TEST(JSON_parse_error, boolean_true_invalid) {
|
20 | 35 | std::istringstream input{"trrue"};
|
21 | 36 | EXPECT_PARSE_ERROR(input, 1, 3);
|
@@ -513,6 +528,16 @@ TEST(JSON_parse_error, integer_negative_with_leading_zero) {
|
513 | 528 | EXPECT_PARSE_ERROR(input, 1, 3);
|
514 | 529 | }
|
515 | 530 |
|
| 531 | +TEST(JSON_parse_error, integer_slightly_over_64_bit_positive_limit) { |
| 532 | + std::istringstream input{"9223372036854776000"}; |
| 533 | + EXPECT_PARSE_ERROR_INTEGER_LIMIT(input, 1, 1); |
| 534 | +} |
| 535 | + |
| 536 | +TEST(JSON_parse_error, integer_slightly_over_64_bit_positive_limit_in_object) { |
| 537 | + std::istringstream input{"{\"foo\":9223372036854776000}"}; |
| 538 | + EXPECT_PARSE_ERROR_INTEGER_LIMIT(input, 1, 8); |
| 539 | +} |
| 540 | + |
516 | 541 | TEST(JSON_parse_error, integer_negative_with_leading_zero_and_space) {
|
517 | 542 | std::istringstream input{"[-0 5]"};
|
518 | 543 | EXPECT_PARSE_ERROR(input, 1, 5);
|
@@ -638,6 +663,24 @@ TEST(JSON_parse_error, read_json_invalid) {
|
638 | 663 | std::filesystem::path{TEST_DIRECTORY} / "stub_invalid.json");
|
639 | 664 | EXPECT_EQ(error.line(), 3);
|
640 | 665 | EXPECT_EQ(error.column(), 9);
|
| 666 | + EXPECT_STREQ(error.what(), "Failed to parse the JSON document"); |
| 667 | + } catch (...) { |
| 668 | + FAIL() << "The parse function was expected to throw a file parse error"; |
| 669 | + } |
| 670 | +} |
| 671 | + |
| 672 | +TEST(JSON_parse_error, read_json_invalid_bigint) { |
| 673 | + try { |
| 674 | + sourcemeta::core::read_json(std::filesystem::path{TEST_DIRECTORY} / |
| 675 | + "stub_bigint.json"); |
| 676 | + } catch (const sourcemeta::core::JSONFileParseError &error) { |
| 677 | + EXPECT_EQ(error.path(), |
| 678 | + std::filesystem::path{TEST_DIRECTORY} / "stub_bigint.json"); |
| 679 | + EXPECT_EQ(error.line(), 1); |
| 680 | + EXPECT_EQ(error.column(), 1); |
| 681 | + EXPECT_STREQ(error.what(), |
| 682 | + "The JSON value is not representable by the IETF RFC 8259 " |
| 683 | + "interoperable signed integer range"); |
641 | 684 | } catch (...) {
|
642 | 685 | FAIL() << "The parse function was expected to throw a file parse error";
|
643 | 686 | }
|
|
0 commit comments