From a216a81e19cbe807e32d0c21995a8d990292495d Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Wed, 30 Mar 2022 01:01:01 +0200 Subject: [PATCH] Added relationship and scope filter test case --- src/common/model/diagram_filter.cc | 8 +-- src/common/model/diagram_filter.h | 6 +- src/config/config.cc | 94 +++++++++++++++--------------- tests/t00040/.clang-uml | 21 +++++++ tests/t00040/t00040.cc | 36 ++++++++++++ tests/t00040/test_case.h | 50 ++++++++++++++++ tests/test_cases.cc | 1 + tests/test_cases.h | 2 +- tests/test_cases.yaml | 3 + 9 files changed, 166 insertions(+), 55 deletions(-) create mode 100644 tests/t00040/.clang-uml create mode 100644 tests/t00040/t00040.cc create mode 100644 tests/t00040/test_case.h diff --git a/src/common/model/diagram_filter.cc b/src/common/model/diagram_filter.cc index 3f24868d..165b6347 100644 --- a/src/common/model/diagram_filter.cc +++ b/src/common/model/diagram_filter.cc @@ -158,7 +158,7 @@ std::optional subclass_filter::match( relationship_filter::relationship_filter( filter_t type, std::vector relationships) : filter_visitor{type} - , relationships_{std::move(relationships)} + , relationships_{relationships} { } @@ -174,7 +174,7 @@ std::optional relationship_filter::match( scope_filter::scope_filter(filter_t type, std::vector scopes) : filter_visitor{type} - , scopes_{std::move(scopes)} + , scopes_{scopes} { } @@ -263,9 +263,9 @@ void diagram_filter::init_filters(const config::diagram &c) exclusive_.emplace_back(std::make_unique( filter_t::kExclusive, c.exclude().elements)); exclusive_.emplace_back(std::make_unique( - filter_t::kExclusive, c.include().relationships)); + filter_t::kExclusive, c.exclude().relationships)); exclusive_.emplace_back(std::make_unique( - filter_t::kExclusive, c.include().scopes)); + filter_t::kExclusive, c.exclude().scopes)); exclusive_.emplace_back(std::make_unique( filter_t::kExclusive, c.exclude().subclasses)); exclusive_.emplace_back(std::make_unique( diff --git a/src/common/model/diagram_filter.h b/src/common/model/diagram_filter.h index 64045315..8e97314f 100644 --- a/src/common/model/diagram_filter.h +++ b/src/common/model/diagram_filter.h @@ -137,8 +137,8 @@ public: bool exc = std::any_of( exclusive_.begin(), exclusive_.end(), [this, &e](const auto &ex) { auto m = ex->match(diagram_, e); - // Return a match if a filter is undefined for specific element - // or it's a match + // Return true if a filter is defined for specific element + // and it's a match return m.has_value() && m.value(); }); if (exc) @@ -147,7 +147,7 @@ public: bool inc = std::all_of( inclusive_.begin(), inclusive_.end(), [this, &e](const auto &in) { auto m = in->match(diagram_, e); - // Return a match if a filter is undefined for specific element + // Return true if a filter is undefined for specific element // or it's a match return !m.has_value() || m.value(); }); diff --git a/src/config/config.cc b/src/config/config.cc index 184113d9..45c7d9f8 100644 --- a/src/config/config.cc +++ b/src/config/config.cc @@ -250,6 +250,53 @@ template <> struct convert { } }; +// +// config relationship_t decoder +// +template <> struct convert { + static bool decode(const Node &node, relationship_t &rhs) + { + assert(node.Type() == NodeType::Scalar); + + auto relationship_name = node.as(); + if (relationship_name == "extension" || + relationship_name == "inheritance") { + rhs = relationship_t::kExtension; + } + else if (relationship_name == "composition") { + rhs = relationship_t::kComposition; + } + else if (relationship_name == "aggregation") { + rhs = relationship_t::kAggregation; + } + else if (relationship_name == "containment") { + rhs = relationship_t::kContainment; + } + else if (relationship_name == "ownership") { + rhs = relationship_t::kOwnership; + } + else if (relationship_name == "association") { + rhs = relationship_t::kAssociation; + } + else if (relationship_name == "instantiation") { + rhs = relationship_t::kInstantiation; + } + else if (relationship_name == "friendship") { + rhs = relationship_t::kFriendship; + } + else if (relationship_name == "dependency") { + rhs = relationship_t::kDependency; + } + else if (relationship_name == "none") { + rhs = relationship_t::kNone; + } + else + return false; + + return true; + } +}; + template <> struct convert> { static bool decode(const Node &node, std::vector &rhs) { @@ -465,53 +512,6 @@ template <> struct convert { } }; -// -// config relationship_t decoder -// -template <> struct convert { - static bool decode(const Node &node, relationship_t &rhs) - { - assert(node.Type() == NodeType::Scalar); - - auto relationship_name = node.as(); - if (relationship_name == "extension" || - relationship_name == "inheritance") { - rhs = relationship_t::kExtension; - } - else if (relationship_name == "composition") { - rhs = relationship_t::kComposition; - } - else if (relationship_name == "aggregation") { - rhs = relationship_t::kAggregation; - } - else if (relationship_name == "containment") { - rhs = relationship_t::kContainment; - } - else if (relationship_name == "ownership") { - rhs = relationship_t::kOwnership; - } - else if (relationship_name == "association") { - rhs = relationship_t::kAssociation; - } - else if (relationship_name == "instantiation") { - rhs = relationship_t::kInstantiation; - } - else if (relationship_name == "friendship") { - rhs = relationship_t::kFriendship; - } - else if (relationship_name == "dependency") { - rhs = relationship_t::kDependency; - } - else if (relationship_name == "none") { - rhs = relationship_t::kNone; - } - else - return false; - - return true; - } -}; - // // config Yaml decoder // diff --git a/tests/t00040/.clang-uml b/tests/t00040/.clang-uml new file mode 100644 index 00000000..5e7a1213 --- /dev/null +++ b/tests/t00040/.clang-uml @@ -0,0 +1,21 @@ +compilation_database_dir: .. +output_directory: puml +diagrams: + t00040_class: + type: class + generate_packages: false + glob: + - ../../tests/t00040/t00040.cc + using_namespace: + - clanguml::t00040 + include: + namespaces: + - clanguml::t00040 + scopes: + - public + - protected + exclude: + relationships: + - dependency + elements: + - clanguml::t00040::B \ No newline at end of file diff --git a/tests/t00040/t00040.cc b/tests/t00040/t00040.cc new file mode 100644 index 00000000..21b76dd2 --- /dev/null +++ b/tests/t00040/t00040.cc @@ -0,0 +1,36 @@ +namespace clanguml::t00040 { + +struct B { +}; + +struct A { +public: + int get_a() { return hidden_a_; } + +protected: + int ii_; + +private: + void foo() { } + + int hidden_a_; +}; + +class AA : public A { +public: +}; + +class AAA : public AA { +public: + int get_aaa() { return hidden_aaa_; } + B *b; + +private: + int hidden_aaa_; +}; + +struct R { + void foo(A *a) { } +}; + +} // namespace clanguml::t00040 diff --git a/tests/t00040/test_case.h b/tests/t00040/test_case.h new file mode 100644 index 00000000..1019a385 --- /dev/null +++ b/tests/t00040/test_case.h @@ -0,0 +1,50 @@ +/** + * tests/t00040/test_case.cc + * + * Copyright (c) 2021-2022 Bartek Kryza + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +TEST_CASE("t00040", "[test-case][class]") +{ + auto [config, db] = load_config("t00040"); + + auto diagram = config.diagrams["t00040_class"]; + + REQUIRE(diagram->name == "t00040_class"); + REQUIRE(diagram->generate_packages() == false); + + auto model = generate_class_diagram(db, diagram); + + REQUIRE(model->name() == "t00040_class"); + + auto puml = generate_class_puml(diagram, *model); + AliasMatcher _A(puml); + + REQUIRE_THAT(puml, StartsWith("@startuml")); + REQUIRE_THAT(puml, EndsWith("@enduml\n")); + + REQUIRE_THAT(puml, IsClass(_A("A"))); + REQUIRE_THAT(puml, IsClass(_A("AA"))); + REQUIRE_THAT(puml, IsClass(_A("AAA"))); + REQUIRE_THAT(puml, IsBaseClass(_A("A"), _A("AA"))); + REQUIRE_THAT(puml, IsBaseClass(_A("AA"), _A("AAA"))); + + REQUIRE_THAT(puml, !IsClass(_A("B"))); + + REQUIRE_THAT(puml, !IsDependency(_A("R"), _A("A"))); + + save_puml( + "./" + config.output_directory() + "/" + diagram->name + ".puml", puml); +} diff --git a/tests/test_cases.cc b/tests/test_cases.cc index 727ecbc0..4c6a175e 100644 --- a/tests/test_cases.cc +++ b/tests/test_cases.cc @@ -198,6 +198,7 @@ using namespace clanguml::test::matchers; #include "t00037/test_case.h" #include "t00038/test_case.h" #include "t00039/test_case.h" +#include "t00040/test_case.h" // // Sequence diagram tests diff --git a/tests/test_cases.h b/tests/test_cases.h index 7d6e20fd..c7d07c69 100644 --- a/tests/test_cases.h +++ b/tests/test_cases.h @@ -245,7 +245,7 @@ ContainsMatcher IsInnerClass(std::string const &parent, } ContainsMatcher IsAssociation(std::string const &from, std::string const &to, - std::string const &label, std::string multiplicity_source = "", + std::string const &label = "", std::string multiplicity_source = "", std::string multiplicity_dest = "", CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes) { diff --git a/tests/test_cases.yaml b/tests/test_cases.yaml index eaaa6069..cef80515 100644 --- a/tests/test_cases.yaml +++ b/tests/test_cases.yaml @@ -114,6 +114,9 @@ test_cases: - name: t00039 title: Subclass class diagram filter test description: + - name: t00040 + title: Relationship and scope filter test + description: Sequence diagrams: - name: t20001 title: Basic sequence diagram test case