From e7b56d4e3cff2154df3c613e7dfb3ad709a050ec Mon Sep 17 00:00:00 2001 From: Austin Gilbert Date: Fri, 17 Oct 2014 17:12:55 -0500 Subject: [PATCH 01/11] Introducing additional functionality to allow client code to stop a unit test when an assert fails. The following macro has been added: REQUIRE An example of when these type of checks are useful: std::vector v = foo(); REQUIRE(CHECK_EQUAL(3, v.size())); // test stops here on a fail // so we don't segfault looking at // v[0] below. CHECK_EQUAL(1, v[0]); CHECK_EQUAL(2, v[1]); CHECK_EQUAL(3, v[2]); Multiple checks are supported as follows: REQUIRE({ CHECK_EQUAL(1, 2); CHECK(true); }; In the multiple check scenario, all the checks in the REQUIRE block will be run. After which, if any failures were reported, the TEST case will be stopped. When UNITTEST_NO_EXCEPTIONS is defined, REQUIRE is a noop. --- UnitTest++/RequireMacros.h | 28 ++ UnitTest++/UnitTestPP.h | 1 + tests/TestRequireMacros.cpp | 842 ++++++++++++++++++++++++++++++++++++ 3 files changed, 871 insertions(+) create mode 100644 UnitTest++/RequireMacros.h create mode 100644 tests/TestRequireMacros.cpp diff --git a/UnitTest++/RequireMacros.h b/UnitTest++/RequireMacros.h new file mode 100644 index 0000000..65817d9 --- /dev/null +++ b/UnitTest++/RequireMacros.h @@ -0,0 +1,28 @@ +#ifndef UNITTEST_REQUIREMACROS_H +#define UNITTEST_REQUIREMACROS_H + +#include "HelperMacros.h" +#include "ExceptionMacros.h" +#include "CurrentTest.h" + +#ifdef REQUIRE + #error UnitTest++ redefines REQUIRE +#endif + +#ifndef UNITTEST_NO_EXCEPTIONS + #define REQUIRE(test) \ + UNITTEST_MULTILINE_MACRO_BEGIN \ + int const failuresBeforeTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \ + test; \ + int const failuresAfterTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \ + if(failuresAfterTest > failuresBeforeTest) \ + { \ + UT_THROW(UnitTest::AssertException()); \ + } \ + UNITTEST_MULTILINE_MACRO_END + #endif +#endif + +#ifdef UNITTEST_NO_EXCEPTIONS + #define REQUIRE(test) test; +#endif diff --git a/UnitTest++/UnitTestPP.h b/UnitTest++/UnitTestPP.h index c9bbc0c..3e02d31 100644 --- a/UnitTest++/UnitTestPP.h +++ b/UnitTest++/UnitTestPP.h @@ -4,6 +4,7 @@ #include "Config.h" #include "TestMacros.h" #include "CheckMacros.h" +#include "RequireMacros.h" #include "TestRunner.h" #include "TimeConstraint.h" #include "ReportAssert.h" diff --git a/tests/TestRequireMacros.cpp b/tests/TestRequireMacros.cpp new file mode 100644 index 0000000..3025a34 --- /dev/null +++ b/tests/TestRequireMacros.cpp @@ -0,0 +1,842 @@ +#include "UnitTest++/UnitTestPP.h" +#include "UnitTest++/CurrentTest.h" +#include "RecordingReporter.h" +#include "ScopedCurrentTest.h" + +using namespace std; + +#ifndef UNITTEST_NO_EXCEPTIONS + +namespace { + +TEST(RequireCheckSucceedsOnTrue) +{ + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK(true)); + } + catch(const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); +} + +TEST(RequiredCheckFailsOnFalse) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK(false)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); +} + + +TEST(RequireMacroSupportsMultipleChecks) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try{ + REQUIRE({ + CHECK(true); + CHECK_EQUAL(1,1); + }); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); +} + + +TEST(RequireMacroSupportsMultipleChecksWithFailingChecks) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try{ + REQUIRE({ + CHECK(true); + CHECK_EQUAL(1,2); + }); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); +} + +TEST(FailureReportsCorrectTestName) +{ + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK(false)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK_EQUAL(m_details.testName, reporter.lastFailedTest); +} + +TEST(RequiredCheckFailureIncludesCheckContents) +{ + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + const bool yaddayadda = false; + + try + { + REQUIRE(CHECK(yaddayadda)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK(strstr(reporter.lastFailedMessage, "yaddayadda")); +} + +TEST(RequiredCheckEqualSucceedsOnEqual) +{ + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_EQUAL(1,1)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); +} + +TEST(RequiredCheckEqualFailsOnNotEqual) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_EQUAL(1, 2)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); +} + +TEST(RequiredCheckEqualFailureContainsCorrectDetails) +{ + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + try + { + line = __LINE__; REQUIRE(CHECK_EQUAL(1, 123)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK_EQUAL("testName", reporter.lastFailedTest); + CHECK_EQUAL("suiteName", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); +} + +int g_sideEffect = 0; +int FunctionWithSideEffects() +{ + ++g_sideEffect; + return 1; +} + +TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenPassing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_EQUAL(1, FunctionWithSideEffects())); + } + catch (const UnitTest::AssertException&) + { + } + } + CHECK_EQUAL(1, g_sideEffect); +} + +TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenFailing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_EQUAL(2, FunctionWithSideEffects())); + } + catch (const UnitTest::AssertException&) + { + } + } + CHECK_EQUAL(1, g_sideEffect); +} + + +TEST(RequiredCheckCloseSucceedsOnEqual) +{ + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_CLOSE(1.0f, 1.001f, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); +} + +TEST(RequiredCheckCloseFailsOnNotEqual) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_CLOSE (1.0f, 1.1f, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); +} + +TEST(RequiredCheckCloseFailureContainsCorrectDetails) +{ + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("test", "suite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + try + { + line = __LINE__; REQUIRE(CHECK_CLOSE(1.0f, 1.1f, 0.01f)); + CHECK(false); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK_EQUAL("test", reporter.lastFailedTest); + CHECK_EQUAL("suite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); +} + +TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenPassing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f)); + } + catch (const UnitTest::AssertException&) + { + } + } + CHECK_EQUAL(1, g_sideEffect); +} + +TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenFailing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { + REQUIRE(CHECK_CLOSE(2, FunctionWithSideEffects(), 0.1f)); + } + catch (const UnitTest::AssertException&) + { + } + } + CHECK_EQUAL(1, g_sideEffect); +} + +TEST(RequiredCheckArrayCloseSucceedsOnEqual) +{ + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + const float data[4] = { 0, 1, 2, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_CLOSE (data, data, 4, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); +} + +TEST(RequiredCheckArrayCloseFailsOnNotEqual) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); +} + +TEST(RequiredCheckArrayCloseFailureIncludesCheckExpectedAndActual) +{ + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_CLOSE(data1, data2, 4, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); +} + +TEST(RequiredCheckArrayCloseFailureContainsCorrectDetails) +{ + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + line = __LINE__; REQUIRE(CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); +} + +TEST(RequiredCheckArrayCloseFailureIncludesTolerance) +{ + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + float const data1[4] = { 0, 1, 2, 3 }; + float const data2[4] = { 0, 1, 3, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK(strstr(reporter.lastFailedMessage, "0.01")); +} + +TEST(RequiredCheckArrayEqualSuceedsOnEqual) +{ + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + const float data[4] = { 0, 1, 2, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_EQUAL (data, data, 4)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); +} + +TEST(RequiredCheckArrayEqualFailsOnNotEqual) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_EQUAL (data1, data2, 4)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); +} + +TEST(RequiredCheckArrayEqualFailureIncludesCheckExpectedAndActual) +{ + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_EQUAL (data1, data2, 4)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); +} + +TEST(RequiredCheckArrayEqualFailureContainsCorrectInfo) +{ + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + line = __LINE__; REQUIRE(CHECK_ARRAY_EQUAL (data1, data2, 4)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK_EQUAL("RequiredCheckArrayEqualFailureContainsCorrectInfo", reporter.lastFailedTest); + CHECK_EQUAL(__FILE__, reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); +} + +float const* FunctionWithSideEffects2() +{ + ++g_sideEffect; + static float const data[] = {1,2,3,4}; + return data; +} + +TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenPassing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 2, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + CHECK_EQUAL(1, g_sideEffect); +} + +TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenFailing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 3, 3 }; + + try + { + REQUIRE(CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK_EQUAL(1, g_sideEffect); +} + +TEST(RequiredCheckArray2DCloseSucceedsOnEqual) +{ + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {2, 3} }; + + try + { + REQUIRE(CHECK_ARRAY2D_CLOSE(data, data, 2, 2, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); +} + +TEST(RequiredCheckArray2DCloseFailsOnNotEqual) +{ + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + + try + { + REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); +} + +TEST(RequiredCheckArray2DCloseFailureIncludesCheckExpectedAndActual) +{ + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + + try + { + REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ [ 0 1 ] [ 2 3 ] ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ [ 0 1 ] [ 3 3 ] ]")); +} + +TEST(RequiredCheckArray2DCloseFailureContainsCorrectDetails) +{ + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + + try + { + line = __LINE__; REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); +} + +TEST(RequiredCheckArray2DCloseFailureIncludesTolerance) +{ + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + float const data1[2][2] = { {0, 1}, {2, 3} }; + float const data2[2][2] = { {0, 1}, {3, 3} }; + + try + { + REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + + CHECK(strstr(reporter.lastFailedMessage, "0.01")); +} + +float const* const* FunctionWithSideEffects3() +{ + ++g_sideEffect; + static float const data1[] = {0,1}; + static float const data2[] = {2,3}; + static const float* const data[] = {data1, data2}; + return data; +} + +TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenPassing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {2, 3} }; + + try + { + REQUIRE(CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + CHECK_EQUAL(1, g_sideEffect); +} + +TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenFailing) +{ + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {3, 3} }; + + try + { + REQUIRE(CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f)); + } + catch (const UnitTest::AssertException&) + { + } + } + CHECK_EQUAL(1, g_sideEffect); +} + +} + +#endif From 06308ee8023de4a0070e45cbdeb52e45ea002b9a Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Tue, 4 Nov 2014 23:41:05 -0600 Subject: [PATCH 02/11] Use for loop to achieve REQUIRE with no parens I changed the definition of the REQUIRE macro to use for loops and some comma operator shenanigans to allow things like: REQUIRE { CHECK(...); CHECK_EQUAL(..., ...); } or REQUIRE CHECK(...); I updated the tests and they all passed on my machine. My only concern is that some compilers might complain about the unreachable code in the (throw UnitTest::AssertException(), true) expression. --- UnitTest++/RequireMacros.h | 19 ++++------ tests/TestRequireMacros.cpp | 74 +++++++++++++++++++------------------ 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/UnitTest++/RequireMacros.h b/UnitTest++/RequireMacros.h index 65817d9..747bda7 100644 --- a/UnitTest++/RequireMacros.h +++ b/UnitTest++/RequireMacros.h @@ -10,19 +10,14 @@ #endif #ifndef UNITTEST_NO_EXCEPTIONS - #define REQUIRE(test) \ - UNITTEST_MULTILINE_MACRO_BEGIN \ - int const failuresBeforeTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \ - test; \ - int const failuresAfterTest = UnitTest::CurrentTest::Results()->GetFailureCount(); \ - if(failuresAfterTest > failuresBeforeTest) \ - { \ - UT_THROW(UnitTest::AssertException()); \ - } \ - UNITTEST_MULTILINE_MACRO_END - #endif + #define REQUIRE \ + for (int failuresBeforeTest = UnitTest::CurrentTest::Results()->GetFailureCount(), newFailures = 0, run = 0; \ + (run == 0) || ((newFailures != 0) && (throw UnitTest::AssertException(), true)); \ + newFailures = UnitTest::CurrentTest::Results()->GetFailureCount() - failuresBeforeTest, run = 1) #endif #ifdef UNITTEST_NO_EXCEPTIONS - #define REQUIRE(test) test; + #define REQUIRE #endif + +#endif \ No newline at end of file diff --git a/tests/TestRequireMacros.cpp b/tests/TestRequireMacros.cpp index 3025a34..869de32 100644 --- a/tests/TestRequireMacros.cpp +++ b/tests/TestRequireMacros.cpp @@ -21,7 +21,7 @@ TEST(RequireCheckSucceedsOnTrue) try { - REQUIRE(CHECK(true)); + REQUIRE CHECK(true); } catch(const UnitTest::AssertException&) { @@ -46,7 +46,7 @@ TEST(RequiredCheckFailsOnFalse) try { - REQUIRE(CHECK(false)); + REQUIRE CHECK(false); } catch (const UnitTest::AssertException&) { @@ -71,10 +71,11 @@ TEST(RequireMacroSupportsMultipleChecks) ScopedCurrentTest scopedResults(testResults); try{ - REQUIRE({ + REQUIRE + { CHECK(true); CHECK_EQUAL(1,1); - }); + } } catch (const UnitTest::AssertException&) { @@ -99,10 +100,11 @@ TEST(RequireMacroSupportsMultipleChecksWithFailingChecks) ScopedCurrentTest scopedResults(testResults); try{ - REQUIRE({ + REQUIRE + { CHECK(true); CHECK_EQUAL(1,2); - }); + } } catch (const UnitTest::AssertException&) { @@ -125,7 +127,7 @@ TEST(FailureReportsCorrectTestName) try { - REQUIRE(CHECK(false)); + REQUIRE CHECK(false); } catch (const UnitTest::AssertException&) { @@ -145,7 +147,7 @@ TEST(RequiredCheckFailureIncludesCheckContents) try { - REQUIRE(CHECK(yaddayadda)); + REQUIRE CHECK(yaddayadda); } catch (const UnitTest::AssertException&) { @@ -166,7 +168,7 @@ TEST(RequiredCheckEqualSucceedsOnEqual) try { - REQUIRE(CHECK_EQUAL(1,1)); + REQUIRE CHECK_EQUAL(1,1); } catch (const UnitTest::AssertException&) { @@ -191,7 +193,7 @@ TEST(RequiredCheckEqualFailsOnNotEqual) try { - REQUIRE(CHECK_EQUAL(1, 2)); + REQUIRE CHECK_EQUAL(1, 2); } catch (const UnitTest::AssertException&) { @@ -216,7 +218,7 @@ TEST(RequiredCheckEqualFailureContainsCorrectDetails) try { - line = __LINE__; REQUIRE(CHECK_EQUAL(1, 123)); + line = __LINE__; REQUIRE CHECK_EQUAL(1, 123); } catch (const UnitTest::AssertException&) { @@ -245,7 +247,7 @@ TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenPassing) try { - REQUIRE(CHECK_EQUAL(1, FunctionWithSideEffects())); + REQUIRE CHECK_EQUAL(1, FunctionWithSideEffects()); } catch (const UnitTest::AssertException&) { @@ -263,7 +265,7 @@ TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenFailing) try { - REQUIRE(CHECK_EQUAL(2, FunctionWithSideEffects())); + REQUIRE CHECK_EQUAL(2, FunctionWithSideEffects()); } catch (const UnitTest::AssertException&) { @@ -284,7 +286,7 @@ TEST(RequiredCheckCloseSucceedsOnEqual) try { - REQUIRE(CHECK_CLOSE(1.0f, 1.001f, 0.01f)); + REQUIRE CHECK_CLOSE(1.0f, 1.001f, 0.01f); } catch (const UnitTest::AssertException&) { @@ -309,7 +311,7 @@ TEST(RequiredCheckCloseFailsOnNotEqual) try { - REQUIRE(CHECK_CLOSE (1.0f, 1.1f, 0.01f)); + REQUIRE CHECK_CLOSE (1.0f, 1.1f, 0.01f); } catch (const UnitTest::AssertException&) { @@ -334,7 +336,7 @@ TEST(RequiredCheckCloseFailureContainsCorrectDetails) try { - line = __LINE__; REQUIRE(CHECK_CLOSE(1.0f, 1.1f, 0.01f)); + line = __LINE__; REQUIRE CHECK_CLOSE(1.0f, 1.1f, 0.01f); CHECK(false); } catch (const UnitTest::AssertException&) @@ -357,7 +359,7 @@ TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenPassing) try { - REQUIRE(CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f)); + REQUIRE CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f); } catch (const UnitTest::AssertException&) { @@ -375,7 +377,7 @@ TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenFailing) try { - REQUIRE(CHECK_CLOSE(2, FunctionWithSideEffects(), 0.1f)); + REQUIRE CHECK_CLOSE(2, FunctionWithSideEffects(), 0.1f); } catch (const UnitTest::AssertException&) { @@ -396,7 +398,7 @@ TEST(RequiredCheckArrayCloseSucceedsOnEqual) try { - REQUIRE(CHECK_ARRAY_CLOSE (data, data, 4, 0.01f)); + REQUIRE CHECK_ARRAY_CLOSE (data, data, 4, 0.01f); } catch (const UnitTest::AssertException&) { @@ -424,7 +426,7 @@ TEST(RequiredCheckArrayCloseFailsOnNotEqual) try { - REQUIRE(CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f)); + REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); } catch (const UnitTest::AssertException&) { @@ -450,7 +452,7 @@ TEST(RequiredCheckArrayCloseFailureIncludesCheckExpectedAndActual) try { - REQUIRE(CHECK_ARRAY_CLOSE(data1, data2, 4, 0.01f)); + REQUIRE CHECK_ARRAY_CLOSE(data1, data2, 4, 0.01f); } catch (const UnitTest::AssertException&) { @@ -475,7 +477,7 @@ TEST(RequiredCheckArrayCloseFailureContainsCorrectDetails) try { - line = __LINE__; REQUIRE(CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f)); + line = __LINE__; REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); } catch (const UnitTest::AssertException&) { @@ -500,7 +502,7 @@ TEST(RequiredCheckArrayCloseFailureIncludesTolerance) try { - REQUIRE(CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f)); + REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); } catch (const UnitTest::AssertException&) { @@ -523,7 +525,7 @@ TEST(RequiredCheckArrayEqualSuceedsOnEqual) try { - REQUIRE(CHECK_ARRAY_EQUAL (data, data, 4)); + REQUIRE CHECK_ARRAY_EQUAL (data, data, 4); } catch (const UnitTest::AssertException&) { @@ -551,7 +553,7 @@ TEST(RequiredCheckArrayEqualFailsOnNotEqual) try { - REQUIRE(CHECK_ARRAY_EQUAL (data1, data2, 4)); + REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); } catch (const UnitTest::AssertException&) { @@ -577,7 +579,7 @@ TEST(RequiredCheckArrayEqualFailureIncludesCheckExpectedAndActual) try { - REQUIRE(CHECK_ARRAY_EQUAL (data1, data2, 4)); + REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); } catch (const UnitTest::AssertException&) { @@ -601,7 +603,7 @@ TEST(RequiredCheckArrayEqualFailureContainsCorrectInfo) try { - line = __LINE__; REQUIRE(CHECK_ARRAY_EQUAL (data1, data2, 4)); + line = __LINE__; REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); } catch (const UnitTest::AssertException&) { @@ -631,7 +633,7 @@ TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenPassing) try { - REQUIRE(CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f)); + REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); } catch (const UnitTest::AssertException&) { @@ -651,7 +653,7 @@ TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenFailing) try { - REQUIRE(CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f)); + REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); } catch (const UnitTest::AssertException&) { @@ -674,7 +676,7 @@ TEST(RequiredCheckArray2DCloseSucceedsOnEqual) try { - REQUIRE(CHECK_ARRAY2D_CLOSE(data, data, 2, 2, 0.01f)); + REQUIRE CHECK_ARRAY2D_CLOSE(data, data, 2, 2, 0.01f); } catch (const UnitTest::AssertException&) { @@ -702,7 +704,7 @@ TEST(RequiredCheckArray2DCloseFailsOnNotEqual) try { - REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } catch (const UnitTest::AssertException&) { @@ -728,7 +730,7 @@ TEST(RequiredCheckArray2DCloseFailureIncludesCheckExpectedAndActual) try { - REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } catch (const UnitTest::AssertException&) { @@ -753,7 +755,7 @@ TEST(RequiredCheckArray2DCloseFailureContainsCorrectDetails) try { - line = __LINE__; REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + line = __LINE__; REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } catch (const UnitTest::AssertException&) { @@ -778,7 +780,7 @@ TEST(RequiredCheckArray2DCloseFailureIncludesTolerance) try { - REQUIRE(CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f)); + REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } catch (const UnitTest::AssertException&) { @@ -808,7 +810,7 @@ TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenPassing) try { - REQUIRE(CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f)); + REQUIRE CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); } catch (const UnitTest::AssertException&) { @@ -828,7 +830,7 @@ TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenFailing) try { - REQUIRE(CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f)); + REQUIRE CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); } catch (const UnitTest::AssertException&) { From a9161c1ba6f62584c435f56323e5a21891ecdffc Mon Sep 17 00:00:00 2001 From: Austin Gilbert Date: Sat, 6 Dec 2014 22:59:28 -0600 Subject: [PATCH 03/11] This commit addresses two issues: (1) unreachable code in for loop shenanigans is eliminated. (2) code after a failing REQUIRE check no longer executes. Used a decorating TestReporter to achive this. --- UnitTest++/CheckMacros.h | 30 ++++++++++++-- UnitTest++/RequireMacros.h | 9 +---- UnitTest++/RequiredCheckTestReporter.cpp | 29 ++++++++++++++ UnitTest++/RequiredCheckTestReporter.h | 29 ++++++++++++++ UnitTest++/TestResults.h | 4 ++ UnitTest++/ThrowingTestReporter.cpp | 51 ++++++++++++++++++++++++ UnitTest++/ThrowingTestReporter.h | 29 ++++++++++++++ tests/TestRequireMacros.cpp | 30 ++++++++++++++ 8 files changed, 201 insertions(+), 10 deletions(-) create mode 100644 UnitTest++/RequiredCheckTestReporter.cpp create mode 100644 UnitTest++/RequiredCheckTestReporter.h create mode 100644 UnitTest++/ThrowingTestReporter.cpp create mode 100644 UnitTest++/ThrowingTestReporter.h diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index d79d503..bc9fbcf 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -41,6 +41,10 @@ if (!UnitTest::Check(value)) \ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ }) \ + UT_CATCH (UnitTest::AssertException, e, \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -60,7 +64,11 @@ UT_TRY \ ({ \ UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ + }) \ + UT_CATCH (UnitTest::AssertException, e, \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -81,6 +89,10 @@ ({ \ UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ + UT_CATCH (UnitTest::AssertException, e, \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -100,7 +112,11 @@ UT_TRY \ ({ \ UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ + }) \ + UT_CATCH (UnitTest::AssertException, e, \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -121,6 +137,10 @@ ({ \ UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ + UT_CATCH (UnitTest::AssertException, e, \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -140,7 +160,11 @@ UT_TRY \ ({ \ UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ + }) \ + UT_CATCH (UnitTest::AssertException, e, \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ diff --git a/UnitTest++/RequireMacros.h b/UnitTest++/RequireMacros.h index 747bda7..9fee8eb 100644 --- a/UnitTest++/RequireMacros.h +++ b/UnitTest++/RequireMacros.h @@ -1,19 +1,14 @@ #ifndef UNITTEST_REQUIREMACROS_H #define UNITTEST_REQUIREMACROS_H -#include "HelperMacros.h" -#include "ExceptionMacros.h" -#include "CurrentTest.h" +#include "RequiredCheckTestReporter.h" #ifdef REQUIRE #error UnitTest++ redefines REQUIRE #endif #ifndef UNITTEST_NO_EXCEPTIONS - #define REQUIRE \ - for (int failuresBeforeTest = UnitTest::CurrentTest::Results()->GetFailureCount(), newFailures = 0, run = 0; \ - (run == 0) || ((newFailures != 0) && (throw UnitTest::AssertException(), true)); \ - newFailures = UnitTest::CurrentTest::Results()->GetFailureCount() - failuresBeforeTest, run = 1) + #define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(UnitTest::CurrentTest::Results()); decoratedReporter.next(); ) #endif #ifdef UNITTEST_NO_EXCEPTIONS diff --git a/UnitTest++/RequiredCheckTestReporter.cpp b/UnitTest++/RequiredCheckTestReporter.cpp new file mode 100644 index 0000000..cae20db --- /dev/null +++ b/UnitTest++/RequiredCheckTestReporter.cpp @@ -0,0 +1,29 @@ +#include "RequiredCheckTestReporter.h" + +#include "CurrentTest.h" +#include "TestResults.h" + +namespace UnitTest { + + RequiredCheckTestReporter::RequiredCheckTestReporter(TestResults* results) + : m_results(results) + , m_throwingReporter(0) + , m_continue(0) + { + if(m_results) + { + m_throwingReporter.setDecorated(m_results->m_testReporter); + m_results->m_testReporter = &m_throwingReporter; + } + } + + RequiredCheckTestReporter::~RequiredCheckTestReporter() + { + if(m_results) m_results->m_testReporter = m_throwingReporter.getDecorated(); + } + + bool RequiredCheckTestReporter::next() + { + return m_continue++ == 0; + } +} \ No newline at end of file diff --git a/UnitTest++/RequiredCheckTestReporter.h b/UnitTest++/RequiredCheckTestReporter.h new file mode 100644 index 0000000..22613e9 --- /dev/null +++ b/UnitTest++/RequiredCheckTestReporter.h @@ -0,0 +1,29 @@ +#ifndef UNITTEST_REQUIRED_CHECK_TEST_REPORTER_H +#define UNITTEST_REQUIRED_CHECK_TEST_REPORTER_H + +#include "HelperMacros.h" +#include "ThrowingTestReporter.h" + +namespace UnitTest { + + class TestResults; + + // This RAII class decorates the current TestReporter with + // a version that throws after reporting a failure. + class UNITTEST_LINKAGE RequiredCheckTestReporter + { + public: + explicit RequiredCheckTestReporter(TestResults* results); + ~RequiredCheckTestReporter(); + + bool next(); + + private: + TestResults* m_results; + ThrowingTestReporter m_throwingReporter; + int m_continue; + }; +} + +#endif + diff --git a/UnitTest++/TestResults.h b/UnitTest++/TestResults.h index c56a632..024ace3 100644 --- a/UnitTest++/TestResults.h +++ b/UnitTest++/TestResults.h @@ -5,6 +5,7 @@ namespace UnitTest { +class RequiredCheckTestReporter; class TestReporter; class TestDetails; @@ -22,6 +23,9 @@ namespace UnitTest { int GetFailureCount() const; private: + friend class RequiredCheckTestReporter; + +private: TestReporter* m_testReporter; int m_totalTestCount; int m_failedTestCount; diff --git a/UnitTest++/ThrowingTestReporter.cpp b/UnitTest++/ThrowingTestReporter.cpp new file mode 100644 index 0000000..45ccade --- /dev/null +++ b/UnitTest++/ThrowingTestReporter.cpp @@ -0,0 +1,51 @@ +#include "ThrowingTestReporter.h" +#include "AssertException.h" + +namespace UnitTest { + + ThrowingTestReporter::ThrowingTestReporter(TestReporter* decoratedReporter) + : m_decoratedReporter(decoratedReporter) + { + } + + //virtual + ThrowingTestReporter::~ThrowingTestReporter() + { + } + + //virtual + void ThrowingTestReporter::ReportTestStart(TestDetails const& test) + { + if(m_decoratedReporter) m_decoratedReporter->ReportTestStart(test); + } + + //virtual + void ThrowingTestReporter::ReportFailure(TestDetails const& test, char const* failure) + { + if(m_decoratedReporter) m_decoratedReporter->ReportFailure(test, failure); + throw AssertException(); + } + + //virtual + void ThrowingTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed) + { + if(m_decoratedReporter) m_decoratedReporter->ReportTestFinish(test, secondsElapsed); + } + + //virtual + void ThrowingTestReporter::ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) + { + if(m_decoratedReporter) m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); + } + + TestReporter* ThrowingTestReporter::getDecorated() const + { + return m_decoratedReporter; + } + + void ThrowingTestReporter::setDecorated(TestReporter* reporter) + { + m_decoratedReporter = reporter; + } + +} diff --git a/UnitTest++/ThrowingTestReporter.h b/UnitTest++/ThrowingTestReporter.h new file mode 100644 index 0000000..0a02a51 --- /dev/null +++ b/UnitTest++/ThrowingTestReporter.h @@ -0,0 +1,29 @@ +#ifndef UNITTEST_THROWINGTESTREPORTER_H +#define UNITTEST_THROWINGTESTREPORTER_H + +#include "TestReporter.h" + +namespace UnitTest { + + // A TestReporter that throws when ReportFailure is called. Otherwise it + // forwards the calls to a decorated TestReporter + class ThrowingTestReporter : public TestReporter + { + public: + explicit ThrowingTestReporter(TestReporter* reporter); + + virtual ~ThrowingTestReporter(); + virtual void ReportTestStart(TestDetails const& test); + virtual void ReportFailure(TestDetails const& test, char const* failure); + virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); + virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); + + TestReporter* getDecorated() const; + void setDecorated(TestReporter* reporter); + + private: + TestReporter* m_decoratedReporter; + }; +} + +#endif diff --git a/tests/TestRequireMacros.cpp b/tests/TestRequireMacros.cpp index 869de32..46732a2 100644 --- a/tests/TestRequireMacros.cpp +++ b/tests/TestRequireMacros.cpp @@ -117,7 +117,37 @@ TEST(RequireMacroSupportsMultipleChecksWithFailingChecks) CHECK(failure); CHECK(exception); } + +TEST(RequireMacroDoesntExecuteCodeAfterAFailingCheck) +{ + bool failure = false; + bool exception = false; + bool run = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try{ + REQUIRE + { + CHECK(false); + run = true; // this shouldn't get executed. + } + } + catch (const UnitTest::AssertException&) + { + exception = true; + } + + failure = (testResults.GetFailureCount() > 0); + } + CHECK(failure); + CHECK(exception); + CHECK(!run); +} + TEST(FailureReportsCorrectTestName) { RecordingReporter reporter; From 89ff2596ee12aeea98975f17ced70184ab5a0322 Mon Sep 17 00:00:00 2001 From: Austin Gilbert Date: Sun, 17 May 2015 21:21:10 -0500 Subject: [PATCH 04/11] Eliminating 'unused exception variable' warnings. --- UnitTest++/CheckMacros.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index bc9fbcf..96b98ba 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -41,7 +41,7 @@ if (!UnitTest::Check(value)) \ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ }) \ - UT_CATCH (UnitTest::AssertException, e, \ + UT_CATCH (UnitTest::AssertException, , \ { \ UT_THROW(); \ }) \ @@ -65,7 +65,7 @@ ({ \ UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, e, \ + UT_CATCH (UnitTest::AssertException, , \ { \ UT_THROW(); \ }) \ @@ -89,7 +89,7 @@ ({ \ UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, e, \ + UT_CATCH (UnitTest::AssertException, , \ { \ UT_THROW(); \ }) \ @@ -113,7 +113,7 @@ ({ \ UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, e, \ + UT_CATCH (UnitTest::AssertException, , \ { \ UT_THROW(); \ }) \ @@ -137,7 +137,7 @@ ({ \ UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, e, \ + UT_CATCH (UnitTest::AssertException, , \ { \ UT_THROW(); \ }) \ @@ -161,7 +161,7 @@ ({ \ UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, e, \ + UT_CATCH (UnitTest::AssertException, , \ { \ UT_THROW(); \ }) \ From 74ca0c301f84abd7833dc9c927261e05ee04b276 Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Thu, 4 Feb 2016 21:16:45 -0600 Subject: [PATCH 05/11] Merge and uncrustify. --- UnitTest++/CheckMacros.h | 54 +- UnitTest++/RequireMacros.h | 4 +- UnitTest++/RequiredCheckTestReporter.cpp | 38 +- UnitTest++/RequiredCheckTestReporter.h | 34 +- UnitTest++/TestResults.h | 6 +- UnitTest++/ThrowingTestReporter.cpp | 86 +- UnitTest++/ThrowingTestReporter.h | 32 +- tests/TestRequireMacros.cpp | 1538 +++++++++++----------- 8 files changed, 885 insertions(+), 907 deletions(-) diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index 96b98ba..82a4042 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -41,10 +41,10 @@ if (!UnitTest::Check(value)) \ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ - { \ - UT_THROW(); \ - }) \ + UT_CATCH (UnitTest::AssertException, , \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -64,11 +64,11 @@ UT_TRY \ ({ \ UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (UnitTest::AssertException, , \ - { \ - UT_THROW(); \ - }) \ + }) \ + UT_CATCH (UnitTest::AssertException, , \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -89,10 +89,10 @@ ({ \ UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ - { \ - UT_THROW(); \ - }) \ + UT_CATCH (UnitTest::AssertException, , \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -112,11 +112,11 @@ UT_TRY \ ({ \ UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (UnitTest::AssertException, , \ - { \ - UT_THROW(); \ - }) \ + }) \ + UT_CATCH (UnitTest::AssertException, , \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -137,10 +137,10 @@ ({ \ UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ - { \ - UT_THROW(); \ - }) \ + UT_CATCH (UnitTest::AssertException, , \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -160,11 +160,11 @@ UT_TRY \ ({ \ UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ - }) \ - UT_CATCH (UnitTest::AssertException, , \ - { \ - UT_THROW(); \ - }) \ + }) \ + UT_CATCH (UnitTest::AssertException, , \ + { \ + UT_THROW(); \ + }) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ diff --git a/UnitTest++/RequireMacros.h b/UnitTest++/RequireMacros.h index 9fee8eb..4a107d4 100644 --- a/UnitTest++/RequireMacros.h +++ b/UnitTest++/RequireMacros.h @@ -8,11 +8,11 @@ #endif #ifndef UNITTEST_NO_EXCEPTIONS - #define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(UnitTest::CurrentTest::Results()); decoratedReporter.next(); ) +#define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(UnitTest::CurrentTest::Results()); decoratedReporter.next(); ) #endif #ifdef UNITTEST_NO_EXCEPTIONS - #define REQUIRE +#define REQUIRE #endif #endif \ No newline at end of file diff --git a/UnitTest++/RequiredCheckTestReporter.cpp b/UnitTest++/RequiredCheckTestReporter.cpp index cae20db..1fd2ec7 100644 --- a/UnitTest++/RequiredCheckTestReporter.cpp +++ b/UnitTest++/RequiredCheckTestReporter.cpp @@ -5,25 +5,25 @@ namespace UnitTest { - RequiredCheckTestReporter::RequiredCheckTestReporter(TestResults* results) - : m_results(results) - , m_throwingReporter(0) - , m_continue(0) - { - if(m_results) - { - m_throwingReporter.setDecorated(m_results->m_testReporter); - m_results->m_testReporter = &m_throwingReporter; - } - } + RequiredCheckTestReporter::RequiredCheckTestReporter(TestResults* results) + : m_results(results) + , m_throwingReporter(0) + , m_continue(0) + { + if(m_results) + { + m_throwingReporter.setDecorated(m_results->m_testReporter); + m_results->m_testReporter = &m_throwingReporter; + } + } - RequiredCheckTestReporter::~RequiredCheckTestReporter() - { - if(m_results) m_results->m_testReporter = m_throwingReporter.getDecorated(); - } + RequiredCheckTestReporter::~RequiredCheckTestReporter() + { + if(m_results) m_results->m_testReporter = m_throwingReporter.getDecorated(); + } - bool RequiredCheckTestReporter::next() - { - return m_continue++ == 0; - } + bool RequiredCheckTestReporter::next() + { + return m_continue++ == 0; + } } \ No newline at end of file diff --git a/UnitTest++/RequiredCheckTestReporter.h b/UnitTest++/RequiredCheckTestReporter.h index 22613e9..a8c635e 100644 --- a/UnitTest++/RequiredCheckTestReporter.h +++ b/UnitTest++/RequiredCheckTestReporter.h @@ -6,23 +6,23 @@ namespace UnitTest { - class TestResults; - - // This RAII class decorates the current TestReporter with - // a version that throws after reporting a failure. - class UNITTEST_LINKAGE RequiredCheckTestReporter - { - public: - explicit RequiredCheckTestReporter(TestResults* results); - ~RequiredCheckTestReporter(); - - bool next(); - - private: - TestResults* m_results; - ThrowingTestReporter m_throwingReporter; - int m_continue; - }; + class TestResults; + + // This RAII class decorates the current TestReporter with + // a version that throws after reporting a failure. + class UNITTEST_LINKAGE RequiredCheckTestReporter + { + public: + explicit RequiredCheckTestReporter(TestResults* results); + ~RequiredCheckTestReporter(); + + bool next(); + + private: + TestResults* m_results; + ThrowingTestReporter m_throwingReporter; + int m_continue; + }; } #endif diff --git a/UnitTest++/TestResults.h b/UnitTest++/TestResults.h index 024ace3..b23418c 100644 --- a/UnitTest++/TestResults.h +++ b/UnitTest++/TestResults.h @@ -5,7 +5,7 @@ namespace UnitTest { -class RequiredCheckTestReporter; + class RequiredCheckTestReporter; class TestReporter; class TestDetails; @@ -23,9 +23,9 @@ class RequiredCheckTestReporter; int GetFailureCount() const; private: - friend class RequiredCheckTestReporter; + friend class RequiredCheckTestReporter; -private: + private: TestReporter* m_testReporter; int m_totalTestCount; int m_failedTestCount; diff --git a/UnitTest++/ThrowingTestReporter.cpp b/UnitTest++/ThrowingTestReporter.cpp index 45ccade..367a8b3 100644 --- a/UnitTest++/ThrowingTestReporter.cpp +++ b/UnitTest++/ThrowingTestReporter.cpp @@ -3,49 +3,47 @@ namespace UnitTest { - ThrowingTestReporter::ThrowingTestReporter(TestReporter* decoratedReporter) - : m_decoratedReporter(decoratedReporter) - { - } - - //virtual - ThrowingTestReporter::~ThrowingTestReporter() - { - } - - //virtual - void ThrowingTestReporter::ReportTestStart(TestDetails const& test) - { - if(m_decoratedReporter) m_decoratedReporter->ReportTestStart(test); - } - - //virtual - void ThrowingTestReporter::ReportFailure(TestDetails const& test, char const* failure) - { - if(m_decoratedReporter) m_decoratedReporter->ReportFailure(test, failure); - throw AssertException(); - } - - //virtual - void ThrowingTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed) - { - if(m_decoratedReporter) m_decoratedReporter->ReportTestFinish(test, secondsElapsed); - } - - //virtual - void ThrowingTestReporter::ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) - { - if(m_decoratedReporter) m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); - } - - TestReporter* ThrowingTestReporter::getDecorated() const - { - return m_decoratedReporter; - } - - void ThrowingTestReporter::setDecorated(TestReporter* reporter) - { - m_decoratedReporter = reporter; - } + ThrowingTestReporter::ThrowingTestReporter(TestReporter* decoratedReporter) + : m_decoratedReporter(decoratedReporter) + {} + + //virtual + ThrowingTestReporter::~ThrowingTestReporter() + {} + + //virtual + void ThrowingTestReporter::ReportTestStart(TestDetails const& test) + { + if(m_decoratedReporter) m_decoratedReporter->ReportTestStart(test); + } + + //virtual + void ThrowingTestReporter::ReportFailure(TestDetails const& test, char const* failure) + { + if(m_decoratedReporter) m_decoratedReporter->ReportFailure(test, failure); + throw AssertException(); + } + + //virtual + void ThrowingTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed) + { + if(m_decoratedReporter) m_decoratedReporter->ReportTestFinish(test, secondsElapsed); + } + + //virtual + void ThrowingTestReporter::ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) + { + if(m_decoratedReporter) m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); + } + + TestReporter* ThrowingTestReporter::getDecorated() const + { + return m_decoratedReporter; + } + + void ThrowingTestReporter::setDecorated(TestReporter* reporter) + { + m_decoratedReporter = reporter; + } } diff --git a/UnitTest++/ThrowingTestReporter.h b/UnitTest++/ThrowingTestReporter.h index 0a02a51..8ed91da 100644 --- a/UnitTest++/ThrowingTestReporter.h +++ b/UnitTest++/ThrowingTestReporter.h @@ -5,25 +5,25 @@ namespace UnitTest { - // A TestReporter that throws when ReportFailure is called. Otherwise it - // forwards the calls to a decorated TestReporter - class ThrowingTestReporter : public TestReporter - { - public: - explicit ThrowingTestReporter(TestReporter* reporter); + // A TestReporter that throws when ReportFailure is called. Otherwise it + // forwards the calls to a decorated TestReporter + class ThrowingTestReporter : public TestReporter + { + public: + explicit ThrowingTestReporter(TestReporter* reporter); - virtual ~ThrowingTestReporter(); - virtual void ReportTestStart(TestDetails const& test); - virtual void ReportFailure(TestDetails const& test, char const* failure); - virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); - virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); + virtual ~ThrowingTestReporter(); + virtual void ReportTestStart(TestDetails const& test); + virtual void ReportFailure(TestDetails const& test, char const* failure); + virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); + virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); - TestReporter* getDecorated() const; - void setDecorated(TestReporter* reporter); + TestReporter* getDecorated() const; + void setDecorated(TestReporter* reporter); - private: - TestReporter* m_decoratedReporter; - }; + private: + TestReporter* m_decoratedReporter; + }; } #endif diff --git a/tests/TestRequireMacros.cpp b/tests/TestRequireMacros.cpp index 46732a2..88cb128 100644 --- a/tests/TestRequireMacros.cpp +++ b/tests/TestRequireMacros.cpp @@ -9,865 +9,845 @@ using namespace std; namespace { -TEST(RequireCheckSucceedsOnTrue) -{ - bool failure = true; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - - ScopedCurrentTest scopedResults(testResults); - - try - { + TEST(RequireCheckSucceedsOnTrue) + { + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK(true); - } - catch(const UnitTest::AssertException&) - { + } + catch(const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); - CHECK(!exception); -} - -TEST(RequiredCheckFailsOnFalse) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try - { + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); + } + + TEST(RequiredCheckFailsOnFalse) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK(false); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } + } - CHECK(failure); - CHECK(exception); -} + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); + } + + + TEST(RequireMacroSupportsMultipleChecks) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); - -TEST(RequireMacroSupportsMultipleChecks) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try{ + try{ REQUIRE { - CHECK(true); - CHECK_EQUAL(1,1); + CHECK(true); + CHECK_EQUAL(1,1); } - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); - CHECK(!exception); -} + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); + } -TEST(RequireMacroSupportsMultipleChecksWithFailingChecks) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try{ + TEST(RequireMacroSupportsMultipleChecksWithFailingChecks) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try{ REQUIRE { - CHECK(true); - CHECK_EQUAL(1,2); + CHECK(true); + CHECK_EQUAL(1,2); } - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); - CHECK(exception); -} - -TEST(RequireMacroDoesntExecuteCodeAfterAFailingCheck) -{ - bool failure = false; - bool exception = false; - bool run = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try{ + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); + } + + TEST(RequireMacroDoesntExecuteCodeAfterAFailingCheck) + { + bool failure = false; + bool exception = false; + bool run = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try{ REQUIRE { - CHECK(false); - run = true; // this shouldn't get executed. + CHECK(false); + run = true; // this shouldn't get executed. } - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); - CHECK(exception); - CHECK(!run); -} + } -TEST(FailureReportsCorrectTestName) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try - { - REQUIRE CHECK(false); - } - catch (const UnitTest::AssertException&) - { - } - } + failure = (testResults.GetFailureCount() > 0); + } - CHECK_EQUAL(m_details.testName, reporter.lastFailedTest); -} + CHECK(failure); + CHECK(exception); + CHECK(!run); + } -TEST(RequiredCheckFailureIncludesCheckContents) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - const bool yaddayadda = false; - - try - { - REQUIRE CHECK(yaddayadda); - } - catch (const UnitTest::AssertException&) - { - } - } + TEST(FailureReportsCorrectTestName) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); - CHECK(strstr(reporter.lastFailedMessage, "yaddayadda")); -} - -TEST(RequiredCheckEqualSucceedsOnEqual) -{ - bool failure = true; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try - { + try + { + REQUIRE CHECK(false); + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK_EQUAL(m_details.testName, reporter.lastFailedTest); + } + + TEST(RequiredCheckFailureIncludesCheckContents) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + const bool yaddayadda = false; + + try + { + REQUIRE CHECK(yaddayadda); + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK(strstr(reporter.lastFailedMessage, "yaddayadda")); + } + + TEST(RequiredCheckEqualSucceedsOnEqual) + { + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_EQUAL(1,1); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); - CHECK(!exception); -} - -TEST(RequiredCheckEqualFailsOnNotEqual) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try - { + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); + } + + TEST(RequiredCheckEqualFailsOnNotEqual) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_EQUAL(1, 2); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); - CHECK(exception); -} - -TEST(RequiredCheckEqualFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - try - { + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); + } + + TEST(RequiredCheckEqualFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + try + { line = __LINE__; REQUIRE CHECK_EQUAL(1, 123); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK_EQUAL("testName", reporter.lastFailedTest); - CHECK_EQUAL("suiteName", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -int g_sideEffect = 0; -int FunctionWithSideEffects() -{ - ++g_sideEffect; - return 1; -} - -TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK_EQUAL("testName", reporter.lastFailedTest); + CHECK_EQUAL("suiteName", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + int g_sideEffect = 0; + int FunctionWithSideEffects() + { + ++g_sideEffect; + return 1; + } + + TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_EQUAL(1, FunctionWithSideEffects()); - } - catch (const UnitTest::AssertException&) - { - } - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(RequiredCheckEqualDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_EQUAL(2, FunctionWithSideEffects()); - } - catch (const UnitTest::AssertException&) - { - } - } - CHECK_EQUAL(1, g_sideEffect); -} - - -TEST(RequiredCheckCloseSucceedsOnEqual) -{ - bool failure = true; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + CHECK_EQUAL(1, g_sideEffect); + } + + + TEST(RequiredCheckCloseSucceedsOnEqual) + { + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_CLOSE(1.0f, 1.001f, 0.01f); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(!failure); - CHECK(!exception); -} - -TEST(RequiredCheckCloseFailsOnNotEqual) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - try - { + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); + } + + TEST(RequiredCheckCloseFailsOnNotEqual) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_CLOSE (1.0f, 1.1f, 0.01f); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } - - CHECK(failure); - CHECK(exception); -} - -TEST(RequiredCheckCloseFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails testDetails("test", "suite", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - try - { + } + + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(failure); + CHECK(exception); + } + + TEST(RequiredCheckCloseFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("test", "suite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + try + { line = __LINE__; REQUIRE CHECK_CLOSE(1.0f, 1.1f, 0.01f); CHECK(false); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK_EQUAL("test", reporter.lastFailedTest); - CHECK_EQUAL("suite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK_EQUAL("test", reporter.lastFailedTest); + CHECK_EQUAL("suite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f); - } - catch (const UnitTest::AssertException&) - { - } - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(RequiredCheckCloseDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + try + { REQUIRE CHECK_CLOSE(2, FunctionWithSideEffects(), 0.1f); - } - catch (const UnitTest::AssertException&) - { - } - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(RequiredCheckArrayCloseSucceedsOnEqual) -{ - bool failure = true; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - const float data[4] = { 0, 1, 2, 3 }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(RequiredCheckArrayCloseSucceedsOnEqual) + { + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + const float data[4] = { 0, 1, 2, 3 }; + + try + { REQUIRE CHECK_ARRAY_CLOSE (data, data, 4, 0.01f); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } + } - CHECK(!failure); - CHECK(!exception); -} + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); + } -TEST(RequiredCheckArrayCloseFailsOnNotEqual) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - - try - { + TEST(RequiredCheckArrayCloseFailsOnNotEqual) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } + } - failure = (testResults.GetFailureCount() > 0); - } + failure = (testResults.GetFailureCount() > 0); + } - CHECK(failure); - CHECK(exception); -} + CHECK(failure); + CHECK(exception); + } -TEST(RequiredCheckArrayCloseFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - - try - { - REQUIRE CHECK_ARRAY_CLOSE(data1, data2, 4, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); - CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); -} + TEST(RequiredCheckArrayCloseFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); -TEST(RequiredCheckArrayCloseFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - - try - { - line = __LINE__; REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; -TEST(RequiredCheckArrayCloseFailureIncludesTolerance) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - float const data1[4] = { 0, 1, 2, 3 }; - float const data2[4] = { 0, 1, 3, 3 }; - - try - { + try + { + REQUIRE CHECK_ARRAY_CLOSE(data1, data2, 4, 0.01f); + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); + } + + TEST(RequiredCheckArrayCloseFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + line = __LINE__; REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST(RequiredCheckArrayCloseFailureIncludesTolerance) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + float const data1[4] = { 0, 1, 2, 3 }; + float const data2[4] = { 0, 1, 3, 3 }; + + try + { REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK(strstr(reporter.lastFailedMessage, "0.01")); -} - -TEST(RequiredCheckArrayEqualSuceedsOnEqual) -{ - bool failure = true; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - const float data[4] = { 0, 1, 2, 3 }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK(strstr(reporter.lastFailedMessage, "0.01")); + } + + TEST(RequiredCheckArrayEqualSuceedsOnEqual) + { + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + const float data[4] = { 0, 1, 2, 3 }; + + try + { REQUIRE CHECK_ARRAY_EQUAL (data, data, 4); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } + } - CHECK(!failure); - CHECK(!exception); -} + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); + } + + TEST(RequiredCheckArrayEqualFailsOnNotEqual) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); -TEST(RequiredCheckArrayEqualFailsOnNotEqual) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - - try - { + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } + } - CHECK(failure); - CHECK(exception); -} + failure = (testResults.GetFailureCount() > 0); + } -TEST(RequiredCheckArrayEqualFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - - try - { - REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); - CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); -} + CHECK(failure); + CHECK(exception); + } -TEST(RequiredCheckArrayEqualFailureContainsCorrectInfo) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[4] = { 0, 1, 2, 3 }; - int const data2[4] = { 0, 1, 3, 3 }; - - try - { - line = __LINE__; REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK_EQUAL("RequiredCheckArrayEqualFailureContainsCorrectInfo", reporter.lastFailedTest); - CHECK_EQUAL(__FILE__, reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} + TEST(RequiredCheckArrayEqualFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); -float const* FunctionWithSideEffects2() -{ - ++g_sideEffect; - static float const data[] = {1,2,3,4}; - return data; -} + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; -TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[] = { 0, 1, 2, 3 }; - - try - { + try + { + REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ 0 1 2 3 ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ 0 1 3 3 ]")); + } + + TEST(RequiredCheckArrayEqualFailureContainsCorrectInfo) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[4] = { 0, 1, 2, 3 }; + int const data2[4] = { 0, 1, 3, 3 }; + + try + { + line = __LINE__; REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK_EQUAL("RequiredCheckArrayEqualFailureContainsCorrectInfo", reporter.lastFailedTest); + CHECK_EQUAL(__FILE__, reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + float const* FunctionWithSideEffects2() + { + ++g_sideEffect; + static float const data[] = {1,2,3,4}; + return data; + } + + TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 2, 3 }; + + try + { REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[] = { 0, 1, 3, 3 }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[] = { 0, 1, 3, 3 }; + + try + { REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(RequiredCheckArray2DCloseSucceedsOnEqual) -{ - bool failure = true; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - const float data[2][2] = { {0, 1}, {2, 3} }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(RequiredCheckArray2DCloseSucceedsOnEqual) + { + bool failure = true; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {2, 3} }; + + try + { REQUIRE CHECK_ARRAY2D_CLOSE(data, data, 2, 2, 0.01f); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } - - failure = (testResults.GetFailureCount() > 0); - } + } - CHECK(!failure); - CHECK(!exception); -} + failure = (testResults.GetFailureCount() > 0); + } + + CHECK(!failure); + CHECK(!exception); + } -TEST(RequiredCheckArray2DCloseFailsOnNotEqual) -{ - bool failure = false; - bool exception = false; - { - RecordingReporter reporter; - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - int const data1[2][2] = { {0, 1}, {2, 3} }; - int const data2[2][2] = { {0, 1}, {3, 3} }; - - try - { + TEST(RequiredCheckArray2DCloseFailsOnNotEqual) + { + bool failure = false; + bool exception = false; + { + RecordingReporter reporter; + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + + try + { REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); - } - catch (const UnitTest::AssertException&) - { + } + catch (const UnitTest::AssertException&) + { exception = true; - } + } - failure = (testResults.GetFailureCount() > 0); - } + failure = (testResults.GetFailureCount() > 0); + } - CHECK(failure); - CHECK(exception); -} + CHECK(failure); + CHECK(exception); + } -TEST(RequiredCheckArray2DCloseFailureIncludesCheckExpectedAndActual) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); + TEST(RequiredCheckArray2DCloseFailureIncludesCheckExpectedAndActual) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); - int const data1[2][2] = { {0, 1}, {2, 3} }; - int const data2[2][2] = { {0, 1}, {3, 3} }; + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; - try - { + try + { REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK(strstr(reporter.lastFailedMessage, "xpected [ [ 0 1 ] [ 2 3 ] ]")); - CHECK(strstr(reporter.lastFailedMessage, "was [ [ 0 1 ] [ 3 3 ] ]")); -} - -TEST(RequiredCheckArray2DCloseFailureContainsCorrectDetails) -{ - int line = 0; - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - UnitTest::TestDetails testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); - ScopedCurrentTest scopedResults(testResults, &testDetails); - - int const data1[2][2] = { {0, 1}, {2, 3} }; - int const data2[2][2] = { {0, 1}, {3, 3} }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK(strstr(reporter.lastFailedMessage, "xpected [ [ 0 1 ] [ 2 3 ] ]")); + CHECK(strstr(reporter.lastFailedMessage, "was [ [ 0 1 ] [ 3 3 ] ]")); + } + + TEST(RequiredCheckArray2DCloseFailureContainsCorrectDetails) + { + int line = 0; + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + UnitTest::TestDetails testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1); + ScopedCurrentTest scopedResults(testResults, &testDetails); + + int const data1[2][2] = { {0, 1}, {2, 3} }; + int const data2[2][2] = { {0, 1}, {3, 3} }; + + try + { line = __LINE__; REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); - CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); - CHECK_EQUAL("filename", reporter.lastFailedFile); - CHECK_EQUAL(line, reporter.lastFailedLine); -} - -TEST(RequiredCheckArray2DCloseFailureIncludesTolerance) -{ - RecordingReporter reporter; - { - UnitTest::TestResults testResults(&reporter); - ScopedCurrentTest scopedResults(testResults); - - float const data1[2][2] = { {0, 1}, {2, 3} }; - float const data2[2][2] = { {0, 1}, {3, 3} }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest); + CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite); + CHECK_EQUAL("filename", reporter.lastFailedFile); + CHECK_EQUAL(line, reporter.lastFailedLine); + } + + TEST(RequiredCheckArray2DCloseFailureIncludesTolerance) + { + RecordingReporter reporter; + { + UnitTest::TestResults testResults(&reporter); + ScopedCurrentTest scopedResults(testResults); + + float const data1[2][2] = { {0, 1}, {2, 3} }; + float const data2[2][2] = { {0, 1}, {3, 3} }; + + try + { REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - - CHECK(strstr(reporter.lastFailedMessage, "0.01")); -} - -float const* const* FunctionWithSideEffects3() -{ - ++g_sideEffect; - static float const data1[] = {0,1}; - static float const data2[] = {2,3}; - static const float* const data[] = {data1, data2}; - return data; -} - -TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenPassing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[2][2] = { {0, 1}, {2, 3} }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + + CHECK(strstr(reporter.lastFailedMessage, "0.01")); + } + + float const* const* FunctionWithSideEffects3() + { + ++g_sideEffect; + static float const data1[] = {0,1}; + static float const data2[] = {2,3}; + static const float* const data[] = {data1, data2}; + return data; + } + + TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenPassing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {2, 3} }; + + try + { REQUIRE CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - CHECK_EQUAL(1, g_sideEffect); -} - -TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenFailing) -{ - g_sideEffect = 0; - { - UnitTest::TestResults testResults; - ScopedCurrentTest scopedResults(testResults); - - const float data[2][2] = { {0, 1}, {3, 3} }; - - try - { + } + catch (const UnitTest::AssertException&) + {} + } + CHECK_EQUAL(1, g_sideEffect); + } + + TEST(RequiredCheckArray2DCloseDoesNotHaveSideEffectsWhenFailing) + { + g_sideEffect = 0; + { + UnitTest::TestResults testResults; + ScopedCurrentTest scopedResults(testResults); + + const float data[2][2] = { {0, 1}, {3, 3} }; + + try + { REQUIRE CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); - } - catch (const UnitTest::AssertException&) - { - } - } - CHECK_EQUAL(1, g_sideEffect); -} + } + catch (const UnitTest::AssertException&) + {} + } + CHECK_EQUAL(1, g_sideEffect); + } } From 40b8f0b17b0a9db2af01529e8e9a6db56830a471 Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Thu, 4 Feb 2016 22:33:37 -0600 Subject: [PATCH 06/11] Use new RequiredCheckException for REQUIRE Rather than re-using AssertException, it felt more correct to create a new special-purpose exception. --- UnitTest++/CheckMacros.h | 13 ++--- UnitTest++/RequiredCheckException.cpp | 17 +++++++ UnitTest++/RequiredCheckException.h | 23 +++++++++ UnitTest++/ThrowingTestReporter.cpp | 4 +- tests/TestRequireMacros.cpp | 70 +++++++++++++-------------- 5 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 UnitTest++/RequiredCheckException.cpp create mode 100644 UnitTest++/RequiredCheckException.h diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index 82a4042..9547c32 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -5,6 +5,7 @@ #include "ExceptionMacros.h" #include "Checks.h" #include "AssertException.h" +#include "RequiredCheckException.h" #include "MemoryOutStream.h" #include "TestDetails.h" #include "CurrentTest.h" @@ -41,7 +42,7 @@ if (!UnitTest::Check(value)) \ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ + UT_CATCH (UnitTest::RequiredCheckException, , \ { \ UT_THROW(); \ }) \ @@ -65,7 +66,7 @@ ({ \ UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ + UT_CATCH (UnitTest::RequiredCheckException, , \ { \ UT_THROW(); \ }) \ @@ -89,7 +90,7 @@ ({ \ UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ + UT_CATCH (UnitTest::RequiredCheckException, , \ { \ UT_THROW(); \ }) \ @@ -113,7 +114,7 @@ ({ \ UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ + UT_CATCH (UnitTest::RequiredCheckException, , \ { \ UT_THROW(); \ }) \ @@ -137,7 +138,7 @@ ({ \ UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ + UT_CATCH (UnitTest::RequiredCheckException, , \ { \ UT_THROW(); \ }) \ @@ -161,7 +162,7 @@ ({ \ UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::AssertException, , \ + UT_CATCH (UnitTest::RequiredCheckException, , \ { \ UT_THROW(); \ }) \ diff --git a/UnitTest++/RequiredCheckException.cpp b/UnitTest++/RequiredCheckException.cpp new file mode 100644 index 0000000..874370e --- /dev/null +++ b/UnitTest++/RequiredCheckException.cpp @@ -0,0 +1,17 @@ +#include "RequiredCheckException.h" + +#ifndef UNITTEST_NO_EXCEPTIONS + +namespace UnitTest { + + RequiredCheckException::RequiredCheckException() + { + } + + RequiredCheckException::~RequiredCheckException() throw() + { + } + +} + +#endif diff --git a/UnitTest++/RequiredCheckException.h b/UnitTest++/RequiredCheckException.h new file mode 100644 index 0000000..c9a36da --- /dev/null +++ b/UnitTest++/RequiredCheckException.h @@ -0,0 +1,23 @@ +#ifndef UNITTEST_REQUIREDCHECKEXCEPTION_H +#define UNITTEST_REQUIREDCHECKEXCEPTION_H + +#include "Config.h" +#ifndef UNITTEST_NO_EXCEPTIONS + +#include "HelperMacros.h" +#include + +namespace UnitTest { + + class UNITTEST_LINKAGE RequiredCheckException : public std::exception + { + public: + RequiredCheckException(); + virtual ~RequiredCheckException() throw(); + }; + +} + +#endif + +#endif diff --git a/UnitTest++/ThrowingTestReporter.cpp b/UnitTest++/ThrowingTestReporter.cpp index 367a8b3..f48e308 100644 --- a/UnitTest++/ThrowingTestReporter.cpp +++ b/UnitTest++/ThrowingTestReporter.cpp @@ -1,5 +1,5 @@ #include "ThrowingTestReporter.h" -#include "AssertException.h" +#include "RequiredCheckException.h" namespace UnitTest { @@ -21,7 +21,7 @@ namespace UnitTest { void ThrowingTestReporter::ReportFailure(TestDetails const& test, char const* failure) { if(m_decoratedReporter) m_decoratedReporter->ReportFailure(test, failure); - throw AssertException(); + throw RequiredCheckException(); } //virtual diff --git a/tests/TestRequireMacros.cpp b/tests/TestRequireMacros.cpp index 88cb128..6b76004 100644 --- a/tests/TestRequireMacros.cpp +++ b/tests/TestRequireMacros.cpp @@ -23,7 +23,7 @@ namespace { { REQUIRE CHECK(true); } - catch(const UnitTest::AssertException&) + catch(const UnitTest::RequiredCheckException&) { exception = true; } @@ -48,7 +48,7 @@ namespace { { REQUIRE CHECK(false); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -77,7 +77,7 @@ namespace { CHECK_EQUAL(1,1); } } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -106,7 +106,7 @@ namespace { CHECK_EQUAL(1,2); } } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -135,7 +135,7 @@ namespace { run = true; // this shouldn't get executed. } } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -159,7 +159,7 @@ namespace { { REQUIRE CHECK(false); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -178,7 +178,7 @@ namespace { { REQUIRE CHECK(yaddayadda); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -198,7 +198,7 @@ namespace { { REQUIRE CHECK_EQUAL(1,1); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -223,7 +223,7 @@ namespace { { REQUIRE CHECK_EQUAL(1, 2); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -248,7 +248,7 @@ namespace { { line = __LINE__; REQUIRE CHECK_EQUAL(1, 123); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -276,7 +276,7 @@ namespace { { REQUIRE CHECK_EQUAL(1, FunctionWithSideEffects()); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } CHECK_EQUAL(1, g_sideEffect); @@ -293,7 +293,7 @@ namespace { { REQUIRE CHECK_EQUAL(2, FunctionWithSideEffects()); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } CHECK_EQUAL(1, g_sideEffect); @@ -313,7 +313,7 @@ namespace { { REQUIRE CHECK_CLOSE(1.0f, 1.001f, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -338,7 +338,7 @@ namespace { { REQUIRE CHECK_CLOSE (1.0f, 1.1f, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -364,7 +364,7 @@ namespace { line = __LINE__; REQUIRE CHECK_CLOSE(1.0f, 1.1f, 0.01f); CHECK(false); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -385,7 +385,7 @@ namespace { { REQUIRE CHECK_CLOSE (1, FunctionWithSideEffects(), 0.1f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } CHECK_EQUAL(1, g_sideEffect); @@ -402,7 +402,7 @@ namespace { { REQUIRE CHECK_CLOSE(2, FunctionWithSideEffects(), 0.1f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } CHECK_EQUAL(1, g_sideEffect); @@ -422,7 +422,7 @@ namespace { { REQUIRE CHECK_ARRAY_CLOSE (data, data, 4, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -450,7 +450,7 @@ namespace { { REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -476,7 +476,7 @@ namespace { { REQUIRE CHECK_ARRAY_CLOSE(data1, data2, 4, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -500,7 +500,7 @@ namespace { { line = __LINE__; REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -524,7 +524,7 @@ namespace { { REQUIRE CHECK_ARRAY_CLOSE (data1, data2, 4, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -546,7 +546,7 @@ namespace { { REQUIRE CHECK_ARRAY_EQUAL (data, data, 4); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -574,7 +574,7 @@ namespace { { REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -600,7 +600,7 @@ namespace { { REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -623,7 +623,7 @@ namespace { { line = __LINE__; REQUIRE CHECK_ARRAY_EQUAL (data1, data2, 4); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -652,7 +652,7 @@ namespace { { REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } CHECK_EQUAL(1, g_sideEffect); @@ -671,7 +671,7 @@ namespace { { REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -693,7 +693,7 @@ namespace { { REQUIRE CHECK_ARRAY2D_CLOSE(data, data, 2, 2, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -721,7 +721,7 @@ namespace { { REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) { exception = true; } @@ -747,7 +747,7 @@ namespace { { REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -771,7 +771,7 @@ namespace { { line = __LINE__; REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -795,7 +795,7 @@ namespace { { REQUIRE CHECK_ARRAY2D_CLOSE (data1, data2, 2, 2, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } @@ -824,7 +824,7 @@ namespace { { REQUIRE CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } CHECK_EQUAL(1, g_sideEffect); @@ -843,7 +843,7 @@ namespace { { REQUIRE CHECK_ARRAY2D_CLOSE (data, FunctionWithSideEffects3(), 2, 2, 0.01f); } - catch (const UnitTest::AssertException&) + catch (const UnitTest::RequiredCheckException&) {} } CHECK_EQUAL(1, g_sideEffect); From 57fc32c1143a5e94da910ed6b6b345d2d278a3b1 Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Thu, 4 Feb 2016 22:45:36 -0600 Subject: [PATCH 07/11] Change camelCase methods to PascalCase --- UnitTest++/RequireMacros.h | 4 ++-- UnitTest++/RequiredCheckTestReporter.cpp | 6 +++--- UnitTest++/RequiredCheckTestReporter.h | 2 +- UnitTest++/TestResults.h | 1 - UnitTest++/ThrowingTestReporter.cpp | 4 ++-- UnitTest++/ThrowingTestReporter.h | 4 ++-- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/UnitTest++/RequireMacros.h b/UnitTest++/RequireMacros.h index 4a107d4..f20353a 100644 --- a/UnitTest++/RequireMacros.h +++ b/UnitTest++/RequireMacros.h @@ -8,11 +8,11 @@ #endif #ifndef UNITTEST_NO_EXCEPTIONS -#define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(UnitTest::CurrentTest::Results()); decoratedReporter.next(); ) + #define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(UnitTest::CurrentTest::Results()); decoratedReporter.Next(); ) #endif #ifdef UNITTEST_NO_EXCEPTIONS -#define REQUIRE + #define REQUIRE #endif #endif \ No newline at end of file diff --git a/UnitTest++/RequiredCheckTestReporter.cpp b/UnitTest++/RequiredCheckTestReporter.cpp index 1fd2ec7..689827a 100644 --- a/UnitTest++/RequiredCheckTestReporter.cpp +++ b/UnitTest++/RequiredCheckTestReporter.cpp @@ -12,17 +12,17 @@ namespace UnitTest { { if(m_results) { - m_throwingReporter.setDecorated(m_results->m_testReporter); + m_throwingReporter.SetDecorated(m_results->m_testReporter); m_results->m_testReporter = &m_throwingReporter; } } RequiredCheckTestReporter::~RequiredCheckTestReporter() { - if(m_results) m_results->m_testReporter = m_throwingReporter.getDecorated(); + if(m_results) m_results->m_testReporter = m_throwingReporter.GetDecorated(); } - bool RequiredCheckTestReporter::next() + bool RequiredCheckTestReporter::Next() { return m_continue++ == 0; } diff --git a/UnitTest++/RequiredCheckTestReporter.h b/UnitTest++/RequiredCheckTestReporter.h index a8c635e..220ae9b 100644 --- a/UnitTest++/RequiredCheckTestReporter.h +++ b/UnitTest++/RequiredCheckTestReporter.h @@ -16,7 +16,7 @@ namespace UnitTest { explicit RequiredCheckTestReporter(TestResults* results); ~RequiredCheckTestReporter(); - bool next(); + bool Next(); private: TestResults* m_results; diff --git a/UnitTest++/TestResults.h b/UnitTest++/TestResults.h index b23418c..a4d8e60 100644 --- a/UnitTest++/TestResults.h +++ b/UnitTest++/TestResults.h @@ -25,7 +25,6 @@ namespace UnitTest { private: friend class RequiredCheckTestReporter; - private: TestReporter* m_testReporter; int m_totalTestCount; int m_failedTestCount; diff --git a/UnitTest++/ThrowingTestReporter.cpp b/UnitTest++/ThrowingTestReporter.cpp index f48e308..acdc38c 100644 --- a/UnitTest++/ThrowingTestReporter.cpp +++ b/UnitTest++/ThrowingTestReporter.cpp @@ -36,12 +36,12 @@ namespace UnitTest { if(m_decoratedReporter) m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); } - TestReporter* ThrowingTestReporter::getDecorated() const + TestReporter* ThrowingTestReporter::GetDecorated() const { return m_decoratedReporter; } - void ThrowingTestReporter::setDecorated(TestReporter* reporter) + void ThrowingTestReporter::SetDecorated(TestReporter* reporter) { m_decoratedReporter = reporter; } diff --git a/UnitTest++/ThrowingTestReporter.h b/UnitTest++/ThrowingTestReporter.h index 8ed91da..15766b8 100644 --- a/UnitTest++/ThrowingTestReporter.h +++ b/UnitTest++/ThrowingTestReporter.h @@ -18,8 +18,8 @@ namespace UnitTest { virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); - TestReporter* getDecorated() const; - void setDecorated(TestReporter* reporter); + TestReporter* GetDecorated() const; + void SetDecorated(TestReporter* reporter); private: TestReporter* m_decoratedReporter; From 5eec0a255f366b58df25e7626a3a65fedfdeba41 Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Thu, 4 Feb 2016 23:00:52 -0600 Subject: [PATCH 08/11] Simplify required check reporter interactions Was able to remove ThrowingTestReporter::SetDecorated and ::GetDecorated by changing RequiredCheckTestReporter to accept its TestResults by reference. This simple change removed several if-checks and some functions. --- UnitTest++/RequireMacros.h | 2 +- UnitTest++/RequiredCheckTestReporter.cpp | 13 +++++-------- UnitTest++/RequiredCheckTestReporter.h | 5 +++-- UnitTest++/ThrowingTestReporter.cpp | 10 ---------- UnitTest++/ThrowingTestReporter.h | 3 --- 5 files changed, 9 insertions(+), 24 deletions(-) diff --git a/UnitTest++/RequireMacros.h b/UnitTest++/RequireMacros.h index f20353a..ea1f6b9 100644 --- a/UnitTest++/RequireMacros.h +++ b/UnitTest++/RequireMacros.h @@ -8,7 +8,7 @@ #endif #ifndef UNITTEST_NO_EXCEPTIONS - #define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(UnitTest::CurrentTest::Results()); decoratedReporter.Next(); ) + #define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(*UnitTest::CurrentTest::Results()); decoratedReporter.Next(); ) #endif #ifdef UNITTEST_NO_EXCEPTIONS diff --git a/UnitTest++/RequiredCheckTestReporter.cpp b/UnitTest++/RequiredCheckTestReporter.cpp index 689827a..7c21d20 100644 --- a/UnitTest++/RequiredCheckTestReporter.cpp +++ b/UnitTest++/RequiredCheckTestReporter.cpp @@ -5,21 +5,18 @@ namespace UnitTest { - RequiredCheckTestReporter::RequiredCheckTestReporter(TestResults* results) + RequiredCheckTestReporter::RequiredCheckTestReporter(TestResults& results) : m_results(results) - , m_throwingReporter(0) + , m_originalTestReporter(results.m_testReporter) + , m_throwingReporter(results.m_testReporter) , m_continue(0) { - if(m_results) - { - m_throwingReporter.SetDecorated(m_results->m_testReporter); - m_results->m_testReporter = &m_throwingReporter; - } + m_results.m_testReporter = &m_throwingReporter; } RequiredCheckTestReporter::~RequiredCheckTestReporter() { - if(m_results) m_results->m_testReporter = m_throwingReporter.GetDecorated(); + m_results.m_testReporter = m_originalTestReporter; } bool RequiredCheckTestReporter::Next() diff --git a/UnitTest++/RequiredCheckTestReporter.h b/UnitTest++/RequiredCheckTestReporter.h index 220ae9b..117ae01 100644 --- a/UnitTest++/RequiredCheckTestReporter.h +++ b/UnitTest++/RequiredCheckTestReporter.h @@ -13,13 +13,14 @@ namespace UnitTest { class UNITTEST_LINKAGE RequiredCheckTestReporter { public: - explicit RequiredCheckTestReporter(TestResults* results); + explicit RequiredCheckTestReporter(TestResults& results); ~RequiredCheckTestReporter(); bool Next(); private: - TestResults* m_results; + TestResults& m_results; + TestReporter* m_originalTestReporter; ThrowingTestReporter m_throwingReporter; int m_continue; }; diff --git a/UnitTest++/ThrowingTestReporter.cpp b/UnitTest++/ThrowingTestReporter.cpp index acdc38c..7fb670b 100644 --- a/UnitTest++/ThrowingTestReporter.cpp +++ b/UnitTest++/ThrowingTestReporter.cpp @@ -36,14 +36,4 @@ namespace UnitTest { if(m_decoratedReporter) m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); } - TestReporter* ThrowingTestReporter::GetDecorated() const - { - return m_decoratedReporter; - } - - void ThrowingTestReporter::SetDecorated(TestReporter* reporter) - { - m_decoratedReporter = reporter; - } - } diff --git a/UnitTest++/ThrowingTestReporter.h b/UnitTest++/ThrowingTestReporter.h index 15766b8..34e105e 100644 --- a/UnitTest++/ThrowingTestReporter.h +++ b/UnitTest++/ThrowingTestReporter.h @@ -18,9 +18,6 @@ namespace UnitTest { virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); - TestReporter* GetDecorated() const; - void SetDecorated(TestReporter* reporter); - private: TestReporter* m_decoratedReporter; }; From 3f5095ba13dda9aea740e6085fc8fe37b8ecf81a Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Thu, 4 Feb 2016 23:14:42 -0600 Subject: [PATCH 09/11] Eliminate some single-line if statements. --- UnitTest++/ThrowingTestReporter.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/UnitTest++/ThrowingTestReporter.cpp b/UnitTest++/ThrowingTestReporter.cpp index 7fb670b..672f042 100644 --- a/UnitTest++/ThrowingTestReporter.cpp +++ b/UnitTest++/ThrowingTestReporter.cpp @@ -14,26 +14,38 @@ namespace UnitTest { //virtual void ThrowingTestReporter::ReportTestStart(TestDetails const& test) { - if(m_decoratedReporter) m_decoratedReporter->ReportTestStart(test); + if(m_decoratedReporter) + { + m_decoratedReporter->ReportTestStart(test); + } } //virtual void ThrowingTestReporter::ReportFailure(TestDetails const& test, char const* failure) { - if(m_decoratedReporter) m_decoratedReporter->ReportFailure(test, failure); + if(m_decoratedReporter) + { + m_decoratedReporter->ReportFailure(test, failure); + } throw RequiredCheckException(); } //virtual void ThrowingTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed) { - if(m_decoratedReporter) m_decoratedReporter->ReportTestFinish(test, secondsElapsed); + if(m_decoratedReporter) + { + m_decoratedReporter->ReportTestFinish(test, secondsElapsed); + } } //virtual void ThrowingTestReporter::ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) { - if(m_decoratedReporter) m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); + if(m_decoratedReporter) + { + m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed); + } } } From 6bb9541ae2d82c04018eb4f1152f99b59db23dff Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Sat, 6 Feb 2016 22:04:52 -0600 Subject: [PATCH 10/11] Fix warnings, errors, 2x error reporting in MSVC Visual Studio 2015 complained about calling UT_THROW with zero arguments. Visual Studio 6 complained about calling UT_CATCH with an empty second argument. For these cases, I added UT_RETHROW(ExceptionName). I also added a catch of RequiredCheckException to ExecuteTest to avoid two error messages on each failed REQUIRE check. --- UnitTest++/CheckMacros.h | 40 ++++++++++-------------------------- UnitTest++/ExceptionMacros.h | 2 ++ UnitTest++/ExecuteTest.h | 2 ++ 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/UnitTest++/CheckMacros.h b/UnitTest++/CheckMacros.h index 9547c32..e9dae64 100644 --- a/UnitTest++/CheckMacros.h +++ b/UnitTest++/CheckMacros.h @@ -38,14 +38,11 @@ #define CHECK(value) \ UNITTEST_MULTILINE_MACRO_BEGIN \ UT_TRY \ - ({ \ + ({ \ if (!UnitTest::Check(value)) \ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ }) \ - UT_CATCH (UnitTest::RequiredCheckException, , \ - { \ - UT_THROW(); \ - }) \ + UT_RETHROW (UnitTest::RequiredCheckException) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -63,13 +60,10 @@ #define CHECK_EQUAL(expected, actual) \ UNITTEST_MULTILINE_MACRO_BEGIN \ UT_TRY \ - ({ \ + ({ \ UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::RequiredCheckException, , \ - { \ - UT_THROW(); \ - }) \ + UT_RETHROW (UnitTest::RequiredCheckException) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -78,7 +72,7 @@ message.GetText()); \ }) \ UT_CATCH_ALL \ - ({ \ + ({ \ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \ }) \ @@ -87,13 +81,10 @@ #define CHECK_CLOSE(expected, actual, tolerance) \ UNITTEST_MULTILINE_MACRO_BEGIN \ UT_TRY \ - ({ \ + ({ \ UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::RequiredCheckException, , \ - { \ - UT_THROW(); \ - }) \ + UT_RETHROW (UnitTest::RequiredCheckException) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -102,7 +93,7 @@ message.GetText()); \ }) \ UT_CATCH_ALL \ - ({ \ + ({ \ UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \ }) \ @@ -114,10 +105,7 @@ ({ \ UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::RequiredCheckException, , \ - { \ - UT_THROW(); \ - }) \ + UT_RETHROW (UnitTest::RequiredCheckException) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -138,10 +126,7 @@ ({ \ UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::RequiredCheckException, , \ - { \ - UT_THROW(); \ - }) \ + UT_RETHROW (UnitTest::RequiredCheckException) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ @@ -162,10 +147,7 @@ ({ \ UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ }) \ - UT_CATCH (UnitTest::RequiredCheckException, , \ - { \ - UT_THROW(); \ - }) \ + UT_RETHROW (UnitTest::RequiredCheckException) \ UT_CATCH (std::exception, e, \ { \ UnitTest::MemoryOutStream message; \ diff --git a/UnitTest++/ExceptionMacros.h b/UnitTest++/ExceptionMacros.h index ca8757e..027a5da 100644 --- a/UnitTest++/ExceptionMacros.h +++ b/UnitTest++/ExceptionMacros.h @@ -6,11 +6,13 @@ #ifndef UNITTEST_NO_EXCEPTIONS #define UT_TRY(x) try x #define UT_THROW(x) throw x + #define UT_RETHROW(ExceptionType) catch(ExceptionType&) { throw; } #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody #define UT_CATCH_ALL(CatchBody) catch(...) CatchBody #else #define UT_TRY(x) x #define UT_THROW(x) + #define UT_RETHROW() #define UT_CATCH(ExceptionType, ExceptionName, CatchBody) #define UT_CATCH_ALL(CatchBody) #endif diff --git a/UnitTest++/ExecuteTest.h b/UnitTest++/ExecuteTest.h index c6917fc..8e516db 100644 --- a/UnitTest++/ExecuteTest.h +++ b/UnitTest++/ExecuteTest.h @@ -7,6 +7,7 @@ #include "TestResults.h" #include "MemoryOutStream.h" #include "AssertException.h" +#include "RequiredCheckException.h" #include "CurrentTest.h" #ifdef UNITTEST_NO_EXCEPTIONS @@ -38,6 +39,7 @@ namespace UnitTest { testObject.RunImpl(); }) #endif + UT_CATCH(RequiredCheckException, e, { (void)e; }) UT_CATCH(AssertException, e, { (void)e; }) UT_CATCH(std::exception, e, { From 1c30bcd16b1c69eb7cde4894bc446d47d4268c92 Mon Sep 17 00:00:00 2001 From: Austin Gilbert Date: Mon, 8 Feb 2016 21:54:58 -0600 Subject: [PATCH 11/11] Correcting RequiredCheckArrayCloseDoesNotHaveSideEffectsWhenPassing to exercise the passing case (per the UT name). --- tests/TestRequireMacros.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/TestRequireMacros.cpp b/tests/TestRequireMacros.cpp index 6b76004..f904708 100644 --- a/tests/TestRequireMacros.cpp +++ b/tests/TestRequireMacros.cpp @@ -646,14 +646,9 @@ namespace { UnitTest::TestResults testResults; ScopedCurrentTest scopedResults(testResults); - const float data[] = { 0, 1, 2, 3 }; + const float data[] = { 1, 2, 3, 4 }; - try - { - REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); - } - catch (const UnitTest::RequiredCheckException&) - {} + REQUIRE CHECK_ARRAY_CLOSE (data, FunctionWithSideEffects2(), 4, 0.01f); } CHECK_EQUAL(1, g_sideEffect); }