From fd7d916e3eb9871e7d152bc3775c11d71d1e80f8 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Tue, 8 Feb 2022 22:19:38 +0100 Subject: [PATCH] Added layout hints config option to class diagrams --- src/config/config.cc | 38 +++++++++++++++++++++++++++++++ src/config/config.h | 10 ++++++++ tests/test_config.cc | 24 +++++++++++++++++++ tests/test_config_data/layout.yml | 22 ++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 tests/test_config_data/layout.yml diff --git a/src/config/config.cc b/src/config/config.cc index 72a8b2f9..fbf459d6 100644 --- a/src/config/config.cc +++ b/src/config/config.cc @@ -204,6 +204,8 @@ using clanguml::common::model::scope_t; using clanguml::config::class_diagram; using clanguml::config::config; using clanguml::config::filter; +using clanguml::config::hint_t; +using clanguml::config::layout_hint; using clanguml::config::method_arguments; using clanguml::config::package_diagram; using clanguml::config::plantuml; @@ -375,6 +377,7 @@ template <> struct convert { return false; get_option(node, rhs.classes); + get_option(node, rhs.layout); get_option(node, rhs.include_relations_also_as_members); get_option(node, rhs.generate_method_arguments); @@ -410,6 +413,41 @@ template <> struct convert { } }; +// +// layout_hint Yaml decoder +// +template <> struct convert { + static bool decode(const Node &node, layout_hint &rhs) + { + assert(node.Type() == NodeType::Map); + + if (node["up"]) { + rhs.hint = hint_t::up; + rhs.entity = node["up"].as(); + } + else if (node["down"]) { + rhs.hint = hint_t::down; + rhs.entity = node["down"].as(); + } + else if (node["left"]) { + rhs.hint = hint_t::left; + rhs.entity = node["left"].as(); + } + else if (node["right"]) { + rhs.hint = hint_t::right; + rhs.entity = node["right"].as(); + } + else if (node["hidden"]) { + rhs.hint = hint_t::hidden; + rhs.entity = node["hidden"].as(); + } + else + return false; + + return true; + } +}; + // // config Yaml decoder // diff --git a/src/config/config.h b/src/config/config.h index cb72d318..b4bf92ed 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -65,6 +65,15 @@ struct filter { std::vector scopes; }; +enum class hint_t { up, down, left, right, hidden }; + +struct layout_hint { + hint_t hint; + std::string entity; +}; + +using layout_hints = std::map>; + std::string to_string(const diagram_type t); struct inheritable_diagram_options { @@ -109,6 +118,7 @@ struct class_diagram : public diagram { diagram_type type() const override; option> classes{"classes"}; + option layout{"layout"}; bool has_class(std::string clazz); }; diff --git a/tests/test_config.cc b/tests/test_config.cc index 1a5aa603..fd693444 100644 --- a/tests/test_config.cc +++ b/tests/test_config.cc @@ -77,4 +77,28 @@ TEST_CASE("Test config includes", "[unit-test]") CHECK(cus.include_relations_also_as_members()); CHECK(cus.generate_method_arguments() == clanguml::config::method_arguments::none); +} + +TEST_CASE("Test config layout", "[unit-test]") +{ + auto cfg = clanguml::config::load("./test_config_data/layout.yml"); + + CHECK(cfg.diagrams.size() == 1); + auto &def = static_cast( + *cfg.diagrams["class_main"]); + CHECK(def.type() == clanguml::config::diagram_type::class_diagram); + + CHECK(def.layout().at("ABCD").size() == 2); + CHECK(def.layout().at("ABCD")[0].hint == clanguml::config::hint_t::up); + CHECK(def.layout().at("ABCD")[0].entity == "ABCD_SUBCLASS"); + CHECK(def.layout().at("ABCD")[1].hint == clanguml::config::hint_t::left); + CHECK(def.layout().at("ABCD")[1].entity == "ABCD_SIBLING"); + + CHECK(def.layout().at("ABCD_SIBLING").size() == 2); + CHECK(def.layout().at("ABCD_SIBLING")[0].hint == + clanguml::config::hint_t::right); + CHECK(def.layout().at("ABCD_SIBLING")[0].entity == "ABCD_OTHER_SIBLING"); + CHECK(def.layout().at("ABCD_SIBLING")[1].hint == + clanguml::config::hint_t::down); + CHECK(def.layout().at("ABCD_SIBLING")[1].entity == "ABCD_SIBLING_SIBLING"); } \ No newline at end of file diff --git a/tests/test_config_data/layout.yml b/tests/test_config_data/layout.yml new file mode 100644 index 00000000..d30f264d --- /dev/null +++ b/tests/test_config_data/layout.yml @@ -0,0 +1,22 @@ +compilation_database_dir: debug +output_directory: output +diagrams: + class_main: + type: class + glob: + - src/**/*.cc + - src/**/*.h + using_namespace: + - clanguml + generate_method_arguments: full + layout: + ABCD: + - up: ABCD_SUBCLASS + - left: ABCD_SIBLING + ABCD_SIBLING: + - right: ABCD_OTHER_SIBLING + - down: ABCD_SIBLING_SIBLING + include: + namespaces: + - clanguml + - ABCD \ No newline at end of file