Refactor HasCall sequence diagram test cases matcher

This commit is contained in:
Bartek Kryza
2022-12-11 22:32:32 +01:00
parent e5e7df43e8
commit fb2bc68d39
3 changed files with 52 additions and 23 deletions

View File

@@ -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,

View File

@@ -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(

View File

@@ -82,26 +82,19 @@ template <typename T, typename... Ts> constexpr bool has_type() noexcept
return (std::is_same_v<T, Ts> || ... || 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 <typename T> class HasCallMatcher : public Catch::MatcherBase<T> {
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,