✅ add tests for comment skipping
This commit is contained in:
parent
74520d8bb0
commit
0585ecc56b
3 changed files with 58 additions and 3 deletions
|
@ -1507,7 +1507,13 @@ scan_number_done:
|
|||
error_message = "invalid comment";
|
||||
return token_type::parse_error;
|
||||
}
|
||||
get();
|
||||
|
||||
// skip following whitespace
|
||||
do
|
||||
{
|
||||
get();
|
||||
}
|
||||
while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
|
||||
}
|
||||
|
||||
switch (current)
|
||||
|
|
|
@ -9574,7 +9574,13 @@ scan_number_done:
|
|||
error_message = "invalid comment";
|
||||
return token_type::parse_error;
|
||||
}
|
||||
get();
|
||||
|
||||
// skip following whitespace
|
||||
do
|
||||
{
|
||||
get();
|
||||
}
|
||||
while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
|
||||
}
|
||||
|
||||
switch (current)
|
||||
|
|
|
@ -224,6 +224,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
|
|||
|
||||
json parser_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)
|
||||
{
|
||||
|
@ -241,6 +242,8 @@ json parser_helper(const std::string& s)
|
|||
json::sax_parse(s, &sdp);
|
||||
CHECK(j_sax == j);
|
||||
|
||||
comments_helper(s);
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
|
@ -275,11 +278,51 @@ bool accept_helper(const std::string& s)
|
|||
// 6. check if this approach came to the same result
|
||||
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;
|
||||
}
|
||||
|
||||
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")
|
||||
{
|
||||
SECTION("parse")
|
||||
|
|
Loading…
Reference in a new issue