Skip to content

Add a unit test for invalid string escape sequences #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions test/PegtlExecutableTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,36 @@ TEST(PegtlExecutableCase, AnalyzeExecutableGrammar)
ASSERT_EQ(size_t { 0 }, analyze<executable_document>(true))
<< "there shouldn't be any infinite loops in the PEG version of the grammar";
}

TEST(PegtlExecutableCase, InvalidStringEscapeSequence)
{
bool parsedQuery = false;
bool caughtException = false;

try
{
memory_input<> input(R"gql(query { foo @something(arg: "\.") })gql",
"InvalidStringEscapeSequence");
parsedQuery = parse<executable_document>(input);
}
catch (const peg::parse_error& e)
{
ASSERT_NE(nullptr, e.what());

using namespace std::literals;

const std::string_view error { e.what() };
constexpr auto c_start = "InvalidStringEscapeSequence:1:31: parse error matching "sv;
constexpr auto c_end = " graphql::peg::string_escape_sequence_content"sv;

ASSERT_TRUE(error.size() > c_start.size()) << "error message is too short";
ASSERT_TRUE(error.size() > c_end.size()) << "error message is too short";
EXPECT_TRUE(error.substr(0, c_start.size()) == c_start) << e.what();
EXPECT_TRUE(error.substr(error.size() - c_end.size()) == c_end) << e.what();

caughtException = true;
}

EXPECT_TRUE(caughtException) << "should catch a parse exception";
EXPECT_FALSE(parsedQuery) << "should not successfully parse the query";
}