Skip to content

Commit

Permalink
Fix #177
Browse files Browse the repository at this point in the history
  • Loading branch information
cschreib committed Aug 22, 2024
1 parent d663212 commit bccf2c8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
4 changes: 1 addition & 3 deletions include/snitch/snitch_capture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ struct scoped_capture {
capture_state& captures;
std::size_t count = 0;

~scoped_capture() {
captures.resize(captures.size() - count);
}
~scoped_capture();
};

SNITCH_EXPORT std::string_view extract_next_name(std::string_view& names) noexcept;
Expand Down
13 changes: 13 additions & 0 deletions src/snitch_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ void trim(std::string_view& str, std::string_view patterns) noexcept {
}
} // namespace

scoped_capture::~scoped_capture() {
#if SNITCH_WITH_EXCEPTIONS
if (std::uncaught_exceptions() > 0) {
// We are unwinding the stack because an exception has been thrown;
// avoid touching the capture state since we will want to preserve the information
// when reporting the exception.
return;
}
#endif

captures.resize(captures.size() - count);
}

std::string_view extract_next_name(std::string_view& names) noexcept {
std::string_view result;

Expand Down
36 changes: 36 additions & 0 deletions tests/runtime_tests/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ TEST_CASE("capture", "[test macros]") {
CHECK_CAPTURES_FOR_FAILURE(0u, "i := 1");
CHECK_CAPTURES_FOR_FAILURE(1u, "i := 1", "2 * i := 2");
}

#if SNITCH_WITH_EXCEPTIONS
SECTION("with exception") {
framework.test_case.func = []() {
for (std::size_t i = 0; i < 5; ++i) {
SNITCH_CAPTURE(i);

if (i % 2 == 1) {
throw std::runtime_error("bad");
}
}
};

framework.run_test();
REQUIRE(framework.get_num_failures() == 1u);
CHECK_CAPTURES_FOR_FAILURE(0u, "i := 1");
}
#endif
}

TEST_CASE("info", "[test macros]") {
Expand Down Expand Up @@ -324,6 +342,24 @@ TEST_CASE("info", "[test macros]") {
framework.run_test();
CHECK_CAPTURES("1", "i := 1");
}

#if SNITCH_WITH_EXCEPTIONS
SECTION("with exception") {
framework.test_case.func = []() {
for (std::size_t i = 0; i < 5; ++i) {
SNITCH_INFO(i);

if (i % 2 == 1) {
throw std::runtime_error("bad");
}
}
};

framework.run_test();
REQUIRE(framework.get_num_failures() == 1u);
CHECK_CAPTURES_FOR_FAILURE(0u, "1");
}
#endif
}

SNITCH_WARNING_POP
2 changes: 1 addition & 1 deletion tests/runtime_tests/section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ TEST_CASE("section readme example", "[test macros]") {
}
};

framework.registry.print_callback = print;
framework.registry.print_callback = print;

framework.test_case.func = []() {
auto& reg = snitch::impl::get_current_test().reg;
Expand Down

0 comments on commit bccf2c8

Please sign in to comment.