Added custom catch matchers for test cases

This commit is contained in:
Bartek Kryza
2021-02-27 13:38:49 +01:00
parent 63143d32b4
commit 74d5ced824
7 changed files with 184 additions and 32 deletions

View File

@@ -52,4 +52,3 @@ test_plantuml: test
.PHONY: clang-format .PHONY: clang-format
clang-format: clang-format:
docker run --rm -v $(CURDIR):/root/sources bkryza/clang-format-check:1.2 docker run --rm -v $(CURDIR):/root/sources bkryza/clang-format-check:1.2
sudo chown -R $(shell id -u):$(shell id -g) src/*

View File

@@ -114,9 +114,9 @@ public:
std::string type{}; std::string type{};
if (m.type != "void") if (m.type != "void")
type = m.type; type = m.type + " ";
ostr << to_string(m.scope) << type << " " << m.name + "()"; ostr << to_string(m.scope) << type << m.name + "()";
if (m.is_const) if (m.is_const)
ostr << " const"; ostr << " const";
@@ -145,7 +145,8 @@ public:
ostr << "}" << std::endl; ostr << "}" << std::endl;
for (const auto &b : c.bases) { for (const auto &b : c.bases) {
ostr << b.name << " <|-- " << c.name << std::endl; ostr << ns_relative(m_config.using_namespace, b.name) << " <|-- "
<< ns_relative(m_config.using_namespace, c.name) << std::endl;
} }
for (const auto &r : c.relationships) { for (const auto &r : c.relationships) {
@@ -161,7 +162,7 @@ public:
void generate(const enum_ &e, std::ostream &ostr) const void generate(const enum_ &e, std::ostream &ostr) const
{ {
ostr << "Enum " << ns_relative(m_config.using_namespace, e.name) << " {" ostr << "enum " << ns_relative(m_config.using_namespace, e.name) << " {"
<< std::endl; << std::endl;
for (const auto &enum_constant : e.constants) { for (const auto &enum_constant : e.constants) {

View File

@@ -43,10 +43,16 @@ TEST_CASE("Test t00002", "[unit-test]")
REQUIRE_THAT(puml, StartsWith("@startuml")); REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n")); REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, Contains("abstract A")); REQUIRE_THAT(puml, IsAbstractClass("A"));
REQUIRE_THAT(puml, Contains("class B")); REQUIRE_THAT(puml, IsClass("B"));
REQUIRE_THAT(puml, Contains("class C")); REQUIRE_THAT(puml, IsClass("C"));
REQUIRE_THAT(puml, Contains("class D")); REQUIRE_THAT(puml, IsClass("D"));
REQUIRE_THAT(puml, IsBaseClass("A", "B"));
REQUIRE_THAT(puml, IsBaseClass("A", "C"));
REQUIRE_THAT(puml, IsBaseClass("B", "D"));
REQUIRE_THAT(puml, IsBaseClass("C", "D"));
REQUIRE_THAT(puml, IsMethod(Abstract(Public("foo_a"))));
REQUIRE_THAT(puml, IsMethod(Abstract(Public("foo_c"))));
save_puml( save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml); "./" + config.output_directory + "/" + diagram->name + ".puml", puml);

View File

@@ -44,21 +44,21 @@ TEST_CASE("Test t00003", "[unit-test]")
REQUIRE_THAT(puml, EndsWith("@enduml\n")); REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, Contains("class A")); REQUIRE_THAT(puml, Contains("class A"));
REQUIRE_THAT(puml, Contains("+ A() = default")); REQUIRE_THAT(puml, IsMethod(Default(Public("A"))));
REQUIRE_THAT(puml, Contains("+ A() = default")); REQUIRE_THAT(puml, IsMethod(Default(Public("~A"))));
REQUIRE_THAT(puml, Contains("+ A() = default"));
REQUIRE_THAT(puml, Contains("+ ~A() = default")); REQUIRE_THAT(puml, IsMethod(Public("basic_method")));
REQUIRE_THAT(puml, Contains("+ basic_method()")); REQUIRE_THAT(puml, IsMethod(Static(Public("int static_method"))));
REQUIRE_THAT(puml, Contains("{static} +int static_method()")); REQUIRE_THAT(puml, IsMethod(Const(Public("const_method"))));
REQUIRE_THAT(puml, Contains("+ const_method() const")); REQUIRE_THAT(puml, IsMethod(Protected("protected_method")));
REQUIRE_THAT(puml, Contains("# protected_method()")); REQUIRE_THAT(puml, IsMethod(Private("private_method")));
REQUIRE_THAT(puml, Contains("- private_method()")); REQUIRE_THAT(puml, IsField(Public("int public_member")));
REQUIRE_THAT(puml, Contains("+int public_member")); REQUIRE_THAT(puml, IsField(Protected("int protected_member")));
REQUIRE_THAT(puml, Contains("#int protected_member")); REQUIRE_THAT(puml, IsField(Private("int private_member")));
REQUIRE_THAT(puml, Contains("-int private_member"));
REQUIRE_THAT(puml, Contains("-int a")); REQUIRE_THAT(puml, IsField(Private("int a")));
REQUIRE_THAT(puml, Contains("-int b")); REQUIRE_THAT(puml, IsField(Private("int b")));
REQUIRE_THAT(puml, Contains("-int c")); REQUIRE_THAT(puml, IsField(Private("int c")));
save_puml( save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml); "./" + config.output_directory + "/" + diagram->name + ".puml", puml);

View File

@@ -44,12 +44,15 @@ TEST_CASE("Test t00004", "[unit-test]")
REQUIRE_THAT(puml, StartsWith("@startuml")); REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n")); REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, Contains("class A")); REQUIRE_THAT(puml, IsClass("A"));
REQUIRE_THAT(puml, Contains("A +-- AA")); REQUIRE_THAT(puml, IsClass("AA"));
REQUIRE_THAT(puml, Contains("AA +-- AAA")); REQUIRE_THAT(puml, IsClass("AAA"));
REQUIRE_THAT(puml, Contains("AA +-- Lights")); REQUIRE_THAT(puml, IsEnum("Lights"));
REQUIRE_THAT(puml, Contains("+ foo() const")); REQUIRE_THAT(puml, IsInnerClass("A", "AA"));
REQUIRE_THAT(puml, Contains("+ foo2() const")); REQUIRE_THAT(puml, IsInnerClass("AA", "AAA"));
REQUIRE_THAT(puml, IsInnerClass("AA", "Lights"));
REQUIRE_THAT(puml, IsMethod(Const(Public("foo"))));
REQUIRE_THAT(puml, IsMethod(Const(Public("foo2"))));
save_puml( save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml); "./" + config.output_directory + "/" + diagram->name + ".puml", puml);

View File

@@ -84,14 +84,25 @@ void save_puml(const std::string &path, const std::string &puml)
{ {
std::filesystem::path p{path}; std::filesystem::path p{path};
std::filesystem::create_directory(p.parent_path()); std::filesystem::create_directory(p.parent_path());
spdlog::error("PWD: {}", std::filesystem::current_path().string());
spdlog::error("SAVING TEST PWD {} DIAGRAM: {}", p.string());
std::ofstream ofs; std::ofstream ofs;
ofs.open(p, std::ofstream::out | std::ofstream::trunc); ofs.open(p, std::ofstream::out | std::ofstream::trunc);
ofs << puml; ofs << puml;
ofs.close(); ofs.close();
} }
using clanguml::test::matchers::Abstract;
using clanguml::test::matchers::Const;
using clanguml::test::matchers::Default;
using clanguml::test::matchers::IsAbstractClass;
using clanguml::test::matchers::IsBaseClass;
using clanguml::test::matchers::IsClass;
using clanguml::test::matchers::IsEnum;
using clanguml::test::matchers::IsInnerClass;
using clanguml::test::matchers::Private;
using clanguml::test::matchers::Protected;
using clanguml::test::matchers::Public;
using clanguml::test::matchers::Static;
#include "t00001/test_case.h" #include "t00001/test_case.h"
#include "t00002/test_case.h" #include "t00002/test_case.h"
#include "t00003/test_case.h" #include "t00003/test_case.h"

View File

@@ -57,3 +57,135 @@ std::string generate_class_puml(
clanguml::model::class_diagram::diagram &model); clanguml::model::class_diagram::diagram &model);
void save_puml(const std::string &path, const std::string &puml); void save_puml(const std::string &path, const std::string &puml);
namespace clanguml {
namespace test {
namespace matchers {
using Catch::CaseSensitive;
using Catch::Matchers::StdString::CasedString;
using Catch::Matchers::StdString::ContainsMatcher;
struct Public {
Public(std::string const &method)
: m_method{method}
{
}
operator std::string() const { return "+" + m_method; }
std::string m_method;
};
struct Protected {
Protected(std::string const &method)
: m_method{method}
{
}
operator std::string() const { return "#" + m_method; }
std::string m_method;
};
struct Private {
Private(std::string const &method)
: m_method{method}
{
}
operator std::string() const { return "-" + m_method; }
std::string m_method;
};
struct Abstract {
Abstract(std::string const &method)
: m_method{method}
{
}
operator std::string() const { return "{abstract} " + m_method; }
std::string m_method;
};
struct Static {
Static(std::string const &method)
: m_method{method}
{
}
operator std::string() const { return "{static} " + m_method; }
std::string m_method;
};
struct Const {
Const(std::string const &method)
: m_method{method}
{
}
operator std::string() const { return m_method; }
std::string m_method;
};
struct Default {
Default(std::string const &method)
: m_method{method}
{
}
operator std::string() const { return m_method; }
std::string m_method;
};
ContainsMatcher IsClass(std::string const &str,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(CasedString("class " + str, caseSensitivity));
}
ContainsMatcher IsEnum(std::string const &str,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(CasedString("enum " + str, caseSensitivity));
}
ContainsMatcher IsAbstractClass(std::string const &str,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(CasedString("abstract " + str, caseSensitivity));
}
ContainsMatcher IsBaseClass(std::string const &base, std::string const &sub,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(CasedString(base + " <|-- " + sub, caseSensitivity));
}
ContainsMatcher IsInnerClass(std::string const &parent,
std::string const &inner,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(
CasedString(parent + " +-- " + inner, caseSensitivity));
}
ContainsMatcher IsMethod(std::string const &name,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(CasedString(name + "()", caseSensitivity));
}
ContainsMatcher IsField(std::string const &name,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(CasedString(name, caseSensitivity));
}
}
}
}