Skip to content

Commit

Permalink
Added test case for relationships to std types
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Jun 27, 2024
1 parent f47d119 commit 4c87c01
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/class_diagram/generators/json/class_diagram_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ void generator::generate(const class_ &c, nlohmann::json &parent) const
object["display_name"] =
common::generators::json::render_name(c.full_name_no_ns());

object["display_name"] =
config().simplify_template_type(object["display_name"]);

for (auto &tp : object["template_parameters"]) {
if (tp.contains("type") && tp.at("type").is_string()) {
tp["type"] = config().using_namespace().relative(tp.at("type"));
Expand Down
15 changes: 8 additions & 7 deletions src/class_diagram/generators/mermaid/class_diagram_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace clanguml::class_diagram::generators::mermaid {

using clanguml::common::eid_t;
using clanguml::common::generators::mermaid::escape_name;
using clanguml::common::generators::mermaid::indent;
using clanguml::common::generators::mermaid::render_name;

Expand All @@ -50,8 +51,8 @@ void generator::generate_alias(

auto class_label = config().simplify_template_type(render_name(full_name));

ostr << indent(1) << "class " << c.alias() << "[\"" << class_label
<< "\"]\n";
ostr << indent(1) << "class " << c.alias() << "[\""
<< escape_name(class_label) << "\"]\n";

// Register the added alias
m_generated_aliases.emplace(c.alias());
Expand Down Expand Up @@ -257,7 +258,7 @@ void generator::generate_method(
ostr << fmt::format("[{}] ", fmt::join(method_mods, ","));
}

ostr << render_name(type);
ostr << escape_name(render_name(type));

if (m.is_pure_virtual())
ostr << "*";
Expand All @@ -276,8 +277,8 @@ void generator::generate_member(

ostr << indent(2) << mermaid_common::to_mermaid(m.access()) << m.name()
<< " : "
<< render_name(
uns.relative(config().simplify_template_type(m.type())));
<< escape_name(uns.relative(
config().simplify_template_type(render_name(m.type()))));
}

void generator::generate(const concept_ &c, std::ostream &ostr) const
Expand All @@ -294,15 +295,15 @@ void generator::generate(const concept_ &c, std::ostream &ostr) const
parameters.reserve(c.requires_parameters().size());
for (const auto &p : c.requires_parameters()) {
parameters.emplace_back(
render_name(p.to_string(config().using_namespace())));
escape_name(p.to_string(config().using_namespace())));
}

ostr << indent(2)
<< fmt::format("\"({})\"\n", fmt::join(parameters, ","));

for (const auto &req : c.requires_statements()) {
ostr << indent(2)
<< fmt::format("\"{}\"\n", render_name(req, false));
<< fmt::format("\"{}\"\n", escape_name(req, false));
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/common/generators/mermaid/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,21 @@ std::string indent(const unsigned level)
return std::string(level * kIndentWidth, ' '); // NOLINT
}

std::string render_name(std::string name, bool round_brackets)
std::string render_name(std::string name)
{
util::replace_all(name, "##", "::");

return name;
}

std::string escape_name(std::string name, bool round_brackets)
{
util::replace_all(name, "<", "&lt;");
util::replace_all(name, ">", "&gt;");
if (round_brackets) {
util::replace_all(name, "(", "&lpar;");
util::replace_all(name, ")", "&rpar;");
}
util::replace_all(name, "##", "::");
util::replace_all(name, "{", "&lbrace;");
util::replace_all(name, "}", "&rbrace;");

Expand Down
3 changes: 2 additions & 1 deletion src/common/generators/mermaid/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ std::string to_mermaid(message_t r);

std::string indent(unsigned level);

std::string render_name(std::string name, bool round_brackets = true);
std::string render_name(std::string name);
std::string escape_name(std::string name, bool round_brackets = true);

/**
* @brief Base class for diagram generators
Expand Down
25 changes: 25 additions & 0 deletions tests/t00081/.clang-uml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
diagrams:
t00081_class:
type: class
glob:
- t00081.cc
filter_mode: advanced
include_system_headers: true
include:
allof:
namespaces:
- clanguml::t00081
- std
context:
- match:
radius: 2
pattern: clanguml::t00081::A
exclude:
anyof:
access:
- private
- public
- protected
relationships:
- dependency
using_namespace: clanguml::t00081
18 changes: 18 additions & 0 deletions tests/t00081/t00081.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <map>
#include <string>
#include <vector>

namespace clanguml {
namespace t00081_detail {
struct C { };
} // namespace t00081_detail
namespace t00081 {
struct A {
std::vector<std::string> as;
std::string s;
std::map<std::string, std::string> ms;

t00081_detail::C *c;
};
} // namespace t00081
} // namespace clanguml
35 changes: 35 additions & 0 deletions tests/t00081/test_case.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* tests/t00081/test_case.h
*
* Copyright (c) 2021-2024 Bartek Kryza <[email protected]>
*
* 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("t00081")
{
using namespace clanguml::test;
using namespace std::string_literals;

auto [config, db, diagram, model] =
CHECK_CLASS_MODEL("t00081", "t00081_class");

CHECK_CLASS_DIAGRAM(*config, diagram, *model, [](const auto &src) {
REQUIRE(IsClass(src, "A"));
REQUIRE(!IsClass(src, "C"));

REQUIRE(IsClass(src, "std::string"));
REQUIRE(IsClass(src, "std::vector<std::string>"));
REQUIRE(IsClass(src, "std::map<std::string,std::string>"));
});
}
1 change: 1 addition & 0 deletions tests/test_cases.cc
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ void CHECK_INCLUDE_DIAGRAM(const clanguml::config::config &config,
#include "t00078/test_case.h"
#include "t00079/test_case.h"
#include "t00080/test_case.h"
#include "t00081/test_case.h"

///
/// Sequence diagram tests
Expand Down
3 changes: 3 additions & 0 deletions tests/test_cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ test_cases:
- name: t00080
title: Test case for including elements from system headers
description:
- name: t00081
title: Test case for class members relationships to std types
description:
Sequence diagrams:
- name: t20001
title: Basic sequence diagram test case
Expand Down

0 comments on commit 4c87c01

Please sign in to comment.