Added support for C++20 module based packages in class diagrams (#101)

This commit is contained in:
Bartek Kryza
2023-12-18 21:55:18 +01:00
parent ea6892f754
commit c51ae5b6ee
24 changed files with 296 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ file(GLOB_RECURSE TEST_CONFIG_YMLS test_config_data/*.yml
test_compilation_database_data/*.json)
set(TEST_CASES_REQUIRING_CXX20 t00056 t00058 t00059 t00065 t00069)
set(TEST_CASES_REQUIRING_CXX20_MODULES t00070)
set(TEST_CASES_REQUIRING_CXX20_MODULES t00070 t00071)
if(ENABLE_CXX_MODULES_TEST_CASES)
foreach(CXX20_MOD_TC ${TEST_CASES_REQUIRING_CXX20_MODULES})

View File

@@ -38,7 +38,7 @@ TEST_CASE("t00065", "[test-case][class]")
// Check if all classes exist
REQUIRE_THAT(src, IsClass(_A("R")));
REQUIRE_THAT(src, IsClass(_A("A")));
REQUIRE_THAT(src, IsClass(_A("AImpl")));
REQUIRE_THAT(src, IsClass(_A("detail::AImpl")));
REQUIRE_THAT(src, IsEnum(_A("XYZ")));
REQUIRE_THAT(src, IsEnum(_A("ABC")));

12
tests/t00071/.clang-uml Normal file
View File

@@ -0,0 +1,12 @@
diagrams:
t00071_class:
type: class
glob:
- t00071.cc
include:
namespaces:
- clanguml::t00071
generate_packages: true
package_type: module
using_namespace: clanguml::t00071
using_module: t00071

View File

@@ -0,0 +1,13 @@
export module t00071.app.lib1;
export namespace clanguml::t00071 {
class B { };
template <typename T> class BB {
T t;
};
namespace detail {
enum class BBB { bbb1, bbb2 };
} // namespace detail
}

View File

@@ -0,0 +1,5 @@
export module t00071.app.lib1.mod1;
export namespace clanguml::t00071 {
class D { };
}

View File

@@ -0,0 +1,5 @@
export module t00071.app.lib1.mod2;
export namespace clanguml::t00071 {
class E { };
}

View File

@@ -0,0 +1,13 @@
export module t00071.app.lib2;
export namespace clanguml::t00071 {
class C { };
template <typename T> class CC {
T t;
};
namespace detail {
enum class CCC { ccc1, ccc2 };
}
}

View File

@@ -0,0 +1,11 @@
export module t00071.app;
export import t00071.app.lib1;
export import t00071.app.lib2;
export namespace clanguml::t00071 {
class A {
int get() { return a; }
int a;
};
}

15
tests/t00071/t00071.cc Normal file
View File

@@ -0,0 +1,15 @@
import t00071.app;
import t00071.app.lib1;
import t00071.app.lib1.mod1;
import t00071.app.lib1.mod2;
import t00071.app.lib2;
namespace clanguml {
namespace t00071 {
class R {
A *a;
B *b;
C *c;
};
}
}

70
tests/t00071/test_case.h Normal file
View File

@@ -0,0 +1,70 @@
/**
* tests/t00071/test_case.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* 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("t00071", "[test-case][class]")
{
auto [config, db] = load_config("t00071");
auto diagram = config.diagrams["t00071_class"];
REQUIRE(diagram->name == "t00071_class");
auto model = generate_class_diagram(*db, diagram);
REQUIRE(model->name() == "t00071_class");
{
auto src = generate_class_puml(diagram, *model);
AliasMatcher _A(src);
REQUIRE_THAT(src, StartsWith("@startuml"));
REQUIRE_THAT(src, EndsWith("@enduml\n"));
REQUIRE_THAT(src, IsClass(_A("A")));
REQUIRE_THAT(src, IsClass(_A("R")));
REQUIRE_THAT(src, IsEnum(_A("detail::BBB")));
REQUIRE_THAT(src, IsEnum(_A("detail::CCC")));
save_puml(config.output_directory(), diagram->name + ".puml", src);
}
{
auto j = generate_class_json(diagram, *model);
using namespace json;
save_json(config.output_directory(), diagram->name + ".json", j);
}
{
auto src = generate_class_mermaid(diagram, *model);
mermaid::AliasMatcher _A(src);
using mermaid::IsClass;
using mermaid::IsEnum;
REQUIRE_THAT(src, IsClass(_A("A")));
REQUIRE_THAT(src, IsClass(_A("R")));
REQUIRE_THAT(src, IsEnum(_A("detail::BBB")));
REQUIRE_THAT(src, IsEnum(_A("detail::CCC")));
save_mermaid(config.output_directory(), diagram->name + ".mmd", src);
}
}

View File

@@ -409,6 +409,7 @@ using namespace clanguml::test::matchers;
#endif
#if defined(ENABLE_CXX_MODULES_TEST_CASES)
#include "t00070/test_case.h"
#include "t00071/test_case.h"
#endif
///

View File

@@ -204,6 +204,12 @@ test_cases:
- name: t00069
title: Coroutine methods in class diagrams
description:
- name: t00070
title: Diagram filter based on C++20 modules
description:
- name: t00071
title: Class diagram with C++20 modules generated as packages
description:
Sequence diagrams:
- name: t20001
title: Basic sequence diagram test case

View File

@@ -377,6 +377,31 @@ TEST_CASE("Test config relative paths handling", "[unit-test]")
"{}/test_config_data", std::filesystem::current_path().string()));
}
TEST_CASE("Test using_module relative to", "[unit-test]")
{
auto cfg = clanguml::config::load("./test_config_data/using_module.yml");
CHECK(cfg.diagrams.size() == 2);
auto &def = *cfg.diagrams["class1"];
CHECK(def.make_module_relative(std::make_optional<std::string>(
"mod1.mod2.mod3")) == std::vector{std::string{"mod3"}});
CHECK(def.make_module_relative(std::make_optional<std::string>(
"mod1.mod2")) == std::vector<std::string>{});
CHECK(def.make_module_relative(
std::make_optional<std::string>("modA.modB.modC")) ==
std::vector{
std::string{"modA"}, std::string{"modB"}, std::string{"modC"}});
def = *cfg.diagrams["class2"];
CHECK(def.make_module_relative(
std::make_optional<std::string>("mod1.mod2.mod3")) ==
std::vector{std::string{"mod2"}, std::string{"mod3"}});
CHECK(def.make_module_relative(
std::make_optional<std::string>("modA.modB.modC")) ==
std::vector{
std::string{"modA"}, std::string{"modB"}, std::string{"modC"}});
}
TEST_CASE("Test config full clang uml dump", "[unit-test]")
{
auto cfg =

View File

@@ -0,0 +1,12 @@
diagrams:
class1:
type: class
glob:
- test.cc
using_module: mod1.mod2
class2:
type: class
relative_to: .
glob:
- test.cc
using_module: mod1