Skip to content

Commit b4e8bcc

Browse files
committed
Adding positive and negative unit test cases
1 parent 44c6466 commit b4e8bcc

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ gtest_add_tests(TARGET pegtl_combined_tests)
7171

7272
add_executable(pegtl_executable_tests PegtlExecutableTests.cpp)
7373
target_link_libraries(pegtl_executable_tests PRIVATE
74-
taocpp::pegtl
74+
graphqlpeg
7575
GTest::GTest
7676
GTest::Main)
7777
target_include_directories(pegtl_executable_tests PRIVATE

test/PegtlExecutableTests.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <gtest/gtest.h>
55

6+
#include "graphqlservice/GraphQLParse.h"
7+
68
#include "graphqlservice/internal/Grammar.h"
79

810
#include <tao/pegtl/contrib/analyze.hpp>
@@ -169,6 +171,62 @@ TEST(PegtlExecutableCase, InvalidStringEscapeSequence)
169171
caughtException = true;
170172
}
171173

174+
EXPECT_TRUE(caughtException) << "should catch a parse exception";
175+
EXPECT_FALSE(parsedQuery) << "should not successfully parse the query";
176+
}
177+
178+
using namespace std::literals;
179+
180+
constexpr auto queryWithDepth3 = R"gql(query {
181+
foo {
182+
bar
183+
}
184+
})gql"sv;
185+
186+
TEST(PegtlExecutableCase, ParserDepthLimitNotExceeded)
187+
{
188+
bool parsedQuery = false;
189+
190+
try
191+
{
192+
auto query = peg::parseString(queryWithDepth3, 3);
193+
194+
parsedQuery = query.root != nullptr;
195+
}
196+
catch (const peg::parse_error& ex)
197+
{
198+
FAIL() << ex.what();
199+
}
200+
201+
EXPECT_TRUE(parsedQuery) << "should parse the query";
202+
}
203+
204+
TEST(PegtlExecutableCase, ParserDepthLimitExceeded)
205+
{
206+
bool parsedQuery = false;
207+
bool caughtException = false;
208+
209+
try
210+
{
211+
auto query = peg::parseString(queryWithDepth3, 2);
212+
213+
parsedQuery = query.root != nullptr;
214+
}
215+
catch (const peg::parse_error& ex)
216+
{
217+
ASSERT_NE(nullptr, ex.what());
218+
219+
using namespace std::literals;
220+
221+
const std::string_view error { ex.what() };
222+
constexpr auto expected =
223+
"GraphQL:4:3: Exceeded 2 nested depth limit for https://spec.graphql.org/October2021/#SelectionSet"sv;
224+
225+
EXPECT_TRUE(error == expected) << ex.what();
226+
227+
caughtException = true;
228+
}
229+
172230
EXPECT_TRUE(caughtException) << "should catch a parse exception";
173231
EXPECT_FALSE(parsedQuery) << "should not successfully parse the query";
174232
}

0 commit comments

Comments
 (0)