userver: userver/utest/assert_macros.hpp Source File
Loading...
Searching...
No Matches
assert_macros.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utest/assert_macros.hpp
4/// @brief Extensions to the gtest macros for printing and testing exceptions
5/// that could work even without coroutine environment.
6/// @ingroup userver_universal
7
8#include <exception>
9#include <functional>
10#include <string>
11#include <string_view>
12#include <type_traits>
13#include <typeinfo>
14
15#include <gtest/gtest.h>
16
17#include <userver/utest/death_tests.hpp>
18#include <userver/utest/impl/assert_macros.hpp>
19#include <userver/utils/invariant_error.hpp>
20
21/// @cond
22// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
23#define IMPL_UTEST_ASSERT_THROW(statement, exception_type, message_substring,
24 failure_macro)
25 if (const auto message_impl_utest =
26 USERVER_NAMESPACE::utest::impl::AssertThrow(
27 [&] { statement; }, #statement,
28 &USERVER_NAMESPACE::utest::impl::IsSubtype<exception_type>,
29 typeid(exception_type), message_substring);
30 !message_impl_utest.empty())
31 failure_macro(message_impl_utest.c_str())
32
33// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
34#define IMPL_UTEST_ASSERT_NO_THROW(statement, failure_macro)
35 if (const auto message_impl_utest =
36 USERVER_NAMESPACE::utest::impl::AssertNoThrow([&] { statement; },
37 #statement);
38 !message_impl_utest.empty())
39 failure_macro(message_impl_utest.c_str())
40/// @endcond
41
42/// @ingroup userver_utest
43///
44/// An equivalent to `EXPECT_THROW` with an additional check for a message
45/// substring
46///
47/// @hideinitializer
48// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
49#define UEXPECT_THROW_MSG(statement, exception_type, message_substring)
50 IMPL_UTEST_ASSERT_THROW(statement, exception_type, message_substring,
51 GTEST_NONFATAL_FAILURE_)
52
53/// @ingroup userver_utest
54///
55/// An equivalent to `ASSERT_THROW` with an additional check for a message
56/// substring
57///
58/// @hideinitializer
59// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
60#define UASSERT_THROW_MSG(statement, exception_type, message_substring)
61 IMPL_UTEST_ASSERT_THROW(statement, exception_type, message_substring,
62 GTEST_FATAL_FAILURE_)
63
64/// @ingroup userver_utest
65///
66/// An equivalent to `EXPECT_THROW` with better diagnostics
67///
68/// @hideinitializer
69// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
70#define UEXPECT_THROW(statement, exception_type)
71 IMPL_UTEST_ASSERT_THROW(statement, exception_type, "",
72 GTEST_NONFATAL_FAILURE_)
73
74/// @ingroup userver_utest
75///
76/// An equivalent to `ASSERT_THROW` with better diagnostics
77///
78/// @hideinitializer
79// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
80#define UASSERT_THROW(statement, exception_type)
81 IMPL_UTEST_ASSERT_THROW(statement, exception_type, "", GTEST_FATAL_FAILURE_)
82
83/// @ingroup userver_utest
84///
85/// An equivalent to `EXPECT_NO_THROW` with better diagnostics
86///
87/// @hideinitializer
88// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
89#define UEXPECT_NO_THROW(statement)
90 IMPL_UTEST_ASSERT_NO_THROW(statement, GTEST_NONFATAL_FAILURE_)
91
92/// @ingroup userver_utest
93///
94/// An equivalent to `EXPECT_THROW` with better diagnostics
95///
96/// @hideinitializer
97// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
98#define UASSERT_NO_THROW(statement)
99 IMPL_UTEST_ASSERT_NO_THROW(statement, GTEST_FATAL_FAILURE_)
100
101/// @cond
102#ifdef NDEBUG
103// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
104#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring)
105 UEXPECT_THROW_MSG(statement, USERVER_NAMESPACE::utils::InvariantError,
106 message_substring)
107#else
108// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
109#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring)
110 UEXPECT_DEATH(statement, message_substring)
111#endif
112/// @endcond
113
114/// @ingroup userver_utest
115///
116/// Test that a UINVARIANT check triggers
117///
118/// @hideinitializer
119// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
120#define EXPECT_UINVARIANT_FAILURE(statement)
121 EXPECT_UINVARIANT_FAILURE_MSG(statement, "")