From e2cc030797abe4d45849f9597c9aa24ce1f22935 Mon Sep 17 00:00:00 2001 From: FranckRJ Date: Sat, 27 Apr 2024 15:56:43 +0200 Subject: [PATCH 1/2] Fixed usage of method on uninitialized class in tpunit and removed -Wno-uninitialized for tests. --- tests/CMakeLists.txt | 2 +- tests/tpunit++.hpp | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 75da1a74..9d3d2e0d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -35,7 +35,7 @@ target_link_libraries(FakeIt_tests PRIVATE ) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$") - target_compile_options(FakeIt_tests PRIVATE -Wall -Wextra -Wno-ignored-qualifiers -Wno-uninitialized -O0 -g3) + target_compile_options(FakeIt_tests PRIVATE -Wall -Wextra -Wno-ignored-qualifiers -O0 -g3) elseif(MSVC) target_compile_options(FakeIt_tests PRIVATE /W3 /sdl /Od) endif() diff --git a/tests/tpunit++.hpp b/tests/tpunit++.hpp index c0e3aa3f..c0feeb64 100644 --- a/tests/tpunit++.hpp +++ b/tests/tpunit++.hpp @@ -155,11 +155,11 @@ * used by all test functions. * TEST(function); registers a function to run as a test within a test fixture. */ -#define AFTER(M) tpunit_detail_method(&M, #M, method::AFTER_METHOD) -#define AFTER_CLASS(M) tpunit_detail_method(&M, #M, method::AFTER_CLASS_METHOD) -#define BEFORE(M) tpunit_detail_method(&M, #M, method::BEFORE_METHOD) -#define BEFORE_CLASS(M) tpunit_detail_method(&M, #M, method::BEFORE_CLASS_METHOD) -#define TEST(M) tpunit_detail_method(&M, #M, method::TEST_METHOD) +#define AFTER(M) tpunit_detail_method(this, &M, #M, method::AFTER_METHOD) +#define AFTER_CLASS(M) tpunit_detail_method(this, &M, #M, method::AFTER_CLASS_METHOD) +#define BEFORE(M) tpunit_detail_method(this, &M, #M, method::BEFORE_METHOD) +#define BEFORE_CLASS(M) tpunit_detail_method(this, &M, #M, method::BEFORE_CLASS_METHOD) +#define TEST(M) tpunit_detail_method(this, &M, #M, method::TEST_METHOD) /** * Try our best to detect compiler support for exception handling so @@ -297,12 +297,14 @@ namespace tpunit { /** * Create a new method to register with the test fixture. * + * @param[in] obj The test fixture for which the new method will be registered. * @param[in] _method A method to register with the test fixture. * @param[in] _name The internal name of the method used when status messages are displayed. + * @param[in] _type The type of method to register. */ template - method* tpunit_detail_method(void (C::*_method)(), const char* _name, unsigned char _type) { - return new method(this, static_cast(_method), _name, _type); + static method* tpunit_detail_method(TestFixture* obj, void (C::*_method)(), const char* _name, unsigned char _type) { + return new method(obj, static_cast(_method), _name, _type); } /** From ccd258987f0a84e86ef4589898034168f3010b93 Mon Sep 17 00:00:00 2001 From: FranckRJ Date: Sat, 27 Apr 2024 21:56:59 +0200 Subject: [PATCH 2/2] Treat warnings as errors when building tests in the CI. --- .github/workflows/ci_linux_clang.yml | 1 + .github/workflows/ci_linux_gcc.yml | 1 + .github/workflows/ci_macos_appleclang.yml | 1 + .github/workflows/ci_windows_msvc.yml | 1 + CMakeLists.txt | 1 + tests/CMakeLists.txt | 8 ++++++++ 6 files changed, 13 insertions(+) diff --git a/.github/workflows/ci_linux_clang.yml b/.github/workflows/ci_linux_clang.yml index 2c6529f5..cc770d24 100644 --- a/.github/workflows/ci_linux_clang.yml +++ b/.github/workflows/ci_linux_clang.yml @@ -85,6 +85,7 @@ jobs: cmake -S . -B build \ -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \ -DENABLE_TESTING=ON \ + -DWARNINGS_AS_ERRORS_FOR_TESTS=ON \ -DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }} \ -DENABLE_SANITIZERS_IN_TESTS=${{ matrix.config.enable_sanitizers_in_tests }} cmake --build build -j diff --git a/.github/workflows/ci_linux_gcc.yml b/.github/workflows/ci_linux_gcc.yml index e5468ea0..5a0f04f0 100644 --- a/.github/workflows/ci_linux_gcc.yml +++ b/.github/workflows/ci_linux_gcc.yml @@ -79,6 +79,7 @@ jobs: cmake -S . -B build \ -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \ -DENABLE_TESTING=ON \ + -DWARNINGS_AS_ERRORS_FOR_TESTS=ON \ -DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }} \ -DENABLE_SANITIZERS_IN_TESTS=${{ matrix.config.enable_sanitizers_in_tests }} cmake --build build -j diff --git a/.github/workflows/ci_macos_appleclang.yml b/.github/workflows/ci_macos_appleclang.yml index 617c51f5..0d362c9f 100644 --- a/.github/workflows/ci_macos_appleclang.yml +++ b/.github/workflows/ci_macos_appleclang.yml @@ -60,6 +60,7 @@ jobs: cmake -S . -B build \ -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \ -DENABLE_TESTING=ON \ + -DWARNINGS_AS_ERRORS_FOR_TESTS=ON \ -DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }} \ -DENABLE_SANITIZERS_IN_TESTS=${{ matrix.config.enable_sanitizers_in_tests }} cmake --build build -j diff --git a/.github/workflows/ci_windows_msvc.yml b/.github/workflows/ci_windows_msvc.yml index 023da0de..aee0aa24 100644 --- a/.github/workflows/ci_windows_msvc.yml +++ b/.github/workflows/ci_windows_msvc.yml @@ -63,6 +63,7 @@ jobs: -G "${{ matrix.config.generator }}" \ -A ${{ matrix.config.architecture }} \ -DENABLE_TESTING=ON \ + -DWARNINGS_AS_ERRORS_FOR_TESTS=ON \ -DOVERRIDE_CXX_STANDARD_FOR_TESTS=${{ matrix.config.cxx_standard }} cmake --build build --config ${{ matrix.config.build_type }} -j - name: Run tests diff --git a/CMakeLists.txt b/CMakeLists.txt index e8a77a44..88e07b71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ project(FakeIt VERSION 2.4.0 LANGUAGES CXX) option(ENABLE_TESTING "Enable build of tests." OFF) set(OVERRIDE_CXX_STANDARD_FOR_TESTS "" CACHE STRING "Override the C++ standard used for building tests.") +option(WARNINGS_AS_ERRORS_FOR_TESTS "Treat warnings as errors when building tests." OFF) option(ENABLE_SANITIZERS_IN_TESTS "Enable address / undefined sanitizers in tests." OFF) option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang." OFF) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9d3d2e0d..d5fdd49d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -45,6 +45,14 @@ if(OVERRIDE_CXX_STANDARD_FOR_TESTS) message(STATUS "Building tests in C++${OVERRIDE_CXX_STANDARD_FOR_TESTS}.") endif() +if(WARNINGS_AS_ERRORS_FOR_TESTS) + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$") + target_compile_options(FakeIt_tests PRIVATE -Werror) + elseif(MSVC) + target_compile_options(FakeIt_tests PRIVATE /WX) + endif() +endif() + if(ENABLE_SANITIZERS_IN_TESTS) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$") target_compile_options(FakeIt_tests PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=address,undefined)