Skip to content

Commit

Permalink
Merge pull request #144 from snitch-org/throw
Browse files Browse the repository at this point in the history
Fix double error reported when a REQUIRE_THROWS* fails for lack of exception being thrown
  • Loading branch information
cschreib authored Nov 28, 2023
2 parents f605e7e + 024a9c4 commit 770286e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
28 changes: 18 additions & 10 deletions include/snitch/snitch_macros_exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

# define SNITCH_REQUIRE_THROWS_AS_IMPL(MAYBE_ABORT, EXPRESSION, ...) \
do { \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
bool SNITCH_NO_EXCEPTION_THROWN = false; \
try { \
static_cast<void>(EXPRESSION); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#__VA_ARGS__ " expected but no exception thrown"); \
MAYBE_ABORT; \
SNITCH_NO_EXCEPTION_THROWN = true; \
} catch (const __VA_ARGS__&) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
true, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
Expand All @@ -37,6 +35,12 @@
MAYBE_ABORT; \
} \
} \
if (SNITCH_NO_EXCEPTION_THROWN) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#__VA_ARGS__ " expected but no exception thrown"); \
MAYBE_ABORT; \
} \
} while (0)

# define SNITCH_REQUIRE_THROWS_AS(EXPRESSION, ...) \
Expand All @@ -46,13 +50,11 @@

# define SNITCH_REQUIRE_THROWS_MATCHES_IMPL(MAYBE_ABORT, EXPRESSION, EXCEPTION, ...) \
do { \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
auto& SNITCH_CURRENT_TEST = snitch::impl::get_current_test(); \
bool SNITCH_NO_EXCEPTION_THROWN = false; \
try { \
static_cast<void>(EXPRESSION); \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#EXCEPTION " expected but no exception thrown"); \
MAYBE_ABORT; \
SNITCH_NO_EXCEPTION_THROWN = true; \
} catch (const EXCEPTION& e) { \
auto&& SNITCH_TEMP_MATCHER = __VA_ARGS__; \
if (!SNITCH_TEMP_MATCHER.match(e)) { \
Expand Down Expand Up @@ -85,6 +87,12 @@
MAYBE_ABORT; \
} \
} \
if (SNITCH_NO_EXCEPTION_THROWN) { \
SNITCH_CURRENT_TEST.reg.report_assertion( \
false, SNITCH_CURRENT_TEST, SNITCH_CURRENT_LOCATION, \
#EXCEPTION " expected but no exception thrown"); \
MAYBE_ABORT; \
} \
} while (0)

# define SNITCH_REQUIRE_THROWS_MATCHES(EXPRESSION, EXCEPTION, ...) \
Expand Down
87 changes: 87 additions & 0 deletions tests/runtime_tests/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,17 @@ SNITCH_WARNING_PUSH
SNITCH_WARNING_DISABLE_PRECEDENCE
SNITCH_WARNING_DISABLE_ASSIGNMENT

#if defined(SNITCH_COMPILER_MSVC) && _MSC_VER == 1937
// Regression in MSVC compiler 19.37.*
// https://github.com/snitch-org/snitch/issues/140
// https://developercommunity.visualstudio.com/t/Regression:-False-positive-C7595:-std::/10509214
# define SNITCH_TEST_NO_SPACESHIP
#endif

TEST_CASE("check no decomposition", "[test macros]") {
event_catcher<1> catcher;

#if !defined(SNITCH_TEST_NO_SPACESHIP)
SECTION("spaceship") {
int value1 = 1;
int value2 = 1;
Expand All @@ -549,6 +557,7 @@ TEST_CASE("check no decomposition", "[test macros]") {
CHECK(value2 == 1);
CHECK_EXPR_FAILURE(catcher, failure_line, "CHECK"sv, "value1 <=> value2 != 0"sv, ""sv);
}
#endif

SECTION("with operator &&") {
int value1 = 1;
Expand Down Expand Up @@ -1493,6 +1502,21 @@ TEST_CASE("check throws as", "[test macros]") {
CHECK_EXPR_SUCCESS(catcher);
}

SECTION("fail no exception") {
std::size_t failure_line = 0u;

{
test_override override(catcher);
auto fun = []() {};
// clang-format off
SNITCH_CHECK_THROWS_AS(fun(), my_exception); failure_line = __LINE__;
// clang-format on
}

CHECK_EXPR_FAILURE(
catcher, failure_line, "my_exception expected but no exception thrown"sv);
}

SECTION("fail other std::exception") {
std::size_t failure_line = 0u;

Expand Down Expand Up @@ -1525,6 +1549,29 @@ TEST_CASE("check throws as", "[test macros]") {
}
}

TEST_CASE("require throws as", "[test macros]") {
event_catcher<1> catcher;

SECTION("fail no exception") {
std::size_t failure_line = 0u;

{
test_override override(catcher);
auto fun = []() {};
try {
// clang-format off
failure_line = __LINE__; SNITCH_REQUIRE_THROWS_AS(fun(), my_exception);
// clang-format on
} catch (const snitch::impl::abort_exception&) {
// Ignore the abort signal.
}
}

CHECK_EXPR_FAILURE(
catcher, failure_line, "my_exception expected but no exception thrown"sv);
}
}

TEST_CASE("check throws matches", "[test macros]") {
event_catcher<1> catcher;

Expand All @@ -1539,6 +1586,22 @@ TEST_CASE("check throws matches", "[test macros]") {
CHECK_EXPR_SUCCESS(catcher);
}

SECTION("fail no exception") {
std::size_t failure_line = 0u;

{
test_override override(catcher);
auto fun = []() {};
auto matcher = snitch::matchers::with_what_contains{"exception"};
// clang-format off
failure_line = __LINE__; SNITCH_CHECK_THROWS_MATCHES(fun(), my_exception, matcher);
// clang-format on
}

CHECK_EXPR_FAILURE(
catcher, failure_line, "my_exception expected but no exception thrown"sv);
}

SECTION("fail other std::exception") {
std::size_t failure_line = 0u;

Expand Down Expand Up @@ -1590,6 +1653,30 @@ TEST_CASE("check throws matches", "[test macros]") {
}
}

TEST_CASE("require throws matches", "[test macros]") {
event_catcher<1> catcher;

SECTION("fail no exception") {
std::size_t failure_line = 0u;

{
test_override override(catcher);
auto fun = []() {};
auto matcher = snitch::matchers::with_what_contains{"exception"};
try {
// clang-format off
failure_line = __LINE__; SNITCH_REQUIRE_THROWS_MATCHES(fun(), my_exception, matcher);
// clang-format on
} catch (const snitch::impl::abort_exception&) {
// Ignore the abort signal.
}
}

CHECK_EXPR_FAILURE(
catcher, failure_line, "my_exception expected but no exception thrown"sv);
}
}

namespace {
[[nodiscard]] int nodiscard_function() {
return 1;
Expand Down

0 comments on commit 770286e

Please sign in to comment.