1 #include "UnitTest++/Config.h" 2 #include "UnitTest++/UnitTestPP.h" 3 4 #include "UnitTest++/ReportAssert.h" 5 #include "UnitTest++/ReportAssertImpl.h" 6 #include "UnitTest++/AssertException.h" 7 8 #include "RecordingReporter.h" 9 #include <csetjmp> 10 11 using namespace UnitTest; 12 13 namespace { 14 15 TEST(CanSetAssertExpected) 16 { 17 Detail::ExpectAssert(true); 18 CHECK(Detail::AssertExpected()); 19 20 Detail::ExpectAssert(false); 21 CHECK(!Detail::AssertExpected()); 22 } 23 24 #ifndef UNITTEST_NO_EXCEPTIONS 25 26 TEST(ReportAssertThrowsAssertException) 27 { 28 bool caught = false; 29 30 try 31 { 32 TestResults testResults; 33 TestDetails testDetails("", "", "", 0); 34 Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); 35 } 36 catch(AssertException const&) 37 { 38 caught = true; 39 } 40 41 CHECK(true == caught); 42 } 43 44 TEST(ReportAssertClearsExpectAssertFlag) 45 { 46 RecordingReporter reporter; 47 TestResults testResults(&reporter); 48 TestDetails testDetails("", "", "", 0); 49 50 try 51 { 52 Detail::ExpectAssert(true); 53 Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); 54 } 55 catch(AssertException const&) 56 { 57 } 58 59 CHECK(Detail::AssertExpected() == false); 60 CHECK_EQUAL(0, reporter.testFailedCount); 61 } 62 63 TEST(ReportAssertWritesFailureToResultsAndDetailsWhenAssertIsNotExpected) 64 { 65 const int lineNumber = 12345; 66 const char* description = "description"; 67 const char* filename = "filename"; 68 69 RecordingReporter reporter; 70 TestResults testResults(&reporter); 71 TestDetails testDetails("", "", "", 0); 72 73 try 74 { 75 Detail::ReportAssertEx(&testResults, &testDetails, description, filename, lineNumber); 76 } 77 catch(AssertException const&) 78 { 79 } 80 81 CHECK_EQUAL(description, reporter.lastFailedMessage); 82 CHECK_EQUAL(filename, reporter.lastFailedFile); 83 CHECK_EQUAL(lineNumber, reporter.lastFailedLine); 84 } 85 86 TEST(ReportAssertReportsNoErrorsWhenAssertIsExpected) 87 { 88 Detail::ExpectAssert(true); 89 90 RecordingReporter reporter; 91 TestResults testResults(&reporter); 92 TestDetails testDetails("", "", "", 0); 93 94 try 95 { 96 Detail::ReportAssertEx(&testResults, &testDetails, "", "", 0); 97 } 98 catch(AssertException const&) 99 { 100 } 101 102 CHECK_EQUAL(0, reporter.testFailedCount); 103 } 104 105 TEST(CheckAssertMacroSetsAssertExpectationToFalseAfterRunning) 106 { 107 Detail::ExpectAssert(true); 108 CHECK_ASSERT(ReportAssert("", "", 0)); 109 CHECK(!Detail::AssertExpected()); 110 Detail::ExpectAssert(false); 111 } 112 113 #else 114 115 TEST(SetAssertJumpTargetReturnsFalseWhenSettingJumpTarget) 116 { 117 CHECK(UNITTEST_SET_ASSERT_JUMP_TARGET() == false); 118 } 119 120 TEST(JumpToAssertJumpTarget_JumpsToSetPoint_ReturnsTrue) 121 { 122 const volatile bool taken = !!UNITTEST_SET_ASSERT_JUMP_TARGET(); 123 124 volatile bool set = false; 125 if (taken == false) 126 { 127 UNITTEST_JUMP_TO_ASSERT_JUMP_TARGET(); 128 set = true; 129 } 130 131 CHECK(set == false); 132 } 133 134 #endif 135 136 } 137