Added test case for C++20 concepts with variadic params

This commit is contained in:
Bartek Kryza
2023-03-01 18:37:31 +01:00
parent 7996cb7a00
commit be2ad5dc9b
9 changed files with 146 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
# CHANGELOG # CHANGELOG
* Added initial support for C++20 concept rendering (#96)
* Added support for plain C11 translation units (#97) * Added support for plain C11 translation units (#97)
* Added 'row' and 'column' layout hints for aligning elements (#90) * Added 'row' and 'column' layout hints for aligning elements (#90)
* Added 'together' layout hint for grouping elements (#43) * Added 'together' layout hint for grouping elements (#43)

View File

@@ -29,6 +29,7 @@ Main features supported so far include:
* Optional package generation from namespaces * Optional package generation from namespaces
* Interactive links to online code to classes, methods and class fields in SVG diagrams * Interactive links to online code to classes, methods and class fields in SVG diagrams
* Support for plain C99/C11 code (struct and units relationships) * Support for plain C99/C11 code (struct and units relationships)
* C++20 concept constraints
* **Sequence diagram generation** * **Sequence diagram generation**
* Generation of sequence diagram from specific method or function * Generation of sequence diagram from specific method or function
* Generation of loop and conditional statements * Generation of loop and conditional statements

View File

@@ -3,7 +3,7 @@ file(GLOB_RECURSE TEST_CASE_SOURCES t*/*.cc t*/*.c t*/src/*.c)
file(GLOB_RECURSE TEST_CASE_CONFIGS t*/.clang-uml) file(GLOB_RECURSE TEST_CASE_CONFIGS t*/.clang-uml)
file(GLOB_RECURSE TEST_CONFIG_YMLS test_config_data/*.yml) file(GLOB_RECURSE TEST_CONFIG_YMLS test_config_data/*.yml)
set(TEST_CASES_REQUIRING_CXX20 t00056) set(TEST_CASES_REQUIRING_CXX20 t00056 t00058)
set(CLANG_UML_TEST_LIBRARIES set(CLANG_UML_TEST_LIBRARIES
clang-umllib clang-umllib

15
tests/t00058/.clang-uml Normal file
View File

@@ -0,0 +1,15 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00058_class:
type: class
glob:
- ../../tests/t00058/t00058.cc
include:
namespaces:
- clanguml::t00058
using_namespace:
- clanguml::t00058
plantuml:
after:
- '{{ alias("same_as_first_type<T,Args...>") }} ..> {{ alias("first_type<T,Args...>") }}'

44
tests/t00058/t00058.cc Normal file
View File

@@ -0,0 +1,44 @@
#include <string>
#include <type_traits>
#include <vector>
// Based on a blog post:
// https://andreasfertig.blog/2020/08/cpp20-concepts-testing-constrained-functions/
namespace clanguml {
namespace t00058 {
template <typename T, typename... Args> struct first_type {
using type = T;
};
template <typename... Args>
using first_type_t = typename first_type<Args...>::type;
// TODO: Dependency of this concept on first_type<> template does not currently
// work due to the fact that I don't know how to extract that information
// from clang::DependentNameType to which first_type_t<> resolves to...
template <typename T, typename... Args>
concept same_as_first_type = std::is_same_v<std::remove_cvref_t<T>,
std::remove_cvref_t<first_type_t<Args...>>>;
template <typename T, typename... Args>
requires same_as_first_type<T, Args...>
struct A {
std::vector<T> a;
};
template <typename T, typename P, typename... Args>
requires same_as_first_type<T, Args...>
struct B {
std::vector<T> b;
P bb;
};
struct R {
A<int, int, double, std::string> aa;
B<int, std::string, int, double, A<int, int>> bb;
};
}
}

69
tests/t00058/test_case.h Normal file
View File

@@ -0,0 +1,69 @@
/**
* tests/t00058/test_case.h
*
* Copyright (c) 2021-2023 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("t00058", "[test-case][class]")
{
auto [config, db] = load_config("t00058");
auto diagram = config.diagrams["t00058_class"];
REQUIRE(diagram->name == "t00058_class");
auto model = generate_class_diagram(*db, diagram);
REQUIRE(model->name() == "t00058_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, IsClassTemplate("A", "int,int,double,std::string"));
REQUIRE_THAT(
puml, IsClassTemplate("B", "int,std::string,int,double,A<int,int>"));
REQUIRE_THAT(puml, IsConcept(_A("same_as_first_type<T,Args...>")));
REQUIRE_THAT(puml,
IsConstraint(_A("A<T,Args...>"), _A("same_as_first_type<T,Args...>"),
"T,Args..."));
REQUIRE_THAT(puml,
IsConstraint(_A("B<T,P,Args...>"), _A("same_as_first_type<T,Args...>"),
"T,Args..."));
REQUIRE_THAT(puml,
IsAggregation(_A("R"), _A("A<int,int,double,std::string>"), "+aa"));
REQUIRE_THAT(puml,
IsAggregation(
_A("R"), _A("B<int,std::string,int,double,A<int,int>>"), "+bb"));
REQUIRE_THAT(puml,
IsInstantiation(
_A("A<T,Args...>"), _A("A<int,int,double,std::string>")));
REQUIRE_THAT(puml,
IsInstantiation(_A("B<T,P,Args...>"),
_A("B<int,std::string,int,double,A<int,int>>")));
REQUIRE_THAT(puml,
IsDependency(
_A("same_as_first_type<T,Args...>"), _A("first_type<T,Args...>")));
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
}

View File

@@ -259,7 +259,9 @@ using namespace clanguml::test::matchers;
#include "t00056/test_case.h" #include "t00056/test_case.h"
#endif #endif
#include "t00057/test_case.h" #include "t00057/test_case.h"
#if defined(ENABLE_CXX_STD_20_TEST_CASES)
#include "t00058/test_case.h"
#endif
/// ///
/// Sequence diagram tests /// Sequence diagram tests
/// ///

View File

@@ -163,11 +163,14 @@ test_cases:
title: Test case for `row` and `column` layout hints title: Test case for `row` and `column` layout hints
description: description:
- name: t00056 - name: t00056
title: Basic C++ concepts test case title: Basic C++20 concepts test case
description: description:
- name: t00057 - name: t00057
title: Test case C99/C11 translation units with structs and unions title: Test case C99/C11 translation units with structs and unions
description: description:
- name: t00058
title: Test case for concepts with variadic parameters and type aliases
description:
Sequence diagrams: Sequence diagrams:
- name: t20001 - name: t20001
title: Basic sequence diagram test case title: Basic sequence diagram test case

View File

@@ -31,6 +31,12 @@ CLASS_DIAGRAM_TEST_CASE_EXAMPLES = """
// Check if class templates exist // Check if class templates exist
//REQUIRE_THAT(puml, IsClassTemplate("A", "T,P,CMP,int N")); //REQUIRE_THAT(puml, IsClassTemplate("A", "T,P,CMP,int N"));
// Check concepts
//REQUIRE_THAT(puml, IsConcept(_A("AConcept<T>")));
//REQUIRE_THAT(puml,
// IsConceptRequirement(
// _A("AConcept<T,P>"), "sizeof (T) > sizeof (P)"));
// Check if all enums exist // Check if all enums exist
//REQUIRE_THAT(puml, IsEnum(_A("Lights"))); //REQUIRE_THAT(puml, IsEnum(_A("Lights")));
@@ -49,8 +55,8 @@ CLASS_DIAGRAM_TEST_CASE_EXAMPLES = """
// Check if all relationships exist // Check if all relationships exist
//REQUIRE_THAT(puml, IsAssociation(_A("D"), _A("A"), "-as")); //REQUIRE_THAT(puml, IsAssociation(_A("D"), _A("A"), "-as"));
//REQUIRE_THAT(puml, IsDependency(_A("R"), _A("B"))); //REQUIRE_THAT(puml, IsDependency(_A("R"), _A("B")));
//REQUIRE_THAT(puml, IsAggregation(_A("R"), _A("D"))); //REQUIRE_THAT(puml, IsAggregation(_A("R"), _A("D"), "-ag"));
//REQUIRE_THAT(puml, IsComposition(_A("R"), _A("D"))); //REQUIRE_THAT(puml, IsComposition(_A("R"), _A("D"), "-ac"));
//REQUIRE_THAT(puml, IsInstantiation(_A("ABCD::F<T>"), _A("F<int>"))); //REQUIRE_THAT(puml, IsInstantiation(_A("ABCD::F<T>"), _A("F<int>")));
""" """