Skip to content

Commit

Permalink
Merge pull request #192 from NicoMuleo/SNITCH_DISABLE
Browse files Browse the repository at this point in the history
implement SNITCH_DISABLE feature
  • Loading branch information
cschreib authored Nov 30, 2024
2 parents b6465f9 + ddf1002 commit a570b90
Show file tree
Hide file tree
Showing 25 changed files with 598 additions and 431 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ jobs:
matrix:
platform:
- { name: Ubuntu GCC, os: ubuntu-latest, publish: true, compiler: g++, arch: "64", build: "ubuntu64-libstdc++-static", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS='--coverage' --warn-uninitialized -Wdev -Wno-deprecated -Werror=dev"}
- { name: Ubuntu GCC disabled, os: ubuntu-latest, publish: false, compiler: g++, arch: "64", build: "ubuntu64-libstdc++-static-disabled", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=g++ --warn-uninitialized -Wdev -Wno-deprecated -Werror=dev -DSNITCH_ENABLE=OFF"}
- { name: Ubuntu GCC noexcept, os: ubuntu-latest, publish: false, compiler: g++, arch: "64", build: "ubuntu64-libstdc++-static-noexcept", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS='-fno-exceptions' --warn-uninitialized -Wdev -Wno-deprecated -Werror=dev"}
- { name: Ubuntu GCC notime, os: ubuntu-latest, publish: false, compiler: g++, arch: "64", build: "ubuntu64-libstdc++-static-notime", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=g++ -DSNITCH_WITH_TIMINGS=0 --warn-uninitialized -Wdev -Wno-deprecated -Werror=dev"}
- { name: Ubuntu GCC nounity, os: ubuntu-latest, publish: false, compiler: g++, arch: "64", build: "ubuntu64-libstdc++-static-nounity", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=g++ -DSNITCH_UNITY_BUILD=0 --warn-uninitialized -Wdev -Wno-deprecated -Werror=dev"}
Expand Down Expand Up @@ -161,19 +162,20 @@ jobs:
- name: Test (with doctest)
# A bug in Node.js/V8 prevents these tests from running in Debug config for WebAssembly.
# https://bugs.chromium.org/p/v8/issues/detail?id=13961
if: ${{!(matrix.platform.compiler == 'em++' && matrix.build-type == 'Debug')}}
if: ${{!(matrix.platform.compiler == 'em++' && matrix.build-type == 'Debug') && !contains(matrix.platform.flags, '-DSNITCH_ENABLE=OFF')}}
shell: bash
working-directory: ${{github.workspace}}/build
run: cmake --build . --config ${{matrix.build-type}} --target snitch_runtime_tests_run

- name: Test (with snitch)
if: ${{!contains(matrix.platform.flags, '-DSNITCH_ENABLE=OFF')}}
shell: bash
working-directory: ${{github.workspace}}/build
run: cmake --build . --config ${{matrix.build-type}} --target snitch_runtime_tests_self_run

- name: Approval tests (with doctest)
# Approval tests only run on "default" library configuration, which happens to be the published one
if: matrix.platform.publish
if: ${{matrix.platform.publish && !contains(matrix.platform.flags, '-DSNITCH_ENABLE=OFF')}}
shell: bash
working-directory: ${{github.workspace}}/build
run: cmake --build . --config ${{matrix.build-type}} --target snitch_approval_tests_run
Expand Down
13 changes: 9 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SNITCH_MAX_PATH_LENGTH 1024 CACHE STRING "Maximum length of a file
set(SNITCH_MAX_REPORTER_SIZE_BYTES 128 CACHE STRING "Maximum size (in bytes) of a reporter object.")

# Feature toggles.
set(SNITCH_ENABLE ON CACHE BOOL "Enable/disable snitch at build time.")
set(SNITCH_DEFINE_MAIN ON CACHE BOOL "Define main() in snitch -- disable to provide your own main() function.")
set(SNITCH_WITH_EXCEPTIONS ON CACHE BOOL "Use exceptions in snitch implementation -- will be forced OFF if exceptions are not available.")
set(SNITCH_WITH_MULTITHREADING ON CACHE BOOL "Make the testing framework thread-safe -- disable if multithreading is not needed.")
Expand Down Expand Up @@ -115,10 +116,14 @@ set(SNITCH_SOURCES_INDIVIDUAL
${PROJECT_SOURCE_DIR}/src/snitch_test_data.cpp
${PROJECT_SOURCE_DIR}/src/snitch_time.cpp)

if (SNITCH_UNITY_BUILD)
set(SNITCH_SOURCES ${PROJECT_SOURCE_DIR}/src/snitch.cpp)
if (SNITCH_ENABLE)
if (SNITCH_UNITY_BUILD)
set(SNITCH_SOURCES ${PROJECT_SOURCE_DIR}/src/snitch.cpp)
else()
set(SNITCH_SOURCES ${SNITCH_SOURCES_INDIVIDUAL})
endif()
else()
set(SNITCH_SOURCES ${SNITCH_SOURCES_INDIVIDUAL})
set(SNITCH_SOURCES ${PROJECT_SOURCE_DIR}/src/snitch_main.cpp)
endif()

function(configure_snitch_exports TARGET)
Expand Down Expand Up @@ -211,7 +216,7 @@ install(FILES
DESTINATION lib/cmake/snitch COMPONENT Development)

# Setup tests
if (SNITCH_DO_TEST)
if (SNITCH_DO_TEST AND SNITCH_ENABLE)
enable_testing()

# We need to use a different snitch configuration for tests, so we can't reuse
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The goal of _snitch_ is to be a simple, cheap, non-invasive, and user-friendly t
- Additional API not in _Catch2_, or different from _Catch2_:
- Matchers use a different API (see [Matchers](#matchers) below).
- Additional macros for testing [`constexpr`](#run-time-and-compile-time) and [`consteval`](#compile-time) expressions.
- Can be disabled at build time, to allow mixing code and tests in the same file with minimal overheads.

If you need features that are not in the list above, please use _Catch2_ or _doctest_.

Expand Down
1 change: 1 addition & 0 deletions include/snitch/snitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "snitch/snitch_macros_test_case.hpp"
#include "snitch/snitch_macros_utility.hpp"
#include "snitch/snitch_macros_warnings.hpp"
#include "snitch/snitch_main.hpp"
#include "snitch/snitch_matcher.hpp"
#include "snitch/snitch_registry.hpp"
#include "snitch/snitch_reporter_catch2_xml.hpp"
Expand Down
3 changes: 3 additions & 0 deletions include/snitch/snitch_config.hpp.config
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
#if !defined(SNITCH_SHARED_LIBRARY)
#cmakedefine01 SNITCH_SHARED_LIBRARY
#endif
#if !defined(SNITCH_ENABLE)
#cmakedefine01 SNITCH_ENABLE
#endif
// clang-format on

#if defined(_MSC_VER)
Expand Down
166 changes: 94 additions & 72 deletions include/snitch/snitch_macros_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,119 @@
#include "snitch/snitch_config.hpp"
#include "snitch/snitch_expression.hpp"
#include "snitch/snitch_macros_check_base.hpp"
#include "snitch/snitch_macros_utility.hpp"
#include "snitch/snitch_macros_warnings.hpp"
#include "snitch/snitch_matcher.hpp"
#include "snitch/snitch_registry.hpp"
#include "snitch/snitch_test_data.hpp"

#define SNITCH_REQUIRE_IMPL(CHECK, EXPECTED, MAYBE_ABORT, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
SNITCH_WARNING_PUSH \
SNITCH_WARNING_DISABLE_PARENTHESES \
SNITCH_WARNING_DISABLE_CONSTANT_COMPARISON \
if constexpr (SNITCH_IS_DECOMPOSABLE(__VA_ARGS__)) { \
SNITCH_EXPR(CHECK, EXPECTED, __VA_ARGS__); \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} else { \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #__VA_ARGS__, {}, static_cast<bool>(__VA_ARGS__) == EXPECTED}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} \
SNITCH_WARNING_POP \
} while (0)
#if SNITCH_ENABLE

# define SNITCH_REQUIRE_IMPL(CHECK, EXPECTED, MAYBE_ABORT, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
SNITCH_WARNING_PUSH \
SNITCH_WARNING_DISABLE_PARENTHESES \
SNITCH_WARNING_DISABLE_CONSTANT_COMPARISON \
if constexpr (SNITCH_IS_DECOMPOSABLE(__VA_ARGS__)) { \
SNITCH_EXPR(CHECK, EXPECTED, __VA_ARGS__); \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} else { \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #__VA_ARGS__, {}, static_cast<bool>(__VA_ARGS__) == EXPECTED}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} \
SNITCH_WARNING_POP \
} while (0)

// clang-format off
#define SNITCH_REQUIRE(...) SNITCH_REQUIRE_IMPL("REQUIRE", true, SNITCH_TESTING_ABORT, __VA_ARGS__)
#define SNITCH_CHECK(...) SNITCH_REQUIRE_IMPL("CHECK", true, (void)0, __VA_ARGS__)
#define SNITCH_REQUIRE_FALSE(...) SNITCH_REQUIRE_IMPL("REQUIRE_FALSE", false, SNITCH_TESTING_ABORT, __VA_ARGS__)
#define SNITCH_CHECK_FALSE(...) SNITCH_REQUIRE_IMPL("CHECK_FALSE", false, (void)0, __VA_ARGS__)
# define SNITCH_REQUIRE(...) SNITCH_REQUIRE_IMPL("REQUIRE", true, SNITCH_TESTING_ABORT, __VA_ARGS__)
# define SNITCH_CHECK(...) SNITCH_REQUIRE_IMPL("CHECK", true, (void)0, __VA_ARGS__)
# define SNITCH_REQUIRE_FALSE(...) SNITCH_REQUIRE_IMPL("REQUIRE_FALSE", false, SNITCH_TESTING_ABORT, __VA_ARGS__)
# define SNITCH_CHECK_FALSE(...) SNITCH_REQUIRE_IMPL("CHECK_FALSE", false, (void)0, __VA_ARGS__)
// clang-format on

#define SNITCH_SUCCEED(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(true, (MESSAGE)); \
} while (0)

#define SNITCH_FAIL(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

#define SNITCH_FAIL_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
} while (0)

#define SNITCH_SKIP(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

#define SNITCH_SKIP_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
} while (0)

#define SNITCH_REQUIRE_THAT_IMPL(CHECK, MAYBE_ABORT, EXPR, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
const auto SNITCH_TEMP_RESULT = snitch::impl::match(EXPR, __VA_ARGS__); \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #EXPR ", " #__VA_ARGS__, \
snitch::resize_or_truncate<snitch::max_expr_length>(SNITCH_TEMP_RESULT.second), \
SNITCH_TEMP_RESULT.first}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} while (0)
# define SNITCH_SUCCEED(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(true, (MESSAGE)); \
} while (0)

# define SNITCH_FAIL(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

# define SNITCH_FAIL_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
} while (0)

# define SNITCH_SKIP(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

# define SNITCH_SKIP_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
} while (0)

# define SNITCH_REQUIRE_THAT_IMPL(CHECK, MAYBE_ABORT, EXPR, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
const auto SNITCH_TEMP_RESULT = snitch::impl::match(EXPR, __VA_ARGS__); \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #EXPR ", " #__VA_ARGS__, \
snitch::resize_or_truncate<snitch::max_expr_length>(SNITCH_TEMP_RESULT.second), \
SNITCH_TEMP_RESULT.first}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} while (0)

// clang-format off
#define SNITCH_REQUIRE_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("REQUIRE_THAT", SNITCH_TESTING_ABORT, EXPR, __VA_ARGS__)
#define SNITCH_CHECK_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("CHECK_THAT", (void)0, EXPR, __VA_ARGS__)
# define SNITCH_REQUIRE_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("REQUIRE_THAT", SNITCH_TESTING_ABORT, EXPR, __VA_ARGS__)
# define SNITCH_CHECK_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("CHECK_THAT", (void)0, EXPR, __VA_ARGS__)
// clang-format on

#else // SNITCH_ENABLE
// clang-format off
# define SNITCH_REQUIRE(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)
# define SNITCH_CHECK(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)
# define SNITCH_REQUIRE_FALSE(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)
# define SNITCH_CHECK_FALSE(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)

# define SNITCH_SUCCEED(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_FAIL(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_FAIL_CHECK(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_SKIP(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_SKIP_CHECK(MESSAGE) SNITCH_VOID_STATEMENT

# define SNITCH_REQUIRE_THAT(EXPR, ...) SNITCH_DISCARD_ARGS(EXPR, __VA_ARGS__)
# define SNITCH_CHECK_THAT(EXPR, ...) SNITCH_DISCARD_ARGS(EXPR, __VA_ARGS__)
// clang-format on

#endif // SNITCH_ENABLE

// clang-format on
#if SNITCH_WITH_SHORTHAND_MACROS
# define SUCCEED(MESSAGE) SNITCH_SUCCEED(MESSAGE)
# define FAIL(MESSAGE) SNITCH_FAIL(MESSAGE)
# define SUCCEED(MESSAGE) SNITCH_SUCCEED(MESSAGE)
# define FAIL(MESSAGE) SNITCH_FAIL(MESSAGE)
# define FAIL_CHECK(MESSAGE) SNITCH_FAIL_CHECK(MESSAGE)
# define SKIP(MESSAGE) SNITCH_SKIP(MESSAGE)
# define SKIP(MESSAGE) SNITCH_SKIP(MESSAGE)
# define SKIP_CHECK(MESSAGE) SNITCH_SKIP_CHECK(MESSAGE)

# define REQUIRE(...) SNITCH_REQUIRE(__VA_ARGS__)
# define CHECK(...) SNITCH_CHECK(__VA_ARGS__)
# define REQUIRE_FALSE(...) SNITCH_REQUIRE_FALSE(__VA_ARGS__)
# define CHECK_FALSE(...) SNITCH_CHECK_FALSE(__VA_ARGS__)
# define REQUIRE(...) SNITCH_REQUIRE(__VA_ARGS__)
# define CHECK(...) SNITCH_CHECK(__VA_ARGS__)
# define REQUIRE_FALSE(...) SNITCH_REQUIRE_FALSE(__VA_ARGS__)
# define CHECK_FALSE(...) SNITCH_CHECK_FALSE(__VA_ARGS__)
# define REQUIRE_THAT(EXP, ...) SNITCH_REQUIRE_THAT(EXP, __VA_ARGS__)
# define CHECK_THAT(EXP, ...) SNITCH_CHECK_THAT(EXP, __VA_ARGS__)
# define CHECK_THAT(EXP, ...) SNITCH_CHECK_THAT(EXP, __VA_ARGS__)
#endif
// clang-format on

Expand Down
4 changes: 3 additions & 1 deletion include/snitch/snitch_macros_check_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#endif

#define SNITCH_NEW_CHECK \
snitch::impl::scoped_test_check { SNITCH_CURRENT_LOCATION }
snitch::impl::scoped_test_check { \
SNITCH_CURRENT_LOCATION \
}

#define SNITCH_EXPR(TYPE, EXPECTED, ...) \
auto SNITCH_CURRENT_EXPRESSION = \
Expand Down
Loading

0 comments on commit a570b90

Please sign in to comment.