From 4eaee8fd9aafa95318104c08bb227f85544fcd61 Mon Sep 17 00:00:00 2001 From: Jonathan Hyry Date: Sun, 8 Jun 2025 21:29:09 -0700 Subject: [PATCH 1/2] Closes #129 and #128. --- CMakePresets.json | 6 ++- include/internal/TestCPPAssertions.h | 18 +++---- include/internal/TestCPPTestCase.h | 48 ++++++++++++------- include/internal/TestCPPTestSuite.h | 19 ++++---- test/src/TestCPPAssertionsBasicMain.cpp | 12 +++-- test/src/TestCPPExceptionsMain.cpp | 12 +++-- test/src/TestCPPTestCaseMain.cpp | 12 +++-- test/src/TestCPPTestSuiteConstructionMain.cpp | 12 +++-- test/src/TestCPPTestSuiteRunningMain.cpp | 12 +++-- .../TestCPPTestSuiteTestPassedMessageMain.cpp | 12 +++-- test/src/TestCase/TestCaseTests.cpp | 30 ++++++++++-- test/src/TestSuite/TestSuiteRunningTests.cpp | 6 +-- 12 files changed, 126 insertions(+), 73 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 27f91d8..38fd7f7 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -154,7 +154,8 @@ "installDir": "${sourceDir}/out/install/win32-msanalysis", "cacheVariables": { "CMAKE_C_FLAGS": "/analyze /analyze:external- /analyze:autolog /analyze:ruleset NativeRecommendedRules.ruleset", - "CMAKE_CXX_FLAGS": "/analyze /analyze:external- /analyze:autolog /analyze:ruleset NativeRecommendedRules.ruleset" + "CMAKE_CXX_FLAGS": "/analyze /analyze:external- /analyze:autolog /analyze:ruleset NativeRecommendedRules.ruleset", + "TESTCPP_TEST_ENABLED": "1" }, "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { @@ -358,7 +359,8 @@ "CMAKE_CXX_FLAGS": "-v -m32", "CMAKE_EXE_LINKER_FLAGS": "/machine:x86", "CMAKE_SYSTEM_PROCESSOR": "X86", - "TESTCPP_ANALYSIS_ENABLED": "1" + "TESTCPP_ANALYSIS_ENABLED": "1", + "TESTCPP_TEST_ENABLED": "1" }, "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { diff --git a/include/internal/TestCPPAssertions.h b/include/internal/TestCPPAssertions.h index c7b6fec..65b1d87 100644 --- a/include/internal/TestCPPAssertions.h +++ b/include/internal/TestCPPAssertions.h @@ -361,15 +361,15 @@ void TestCPP::Assertions::assertEquals( /** * @brief Check that char*'s are not equal using strcmp. - * @param expected The value that the actual value should not - * be equivalent to. - * @param actual The actual value that will be checked against - * the expected value. - * @param failureMessage Failure message that should be logged - * if the assertion fails. This - * defaults to a generic failure - * message related to the assertion - * type. + * + * @param shouldNotBe The value that the actual value should not be + * equivalent to. + * @param actual The actual value that will be checked against + * the expected value. + * @param failureMessage Failure message that should be logged if the + * assertion fails. This defaults to a generic + * failure message related to the assertion + * type. */ template<> void TestCPP::Assertions::assertNotEquals( diff --git a/include/internal/TestCPPTestCase.h b/include/internal/TestCPPTestCase.h index 5e4b35f..de2a594 100644 --- a/include/internal/TestCPPTestCase.h +++ b/include/internal/TestCPPTestCase.h @@ -96,20 +96,23 @@ namespace TestCPP { /** * @brief Construct a test case with possibility to use all - * options. + * options. * - * @param testName The name of the test as a TestObjName. - * @param test The implementation or pointer to the test. - * @param testPassedMessage Whether to omit a message indicating - * test passage. - * @param captureOut Whether to capture stdout output for - * analysis after the test run. - * @param captureLog Whether to capture clog output for - * analysis after the test run. - * @param captureErr Whether to capture stderr output for - * analysis after the test run. - * @param opt Technique for comparing actual output with - * expected output after the test run. + * @param name The name of the test as a + * TestObjName. + * @param testFn The implementation or pointer to + * the test. + * @param testPassedMessage Whether to omit a message + * indicating test passage. + * @param captureOut Whether to capture stdout output + * for analysis after the test run. + * @param captureLog Whether to capture clog output for + * analysis after the test run. + * @param captureErr Whether to capture stderr output + * for analysis after the test run. + * @param opt Technique for comparing actual + * output with expected output after + * the test run. * * Instantiate and define a test case. * All parameters are optional other than the test name and the @@ -284,23 +287,29 @@ namespace TestCPP { * active. */ void captureStdErr (); + /** * @brief Write a test failure reason to the specified stream. - * @param out The stream to write the test failure reason to. - * @param reason The test failure reason to write. + * + * @param out The stream to write the test failure + * reason to. + * @param failureReason The test failure reason to write. */ void logFailure (ostream& out, const string& failureReason) const; /** * @brief If a test encounters an error while running, this * function will be called to log the test error. - * @param failureMessage The error message from the test that - * should be logged. + * + * @param failureReason The error message from the test that + * should be logged. */ void logTestFailure (const string& failureReason); + /** * @brief Internal test run controller. */ void runTest (); + /** * @brief Handles the internal logic for calls to checkStdout, * checkLog, and checkStderr based on the selected @@ -336,9 +345,12 @@ namespace TestCPP { /** * @brief Measure the duration of a function when run. + * * @param func Measure the duration of this function when run. * @param args A template pack of arguments to apply to the - * given function of which to measure the duration. + * given function of which to measure the + * duration. + * * @return The duration of the function run, in nanoseconds. */ template diff --git a/include/internal/TestCPPTestSuite.h b/include/internal/TestCPPTestSuite.h index fdfe4c3..7d5b7ac 100644 --- a/include/internal/TestCPPTestSuite.h +++ b/include/internal/TestCPPTestSuite.h @@ -87,10 +87,10 @@ namespace TestCPP { * tests. */ template - TestSuite (TestObjName&& newSuiteName, TestType ...tests) + TestSuite (TestObjName&& newSuiteName, TestType ...newSuiteTests) { commonInit(std::forward(newSuiteName)); - this->addTests(tests...); + this->addTests(newSuiteTests...); } /** @@ -99,7 +99,7 @@ namespace TestCPP { * Appropriate specializations defined in the source file. */ template - void addTest (T&& test); + void addTest (T&& addTestToSuite); /** * @brief Specialization to handle when someone tries to call @@ -113,13 +113,16 @@ namespace TestCPP { /** * @brief Add one or more tests at once to the test suite. - * @param test The first test to add. - * @param tests The rest of the tests to add. + * + * @param addTestToSuite The first test to add. + * @param addTheseOtherTestsAlso The rest of the tests to add. */ template - void addTests (Test test, OtherTests ...tests) { - addTest(std::move(test)); - addTests(tests...); + void addTests (Test addTestToSuite, + OtherTests ...addTheseOtherTestsAlso) + { + addTest(std::move(addTestToSuite)); + addTests(addTheseOtherTestsAlso...); } /** diff --git a/test/src/TestCPPAssertionsBasicMain.cpp b/test/src/TestCPPAssertionsBasicMain.cpp index 04e7c3b..e3afbec 100644 --- a/test/src/TestCPPAssertionsBasicMain.cpp +++ b/test/src/TestCPPAssertionsBasicMain.cpp @@ -1,10 +1,12 @@ #include "TestCPP.h" -using TestCPP::TestCase; -using TestCPP::TestSuite; -using std::string; -using std::make_tuple; -using std::function; +namespace TestCPP { + namespace Testing { + namespace AssertionsSuites { + extern TestSuite basicSuite; + } + } +} #include "Assertions/AssertionsBasicSuite.h" diff --git a/test/src/TestCPPExceptionsMain.cpp b/test/src/TestCPPExceptionsMain.cpp index 7cb3822..0020103 100644 --- a/test/src/TestCPPExceptionsMain.cpp +++ b/test/src/TestCPPExceptionsMain.cpp @@ -1,10 +1,12 @@ #include "TestCPP.h" -using TestCPP::TestCase; -using TestCPP::TestSuite; -using std::string; -using std::make_tuple; -using std::function; +namespace TestCPP { + namespace Testing { + namespace ExceptionsSuite { + extern TestSuite suite; + } + } +} #include "Exceptions/ExceptionsSuite.h" diff --git a/test/src/TestCPPTestCaseMain.cpp b/test/src/TestCPPTestCaseMain.cpp index 3db3ee6..47c4f57 100644 --- a/test/src/TestCPPTestCaseMain.cpp +++ b/test/src/TestCPPTestCaseMain.cpp @@ -1,10 +1,12 @@ #include "TestCPP.h" -using TestCPP::TestCase; -using TestCPP::TestSuite; -using std::string; -using std::make_tuple; -using std::function; +namespace TestCPP { + namespace Testing { + namespace TestCaseSuite { + extern TestSuite suite; + } + } +} #include "TestCase/TestCaseSuite.h" diff --git a/test/src/TestCPPTestSuiteConstructionMain.cpp b/test/src/TestCPPTestSuiteConstructionMain.cpp index 2d39584..5f47cee 100644 --- a/test/src/TestCPPTestSuiteConstructionMain.cpp +++ b/test/src/TestCPPTestSuiteConstructionMain.cpp @@ -1,10 +1,12 @@ #include "TestCPP.h" -using TestCPP::TestCase; -using TestCPP::TestSuite; -using std::string; -using std::make_tuple; -using std::function; +namespace TestCPP { + namespace Testing { + namespace TestSuiteSuites { + extern TestSuite ctorSuite; + } + } +} #include "TestSuite/TestSuiteConstructionSuite.h" diff --git a/test/src/TestCPPTestSuiteRunningMain.cpp b/test/src/TestCPPTestSuiteRunningMain.cpp index 73b3e70..89fbd3b 100644 --- a/test/src/TestCPPTestSuiteRunningMain.cpp +++ b/test/src/TestCPPTestSuiteRunningMain.cpp @@ -1,10 +1,12 @@ #include "TestCPP.h" -using TestCPP::TestCase; -using TestCPP::TestSuite; -using std::string; -using std::make_tuple; -using std::function; +namespace TestCPP { + namespace Testing { + namespace TestSuiteSuites { + extern TestSuite runSuite; + } + } +} #include "TestSuite/TestSuiteRunningSuite.h" diff --git a/test/src/TestCPPTestSuiteTestPassedMessageMain.cpp b/test/src/TestCPPTestSuiteTestPassedMessageMain.cpp index c5390ab..64f44df 100644 --- a/test/src/TestCPPTestSuiteTestPassedMessageMain.cpp +++ b/test/src/TestCPPTestSuiteTestPassedMessageMain.cpp @@ -1,10 +1,12 @@ #include "TestCPP.h" -using TestCPP::TestCase; -using TestCPP::TestSuite; -using std::string; -using std::make_tuple; -using std::function; +namespace TestCPP { + namespace Testing { + namespace TestSuiteSuites { + extern TestSuite testPassedMessageSuite; + } + } +} #include "TestSuite/TestSuiteTestPassedMessageSuite.h" diff --git a/test/src/TestCase/TestCaseTests.cpp b/test/src/TestCase/TestCaseTests.cpp index b880166..4e0207d 100644 --- a/test/src/TestCase/TestCaseTests.cpp +++ b/test/src/TestCase/TestCaseTests.cpp @@ -1,4 +1,5 @@ #include "TestCPP.h" +#include "TestCase/TestCaseTests.h" #include "TestCase/TestCaseTestChunks.h" using TestCPP::Util::debugLog; @@ -25,9 +26,10 @@ namespace TestCPP { debugLog("Construct with nullptr string", true); debugLog(" - assertThrows lambda"); - auto test = unique_ptr(new TestCase( - nullptr, - function([](){}) + auto testShouldThrow = unique_ptr( + new TestCase( + nullptr, + function([](){}) )); }, "Should have thrown on nullptr string!" @@ -110,9 +112,20 @@ namespace TestCPP { auto test = unique_ptr(new TestCase( "SUB-TEST TestCaseGo case Test - throws str", +#ifdef TESTCPP_STACKTRACE_ENABLED + // We use C++14 when stacktraces are enabled, which + // allows us to avoid needing to use + // -Wno-unused-lambda-capture to avoid MSVC C3493 + // since the implementations can be reconciled in + // the newer standard. + function([&toThrow = throwStr](){ + throw toThrow; + }), +#else function([&throwStr](){ throw throwStr; }), +#endif true, false, true, false, TestCase::TestCaseOutCompareOptions::CONTAINS )); @@ -134,9 +147,20 @@ namespace TestCPP { auto test = unique_ptr(new TestCase( "SUB-TEST TestCaseGo case Test - throws chr", +#ifdef TESTCPP_STACKTRACE_ENABLED + // We use C++14 when stacktraces are enabled, which + // allows us to avoid needing to use + // -Wno-unused-lambda-capture to avoid MSVC C3493 + // since the implementations can be reconciled in + // the newer standard. + function([&toThrow = throwChr](){ + throw toThrow; + }), +#else function([&throwChr](){ throw throwChr; }), +#endif true, false, true, false, TestCase::TestCaseOutCompareOptions::CONTAINS )); diff --git a/test/src/TestSuite/TestSuiteRunningTests.cpp b/test/src/TestSuite/TestSuiteRunningTests.cpp index 986961a..04b0541 100644 --- a/test/src/TestSuite/TestSuiteRunningTests.cpp +++ b/test/src/TestSuite/TestSuiteRunningTests.cpp @@ -4,9 +4,9 @@ namespace TestCPP { namespace Testing { namespace TestSuiteTests { - unsigned zero = 0; - unsigned one = 1; - unsigned three = 3; + static unsigned zero = 0; + static unsigned one = 1; + static unsigned three = 3; void TestSuiteRunNoTests() { auto testSuite = unique_ptr(new TestSuite( From 1fe2233ef7f1195475ee3a83879aff7c0c952020 Mon Sep 17 00:00:00 2001 From: Jonathan Hyry Date: Sun, 8 Jun 2025 21:40:26 -0700 Subject: [PATCH 2/2] Fixes #130, which this PR requires. --- CMakeLists.txt | 3 --- cmake/VarChecks.cmake | 9 +++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ce2e566..8356ec7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,9 +5,6 @@ set (PROJECT_GROUP_NAME "cpptesting") project (${PROJECT_NAME} VERSION 1.0.0 LANGUAGES CXX) -set (CMAKE_CXX_STANDARD 11) -set (CMAKE_CXX_STANDARD_REQUIRED ON) - list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) include (VarChecks) diff --git a/cmake/VarChecks.cmake b/cmake/VarChecks.cmake index c19c896..07fcb5c 100644 --- a/cmake/VarChecks.cmake +++ b/cmake/VarChecks.cmake @@ -38,11 +38,20 @@ if (NOT DEFINED TESTCPP_STACKTRACE_ENABLED) message (STATUS "Not defined TESTCPP_STACKTRACE_ENABLED") set (TESTCPP_STACKTRACE_ENABLED 0) + set (CMAKE_CXX_STANDARD 11) else () message (STATUS "Defd TESTCPP_STACKTRACE_ENABLED ${TESTCPP_STACKTRACE_ENABLED}") + + if (${TESTCPP_STACKTRACE_ENABLED}) + set (CMAKE_CXX_STANDARD 14) + else () + set (CMAKE_CXX_STANDARD 11) + endif () endif () +set (CMAKE_CXX_STANDARD_REQUIRED ON) + if (${TESTCPP_TEST_ENABLED}) message (STATUS "Testing enabled!") include (CTest)