15#include <gtest/gtest.h>
17#include <userver/utest/death_tests.hpp>
18#include <userver/utils/invariant_error.hpp>
20USERVER_NAMESPACE_BEGIN
22namespace utest::impl {
24template <
typename ExceptionType>
25bool IsSubtype(
const std::exception& ex)
noexcept {
27 std::is_base_of_v<std::exception, ExceptionType>,
28 "Exception types not inherited from std::exception are not supported"
30 if constexpr (std::is_same_v<ExceptionType, std::exception>) {
33 return dynamic_cast<
const ExceptionType*>(&ex) !=
nullptr;
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
45std::string AssertNoThrow(std::function<
void()> statement, std::string_view statement_text);
53#define IMPL_UTEST_ASSERT_THROW(statement, exception_type, message_substring, failure_macro)
54 if (const auto message_impl_utest = USERVER_NAMESPACE
::utest::impl::AssertThrow(
57 &USERVER_NAMESPACE
::utest::impl::IsSubtype<exception_type>,
58 typeid(exception_type),
61 !message_impl_utest.empty())
62 failure_macro(message_impl_utest.c_str())
65#define IMPL_UTEST_ASSERT_NO_THROW(statement, failure_macro)
66 if (const auto message_impl_utest = USERVER_NAMESPACE
::utest::impl::AssertNoThrow([&] { statement; }, #statement);
67 !message_impl_utest.empty())
68 failure_macro(message_impl_utest.c_str())
78#define UEXPECT_THROW_MSG(statement, exception_type, message_substring)
79 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, message_substring, GTEST_NONFATAL_FAILURE_
)
88#define UASSERT_THROW_MSG(statement, exception_type, message_substring)
89 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, message_substring, GTEST_FATAL_FAILURE_
)
97#define UEXPECT_THROW(statement, exception_type)
98 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, "", GTEST_NONFATAL_FAILURE_
)
106#define UASSERT_THROW(statement, exception_type)
107 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, "", GTEST_FATAL_FAILURE_
)
115#define UEXPECT_NO_THROW(statement) IMPL_UTEST_ASSERT_NO_THROW
(statement, GTEST_NONFATAL_FAILURE_
)
123#define UASSERT_NO_THROW(statement) IMPL_UTEST_ASSERT_NO_THROW
(statement, GTEST_FATAL_FAILURE_
)
128#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring)
129 UEXPECT_THROW_MSG(statement, USERVER_NAMESPACE::utils::InvariantError, message_substring)
132#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring) UEXPECT_DEATH(statement, message_substring)
142#define EXPECT_UINVARIANT_FAILURE(statement) EXPECT_UINVARIANT_FAILURE_MSG
(statement, "")