From f14357537e624d332b3277a09b2c00fcc9b05c57 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 13 Feb 2021 16:18:53 +0100 Subject: [PATCH] add unit tests for not consuming matchers --- .../Parsing/Matcher/AbstractMatcher.h | 3 ++ .../Parsing/Matcher/MatcherTests.cpp | 54 ++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/ZoneCodeGeneratorLib/Parsing/Matcher/AbstractMatcher.h b/src/ZoneCodeGeneratorLib/Parsing/Matcher/AbstractMatcher.h index 90891c82..44ab5fca 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Matcher/AbstractMatcher.h +++ b/src/ZoneCodeGeneratorLib/Parsing/Matcher/AbstractMatcher.h @@ -95,6 +95,9 @@ public: result.m_captures.emplace_back(m_capture_id, match); } + if (m_no_consume) + result.m_consumed_token_count = 0; + return result; } }; diff --git a/test/ZoneCodeGeneratorLibTests/Parsing/Matcher/MatcherTests.cpp b/test/ZoneCodeGeneratorLibTests/Parsing/Matcher/MatcherTests.cpp index 60409bdb..e6fbf3e2 100644 --- a/test/ZoneCodeGeneratorLibTests/Parsing/Matcher/MatcherTests.cpp +++ b/test/ZoneCodeGeneratorLibTests/Parsing/Matcher/MatcherTests.cpp @@ -772,7 +772,7 @@ namespace test::parsing::matcher REQUIRE(capture.m_type == HeaderParserValueType::IDENTIFIER); REQUIRE(capture.IdentifierValue() == "hello_world"); } - + REQUIRE(result.HasNextCapture(CAPTURE_LOOP_GROUP)); REQUIRE(result.NextCapture(CAPTURE_LOOP_GROUP).m_type == HeaderParserValueType::NAMESPACE); @@ -971,4 +971,56 @@ namespace test::parsing::matcher REQUIRE(test.PerformTest()); REQUIRE(test.GetConsumedTokenCount() == 3); } + + TEST_CASE("Matcher: Ensure noconsume does not consume a token", "[parsing][matcher]") + { + MatchersTestsHelper test; + const TokenPos pos; + test.Tokens({ + HeaderParserValue::Keyword(pos, HeaderParserValueType::NAMESPACE), + HeaderParserValue::Identifier(pos, new std::string("test_namespace")), + HeaderParserValue::Character(pos, '{'), + HeaderParserValue::Invalid(pos) + }); + const auto create = test.Factory(); + test.Matchers({ + create.Type(HeaderParserValueType::NAMESPACE), + create.Identifier(), + create.Char('{').NoConsume() + }); + + REQUIRE(test.PerformTest()); + REQUIRE(test.GetConsumedTokenCount() == 2); + } + + TEST_CASE("Matcher: Ensure noconsume can be captured", "[parsing][matcher]") + { + static constexpr auto CAPTURE_NAME = 1; + + MatchersTestsHelper test; + const TokenPos pos; + test.Tokens({ + HeaderParserValue::Keyword(pos, HeaderParserValueType::NAMESPACE), + HeaderParserValue::Identifier(pos, new std::string("test_namespace")), + HeaderParserValue::Character(pos, '{'), + HeaderParserValue::Invalid(pos) + }); + const auto create = test.Factory(); + test.Matchers({ + create.Type(HeaderParserValueType::NAMESPACE), + create.Identifier().NoConsume().Capture(CAPTURE_NAME) + }); + test.MatchCallback([](sequence_result_t& result) + { + REQUIRE(result.HasNextCapture(CAPTURE_NAME)); + { + const auto& capture = result.NextCapture(CAPTURE_NAME); + REQUIRE(capture.m_type == HeaderParserValueType::IDENTIFIER); + REQUIRE(capture.IdentifierValue() == "test_namespace"); + } + }); + + REQUIRE(test.PerformTest()); + REQUIRE(test.GetConsumedTokenCount() == 1); + } }