1 #include "UnitTest++/UnitTestPP.h" 2 #include "UnitTest++/TestReporter.h" 3 #include "UnitTest++/TimeHelpers.h" 4 #include "ScopedCurrentTest.h" 5 6 using namespace UnitTest; 7 8 namespace { 9 10 TEST(PassingTestHasNoFailures) 11 { 12 class PassingTest : public Test 13 { 14 public: 15 PassingTest() : Test("passing") {} 16 virtual void RunImpl() const 17 { 18 CHECK(true); 19 } 20 }; 21 22 TestResults results; 23 { 24 ScopedCurrentTest scopedResults(results); 25 PassingTest().Run(); 26 } 27 28 CHECK_EQUAL(0, results.GetFailureCount()); 29 } 30 31 32 TEST(FailingTestHasFailures) 33 { 34 class FailingTest : public Test 35 { 36 public: 37 FailingTest() : Test("failing") {} 38 virtual void RunImpl() const 39 { 40 CHECK(false); 41 } 42 }; 43 44 TestResults results; 45 { 46 ScopedCurrentTest scopedResults(results); 47 FailingTest().Run(); 48 } 49 50 CHECK_EQUAL(1, results.GetFailureCount()); 51 } 52 53 #ifndef UNITTEST_NO_EXCEPTIONS 54 TEST(ThrowingTestsAreReportedAsFailures) 55 { 56 class CrashingTest : public Test 57 { 58 public: 59 CrashingTest() : Test("throwing") {} 60 virtual void RunImpl() const 61 { 62 throw "Blah"; 63 } 64 }; 65 66 TestResults results; 67 { 68 ScopedCurrentTest scopedResult(results); 69 CrashingTest().Run(); 70 } 71 72 CHECK_EQUAL(1, results.GetFailureCount()); 73 } 74 75 #if !defined(UNITTEST_MINGW) && !defined(UNITTEST_WIN32) 76 TEST(CrashingTestsAreReportedAsFailures) 77 { 78 class CrashingTest : public Test 79 { 80 public: 81 CrashingTest() : Test("crashing") {} 82 virtual void RunImpl() const 83 { 84 85 reinterpret_cast< void (*)() >(0)(); 86 } 87 }; 88 89 TestResults results; 90 { 91 ScopedCurrentTest scopedResult(results); 92 CrashingTest().Run(); 93 } 94 95 CHECK_EQUAL(1, results.GetFailureCount()); 96 } 97 #endif 98 #endif 99 100 TEST(TestWithUnspecifiedSuiteGetsDefaultSuite) 101 { 102 Test test("test"); 103 CHECK(test.m_details.suiteName != NULL); 104 CHECK_EQUAL("DefaultSuite", test.m_details.suiteName); 105 } 106 107 TEST(TestReflectsSpecifiedSuiteName) 108 { 109 Test test("test", "testSuite"); 110 CHECK(test.m_details.suiteName != NULL); 111 CHECK_EQUAL("testSuite", test.m_details.suiteName); 112 } 113 114 void Fail() 115 { 116 CHECK(false); 117 } 118 119 TEST(OutOfCoreCHECKMacrosCanFailTests) 120 { 121 TestResults results; 122 { 123 ScopedCurrentTest scopedResult(results); 124 Fail(); 125 } 126 127 CHECK_EQUAL(1, results.GetFailureCount()); 128 } 129 130 } 131