From c033879b016d6811fcbdec6d3cf9407c4fc2e640 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Sun, 10 Apr 2022 22:42:57 +0200 Subject: [PATCH] Added cyclic dependency include diagram test case --- .../visitor/translation_unit_visitor.cc | 21 +++++--- tests/t40002/.clang-uml | 19 +++++++ tests/t40002/include/lib1/lib1.h | 13 +++++ tests/t40002/include/lib2/lib2.h | 13 +++++ tests/t40002/src/lib1/lib1.cc | 11 ++++ tests/t40002/src/lib2/lib2.cc | 11 ++++ tests/t40002/src/t40002.cc | 8 +++ tests/t40002/test_case.h | 53 +++++++++++++++++++ tests/test_cases.cc | 1 + tests/test_cases.yaml | 3 ++ 10 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 tests/t40002/.clang-uml create mode 100644 tests/t40002/include/lib1/lib1.h create mode 100644 tests/t40002/include/lib2/lib2.h create mode 100644 tests/t40002/src/lib1/lib1.cc create mode 100644 tests/t40002/src/lib2/lib2.cc create mode 100644 tests/t40002/src/t40002.cc create mode 100644 tests/t40002/test_case.h diff --git a/src/include_diagram/visitor/translation_unit_visitor.cc b/src/include_diagram/visitor/translation_unit_visitor.cc index f91cdd28..4037900b 100644 --- a/src/include_diagram/visitor/translation_unit_visitor.cc +++ b/src/include_diagram/visitor/translation_unit_visitor.cc @@ -130,6 +130,15 @@ void translation_unit_visitor::process_file(const std::string &file, } } + auto diagram_file_path = f->path() | f->name(); + + if (!ctx.diagram().get_element(diagram_file_path).has_value()) { + ctx.diagram().add_file(std::move(f)); + } + + auto &diagram_source_file = + ctx.diagram().get_element(diagram_file_path).value(); + if (!register_as_current) { auto relationship_type = common::model::relationship_t::kAssociation; @@ -138,17 +147,15 @@ void translation_unit_visitor::process_file(const std::string &file, relationship_type = common::model::relationship_t::kDependency; ctx.get_current_file().value().add_relationship( - common::model::relationship{relationship_type, f->alias()}); + common::model::relationship{ + relationship_type, diagram_source_file.alias()}); auto fp = std::filesystem::absolute(file).lexically_normal(); - f->set_file(fp.string()); - f->set_line(0); + diagram_source_file.set_file(fp.string()); + diagram_source_file.set_line(0); } - if (!ctx.diagram().get_element(f->path() | f->name()).has_value()) { - ctx.set_current_file(type_safe::opt_ref(*f)); - ctx.diagram().add_file(std::move(f)); - } + ctx.set_current_file(type_safe::opt_ref(diagram_source_file)); } } } diff --git a/tests/t40002/.clang-uml b/tests/t40002/.clang-uml new file mode 100644 index 00000000..accff299 --- /dev/null +++ b/tests/t40002/.clang-uml @@ -0,0 +1,19 @@ +compilation_database_dir: .. +output_directory: puml +diagrams: + t40002_include: + type: include + # Provide the files to parse in order to look + # for #include directives + glob: + - ../../tests/t40002/**/*.cc + - ../../tests/t40002/**/*.h + # Render the paths relative to this directory + relative_to: ../../tests/t40002 + include: + # Include only files belonging to these paths + paths: + - ../../tests/t40002 + plantuml: + before: + - "' t40002 test include diagram" \ No newline at end of file diff --git a/tests/t40002/include/lib1/lib1.h b/tests/t40002/include/lib1/lib1.h new file mode 100644 index 00000000..250e3389 --- /dev/null +++ b/tests/t40002/include/lib1/lib1.h @@ -0,0 +1,13 @@ +#pragma once + +#include "../lib2/lib2.h" + +namespace clanguml::t40002::lib1 { + +int foo0(); + +int foo1(); + +int foo(); + +} \ No newline at end of file diff --git a/tests/t40002/include/lib2/lib2.h b/tests/t40002/include/lib2/lib2.h new file mode 100644 index 00000000..dc7b5dba --- /dev/null +++ b/tests/t40002/include/lib2/lib2.h @@ -0,0 +1,13 @@ +#pragma once + +#include "../lib1/lib1.h" + +namespace clanguml::t40002::lib2 { + +int foo2(); + +int foo3(); + +int foo(); + +} \ No newline at end of file diff --git a/tests/t40002/src/lib1/lib1.cc b/tests/t40002/src/lib1/lib1.cc new file mode 100644 index 00000000..de88c7b5 --- /dev/null +++ b/tests/t40002/src/lib1/lib1.cc @@ -0,0 +1,11 @@ +#include "../../include/lib1/lib1.h" + +namespace clanguml::t40002::lib1 { + +int foo0() { return 0; } + +int foo1() { return 1; } + +int foo() { return foo1(); } + +} \ No newline at end of file diff --git a/tests/t40002/src/lib2/lib2.cc b/tests/t40002/src/lib2/lib2.cc new file mode 100644 index 00000000..a4d06231 --- /dev/null +++ b/tests/t40002/src/lib2/lib2.cc @@ -0,0 +1,11 @@ +#include "../../include/lib2/lib2.h" + +namespace clanguml::t40002::lib2 { + +int foo0() { return 0; } + +int foo1() { return 1; } + +int foo() { return foo1(); } + +} \ No newline at end of file diff --git a/tests/t40002/src/t40002.cc b/tests/t40002/src/t40002.cc new file mode 100644 index 00000000..7ea55fc8 --- /dev/null +++ b/tests/t40002/src/t40002.cc @@ -0,0 +1,8 @@ +#include "../include/lib1/lib1.h" +#include "../include/lib2/lib2.h" + +namespace clanguml::t40002 { + +int foo() { return lib1::foo() + lib2::foo(); } + +} \ No newline at end of file diff --git a/tests/t40002/test_case.h b/tests/t40002/test_case.h new file mode 100644 index 00000000..ef752157 --- /dev/null +++ b/tests/t40002/test_case.h @@ -0,0 +1,53 @@ +/** + * tests/t40002/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("t40002", "[test-case][package]") +{ + auto [config, db] = load_config("t40002"); + + auto diagram = config.diagrams["t40002_include"]; + + REQUIRE(diagram->name == "t40002_include"); + + auto model = generate_include_diagram(db, diagram); + + REQUIRE(model->name() == "t40002_include"); + + auto puml = generate_include_puml(diagram, *model); + + AliasMatcher _A(puml); + + REQUIRE_THAT(puml, StartsWith("@startuml")); + REQUIRE_THAT(puml, EndsWith("@enduml\n")); + + REQUIRE_THAT(puml, IsFolder("lib1")); + REQUIRE_THAT(puml, IsFolder("lib2")); + REQUIRE_THAT(puml, IsFile("lib1.h")); + REQUIRE_THAT(puml, IsFile("lib2.h")); + REQUIRE_THAT(puml, IsFile("t40002.cc")); + REQUIRE_THAT(puml, IsFile("lib1.cc")); + REQUIRE_THAT(puml, IsFile("lib2.cc")); + + REQUIRE_THAT(puml, IsAssociation(_A("t40002.cc"), _A("lib1.h"))); + REQUIRE_THAT(puml, IsAssociation(_A("lib1.h"), _A("lib2.h"))); + REQUIRE_THAT(puml, IsAssociation(_A("lib1.cc"), _A("lib1.h"))); + REQUIRE_THAT(puml, IsAssociation(_A("lib2.cc"), _A("lib2.h"))); + + save_puml( + "./" + config.output_directory() + "/" + diagram->name + ".puml", puml); +} diff --git a/tests/test_cases.cc b/tests/test_cases.cc index 422c806b..2a88894e 100644 --- a/tests/test_cases.cc +++ b/tests/test_cases.cc @@ -256,6 +256,7 @@ using namespace clanguml::test::matchers; // Include diagram tests // #include "t40001/test_case.h" +#include "t40002/test_case.h" // // Other tests (e.g. configuration file) diff --git a/tests/test_cases.yaml b/tests/test_cases.yaml index 5713d322..08962477 100644 --- a/tests/test_cases.yaml +++ b/tests/test_cases.yaml @@ -153,6 +153,9 @@ test_cases: - name: t40001 title: Basic include graph diagram test case description: + - name: t40002 + title: Cyclic include graph diagram test case + description: Configuration diagrams: - name: t90000 title: Basic config test