From fb2bc68d39857eac9d69f45b5f2925bf6c0a92bf Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Sun, 11 Dec 2022 22:32:32 +0100 Subject: [PATCH] Refactor HasCall sequence diagram test cases matcher --- tests/t20018/test_case.h | 3 -- tests/t20021/test_case.h | 2 +- tests/test_cases.h | 70 +++++++++++++++++++++++++++++----------- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/tests/t20018/test_case.h b/tests/t20018/test_case.h index 22e4522a..d48116cb 100644 --- a/tests/t20018/test_case.h +++ b/tests/t20018/test_case.h @@ -42,9 +42,6 @@ TEST_CASE("t20018", "[test-case][sequence]") "__print(int)__")); REQUIRE_THAT(puml, HasCall(_A("Factorial<5>"), _A("Factorial<4>"), "__print(int)__")); - REQUIRE_THAT(puml, - !HasCallWithResponse( - _A("Factorial<5>"), _A("Factorial<4>"), "__print(int)__")); REQUIRE_THAT(puml, HasCall(_A("Factorial<4>"), _A("Factorial<3>"), "__print(int)__")); REQUIRE_THAT(puml, diff --git a/tests/t20021/test_case.h b/tests/t20021/test_case.h index f034e315..5868931e 100644 --- a/tests/t20021/test_case.h +++ b/tests/t20021/test_case.h @@ -39,7 +39,7 @@ TEST_CASE("t20021", "[test-case][sequence]") REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("A"), "a2()")); REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("A"), "a3()")); - REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B"), "b1()")); + REQUIRE_THAT(puml, !HasCall(_A("tmain()"), _A("B"), "b1()")); REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B"), "b2()")); save_puml( diff --git a/tests/test_cases.h b/tests/test_cases.h index d4d3287f..276a534d 100644 --- a/tests/test_cases.h +++ b/tests/test_cases.h @@ -82,26 +82,19 @@ template constexpr bool has_type() noexcept return (std::is_same_v || ... || false); } -struct Public { -}; +struct Public { }; -struct Protected { -}; +struct Protected { }; -struct Private { -}; +struct Private { }; -struct Abstract { -}; +struct Abstract { }; -struct Static { -}; +struct Static { }; -struct Const { -}; +struct Const { }; -struct Default { -}; +struct Default { }; struct HasCallWithResultMatcher : ContainsMatcher { HasCallWithResultMatcher( @@ -122,12 +115,51 @@ struct HasCallWithResultMatcher : ContainsMatcher { CasedString m_resultComparator; }; +template class HasCallMatcher : public Catch::MatcherBase { + T m_from, m_to, m_message; + +public: + HasCallMatcher(T from, T to, T message) + : m_from(from) + , m_to{to} + , m_message{message} + { + util::replace_all(m_message, "(", "\\("); + util::replace_all(m_message, "*", "\\*"); + util::replace_all(m_message, ")", "\\)"); + } + + bool match(T const &in) const override + { + std::istringstream fin(in); + std::string line; + std::regex r{fmt::format("{} -> {} " + "(\\[\\[.*\\]\\] )?: {}", + m_from, m_to, m_message)}; + while (std::getline(fin, line)) { + std::smatch base_match; + std::regex_search(in, base_match, r); + if (base_match.size() > 0) + return true; + } + + return false; + } + + std::string describe() const override + { + std::ostringstream ss; + ss << "has call " + << fmt::format("{} -> {} : {}", m_from, m_to, m_message); + return ss.str(); + } +}; + auto HasCall(std::string const &from, std::string const &to, std::string const &message, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes) { - return ContainsMatcher( - CasedString(fmt::format("{} -> {}", from, to), caseSensitivity)); + return HasCallMatcher(from, to, message); } auto HasCall(std::string const &from, std::string const &message, @@ -140,9 +172,9 @@ auto HasCallWithResponse(std::string const &from, std::string const &to, std::string const &message, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes) { - return HasCallWithResultMatcher( - CasedString(fmt::format("{} -> {}", from, to), caseSensitivity), - CasedString(fmt::format("{} --> {}", to, from), caseSensitivity)); + return ContainsMatcher(CasedString( + fmt::format("{} --> {}", to, from), caseSensitivity)) && + HasCallMatcher(from, to, message); } ContainsMatcher HasEntrypoint(std::string const &to, std::string const &message,