Skip to content

Commit e4f0554

Browse files
committed
add a helper function
1 parent 771ea78 commit e4f0554

File tree

1 file changed

+73
-17
lines changed

1 file changed

+73
-17
lines changed

src/test_lib_json/main.cpp

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,7 +2642,21 @@ JSONTEST_FIXTURE_LOCAL(StreamWriterTest, unicode) {
26422642
"\"\\t\\n\\ud806\\udca1=\\u0133\\ud82c\\udd1b\\uff67\"\n}");
26432643
}
26442644

2645-
struct ReaderTest : JsonTest::TestCase {};
2645+
struct ReaderTest : JsonTest::TestCase {
2646+
void testStructuredError(Json::Reader reader, Json::ArrayIndex errors_size,
2647+
Json::Reader::StructuredError structederror[]) {
2648+
std::vector<Json::Reader::StructuredError> errors =
2649+
reader.getStructuredErrors();
2650+
JSONTEST_ASSERT(errors.size() == errors_size);
2651+
for (size_t i = 0; i < errors_size; i++) {
2652+
JSONTEST_ASSERT(errors.at(i).offset_start ==
2653+
structederror[i].offset_start);
2654+
JSONTEST_ASSERT(errors.at(i).offset_limit ==
2655+
structederror[i].offset_limit);
2656+
JSONTEST_ASSERT(errors.at(i).message == structederror[i].message);
2657+
}
2658+
}
2659+
};
26462660

26472661
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrors) {
26482662
Json::Reader reader;
@@ -2662,20 +2676,29 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseObject) {
26622676
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
26632677
"* Line 1, Column 12\n Missing ':' after object "
26642678
"member name\n");
2679+
Json::Reader::StructuredError structuredError[] = {
2680+
{11, 12, "Missing ':' after object member name"}};
2681+
testStructuredError(reader, 1, structuredError);
26652682
}
26662683
{
26672684
bool ok = reader.parse("{\"property\" : \"value\" ", root);
26682685
JSONTEST_ASSERT(!ok);
26692686
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
26702687
"* Line 1, Column 23\n Missing ',' or '}' in object "
26712688
"declaration\n");
2689+
Json::Reader::StructuredError structuredError[] = {
2690+
{22, 22, "Missing ',' or '}' in object declaration"}};
2691+
testStructuredError(reader, 1, structuredError);
26722692
}
26732693
{
26742694
bool ok = reader.parse("{\"property\" : \"value\", ", root);
26752695
JSONTEST_ASSERT(!ok);
26762696
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
26772697
"* Line 1, Column 24\n Missing '}' or object "
26782698
"member name\n");
2699+
Json::Reader::StructuredError structuredError[] = {
2700+
{23, 23, "Missing '}' or object member name"}};
2701+
testStructuredError(reader, 1, structuredError);
26792702
}
26802703
}
26812704

@@ -2688,13 +2711,19 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseArray) {
26882711
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
26892712
"* Line 1, Column 11\n Missing ',' or ']' in array "
26902713
"declaration\n");
2714+
Json::Reader::StructuredError structuredError[] = {
2715+
{10, 10, "Missing ',' or ']' in array declaration"}};
2716+
testStructuredError(reader, 1, structuredError);
26912717
}
26922718
{
26932719
bool ok = reader.parse("[ \"value1\" \"value2\" ] ", root);
26942720
JSONTEST_ASSERT(!ok);
26952721
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
26962722
"* Line 1, Column 12\n Missing ',' or ']' in array "
26972723
"declaration\n");
2724+
Json::Reader::StructuredError structuredError[] = {
2725+
{11, 19, "Missing ',' or ']' in array declaration"}};
2726+
testStructuredError(reader, 1, structuredError);
26982727
}
26992728
}
27002729

@@ -2708,13 +2737,18 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseString) {
27082737
JSONTEST_ASSERT(reader.getStructuredErrors().empty());
27092738
}
27102739
{
2711-
bool ok = reader.parse("[ \"\\uD801\" ]", root);
2740+
bool ok = reader.parse("[ \"\\ud801\" ]", root);
27122741
JSONTEST_ASSERT(!ok);
27132742
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
27142743
"* Line 1, Column 3\n"
27152744
" additional six characters expected to "
27162745
"parse unicode surrogate pair.\n"
27172746
"See Line 1, Column 10 for detail.\n");
2747+
Json::Reader::StructuredError structuredError[] = {
2748+
{2, 10,
2749+
"additional six characters expected to "
2750+
"parse unicode surrogate pair."}};
2751+
testStructuredError(reader, 1, structuredError);
27182752
}
27192753
{
27202754
bool ok = reader.parse("[ \"\\ud801\\d1234\" ]", root);
@@ -2724,6 +2758,11 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseString) {
27242758
" expecting another \\u token to begin the "
27252759
"second half of a unicode surrogate pair\n"
27262760
"See Line 1, Column 12 for detail.\n");
2761+
Json::Reader::StructuredError structuredError[] = {
2762+
{2, 16,
2763+
"expecting another \\u token to begin the "
2764+
"second half of a unicode surrogate pair"}};
2765+
testStructuredError(reader, 1, structuredError);
27272766
}
27282767
{
27292768
bool ok = reader.parse("[ \"\\ua3t@\" ]", root);
@@ -2733,6 +2772,11 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseString) {
27332772
" Bad unicode escape sequence in string: "
27342773
"hexadecimal digit expected.\n"
27352774
"See Line 1, Column 9 for detail.\n");
2775+
Json::Reader::StructuredError structuredError[] = {
2776+
{2, 10,
2777+
"Bad unicode escape sequence in string: "
2778+
"hexadecimal digit expected."}};
2779+
testStructuredError(reader, 1, structuredError);
27362780
}
27372781
{
27382782
bool ok = reader.parse("[ \"\\ua3t\" ]", root);
@@ -2742,6 +2786,9 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseString) {
27422786
"* Line 1, Column 3\n"
27432787
" Bad unicode escape sequence in string: four digits expected.\n"
27442788
"See Line 1, Column 6 for detail.\n");
2789+
Json::Reader::StructuredError structuredError[] = {
2790+
{2, 9, "Bad unicode escape sequence in string: four digits expected."}};
2791+
testStructuredError(reader, 1, structuredError);
27452792
}
27462793
}
27472794

@@ -2816,7 +2863,7 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrorsTestingOffsets) {
28162863
JSONTEST_ASSERT(root.getOffsetLimit() == 115);
28172864
}
28182865

2819-
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneErrorTestingOffsets) {
2866+
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneError) {
28202867
Json::Reader reader;
28212868
Json::Value root;
28222869
{
@@ -2826,13 +2873,9 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneErrorTestingOffsets) {
28262873
reader.getFormattedErrorMessages() ==
28272874
"* Line 1, Column 15\n Syntax error: value, object or array "
28282875
"expected.\n");
2829-
std::vector<Json::Reader::StructuredError> errors =
2830-
reader.getStructuredErrors();
2831-
JSONTEST_ASSERT(errors.size() == 1);
2832-
JSONTEST_ASSERT(errors.at(0).offset_start == 14);
2833-
JSONTEST_ASSERT(errors.at(0).offset_limit == 15);
2834-
JSONTEST_ASSERT(errors.at(0).message ==
2835-
"Syntax error: value, object or array expected.");
2876+
Json::Reader::StructuredError structuredError[] = {
2877+
{14, 15, "Syntax error: value, object or array expected."}};
2878+
testStructuredError(reader, 1, structuredError);
28362879
}
28372880
{
28382881
bool ok = reader.parse("s", root);
@@ -2841,13 +2884,9 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneErrorTestingOffsets) {
28412884
reader.getFormattedErrorMessages() ==
28422885
"* Line 1, Column 1\n Syntax error: value, object or array "
28432886
"expected.\n");
2844-
std::vector<Json::Reader::StructuredError> errors =
2845-
reader.getStructuredErrors();
2846-
JSONTEST_ASSERT(errors.size() == 1);
2847-
JSONTEST_ASSERT(errors.at(0).offset_start == 0);
2848-
JSONTEST_ASSERT(errors.at(0).offset_limit == 1);
2849-
JSONTEST_ASSERT(errors.at(0).message ==
2850-
"Syntax error: value, object or array expected.");
2887+
Json::Reader::StructuredError structuredError[] = {
2888+
{0, 1, "Syntax error: value, object or array expected."}};
2889+
testStructuredError(reader, 1, structuredError);
28512890
}
28522891
}
28532892

@@ -2861,6 +2900,9 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseSpecialFloat) {
28612900
reader.getFormattedErrorMessages() ==
28622901
"* Line 1, Column 9\n Syntax error: value, object or array "
28632902
"expected.\n");
2903+
Json::Reader::StructuredError structuredError[] = {
2904+
{8, 9, "Syntax error: value, object or array expected."}};
2905+
testStructuredError(reader, 1, structuredError);
28642906
}
28652907
{
28662908
bool ok = reader.parse("{ \"a\" : Infiniaa }", root);
@@ -2869,6 +2911,9 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseSpecialFloat) {
28692911
reader.getFormattedErrorMessages() ==
28702912
"* Line 1, Column 9\n Syntax error: value, object or array "
28712913
"expected.\n");
2914+
Json::Reader::StructuredError structuredError[] = {
2915+
{8, 9, "Syntax error: value, object or array expected."}};
2916+
testStructuredError(reader, 1, structuredError);
28722917
}
28732918
}
28742919

@@ -2882,6 +2927,11 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, strictModeParseNumber) {
28822927
"* Line 1, Column 1\n"
28832928
" A valid JSON document must be either an array or"
28842929
" an object value.\n");
2930+
Json::Reader::StructuredError structuredError[] = {
2931+
{0, 3,
2932+
"A valid JSON document must be either an array or"
2933+
" an object value."}};
2934+
testStructuredError(reader, 1, structuredError);
28852935
}
28862936

28872937
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseChineseWithOneError) {
@@ -2892,6 +2942,9 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseChineseWithOneError) {
28922942
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
28932943
"* Line 1, Column 19\n Syntax error: value, object or array "
28942944
"expected.\n");
2945+
Json::Reader::StructuredError structuredError[] = {
2946+
{18, 19, "Syntax error: value, object or array expected."}};
2947+
testStructuredError(reader, 1, structuredError);
28952948
}
28962949

28972950
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithDetailError) {
@@ -2902,6 +2955,9 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithDetailError) {
29022955
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
29032956
"* Line 1, Column 16\n Bad escape sequence in string\nSee "
29042957
"Line 1, Column 20 for detail.\n");
2958+
Json::Reader::StructuredError structuredError[] = {
2959+
{15, 23, "Bad escape sequence in string"}};
2960+
testStructuredError(reader, 1, structuredError);
29052961
}
29062962

29072963
JSONTEST_FIXTURE_LOCAL(ReaderTest, pushErrorTest) {

0 commit comments

Comments
 (0)