TestTestMacros.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "../UnitTest++.h"
  2. #include "../TestMacros.h"
  3. #include "../TestList.h"
  4. #include "../TestResults.h"
  5. #include "../TestReporter.h"
  6. #include "../ReportAssert.h"
  7. #include "RecordingReporter.h"
  8. #include "ScopedCurrentTest.h"
  9. using namespace UnitTest;
  10. namespace {
  11. TestList list1;
  12. TEST_EX(DummyTest, list1)
  13. {
  14. }
  15. TEST (TestsAreAddedToTheListThroughMacro)
  16. {
  17. CHECK(list1.GetHead() != 0);
  18. CHECK(list1.GetHead()->next == 0);
  19. }
  20. struct ThrowingThingie
  21. {
  22. ThrowingThingie() : dummy(false)
  23. {
  24. if (!dummy)
  25. throw "Oops";
  26. }
  27. bool dummy;
  28. };
  29. TestList list2;
  30. TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2)
  31. {
  32. }
  33. TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture)
  34. {
  35. RecordingReporter reporter;
  36. TestResults result(&reporter);
  37. {
  38. ScopedCurrentTest scopedResults(result);
  39. list2.GetHead()->Run();
  40. }
  41. CHECK(strstr(reporter.lastFailedMessage, "xception"));
  42. CHECK(strstr(reporter.lastFailedMessage, "fixture"));
  43. CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie"));
  44. }
  45. struct DummyFixture
  46. {
  47. int x;
  48. };
  49. // We're really testing the macros so we just want them to compile and link
  50. SUITE(TestSuite1)
  51. {
  52. TEST(SimilarlyNamedTestsInDifferentSuitesWork)
  53. {
  54. }
  55. TEST_FIXTURE(DummyFixture, SimilarlyNamedFixtureTestsInDifferentSuitesWork)
  56. {
  57. }
  58. }
  59. SUITE(TestSuite2)
  60. {
  61. TEST(SimilarlyNamedTestsInDifferentSuitesWork)
  62. {
  63. }
  64. TEST_FIXTURE(DummyFixture,SimilarlyNamedFixtureTestsInDifferentSuitesWork)
  65. {
  66. }
  67. }
  68. TestList macroTestList1;
  69. TEST_EX(MacroTestHelper1, macroTestList1)
  70. {
  71. }
  72. TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite)
  73. {
  74. CHECK(macroTestList1.GetHead() != NULL);
  75. CHECK_EQUAL ("MacroTestHelper1", macroTestList1.GetHead()->m_details.testName);
  76. CHECK_EQUAL ("DefaultSuite", macroTestList1.GetHead()->m_details.suiteName);
  77. }
  78. TestList macroTestList2;
  79. TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2)
  80. {
  81. }
  82. TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite)
  83. {
  84. CHECK(macroTestList2.GetHead() != NULL);
  85. CHECK_EQUAL ("MacroTestHelper2", macroTestList2.GetHead()->m_details.testName);
  86. CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName);
  87. }
  88. struct FixtureCtorThrows
  89. {
  90. FixtureCtorThrows() { throw "exception"; }
  91. };
  92. TestList throwingFixtureTestList1;
  93. TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1)
  94. {
  95. }
  96. TEST(FixturesWithThrowingCtorsAreFailures)
  97. {
  98. CHECK(throwingFixtureTestList1.GetHead() != NULL);
  99. RecordingReporter reporter;
  100. TestResults result(&reporter);
  101. {
  102. ScopedCurrentTest scopedResult(result);
  103. throwingFixtureTestList1.GetHead()->Run();
  104. }
  105. int const failureCount = result.GetFailedTestCount();
  106. CHECK_EQUAL(1, failureCount);
  107. CHECK(strstr(reporter.lastFailedMessage, "while constructing fixture"));
  108. }
  109. struct FixtureDtorThrows
  110. {
  111. ~FixtureDtorThrows() { throw "exception"; }
  112. };
  113. TestList throwingFixtureTestList2;
  114. TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2)
  115. {
  116. }
  117. TEST(FixturesWithThrowingDtorsAreFailures)
  118. {
  119. CHECK(throwingFixtureTestList2.GetHead() != NULL);
  120. RecordingReporter reporter;
  121. TestResults result(&reporter);
  122. {
  123. ScopedCurrentTest scopedResult(result);
  124. throwingFixtureTestList2.GetHead()->Run();
  125. }
  126. int const failureCount = result.GetFailedTestCount();
  127. CHECK_EQUAL(1, failureCount);
  128. CHECK(strstr(reporter.lastFailedMessage, "while destroying fixture"));
  129. }
  130. const int FailingLine = 123;
  131. struct FixtureCtorAsserts
  132. {
  133. FixtureCtorAsserts()
  134. {
  135. UnitTest::ReportAssert("assert failure", "file", FailingLine);
  136. }
  137. };
  138. TestList ctorAssertFixtureTestList;
  139. TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList)
  140. {
  141. }
  142. TEST(CorrectlyReportsFixturesWithCtorsThatAssert)
  143. {
  144. RecordingReporter reporter;
  145. TestResults result(&reporter);
  146. {
  147. ScopedCurrentTest scopedResults(result);
  148. ctorAssertFixtureTestList.GetHead()->Run();
  149. }
  150. const int failureCount = result.GetFailedTestCount();
  151. CHECK_EQUAL(1, failureCount);
  152. CHECK_EQUAL(FailingLine, reporter.lastFailedLine);
  153. CHECK(strstr(reporter.lastFailedMessage, "assert failure"));
  154. }
  155. }
  156. // We're really testing if it's possible to use the same suite in two files
  157. // to compile and link successfuly (TestTestSuite.cpp has suite with the same name)
  158. // Note: we are outside of the anonymous namespace
  159. SUITE(SameTestSuite)
  160. {
  161. TEST(DummyTest1)
  162. {
  163. }
  164. }
  165. #define CUR_TEST_NAME CurrentTestDetailsContainCurrentTestInfo
  166. #define INNER_STRINGIFY(X) #X
  167. #define STRINGIFY(X) INNER_STRINGIFY(X)
  168. TEST(CUR_TEST_NAME)
  169. {
  170. const UnitTest::TestDetails* details = CurrentTest::Details();
  171. CHECK_EQUAL(STRINGIFY(CUR_TEST_NAME), details->testName);
  172. }
  173. #undef CUR_TEST_NAME
  174. #undef INNER_STRINGIFY
  175. #undef STRINGIFY