Added package diagram test case with C++20 module partitions

This commit is contained in:
Bartek Kryza
2023-12-23 19:23:40 +01:00
parent bf7b69bcca
commit 453f265feb
14 changed files with 191 additions and 10 deletions

View File

@@ -301,7 +301,8 @@ tvl::value_t modules_filter::match(
if (!e.module().has_value()) if (!e.module().has_value())
return {false}; return {false};
auto module_toks = util::split(e.module().value(), ".", true); // NOLINT auto module_toks =
path::split(e.module().value(), path_type::kModule); // NOLINT
if (dynamic_cast<const package *>(&e) != nullptr && if (dynamic_cast<const package *>(&e) != nullptr &&
e.get_namespace().type() == path_type::kModule) { e.get_namespace().type() == path_type::kModule) {
@@ -312,7 +313,8 @@ tvl::value_t modules_filter::match(
[&e, &module_toks](const auto &modit) { [&e, &module_toks](const auto &modit) {
if (std::holds_alternative<std::string>(modit.value())) { if (std::holds_alternative<std::string>(modit.value())) {
const auto &modit_str = std::get<std::string>(modit.value()); const auto &modit_str = std::get<std::string>(modit.value());
const auto modit_toks = util::split(modit_str, ".", true); const auto modit_toks =
path::split(modit_str, path_type::kModule);
return e.module() == modit_str || return e.module() == modit_str ||
util::starts_with(module_toks, modit_toks); util::starts_with(module_toks, modit_toks);

View File

@@ -39,8 +39,6 @@ class element : public diagram_element {
public: public:
element(namespace_ using_namespace, path_type pt = path_type::kNamespace); element(namespace_ using_namespace, path_type pt = path_type::kNamespace);
element(path_type pt);
~element() override = default; ~element() override = default;
/** /**

View File

@@ -50,9 +50,9 @@ class path {
* *
* @return Path separator * @return Path separator
*/ */
const char *separator() const static const char *separator(path_type pt)
{ {
switch (path_type_) { switch (pt) {
case path_type::kNamespace: case path_type::kNamespace:
return "::"; return "::";
case path_type::kModule: case path_type::kModule:
@@ -68,9 +68,38 @@ class path {
return "::"; return "::";
} }
/**
* Returns the path separator based on the type of the instance path.
*
* @return Path separator
*/
const char *separator() const { return separator(path_type_); }
public: public:
using container_type = std::vector<std::string>; using container_type = std::vector<std::string>;
static container_type split(
const std::string &ns, path_type pt = path_type::kNamespace)
{
container_type result;
if (pt == path_type::kModule) {
auto path_toks = util::split(ns, separator(pt));
for (const auto &pt : path_toks) {
const auto subtoks = util::split(pt, ":");
if (subtoks.size() == 2) {
result.push_back(subtoks.at(0));
result.push_back(fmt::format(":{}", subtoks.at(1)));
}
else
result.push_back(subtoks.at(0));
}
}
else
result = util::split(ns, separator(pt));
return result;
}
path(path_type pt = path_type::kNamespace) path(path_type pt = path_type::kNamespace)
: path_type_{pt} : path_type_{pt}
{ {
@@ -82,7 +111,7 @@ public:
if (ns.empty()) if (ns.empty())
return; return;
path_ = util::split(ns, separator()); path_ = split(ns, pt);
} }
virtual ~path() = default; virtual ~path() = default;
@@ -163,7 +192,14 @@ public:
*/ */
std::string to_string() const std::string to_string() const
{ {
return fmt::format("{}", fmt::join(path_, std::string{separator()})); auto result =
fmt::format("{}", fmt::join(path_, std::string{separator()}));
if (path_type_ == path_type::kModule) {
util::replace_all(result, ".:", ":");
}
return result;
} }
/** /**

View File

@@ -277,10 +277,14 @@ std::vector<std::string> diagram::make_module_relative(
if (!maybe_module) if (!maybe_module)
return {}; return {};
auto module_path = util::split(maybe_module.value(), "."); auto module_path = common::model::path(
maybe_module.value(), common::model::path_type::kModule)
.tokens();
if (using_module.has_value) { if (using_module.has_value) {
auto using_module_path = util::split(using_module(), "."); auto using_module_path = common::model::path(
using_module(), common::model::path_type::kModule)
.tokens();
if (util::starts_with(module_path, using_module_path)) { if (util::starts_with(module_path, using_module_path)) {
util::remove_prefix(module_path, using_module_path); util::remove_prefix(module_path, using_module_path);

13
tests/t30014/.clang-uml Normal file
View File

@@ -0,0 +1,13 @@
diagrams:
t30014_package:
type: package
glob:
- t30014.cc
package_type: module
include:
modules:
- t30014
exclude:
modules:
- t30014.app:lib1.mod2
using_module: t30014

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,13 @@
export module t30014.app;
import :lib1;
import :lib1.mod1;
import :lib1.mod2;
import :lib2;
export namespace clanguml::t30014 {
class A {
int get() { return a; }
int a;
};
} // namespace clanguml::t30014

6
tests/t30014/t30014.cc Normal file
View File

@@ -0,0 +1,6 @@
import t30014.app;
namespace clanguml {
namespace t30014 {
}
}

69
tests/t30014/test_case.h Normal file
View File

@@ -0,0 +1,69 @@
/**
* tests/t30014/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("t30014", "[test-case][package]")
{
auto [config, db] = load_config("t30014");
auto diagram = config.diagrams["t30014_package"];
REQUIRE(diagram->name == "t30014_package");
auto model = generate_package_diagram(*db, diagram);
REQUIRE(model->name() == "t30014_package");
{
auto src = generate_package_puml(diagram, *model);
AliasMatcher _A(src);
REQUIRE_THAT(src, StartsWith("@startuml"));
REQUIRE_THAT(src, EndsWith("@enduml\n"));
// Check if all packages exist
REQUIRE_THAT(src, IsPackage("app"));
REQUIRE_THAT(src, IsPackage(":lib1"));
REQUIRE_THAT(src, IsPackage(":lib2"));
REQUIRE_THAT(src, IsPackage("mod1"));
REQUIRE_THAT(src, !IsPackage("mod2"));
save_puml(config.output_directory(), diagram->name + ".puml", src);
}
{
auto j = generate_package_json(diagram, *model);
using namespace json;
save_json(config.output_directory(), diagram->name + ".json", j);
}
{
auto src = generate_package_mermaid(diagram, *model);
mermaid::AliasMatcher _A(src);
using mermaid::IsPackage;
REQUIRE_THAT(src, IsPackage(_A("app")));
REQUIRE_THAT(src, IsPackage(_A(":lib1")));
REQUIRE_THAT(src, IsPackage(_A(":lib2")));
REQUIRE_THAT(src, IsPackage(_A("mod1")));
save_mermaid(config.output_directory(), diagram->name + ".mmd", src);
}
}

View File

@@ -475,6 +475,7 @@ using namespace clanguml::test::matchers;
#if defined(ENABLE_CXX_MODULES_TEST_CASES) #if defined(ENABLE_CXX_MODULES_TEST_CASES)
#include "t30012/test_case.h" #include "t30012/test_case.h"
#include "t30013/test_case.h" #include "t30013/test_case.h"
#include "t30014/test_case.h"
#endif #endif
/// ///
/// Include diagram tests /// Include diagram tests

View File

@@ -365,6 +365,9 @@ test_cases:
- name: t30013 - name: t30013
title: C++20 modules package dependencies diagram test title: C++20 modules package dependencies diagram test
description: description:
- name: t30014
title: C++20 modules package diagram test with partitions
description:
Include diagrams: Include diagrams:
- name: t40001 - name: t40001
title: Basic include graph diagram test case title: Basic include graph diagram test case