From ed6bcf1c7161effae19c2f2506214a63bb101ec1 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Sun, 2 May 2021 22:47:57 +0200 Subject: [PATCH] Added test for unnamed enums --- src/uml/class_diagram_visitor.cc | 58 ++++++++++++++++++-------------- tests/t00016/.clanguml | 12 +++++++ tests/t00016/t00016.cc | 30 +++++++++++++++++ tests/t00016/test_case.h | 48 ++++++++++++++++++++++++++ tests/test_cases.cc | 1 + tests/test_cases.yaml | 3 ++ 6 files changed, 127 insertions(+), 25 deletions(-) create mode 100644 tests/t00016/.clanguml create mode 100644 tests/t00016/t00016.cc create mode 100644 tests/t00016/test_case.h diff --git a/src/uml/class_diagram_visitor.cc b/src/uml/class_diagram_visitor.cc index c15e6b51..f3aed6b0 100644 --- a/src/uml/class_diagram_visitor.cc +++ b/src/uml/class_diagram_visitor.cc @@ -296,30 +296,38 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls) if (cppast::is_templated(cls)) { LOG_DBG("Processing class template parameters..."); auto scope = cppast::cpp_scope_name(type_safe::ref(cls)); - for (const auto &tp : scope.template_parameters()) { - if (tp.kind() == - cppast::cpp_entity_kind::template_type_parameter_t) { - LOG_DBG("Processing template type parameter {}", tp.name()); - process_template_type_parameter( - static_cast( - tp), - c); - } - else if (tp.kind() == - cppast::cpp_entity_kind::non_type_template_parameter_t) { - LOG_DBG("Processing template nontype parameter {}", tp.name()); - process_template_nontype_parameter( - static_cast< - const cppast::cpp_non_type_template_parameter &>(tp), - c); - } - else if (tp.kind() == - cppast::cpp_entity_kind::template_template_parameter_t) { - LOG_DBG("Processing template template parameter {}", tp.name()); - process_template_template_parameter( - static_cast< - const cppast::cpp_template_template_parameter &>(tp), - c); + // Even if this is a template the scope.is_templated() returns + // false when the template parameter list is empty + if (scope.is_templated()) { + for (const auto &tp : scope.template_parameters()) { + if (tp.kind() == + cppast::cpp_entity_kind::template_type_parameter_t) { + LOG_DBG("Processing template type parameter {}", tp.name()); + process_template_type_parameter( + static_cast< + const cppast::cpp_template_type_parameter &>(tp), + c); + } + else if (tp.kind() == + cppast::cpp_entity_kind::non_type_template_parameter_t) { + LOG_DBG( + "Processing template nontype parameter {}", tp.name()); + process_template_nontype_parameter( + static_cast< + const cppast::cpp_non_type_template_parameter &>( + tp), + c); + } + else if (tp.kind() == + cppast::cpp_entity_kind::template_template_parameter_t) { + LOG_DBG( + "Processing template template parameter {}", tp.name()); + process_template_template_parameter( + static_cast< + const cppast::cpp_template_template_parameter &>( + tp), + c); + } } } } @@ -756,7 +764,7 @@ void tu_visitor::process_friend(const cppast::cpp_friend &f, class_ &parent) r.destination = name; } else if (f.entity()) { - std::string name{}; + std::string name {}; if (f.entity().value().kind() == cppast::cpp_entity_kind::class_template_t) { diff --git a/tests/t00016/.clanguml b/tests/t00016/.clanguml new file mode 100644 index 00000000..31881559 --- /dev/null +++ b/tests/t00016/.clanguml @@ -0,0 +1,12 @@ +compilation_database_dir: .. +output_directory: puml +diagrams: + t00016_class: + type: class + glob: + - ../../tests/t00016/t00016.cc + using_namespace: + - clanguml::t00016 + include: + namespaces: + - clanguml::t00016 diff --git a/tests/t00016/t00016.cc b/tests/t00016/t00016.cc new file mode 100644 index 00000000..5c8c0886 --- /dev/null +++ b/tests/t00016/t00016.cc @@ -0,0 +1,30 @@ +namespace clanguml { +namespace t00016 { + +template +struct is_numeric { + enum { value = false }; +}; + +template <> +struct is_numeric { + enum { value = true }; +}; + +template <> +struct is_numeric { + enum { value = true }; +}; + +template <> +struct is_numeric { + enum { value = true }; +}; + +template <> +struct is_numeric { + enum { value = false }; +}; + +} +} diff --git a/tests/t00016/test_case.h b/tests/t00016/test_case.h new file mode 100644 index 00000000..5f50d76b --- /dev/null +++ b/tests/t00016/test_case.h @@ -0,0 +1,48 @@ +/** + * tests/t00016/test_case.cc + * + * Copyright (c) 2021 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("t00016", "[test-case][class]") +{ + auto [config, db] = load_config("t00016"); + + auto diagram = config.diagrams["t00016_class"]; + + REQUIRE(diagram->name == "t00016_class"); + + REQUIRE(diagram->include.namespaces.size() == 1); + REQUIRE_THAT(diagram->include.namespaces, + VectorContains(std::string{"clanguml::t00016"})); + + REQUIRE(diagram->exclude.namespaces.size() == 0); + + REQUIRE(diagram->should_include("clanguml::t00016::is_numeric")); + + auto model = generate_class_diagram(db, diagram); + + REQUIRE(model.name == "t00016_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("is_numeric"))); + + save_puml( + "./" + config.output_directory + "/" + diagram->name + ".puml", puml); +} diff --git a/tests/test_cases.cc b/tests/test_cases.cc index 865e72c3..247dd161 100644 --- a/tests/test_cases.cc +++ b/tests/test_cases.cc @@ -119,6 +119,7 @@ using namespace clanguml::test::matchers; #include "t00013/test_case.h" #include "t00014/test_case.h" #include "t00015/test_case.h" +#include "t00016/test_case.h" // // Sequence diagram tests diff --git a/tests/test_cases.yaml b/tests/test_cases.yaml index 33e70e19..12a11e65 100644 --- a/tests/test_cases.yaml +++ b/tests/test_cases.yaml @@ -42,6 +42,9 @@ test_cases: - name: t00015 title: Namespace fun description: + - name: t00016 + title: Unnamed enums and empty templates + description: Sequence diagrams: - name: t20001 title: Basic sequence diagram