Added option inline_lambda_messages to omit lambda expressions from sequence diagrams (#261)

This commit is contained in:
Bartek Kryza
2024-05-03 14:42:15 +02:00
parent 052d9b7ff3
commit 14a13b45aa
18 changed files with 498 additions and 42 deletions

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

@@ -0,0 +1,12 @@
diagrams:
t20052_sequence:
type: sequence
glob:
- t20052.cc
include:
namespaces:
- clanguml::t20052
using_namespace: clanguml::t20052
inline_lambda_messages: true
from:
- function: "clanguml::t20052::tmain()"

105
tests/t20052/t20052.cc Normal file
View File

@@ -0,0 +1,105 @@
#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
namespace clanguml {
namespace t20052 {
struct A {
void a() { aa(); }
void aa() { aaa(); }
void aaa() { }
};
struct B {
void b() { bb(); }
void bb() { bbb(); }
void bbb() { }
void eb() { }
};
struct C {
void c() { cc(); }
void cc() { ccc(); }
void ccc() { }
};
struct D {
int add5(int arg) const { return arg + 5; }
};
class E {
std::optional<std::shared_ptr<B>> maybe_b;
std::shared_ptr<A> a;
public:
template <typename F> void setup(F &&f) { f(maybe_b); }
};
template <typename F> struct R {
R(F &&f)
: f_{std::move(f)}
{
}
void r() { f_(); }
F f_;
};
void tmain()
{
A a;
B b;
C c;
// The activity shouldn't be marked at the lambda definition, but
// wherever it is actually called...
auto alambda = [&a, &b]() {
a.a();
b.b();
};
// ...like here
alambda();
// There should be no call to B in the sequence diagram as the blambda
// is never called
[[maybe_unused]] auto blambda = [&b]() { b.b(); };
// Nested lambdas should also work
auto clambda = [alambda, &c]() {
c.c();
alambda();
};
clambda();
R r{[&c]() { c.c(); }};
r.r();
D d;
std::vector<int> ints{0, 1, 2, 3, 4};
std::transform(ints.begin(), ints.end(), ints.begin(),
[&d](auto i) { return d.add5(i); });
// TODO: Fix naming function call arguments which are lambdas
// E e;
//
// e.setup([](auto &&arg) mutable {
// // We cannot know here what 'arg' might be
// arg.value()->eb();
// });
}
}
}

176
tests/t20052/test_case.h Normal file
View File

@@ -0,0 +1,176 @@
/**
* tests/t20052/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("t20052", "[test-case][sequence]")
{
auto [config, db] = load_config("t20052");
auto diagram = config.diagrams["t20052_sequence"];
REQUIRE(diagram->name == "t20052_sequence");
auto model = generate_sequence_diagram(*db, diagram);
REQUIRE(model->name() == "t20052_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("tmain()::(lambda t20052.cc:67:20)"),
"operator()() const"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:67:20)"), _A("A"), "a()"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("A"), "a()"));
REQUIRE_THAT(src, HasCall(_A("A"), _A("A"), "aa()"));
REQUIRE_THAT(src, HasCall(_A("A"), _A("A"), "aaa()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:67:20)"), _A("B"), "b()"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("B"), "b()"));
REQUIRE_THAT(src, HasCall(_A("B"), _A("B"), "bb()"));
REQUIRE_THAT(src, HasCall(_A("B"), _A("B"), "bbb()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:80:20)"), _A("C"), "c()"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("C"), "c()"));
REQUIRE_THAT(src, HasCall(_A("C"), _A("C"), "cc()"));
REQUIRE_THAT(src, HasCall(_A("C"), _A("C"), "ccc()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:80:20)"),
_A("tmain()::(lambda t20052.cc:67:20)"), "operator()() const"));
REQUIRE_THAT(src, HasCall(_A("C"), _A("C"), "ccc()"));
REQUIRE_THAT(src,
HasCall(_A("tmain()"), _A("R<(lambda at t20052.cc:86:9)>"),
"R((lambda at t20052.cc:86:9) &&)"));
REQUIRE_THAT(src,
HasCall(_A("tmain()"), _A("R<(lambda at t20052.cc:86:9)>"), "r()"));
REQUIRE_THAT(src,
!HasCall(_A("R<(lambda at t20052.cc:86:9)>"),
_A("tmain()::(lambda t20052.cc:86:9)"), "operator()() const"));
REQUIRE_THAT(
src, HasCall(_A("R<(lambda at t20052.cc:86:9)>"), _A("C"), "c()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()"), _A("tmain()::(lambda t20052.cc:94:9)"),
"operator()(auto) const"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:94:9)"), _A("D"),
"add5(int) const"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("D"), "add5(int) const"));
save_puml(config.output_directory(), diagram->name + ".puml", src);
}
{
auto j = generate_sequence_json(diagram, *model);
using namespace json;
std::vector<int> messages = {
FindMessage(j, "tmain()", "A", "a()"),
FindMessage(j, "A", "A", "aa()"),
FindMessage(j, "A", "A", "aaa()"),
FindMessage(j, "tmain()", "B", "b()"),
FindMessage(j, "B", "B", "bb()"),
FindMessage(j, "B", "B", "bbb()"),
FindMessage(j, "tmain()", "C", "c()"),
FindMessage(j, "C", "C", "cc()"),
FindMessage(j, "C", "C", "ccc()"),
FindMessage(j, "tmain()", "R<(lambda at t20052.cc:86:9)>", "r()"),
FindMessage(j, "R<(lambda at t20052.cc:86:9)>", "C", "c()"),
};
REQUIRE(std::is_sorted(messages.begin(), messages.end()));
save_json(config.output_directory(), diagram->name + ".json", j);
}
{
auto src = generate_sequence_mermaid(diagram, *model);
mermaid::SequenceDiagramAliasMatcher _A(src);
using mermaid::HasCall;
REQUIRE_THAT(src,
!HasCall(_A("tmain()"), _A("tmain()::(lambda t20052.cc:67:20)"),
"operator()() const"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:67:20)"), _A("A"), "a()"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("A"), "a()"));
REQUIRE_THAT(src, HasCall(_A("A"), _A("A"), "aa()"));
REQUIRE_THAT(src, HasCall(_A("A"), _A("A"), "aaa()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:67:20)"), _A("B"), "b()"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("B"), "b()"));
REQUIRE_THAT(src, HasCall(_A("B"), _A("B"), "bb()"));
REQUIRE_THAT(src, HasCall(_A("B"), _A("B"), "bbb()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:80:20)"), _A("C"), "c()"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("C"), "c()"));
REQUIRE_THAT(src, HasCall(_A("C"), _A("C"), "cc()"));
REQUIRE_THAT(src, HasCall(_A("C"), _A("C"), "ccc()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:80:20)"),
_A("tmain()::(lambda t20052.cc:67:20)"), "operator()() const"));
REQUIRE_THAT(src, HasCall(_A("C"), _A("C"), "ccc()"));
REQUIRE_THAT(src,
HasCall(_A("tmain()"), _A("R<(lambda at t20052.cc:86:9)>"),
"R((lambda at t20052.cc:86:9) &&)"));
REQUIRE_THAT(src,
HasCall(_A("tmain()"), _A("R<(lambda at t20052.cc:86:9)>"), "r()"));
REQUIRE_THAT(src,
!HasCall(_A("R<(lambda at t20052.cc:86:9)>"),
_A("tmain()::(lambda t20052.cc:86:9)"), "operator()() const"));
REQUIRE_THAT(
src, HasCall(_A("R<(lambda at t20052.cc:86:9)>"), _A("C"), "c()"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()"), _A("tmain()::(lambda t20052.cc:94:9)"),
"operator()(auto) const"));
REQUIRE_THAT(src,
!HasCall(_A("tmain()::(lambda t20052.cc:94:9)"), _A("D"),
"add5(int) const"));
REQUIRE_THAT(src, HasCall(_A("tmain()"), _A("D"), "add5(int) const"));
save_mermaid(config.output_directory(), diagram->name + ".mmd", src);
}
}

View File

@@ -480,6 +480,7 @@ using namespace clanguml::test::matchers;
#include "t20050/test_case.h"
#include "t20051/test_case.h"
#endif
#include "t20052/test_case.h"
///
/// Package diagram tests

View File

@@ -376,6 +376,9 @@ test_cases:
- name: t20051
title: Test case for CUDA calls callee_type filter
description:
- name: t20052
title: Test case for CUDA calls callee_type filter
description:
Package diagrams:
- name: t30001
title: Basic package diagram test case