Improved handling of message call comments (#264)

This commit is contained in:
Bartek Kryza
2024-04-30 00:06:42 +02:00
parent 6c6575bc7f
commit 4293a6cc79
11 changed files with 267 additions and 41 deletions

14
tests/t20048/.clang-uml Normal file
View File

@@ -0,0 +1,14 @@
add_compile_flags:
- -fparse-all-comments
diagrams:
t20048_sequence:
type: sequence
glob:
- t20048.cc
generate_message_comments: true
include:
namespaces:
- clanguml::t20048
using_namespace: clanguml::t20048
from:
- function: "clanguml::t20048::tmain()"

40
tests/t20048/t20048.cc Normal file
View File

@@ -0,0 +1,40 @@
#include <future>
namespace clanguml {
namespace t20048 {
int a1(int x) { return x + 1; }
int a2(int x) { return x + 2; }
int a3(int x) { return x + 3; }
int a4(int x) { return x + 4; }
int a5(int x) { return x + 5; }
int a6(int x) { return x + 6; }
int a7(int x) { return x + 7; }
int tmain()
{
// a1() adds `1` to the result of a2()
auto res = a1(a2(a3(0)));
// This lambda calls a4() which adds `4` to it's argument
res = [](auto &&x) { return a4(x); }(0);
// a5() adds `1` to the result of a6()
res = a5(
// a6() adds `1` to its argument
a6(0));
// a7() is called via add std::async
// \uml{call clanguml::t20048::a7(int)}
res = std::async(a7, 10).get();
return 0;
}
}
}

91
tests/t20048/test_case.h Normal file
View File

@@ -0,0 +1,91 @@
/**
* tests/t20048/test_case.h
*
* Copyright (c) 2021-2024 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("t20048", "[test-case][sequence]")
{
auto [config, db] = load_config("t20048");
auto diagram = config.diagrams["t20048_sequence"];
REQUIRE(diagram->name == "t20048_sequence");
auto model = generate_sequence_diagram(*db, diagram);
REQUIRE(model->name() == "t20048_sequence");
{
auto src = generate_sequence_puml(diagram, *model);
AliasMatcher _A(src);
REQUIRE_THAT(src, StartsWith("@startuml"));
REQUIRE_THAT(src, EndsWith("@enduml\n"));
// Check if all calls exist
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("a3(int)"), ""));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("a2(int)"), ""));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("a1(int)"), ""));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("a5(int)"), ""));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("a6(int)"), ""));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("a7(int)"), ""));
REQUIRE_THAT(src,
HasCall(_A("tmain()"), _A("tmain()::(lambda t20048.cc:26:11)"),
"operator()()"));
REQUIRE_THAT(src,
HasCall(
_A("tmain()::(lambda t20048.cc:26:11)"), _A("a4(int)"), ""));
REQUIRE_THAT(src,
HasMessageComment(_A("tmain()"),
"a1\\(\\) adds `1` to the result\\n"
"of a2\\(\\)"));
REQUIRE_THAT(src,
HasMessageComment(_A("tmain()"),
"This lambda calls a4\\(\\) which\\n"
"adds `4` to it's argument"));
REQUIRE_THAT(src,
HasMessageComment(
_A("tmain()"), "a6\\(\\) adds `1` to its argument"));
REQUIRE_THAT(src,
HasMessageComment(_A("tmain()"),
"a5\\(\\) adds `1` to the result\\n"
"of a6\\(\\)"));
REQUIRE_THAT(src,
HasMessageComment(
_A("tmain()"), "a7\\(\\) is called via add std::async"));
save_puml(config.output_directory(), diagram->name + ".puml", src);
}
{
auto j = generate_sequence_json(diagram, *model);
using namespace json;
save_json(config.output_directory(), diagram->name + ".json", j);
}
{
auto src = generate_sequence_mermaid(diagram, *model);
mermaid::AliasMatcher _A(src);
using mermaid::IsClass;
save_mermaid(config.output_directory(), diagram->name + ".mmd", src);
}
}

View File

@@ -474,6 +474,7 @@ using namespace clanguml::test::matchers;
#include "t20045/test_case.h"
#include "t20046/test_case.h"
#include "t20047/test_case.h"
#include "t20048/test_case.h"
///
/// Package diagram tests

View File

@@ -20,6 +20,7 @@
#include "decorators/decorators.h"
#include "catch.h"
#include "util/util.h"
TEST_CASE("Test decorator parser on regular comment", "[unit-test]")
{
@@ -35,9 +36,10 @@ TEST_CASE("Test decorator parser on regular comment", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment);
auto [decorators, stripped] = parse(comment);
CHECK(decorators.empty());
CHECK(clanguml::util::trim(comment) == stripped);
}
TEST_CASE("Test decorator parser on note", "[unit-test]")
@@ -62,7 +64,7 @@ TEST_CASE("Test decorator parser on note", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment);
auto [decorators, stripped] = parse(comment);
CHECK(decorators.size() == 4);
@@ -86,6 +88,15 @@ TEST_CASE("Test decorator parser on note", "[unit-test]")
CHECK(n4);
CHECK(n4->position == "left");
CHECK(n4->text == "This is a comment");
CHECK(stripped == R"(\brief This is a comment.
This is a longer comment.
\
\param a int an int
\param b float a float)");
}
TEST_CASE("Test decorator parser on note with custom tag", "[unit-test]")
@@ -110,7 +121,7 @@ TEST_CASE("Test decorator parser on note with custom tag", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment, "clanguml");
auto [decorators, stripped] = parse(comment, "clanguml");
CHECK(decorators.size() == 4);
@@ -134,6 +145,15 @@ TEST_CASE("Test decorator parser on note with custom tag", "[unit-test]")
CHECK(n4);
CHECK(n4->position == "left");
CHECK(n4->text == "This is a comment");
CHECK(stripped == R"(\brief This is a comment.
This is a longer comment.
\
\param a int an int
\param b float a float)");
}
TEST_CASE("Test decorator parser on style", "[unit-test]")
@@ -144,7 +164,7 @@ TEST_CASE("Test decorator parser on style", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment);
auto [decorators, stripped] = parse(comment);
CHECK(decorators.size() == 1);
@@ -152,6 +172,7 @@ TEST_CASE("Test decorator parser on style", "[unit-test]")
CHECK(n1);
CHECK(n1->spec == "#green,dashed,thickness=4");
CHECK(stripped.empty());
}
TEST_CASE("Test decorator parser on aggregation", "[unit-test]")
@@ -162,7 +183,7 @@ TEST_CASE("Test decorator parser on aggregation", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment);
auto [decorators, stripped] = parse(comment);
CHECK(decorators.size() == 1);
@@ -170,6 +191,7 @@ TEST_CASE("Test decorator parser on aggregation", "[unit-test]")
CHECK(n1);
CHECK(n1->multiplicity == "0..1:0..*");
CHECK(stripped.empty());
}
TEST_CASE("Test decorator parser on skip", "[unit-test]")
@@ -180,13 +202,14 @@ TEST_CASE("Test decorator parser on skip", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment);
auto [decorators, stripped] = parse(comment);
CHECK(decorators.size() == 1);
auto n1 = std::dynamic_pointer_cast<skip>(decorators.at(0));
CHECK(n1);
CHECK(stripped.empty());
}
TEST_CASE("Test decorator parser on skiprelationship", "[unit-test]")
@@ -197,13 +220,14 @@ TEST_CASE("Test decorator parser on skiprelationship", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment);
auto [decorators, stripped] = parse(comment);
CHECK(decorators.size() == 1);
auto n1 = std::dynamic_pointer_cast<skip_relationship>(decorators.at(0));
CHECK(n1);
CHECK(stripped.empty());
}
TEST_CASE("Test decorator parser on diagram scope", "[unit-test]")
@@ -215,7 +239,7 @@ TEST_CASE("Test decorator parser on diagram scope", "[unit-test]")
using namespace clanguml::decorators;
auto decorators = parse(comment);
auto [decorators, stripped] = parse(comment);
CHECK(decorators.size() == 1);
@@ -232,4 +256,21 @@ TEST_CASE("Test decorator parser on diagram scope", "[unit-test]")
CHECK(n1->applies_to_diagram("diagram2"));
CHECK(!n1->applies_to_diagram("diagram4"));
CHECK(stripped.empty());
}
TEST_CASE("Test invalid comment - unterminated curly brace", "[unit-test]")
{
std::string comment = R"(
Test test test
\uml{call clanguml::test:aa()
)";
using namespace clanguml::decorators;
auto [decorators, stripped] = parse(comment);
CHECK(decorators.size() == 0);
CHECK(stripped.empty());
}