Added note decorator test case

This commit is contained in:
Bartek Kryza
2021-07-29 19:19:24 +02:00
parent 75cb5765f8
commit 4ee1aee0e7
11 changed files with 189 additions and 7 deletions

View File

@@ -289,8 +289,9 @@ public:
for (auto decorator : c.decorators) {
auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
if (note) {
ostr << "note " << note->position << " of " << c.alias()
<< " : " << note->text << '\n';
ostr << "note " << note->position << " of " << c.alias() << '\n'
<< note->text << '\n'
<< "end note\n";
}
}
@@ -339,6 +340,7 @@ public:
relstr << " : " << r.label;
relstr << '\n';
ostr << relstr.str();
}
catch (error::uml_alias_missing &ex) {
@@ -347,6 +349,18 @@ public:
to_string(r.type), e.name, destination, ex.what());
}
}
//
// Process notes
//
for (auto decorator : e.decorators) {
auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
if (note) {
ostr << "note " << note->position << " of " << e.alias() << '\n'
<< note->text << '\n'
<< "end note\n";
}
}
}
void generate(std::ostream &ostr) const

View File

@@ -193,6 +193,10 @@ void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
enum_ e;
e.name = cx::util::full_name(ctx.namespace_, enm);
// Process enum documentation comment
if (enm.comment().has_value())
e.decorators = decorators::parse(enm.comment().value());
for (const auto &ev : enm) {
if (ev.kind() == cppast::cpp_entity_kind::enum_value_t) {
e.constants.push_back(ev.name());
@@ -230,8 +234,15 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
cppast::cpp_access_specifier_kind::cpp_private;
// Process class documentation comment
if(cls.comment().has_value())
c.decorators = decorators::parse(cls.comment().value());
if (cppast::is_templated(cls)) {
if (cls.parent().value().comment().has_value())
c.decorators =
decorators::parse(cls.parent().value().comment().value());
}
else {
if (cls.comment().has_value())
c.decorators = decorators::parse(cls.comment().value());
}
// Process class child entities
if (c.is_struct)

View File

@@ -68,7 +68,7 @@ std::shared_ptr<decorator> note::from_string(std::string_view c)
std::advance(it, note_position.size() + 1);
}
else if (*it == ' ') {
else if (std::isspace(*it)) {
std::advance(it, 1);
}
else {

View File

@@ -74,5 +74,5 @@ foreach(TEST_CASE_CONFIG ${TEST_CASE_CONFIGS})
endforeach()
add_test(NAME test_util COMMAND test_util)
add_test(NAME test_command_parser COMMAND test_command_parser)
add_test(NAME test_decorator_parser COMMAND test_decorator_parser)
add_test(NAME test_cases COMMAND test_cases)

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

@@ -0,0 +1,12 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00028_class:
type: class
glob:
- ../../tests/t00028/t00028.cc
using_namespace:
- clanguml::t00028
include:
namespaces:
- clanguml::t00028

54
tests/t00028/t00028.cc Normal file
View File

@@ -0,0 +1,54 @@
#include <memory>
#include <vector>
namespace clanguml {
namespace t00028 {
/// \clanguml{note[top] A class note.}
class A {
};
/// \clanguml{note[] B class note.}
class B {
};
///
/// @clanguml{note[bottom] C class note.}
/// This is class C.
class C {
};
/// \clanguml{note
/// D
/// class
/// note.}
class D {
};
/// \clanguml{note E template class note.}
template<typename T> class E {
T param;
};
/// @clanguml{note[ bottom ] F enum note.}
enum class F {
one,
two,
three
};
/// \clanguml{note[right] R class note.}
class R {
A aaa;
B *bbb;
C &ccc;
std::vector<std::shared_ptr<D>> ddd;
E<int> eee;
};
} // namespace t00028
} // namespace clanguml

66
tests/t00028/test_case.h Normal file
View File

@@ -0,0 +1,66 @@
/**
* tests/t00028/test_case.cc
*
* Copyright (c) 2021 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("t00028", "[test-case][class]")
{
auto [config, db] = load_config("t00028");
auto diagram = config.diagrams["t00028_class"];
REQUIRE(diagram->name == "t00028_class");
REQUIRE(diagram->include.namespaces.size() == 1);
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t00028"}));
REQUIRE(diagram->exclude.namespaces.size() == 0);
REQUIRE(diagram->should_include("clanguml::t00028::A"));
auto model = generate_class_diagram(db, diagram);
REQUIRE(model.name == "t00028_class");
auto puml = generate_class_puml(diagram, model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClass(_A("A")));
REQUIRE_THAT(puml, IsClass(_A("B")));
REQUIRE_THAT(puml, IsClass(_A("C")));
REQUIRE_THAT(puml, IsClass(_A("D")));
REQUIRE_THAT(puml, IsClassTemplate("E", "T"));
REQUIRE_THAT(puml, IsEnum(_A("F")));
REQUIRE_THAT(puml, IsClass(_A("R")));
REQUIRE_THAT(puml, HasNote(_A("A"), "top", "A class note."));
REQUIRE_THAT(puml, HasNote(_A("B"), "left", "B class note."));
REQUIRE_THAT(puml, HasNote(_A("C"), "bottom", "C class note."));
const auto d_note = R"(
D
class
note.)";
REQUIRE_THAT(puml, HasNote(_A("D"), "left", d_note));
REQUIRE_THAT(puml, HasNote(_A("E<T>"), "left", "E template class note."));
REQUIRE_THAT(puml, HasNote(_A("F"), "bottom", "F enum note."));
REQUIRE_THAT(puml, HasNote(_A("R"), "right", "R class note."));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -131,6 +131,7 @@ using namespace clanguml::test::matchers;
#include "t00025/test_case.h"
#include "t00026/test_case.h"
#include "t00027/test_case.h"
#include "t00028/test_case.h"
//
// Sequence diagram tests

View File

@@ -262,6 +262,15 @@ ContainsMatcher IsDependency(std::string const &from, std::string const &to,
CasedString(fmt::format("{} ..> {}", from, to), caseSensitivity));
}
ContainsMatcher HasNote(std::string const &cls, std::string const &position,
std::string const &note,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(
CasedString(fmt::format("note {} of {}", position, cls),
caseSensitivity));
}
template <typename... Ts>
ContainsMatcher IsMethod(std::string const &name,
std::string const &type = "void",

View File

@@ -78,6 +78,9 @@ test_cases:
- name: t00027
title: Template decorator pattern
description:
- name: t00028
title: PlantUML note decorator test case
description:
Sequence diagrams:
- name: t20001
title: Basic sequence diagram

View File

@@ -56,16 +56,20 @@ TEST_CASE("Test decorator parser on note", "[unit-test]")
@clanguml{note[ top ]
This is a comment }
\clanguml{note This is a comment}
\clanguml{note[] This is a comment}
)";
using namespace clanguml::decorators;
auto decorators = parse(comment);
CHECK(decorators.size() == 2);
CHECK(decorators.size() == 4);
auto n1 = std::dynamic_pointer_cast<note>(decorators.at(0));
auto n2 = std::dynamic_pointer_cast<note>(decorators.at(1));
auto n3 = std::dynamic_pointer_cast<note>(decorators.at(2));
auto n4 = std::dynamic_pointer_cast<note>(decorators.at(3));
CHECK(n1);
CHECK(n1->position == "left");
@@ -74,6 +78,14 @@ TEST_CASE("Test decorator parser on note", "[unit-test]")
CHECK(n2);
CHECK(n2->position == "top");
CHECK(n2->text == "This is a comment");
CHECK(n3);
CHECK(n3->position == "left");
CHECK(n3->text == "This is a comment");
CHECK(n4);
CHECK(n4->position == "left");
CHECK(n4->text == "This is a comment");
}
TEST_CASE("Test decorator parser on style", "[unit-test]")