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
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
19
20USERVER_NAMESPACE_BEGIN
21
22namespace utest::impl {
23
24template <typename ExceptionType>
25bool IsSubtype(const std::exception& ex) noexcept {
26 static_assert(
27 std::is_base_of_v<std::exception, ExceptionType>,
28 "Exception types not inherited from std::exception are not supported"
29 );
30 if constexpr (std::is_same_v<ExceptionType, std::exception>) {
31 return true;
32 } else {
33 return dynamic_cast<const ExceptionType*>(&ex) != nullptr;
34 }
35}
36
37std::string AssertThrow(
38 std::function<void()> statement,
39 std::string_view statement_text,
40 std::function<bool(const std::exception&)> type_checker,
41 const std::type_info& expected_type,
42 std::string_view message_substring
43);
44
45std::string AssertNoThrow(std::function<void()> statement, std::string_view statement_text);
46
47testing::Matcher<const std::string&> MakeHasSubstrMatcher(std::string_view expected);
48
49} // namespace utest::impl
50
51USERVER_NAMESPACE_END
52
54// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
55#define IMPL_UTEST_ASSERT_THROW(statement, exception_type, message_substring, failure_macro) \
56 if (const auto message_impl_utest = USERVER_NAMESPACE::utest::impl::AssertThrow( \
57 [&] { statement; }, \
58 #statement, \
59 &USERVER_NAMESPACE::utest::impl::IsSubtype<exception_type>, \
60 typeid(exception_type), \
61 message_substring \
62 ); \
63 !message_impl_utest.empty()) \
64 failure_macro(message_impl_utest.c_str())
65
66// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
67#define IMPL_UTEST_ASSERT_NO_THROW(statement, failure_macro) \
68 if (const auto message_impl_utest = USERVER_NAMESPACE::utest::impl::AssertNoThrow([&] { statement; }, #statement); \
69 !message_impl_utest.empty()) \
70 failure_macro(message_impl_utest.c_str())
72
79// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
80#define UEXPECT_THROW_MSG(statement, exception_type, message_substring) \
81 IMPL_UTEST_ASSERT_THROW(statement, exception_type, message_substring, GTEST_NONFATAL_FAILURE_)
82
89// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
90#define UASSERT_THROW_MSG(statement, exception_type, message_substring) \
91 IMPL_UTEST_ASSERT_THROW(statement, exception_type, message_substring, GTEST_FATAL_FAILURE_)
92
98// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
99#define UEXPECT_THROW(statement, exception_type) \
100 IMPL_UTEST_ASSERT_THROW(statement, exception_type, "", GTEST_NONFATAL_FAILURE_)
101
107// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
108#define UASSERT_THROW(statement, exception_type) \
109 IMPL_UTEST_ASSERT_THROW(statement, exception_type, "", GTEST_FATAL_FAILURE_)
110
116// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
117#define UEXPECT_NO_THROW(statement) IMPL_UTEST_ASSERT_NO_THROW(statement, GTEST_NONFATAL_FAILURE_)
118
124// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
125#define UASSERT_NO_THROW(statement) IMPL_UTEST_ASSERT_NO_THROW(statement, GTEST_FATAL_FAILURE_)
126
128#ifdef NDEBUG
129// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
130#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring) \
131 UEXPECT_THROW_MSG(statement, USERVER_NAMESPACE::utils::InvariantError, message_substring)
132#else
133// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
134#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring) \
135 UEXPECT_DEATH(statement, USERVER_NAMESPACE::utest::impl::MakeHasSubstrMatcher(message_substring))
136#endif
138
144// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
145#define EXPECT_UINVARIANT_FAILURE(statement) EXPECT_UINVARIANT_FAILURE_MSG(statement, "")