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);
47testing::Matcher<
const std::string&> MakeHasSubstrMatcher(std::string_view expected);
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(
59 &USERVER_NAMESPACE
::utest::impl::IsSubtype<exception_type>,
60 typeid(exception_type),
63 !message_impl_utest.empty())
64 failure_macro(message_impl_utest.c_str())
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())
80#define UEXPECT_THROW_MSG(statement, exception_type, message_substring)
81 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, message_substring, GTEST_NONFATAL_FAILURE_
)
90#define UASSERT_THROW_MSG(statement, exception_type, message_substring)
91 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, message_substring, GTEST_FATAL_FAILURE_
)
99#define UEXPECT_THROW(statement, exception_type)
100 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, "", GTEST_NONFATAL_FAILURE_
)
108#define UASSERT_THROW(statement, exception_type)
109 IMPL_UTEST_ASSERT_THROW
(statement, exception_type, "", GTEST_FATAL_FAILURE_
)
117#define UEXPECT_NO_THROW(statement) IMPL_UTEST_ASSERT_NO_THROW
(statement, GTEST_NONFATAL_FAILURE_
)
125#define UASSERT_NO_THROW(statement) IMPL_UTEST_ASSERT_NO_THROW
(statement, GTEST_FATAL_FAILURE_
)
130#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring)
131 UEXPECT_THROW_MSG(statement, USERVER_NAMESPACE::utils::InvariantError, message_substring)
134#define EXPECT_UINVARIANT_FAILURE_MSG(statement, message_substring)
135 UEXPECT_DEATH(statement, USERVER_NAMESPACE
::utest::impl::MakeHasSubstrMatcher(message_substring))
145#define EXPECT_UINVARIANT_FAILURE(statement) EXPECT_UINVARIANT_FAILURE_MSG
(statement, "")