Skip to content

Commit

Permalink
Update catch2 interface to support new ansi line wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed May 5, 2024
1 parent 56c6251 commit cce3401
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions include/libassert/assert-catch2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <libassert/assert.hpp>

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_version_macros.hpp>

#if defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL != 0
#error "Libassert integration does not work with MSVC's non-conformant preprocessor. /Zc:preprocessor must be used."
Expand All @@ -13,16 +14,22 @@
#define ASSERT(...) do { try { LIBASSERT_ASSERT(__VA_ARGS__); SUCCEED(); } catch(std::exception& e) { FAIL(e.what()); } } while(false)

namespace libassert::detail {
// catch line wrapping can't handle ansi sequences before 3.6 https://github.com/catchorg/Catch2/issues/2833
inline constexpr bool use_color = CATCH_VERSION_MAJOR > 3 || (CATCH_VERSION_MAJOR == 3 && CATCH_VERSION_MINOR >= 6);

inline void catch2_failure_handler(const assertion_info& info) {
if(use_color) {
enable_virtual_terminal_processing_if_needed();
}
auto scheme = use_color ? color_scheme::ansi_rgb : color_scheme::blank;
std::string message = std::string(info.action()) + " at " + info.location() + ":";
if(info.message) {
message += " " + *info.message;
}
message += "\n";
message += info.statement(color_scheme::blank)
+ info.print_binary_diagnostics(0, color_scheme::blank)
+ info.print_extra_diagnostics(0, color_scheme::blank);
// catch line wrapping has issues with ansi sequences https://github.com/catchorg/Catch2/issues/2833
message += info.statement(scheme)
+ info.print_binary_diagnostics(CATCH_CONFIG_CONSOLE_WIDTH, scheme)
+ info.print_extra_diagnostics(CATCH_CONFIG_CONSOLE_WIDTH, scheme);
throw std::runtime_error(std::move(message));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
Catch2
GIT_SHALLOW TRUE
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG 8ac8190e494a381072c89f5e161b92a08d98b37b # v3.5.3
GIT_TAG 4e8d92bf02f7d1c8006a0e7a5ecabd8e62d98502 # v3.6.0
)
FetchContent_MakeAvailable(Catch2)

Expand Down

0 comments on commit cce3401

Please sign in to comment.