Added initial support for package diagrams

This commit is contained in:
Bartek Kryza
2022-01-22 21:01:33 +01:00
parent d6061dddff
commit 20759f412d
25 changed files with 1486 additions and 7 deletions

20
tests/t30001/.clang-uml Normal file
View File

@@ -0,0 +1,20 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t30001_package:
type: package
glob:
- ../../tests/t30001/t30001.cc
include:
namespaces:
- clanguml::t30001
exclude:
namespaces:
- clanguml::t30001::detail
using_namespace:
- clanguml::t30001
plantuml:
before:
- "' t30001 test package diagram"
after:
- "note left of @A(A::AA::AAA): A AAA note..."

25
tests/t30001/t30001.cc Normal file
View File

@@ -0,0 +1,25 @@
namespace clanguml {
namespace t30001 {
namespace A {
namespace AA {
namespace AAA {
} // namespace AAA
namespace BBB {
} // namespace BBB
} // namespace AA
namespace BB {
} // namespace BB
} // namespace A
namespace B {
namespace AA {
namespace AAA {
} // namespace AAA
namespace BBB {
} // namespace BBB
} // namespace AA
namespace BB {
} // namespace BB
} // namespace B
}
}

55
tests/t30001/test_case.h Normal file
View File

@@ -0,0 +1,55 @@
/**
* tests/t30001/test_case.cc
*
* Copyright (c) 2021-2022 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("t30001", "[test-case][package]")
{
auto [config, db] = load_config("t30001");
auto diagram = config.diagrams["t30001_package"];
REQUIRE(diagram->include.namespaces.size() == 1);
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t30001"}));
REQUIRE(diagram->exclude.namespaces.size() == 1);
REQUIRE_THAT(diagram->exclude.namespaces,
VectorContains(std::string{"clanguml::t30001::detail"}));
REQUIRE(diagram->should_include("clanguml::t30001::A"));
REQUIRE(!diagram->should_include("clanguml::t30001::detail::C"));
REQUIRE(!diagram->should_include("std::vector"));
REQUIRE(diagram->name == "t30001_package");
auto model = generate_package_diagram(db, diagram);
REQUIRE(model.name() == "t30001_package");
auto puml = generate_package_puml(diagram, model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, Contains("component [A]"));
REQUIRE_THAT(puml, Contains("component [AA]"));
REQUIRE_THAT(puml, Contains("component [AAA]"));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -64,6 +64,18 @@ clanguml::class_diagram::model::diagram generate_class_diagram(
return diagram_model;
}
clanguml::package_diagram::model::diagram generate_package_diagram(
cppast::libclang_compilation_database &db,
std::shared_ptr<clanguml::config::diagram> diagram)
{
auto diagram_model =
clanguml::package_diagram::generators::plantuml::generate(db,
diagram->name,
dynamic_cast<clanguml::config::package_diagram &>(*diagram));
return diagram_model;
}
std::string generate_sequence_puml(
std::shared_ptr<clanguml::config::diagram> config,
clanguml::sequence_diagram::model::diagram &model)
@@ -92,6 +104,20 @@ std::string generate_class_puml(
return ss.str();
}
std::string generate_package_puml(
std::shared_ptr<clanguml::config::diagram> config,
clanguml::package_diagram::model::diagram &model)
{
using namespace clanguml::package_diagram::generators::plantuml;
std::stringstream ss;
ss << generator(
dynamic_cast<clanguml::config::package_diagram &>(*config), model);
return ss.str();
}
void save_puml(const std::string &path, const std::string &puml)
{
std::filesystem::path p{path};
@@ -147,6 +173,11 @@ using namespace clanguml::test::matchers;
#include "t20001/test_case.h"
#include "t20002/test_case.h"
//
// Package diagram tests
//
#include "t30001/test_case.h"
//
// Other tests (e.g. configuration file)
//

View File

@@ -24,6 +24,8 @@
#include "class_diagram/visitor/translation_unit_visitor.h"
#include "config/config.h"
#include "cx/compilation_database.h"
#include "package_diagram/generators/plantuml/package_diagram_generator.h"
#include "package_diagram/visitor/translation_unit_visitor.h"
#include "sequence_diagram/generators/plantuml/sequence_diagram_generator.h"
#include "sequence_diagram/visitor/translation_unit_visitor.h"
#include "util/util.h"
@@ -32,6 +34,7 @@
#include "catch.h"
#include <algorithm>
#include <complex>
#include <filesystem>
#include <string>
@@ -168,6 +171,8 @@ struct AliasMatcher {
patterns.push_back("class \"" + name + "\" as ");
patterns.push_back("abstract \"" + name + "\" as ");
patterns.push_back("enum \"" + name + "\" as ");
patterns.push_back("component \"" + name + "\" as ");
patterns.push_back("component [" + name + "] as ");
for (const auto &line : puml) {
for (const auto &pattern : patterns) {
@@ -396,6 +401,13 @@ ContainsMatcher IsField(std::string const &name,
return ContainsMatcher(
CasedString(pattern + " : " + type, caseSensitivity));
}
ContainsMatcher IsPackage(std::string const &str,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(
CasedString("component [" + str + "]", caseSensitivity));
}
}
}
}