Add test cases for Reader (#1108)

* update testcase for reader

* add a helper function

* refactor structured error testing
This commit is contained in:
Chen 2019-12-04 10:24:52 +08:00 committed by GitHub
parent 2983f5a89a
commit a3c8642886
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,7 @@
#include <json/config.h> #include <json/config.h>
#include <json/json.h> #include <json/json.h>
#include <limits> #include <limits>
#include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
@ -2641,168 +2642,214 @@ JSONTEST_FIXTURE_LOCAL(StreamWriterTest, unicode) {
"\"\\t\\n\\ud806\\udca1=\\u0133\\ud82c\\udd1b\\uff67\"\n}"); "\"\\t\\n\\ud806\\udca1=\\u0133\\ud82c\\udd1b\\uff67\"\n}");
} }
struct ReaderTest : JsonTest::TestCase {}; struct ReaderTest : JsonTest::TestCase {
void setStrictMode() {
reader = std::unique_ptr<Json::Reader>(
new Json::Reader(Json::Features{}.strictMode()));
}
void checkStructuredErrors(
const std::vector<Json::Reader::StructuredError>& actual,
const std::vector<Json::Reader::StructuredError>& expected) {
JSONTEST_ASSERT_EQUAL(actual.size(), expected.size());
for (size_t i = 0; i < actual.size(); ++i) {
const auto& a = actual[i];
const auto& e = expected[i];
JSONTEST_ASSERT_EQUAL(a.offset_start, e.offset_start) << i;
JSONTEST_ASSERT_EQUAL(a.offset_limit, e.offset_limit) << i;
JSONTEST_ASSERT_EQUAL(a.message, e.message) << i;
}
}
template <typename Input> void checkParse(Input&& input) {
JSONTEST_ASSERT(reader->parse(input, root));
}
template <typename Input>
void
checkParse(Input&& input,
const std::vector<Json::Reader::StructuredError>& structured) {
JSONTEST_ASSERT(!reader->parse(input, root));
checkStructuredErrors(reader->getStructuredErrors(), structured);
}
template <typename Input>
void checkParse(Input&& input,
const std::vector<Json::Reader::StructuredError>& structured,
const std::string& formatted) {
checkParse(input, structured);
JSONTEST_ASSERT_EQUAL(reader->getFormattedErrorMessages(), formatted);
}
std::unique_ptr<Json::Reader> reader{new Json::Reader()};
Json::Value root;
};
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrors) { JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrors) {
Json::Reader reader; checkParse(R"({ "property" : "value" })");
Json::Value root; }
bool ok = reader.parse("{ \"property\" : \"value\" }", root);
JSONTEST_ASSERT(ok); JSONTEST_FIXTURE_LOCAL(ReaderTest, parseObject) {
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty()); checkParse(R"({"property"})",
JSONTEST_ASSERT(reader.getStructuredErrors().empty()); {{11, 12, "Missing ':' after object member name"}},
"* Line 1, Column 12\n Missing ':' after object member name\n");
checkParse(
R"({"property" : "value" )",
{{22, 22, "Missing ',' or '}' in object declaration"}},
"* Line 1, Column 23\n Missing ',' or '}' in object declaration\n");
checkParse(R"({"property" : "value", )",
{{23, 23, "Missing '}' or object member name"}},
"* Line 1, Column 24\n Missing '}' or object member name\n");
}
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseArray) {
checkParse(
R"([ "value" )", {{10, 10, "Missing ',' or ']' in array declaration"}},
"* Line 1, Column 11\n Missing ',' or ']' in array declaration\n");
checkParse(
R"([ "value1" "value2" ] )",
{{11, 19, "Missing ',' or ']' in array declaration"}},
"* Line 1, Column 12\n Missing ',' or ']' in array declaration\n");
}
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseString) {
checkParse(R"([ "\u8a2a" ])");
checkParse(
R"([ "\ud801" ])",
{{2, 10,
"additional six characters expected to parse unicode surrogate "
"pair."}},
"* Line 1, Column 3\n"
" additional six characters expected to parse unicode surrogate pair.\n"
"See Line 1, Column 10 for detail.\n");
checkParse(R"([ "\ud801\d1234" ])",
{{2, 16,
"expecting another \\u token to begin the "
"second half of a unicode surrogate pair"}},
"* Line 1, Column 3\n"
" expecting another \\u token to begin the "
"second half of a unicode surrogate pair\n"
"See Line 1, Column 12 for detail.\n");
checkParse(R"([ "\ua3t@" ])",
{{2, 10,
"Bad unicode escape sequence in string: "
"hexadecimal digit expected."}},
"* Line 1, Column 3\n"
" Bad unicode escape sequence in string: "
"hexadecimal digit expected.\n"
"See Line 1, Column 9 for detail.\n");
checkParse(
R"([ "\ua3t" ])",
{{2, 9, "Bad unicode escape sequence in string: four digits expected."}},
"* Line 1, Column 3\n"
" Bad unicode escape sequence in string: four digits expected.\n"
"See Line 1, Column 6 for detail.\n");
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseComment) { JSONTEST_FIXTURE_LOCAL(ReaderTest, parseComment) {
Json::Reader reader; checkParse(
Json::Value root; R"({ /*commentBeforeValue*/ "property" : "value" }//commentAfterValue)"
bool ok = reader.parse("{ /*commentBeforeValue*/" "\n");
" \"property\" : \"value\" }" checkParse(" true //comment1\n//comment2\r//comment3\r\n");
"//commentAfterValue\n",
root);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty());
JSONTEST_ASSERT(reader.getStructuredErrors().empty());
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, streamParseWithNoErrors) { JSONTEST_FIXTURE_LOCAL(ReaderTest, streamParseWithNoErrors) {
Json::Reader reader; std::string styled = R"({ "property" : "value" })";
std::string styled = "{ \"property\" : \"value\" }";
std::istringstream iss(styled); std::istringstream iss(styled);
Json::Value root; checkParse(iss);
bool ok = reader.parse(iss, root);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty());
JSONTEST_ASSERT(reader.getStructuredErrors().empty());
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrorsTestingOffsets) { JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithNoErrorsTestingOffsets) {
Json::Reader reader; checkParse(R"({)"
Json::Value root; R"( "property" : ["value", "value2"],)"
bool ok = reader.parse("{ \"property\" : [\"value\", \"value2\"], \"obj\" : " R"( "obj" : { "nested" : -6.2e+15, "bool" : true},)"
"{ \"nested\" : -6.2e+15, \"bool\" : true}, \"null\" :" R"( "null" : null,)"
" null, \"false\" : false }", R"( "false" : false)"
root); R"( })");
JSONTEST_ASSERT(ok); auto checkOffsets = [&](const Json::Value& v, int start, int limit) {
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty()); JSONTEST_ASSERT_EQUAL(v.getOffsetStart(), start);
JSONTEST_ASSERT(reader.getStructuredErrors().empty()); JSONTEST_ASSERT_EQUAL(v.getOffsetLimit(), limit);
JSONTEST_ASSERT(root["property"].getOffsetStart() == 15); };
JSONTEST_ASSERT(root["property"].getOffsetLimit() == 34); checkOffsets(root, 0, 115);
JSONTEST_ASSERT(root["property"][0].getOffsetStart() == 16); checkOffsets(root["property"], 15, 34);
JSONTEST_ASSERT(root["property"][0].getOffsetLimit() == 23); checkOffsets(root["property"][0], 16, 23);
JSONTEST_ASSERT(root["property"][1].getOffsetStart() == 25); checkOffsets(root["property"][1], 25, 33);
JSONTEST_ASSERT(root["property"][1].getOffsetLimit() == 33); checkOffsets(root["obj"], 44, 81);
JSONTEST_ASSERT(root["obj"].getOffsetStart() == 44); checkOffsets(root["obj"]["nested"], 57, 65);
JSONTEST_ASSERT(root["obj"].getOffsetLimit() == 81); checkOffsets(root["obj"]["bool"], 76, 80);
JSONTEST_ASSERT(root["obj"]["nested"].getOffsetStart() == 57); checkOffsets(root["null"], 92, 96);
JSONTEST_ASSERT(root["obj"]["nested"].getOffsetLimit() == 65); checkOffsets(root["false"], 108, 113);
JSONTEST_ASSERT(root["obj"]["bool"].getOffsetStart() == 76);
JSONTEST_ASSERT(root["obj"]["bool"].getOffsetLimit() == 80);
JSONTEST_ASSERT(root["null"].getOffsetStart() == 92);
JSONTEST_ASSERT(root["null"].getOffsetLimit() == 96);
JSONTEST_ASSERT(root["false"].getOffsetStart() == 108);
JSONTEST_ASSERT(root["false"].getOffsetLimit() == 113);
JSONTEST_ASSERT(root.getOffsetStart() == 0);
JSONTEST_ASSERT(root.getOffsetLimit() == 115);
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneError) { JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithOneError) {
Json::Reader reader; checkParse(R"({ "property" :: "value" })",
Json::Value root; {{14, 15, "Syntax error: value, object or array expected."}},
bool ok = reader.parse("{ \"property\" :: \"value\" }", root);
JSONTEST_ASSERT(!ok);
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
"* Line 1, Column 15\n Syntax error: value, object or array " "* Line 1, Column 15\n Syntax error: value, object or array "
"expected.\n"); "expected.\n");
std::vector<Json::Reader::StructuredError> errors = checkParse("s", {{0, 1, "Syntax error: value, object or array expected."}},
reader.getStructuredErrors(); "* Line 1, Column 1\n Syntax error: value, object or array "
JSONTEST_ASSERT(errors.size() == 1); "expected.\n");
JSONTEST_ASSERT(errors.at(0).offset_start == 14); }
JSONTEST_ASSERT(errors.at(0).offset_limit == 15);
JSONTEST_ASSERT(errors.at(0).message == JSONTEST_FIXTURE_LOCAL(ReaderTest, parseSpecialFloat) {
"Syntax error: value, object or array expected."); checkParse(R"({ "a" : Infi })",
{{8, 9, "Syntax error: value, object or array expected."}},
"* Line 1, Column 9\n Syntax error: value, object or array "
"expected.\n");
checkParse(R"({ "a" : Infiniaa })",
{{8, 9, "Syntax error: value, object or array expected."}},
"* Line 1, Column 9\n Syntax error: value, object or array "
"expected.\n");
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, strictModeParseNumber) { JSONTEST_FIXTURE_LOCAL(ReaderTest, strictModeParseNumber) {
Json::Features feature; setStrictMode();
Json::Reader reader(feature.strictMode()); checkParse(
Json::Value root; "123",
bool ok = reader.parse("123", root); {{0, 3,
JSONTEST_ASSERT(!ok); "A valid JSON document must be either an array or an object value."}},
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
"* Line 1, Column 1\n" "* Line 1, Column 1\n"
" A valid JSON document must be either an array or" " A valid JSON document must be either an array or an object value.\n");
" an object value.\n");
std::vector<Json::Reader::StructuredError> errors =
reader.getStructuredErrors();
JSONTEST_ASSERT(errors.size() == 1);
JSONTEST_ASSERT(errors.at(0).offset_start == 0);
JSONTEST_ASSERT(errors.at(0).offset_limit == 3);
JSONTEST_ASSERT(errors.at(0).message ==
"A valid JSON document must be either an array or"
" an object value.");
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseChineseWithOneError) { JSONTEST_FIXTURE_LOCAL(ReaderTest, parseChineseWithOneError) {
Json::Reader reader; // \u4f50\u85e4 佐藤
Json::Value root; checkParse(R"({ "pr)"
bool ok = reader.parse("{ \"pr佐藤erty\" :: \"value\" }", root); "佐藤"
JSONTEST_ASSERT(!ok); R"(erty" :: "value" })",
JSONTEST_ASSERT(reader.getFormattedErrorMessages() == {{18, 19, "Syntax error: value, object or array expected."}},
"* Line 1, Column 19\n Syntax error: value, object or array " "* Line 1, Column 19\n Syntax error: value, object or array "
"expected.\n"); "expected.\n");
std::vector<Json::Reader::StructuredError> errors =
reader.getStructuredErrors();
JSONTEST_ASSERT(errors.size() == 1);
JSONTEST_ASSERT(errors.at(0).offset_start == 18);
JSONTEST_ASSERT(errors.at(0).offset_limit == 19);
JSONTEST_ASSERT(errors.at(0).message ==
"Syntax error: value, object or array expected.");
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithDetailError) { JSONTEST_FIXTURE_LOCAL(ReaderTest, parseWithDetailError) {
Json::Reader reader; checkParse(R"({ "property" : "v\alue" })",
Json::Value root; {{15, 23, "Bad escape sequence in string"}},
bool ok = reader.parse("{ \"property\" : \"v\\alue\" }", root); "* Line 1, Column 16\n"
JSONTEST_ASSERT(!ok); " Bad escape sequence in string\n"
JSONTEST_ASSERT(reader.getFormattedErrorMessages() == "See Line 1, Column 20 for detail.\n");
"* Line 1, Column 16\n Bad escape sequence in string\nSee "
"Line 1, Column 20 for detail.\n");
std::vector<Json::Reader::StructuredError> errors =
reader.getStructuredErrors();
JSONTEST_ASSERT(errors.size() == 1);
JSONTEST_ASSERT(errors.at(0).offset_start == 15);
JSONTEST_ASSERT(errors.at(0).offset_limit == 23);
JSONTEST_ASSERT(errors.at(0).message == "Bad escape sequence in string");
} }
JSONTEST_FIXTURE_LOCAL(ReaderTest, pushErrorTest) { JSONTEST_FIXTURE_LOCAL(ReaderTest, pushErrorTest) {
Json::Reader reader; checkParse(R"({ "AUTHOR" : 123 })");
Json::Value root;
{
bool ok = reader.parse("{ \"AUTHOR\" : 123 }", root);
JSONTEST_ASSERT(ok);
if (!root["AUTHOR"].isString()) { if (!root["AUTHOR"].isString()) {
ok = reader.pushError(root["AUTHOR"], "AUTHOR must be a string"); JSONTEST_ASSERT(
reader->pushError(root["AUTHOR"], "AUTHOR must be a string"));
} }
JSONTEST_ASSERT(ok); JSONTEST_ASSERT_STRING_EQUAL(reader->getFormattedErrorMessages(),
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
"* Line 1, Column 14\n" "* Line 1, Column 14\n"
" AUTHOR must be a string\n"); " AUTHOR must be a string\n");
}
{ checkParse(R"({ "AUTHOR" : 123 })");
bool ok = reader.parse("{ \"AUTHOR\" : 123 }", root);
JSONTEST_ASSERT(ok);
if (!root["AUTHOR"].isString()) { if (!root["AUTHOR"].isString()) {
ok = reader.pushError(root["AUTHOR"], "AUTHOR must be a string", JSONTEST_ASSERT(reader->pushError(root["AUTHOR"], "AUTHOR must be a string",
root["AUTHOR"]); root["AUTHOR"]));
} }
JSONTEST_ASSERT(ok); JSONTEST_ASSERT_STRING_EQUAL(reader->getFormattedErrorMessages(),
JSONTEST_ASSERT(reader.getFormattedErrorMessages() ==
"* Line 1, Column 14\n" "* Line 1, Column 14\n"
" AUTHOR must be a string\n" " AUTHOR must be a string\n"
"See Line 1, Column 14 for detail.\n"); "See Line 1, Column 14 for detail.\n");
} }
}
struct CharReaderTest : JsonTest::TestCase {}; struct CharReaderTest : JsonTest::TestCase {};