add tests for comment skipping

This commit is contained in:
Niels Lohmann 2020-06-19 13:10:35 +02:00
parent 74520d8bb0
commit 0585ecc56b
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 58 additions and 3 deletions

View file

@ -1507,8 +1507,14 @@ scan_number_done:
error_message = "invalid comment"; error_message = "invalid comment";
return token_type::parse_error; return token_type::parse_error;
} }
// skip following whitespace
do
{
get(); get();
} }
while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
}
switch (current) switch (current)
{ {

View file

@ -9574,8 +9574,14 @@ scan_number_done:
error_message = "invalid comment"; error_message = "invalid comment";
return token_type::parse_error; return token_type::parse_error;
} }
// skip following whitespace
do
{
get(); get();
} }
while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
}
switch (current) switch (current)
{ {

View file

@ -224,6 +224,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
json parser_helper(const std::string& s); json parser_helper(const std::string& s);
bool accept_helper(const std::string& s); bool accept_helper(const std::string& s);
void comments_helper(const std::string& s);
json parser_helper(const std::string& s) json parser_helper(const std::string& s)
{ {
@ -241,6 +242,8 @@ json parser_helper(const std::string& s)
json::sax_parse(s, &sdp); json::sax_parse(s, &sdp);
CHECK(j_sax == j); CHECK(j_sax == j);
comments_helper(s);
return j; return j;
} }
@ -275,11 +278,51 @@ bool accept_helper(const std::string& s)
// 6. check if this approach came to the same result // 6. check if this approach came to the same result
CHECK(ok_noexcept == ok_noexcept_cb); CHECK(ok_noexcept == ok_noexcept_cb);
// 7. return result // 7. check if comments are properly ignored
if (ok_accept)
{
comments_helper(s);
}
// 8. return result
return ok_accept; return ok_accept;
} }
void comments_helper(const std::string& s)
{
json _;
// parse/accept with default parser
CHECK_NOTHROW(_ = json::parse(s));
CHECK(json::accept(s));
// parse/accept while skipping comments
CHECK_NOTHROW(_ = json::parse(s, nullptr, false, true));
CHECK(json::accept(s, true));
std::vector<std::string> json_with_comments;
// start with a comment
json_with_comments.push_back(std::string("// this is a comment\n") + s);
json_with_comments.push_back(std::string("/* this is a comment */") + s);
// end with a comment
json_with_comments.push_back(s + "// this is a comment");
json_with_comments.push_back(s + "/* this is a comment */");
// check all strings
for (const auto& json_with_comment : json_with_comments)
{
CAPTURE(json_with_comment)
CHECK_THROWS_AS(_ = json::parse(json_with_comment), json::parse_error);
CHECK(not json::accept(json_with_comment));
CHECK_NOTHROW(_ = json::parse(json_with_comment, nullptr, true, true));
CHECK(json::accept(json_with_comment, true));
}
} }
} // namespace
TEST_CASE("parser class") TEST_CASE("parser class")
{ {
SECTION("parse") SECTION("parse")