TestHelpers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
7 
8 #include <string>
9 
10 #include "DataStructures/DataBox/Tag.hpp"
11 #include "DataStructures/DataBox/TagName.hpp"
12 #include "DataStructures/DataBox/TagTraits.hpp"
13 #include "Utilities/TypeTraits.hpp"
14 #include "Utilities/TypeTraits/CreateHasStaticMemberVariable.hpp"
15 #include "Utilities/TypeTraits/CreateHasTypeAlias.hpp"
16 #include "Utilities/TypeTraits/CreateIsCallable.hpp"
17 
18 namespace TestHelpers {
19 
20 namespace db {
21 
22 namespace detail {
23 CREATE_HAS_TYPE_ALIAS(argument_tags)
24 CREATE_HAS_TYPE_ALIAS_V(argument_tags)
26 CREATE_HAS_TYPE_ALIAS_V(base)
27 CREATE_HAS_TYPE_ALIAS(return_type)
28 CREATE_HAS_TYPE_ALIAS_V(return_type)
30 CREATE_HAS_TYPE_ALIAS_V(tag)
32 CREATE_HAS_TYPE_ALIAS_V(type)
33 
35 CREATE_IS_CALLABLE_V(name)
36 
37 template <typename Tag>
38 void check_tag_name(const std::string& expected_name) {
39  CHECK(::db::tag_name<Tag>() == expected_name);
40  if (is_name_callable_v<Tag> and not ::db::is_compute_tag_v<Tag>) {
41  INFO("Do not define name for Tag '" << ::db::tag_name<Tag>() << "',");
42  INFO("as it will automatically be generated with that name.");
43  CHECK(::db::tag_name<Tag>() != pretty_type::short_name<Tag>());
44  }
45 }
46 } // namespace detail
47 
48 template <typename Tag>
49 void test_base_tag(const std::string& expected_name) {
50  static_assert(::db::is_base_tag_v<Tag>,
51  "A base tag must be derived from db::BaseTag, but "
52  "not from db::SimpleTag nor db::ComputeTag");
53  static_assert(
54  not detail::has_type_v<Tag>,
55  "The only reason to use a base tag is fetch an item without knowing the "
56  "type. Since the type is known, make it a simple tag.");
57  detail::check_tag_name<Tag>(expected_name);
58 }
59 
60 template <typename Tag>
61 void test_compute_tag(const std::string& expected_name) {
62  static_assert(::db::is_compute_tag_v<Tag>,
63  "A compute tag must be derived from db::ComputeTag");
64  static_assert(detail::has_return_type_v<Tag>);
65  static_assert(detail::has_argument_tags_v<Tag>);
66  static_assert(detail::has_base_v<Tag>);
67  static_assert(::db::is_simple_tag_v<typename Tag::base>,
68  "The base type alias of a compute tag must be a simple tag.");
69  static_assert(std::is_same_v<typename Tag::return_type, typename Tag::type>);
70  detail::check_tag_name<Tag>(expected_name);
71 }
72 
73 template <typename Tag>
74 void test_prefix_tag(const std::string& expected_name) {
75  static_assert(std::is_base_of_v<::db::PrefixTag, Tag>,
76  "A prefix tag must be derived from db::PrefixTag");
77  static_assert(::db::is_simple_tag_v<Tag>,
78  "A prefix tag should also be a simple tag");
79  static_assert(detail::has_type_v<Tag>);
80  static_assert(detail::has_tag_v<Tag>);
81  detail::check_tag_name<Tag>(expected_name);
82 }
83 
84 template <typename Tag>
85 void test_simple_tag(const std::string& expected_name) {
86  static_assert(::db::is_simple_tag_v<Tag>,
87  "A simple tag must be derived from db::SimpleTag, but not "
88  "db::ComputeTag");
89  static_assert(detail::has_type_v<Tag>);
90  if constexpr (detail::has_base_v<Tag>) {
91  static_assert(::db::is_base_tag_v<Tag::base>,
92  "The base type alias of a simple tag must be a base tag.");
93  }
94  static_assert(not std::is_same_v<Tag, typename Tag::type>,
95  "A type cannot be its own tag.");
96  detail::check_tag_name<Tag>(expected_name);
97 }
98 
99 } // namespace db
100 
101 } // namespace TestHelpers
std::string
TestingFramework.hpp
CREATE_HAS_TYPE_ALIAS
#define CREATE_HAS_TYPE_ALIAS(ALIAS_NAME)
Generate a type trait to check if a class has a type alias with a particular name,...
Definition: CreateHasTypeAlias.hpp:27
db
Namespace for DataBox related things.
Definition: DataBox.hpp:43
CREATE_IS_CALLABLE
#define CREATE_IS_CALLABLE(METHOD_NAME)
Generate a type trait to check if a class has a member function that can be invoked with arguments of...
Definition: CreateIsCallable.hpp:30
string