PartTracker.tests.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/generators/catch_generators.hpp>
  8. #include <catch2/internal/catch_test_case_tracker.hpp>
  9. using namespace Catch;
  10. namespace {
  11. Catch::TestCaseTracking::NameAndLocation makeNAL( std::string const& name ) {
  12. return Catch::TestCaseTracking::NameAndLocation( name, Catch::SourceLineInfo("",0) );
  13. }
  14. }
  15. TEST_CASE( "Tracker" ) {
  16. TrackerContext ctx;
  17. ctx.startRun();
  18. ctx.startCycle();
  19. ITracker& testCase = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
  20. REQUIRE( testCase.isOpen() );
  21. ITracker& s1 = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
  22. REQUIRE( s1.isOpen() );
  23. SECTION( "successfully close one section" ) {
  24. s1.close();
  25. REQUIRE( s1.isSuccessfullyCompleted() );
  26. REQUIRE( testCase.isComplete() == false );
  27. testCase.close();
  28. REQUIRE( ctx.completedCycle() );
  29. REQUIRE( testCase.isSuccessfullyCompleted() );
  30. }
  31. SECTION( "fail one section" ) {
  32. s1.fail();
  33. REQUIRE( s1.isComplete() );
  34. REQUIRE( s1.isSuccessfullyCompleted() == false );
  35. REQUIRE( testCase.isComplete() == false );
  36. testCase.close();
  37. REQUIRE( ctx.completedCycle() );
  38. REQUIRE( testCase.isSuccessfullyCompleted() == false );
  39. SECTION( "re-enter after failed section" ) {
  40. ctx.startCycle();
  41. ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
  42. REQUIRE( testCase2.isOpen() );
  43. ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
  44. REQUIRE( s1b.isOpen() == false );
  45. testCase2.close();
  46. REQUIRE( ctx.completedCycle() );
  47. REQUIRE( testCase.isComplete() );
  48. REQUIRE( testCase.isSuccessfullyCompleted() );
  49. }
  50. SECTION( "re-enter after failed section and find next section" ) {
  51. ctx.startCycle();
  52. ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
  53. REQUIRE( testCase2.isOpen() );
  54. ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
  55. REQUIRE( s1b.isOpen() == false );
  56. ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
  57. REQUIRE( s2.isOpen() );
  58. s2.close();
  59. REQUIRE( ctx.completedCycle() );
  60. testCase2.close();
  61. REQUIRE( testCase.isComplete() );
  62. REQUIRE( testCase.isSuccessfullyCompleted() );
  63. }
  64. }
  65. SECTION( "successfully close one section, then find another" ) {
  66. s1.close();
  67. ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
  68. REQUIRE( s2.isOpen() == false );
  69. testCase.close();
  70. REQUIRE( testCase.isComplete() == false );
  71. SECTION( "Re-enter - skips S1 and enters S2" ) {
  72. ctx.startCycle();
  73. ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
  74. REQUIRE( testCase2.isOpen() );
  75. ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
  76. REQUIRE( s1b.isOpen() == false );
  77. ITracker& s2b = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
  78. REQUIRE( s2b.isOpen() );
  79. REQUIRE( ctx.completedCycle() == false );
  80. SECTION ("Successfully close S2") {
  81. s2b.close();
  82. REQUIRE( ctx.completedCycle() );
  83. REQUIRE( s2b.isSuccessfullyCompleted() );
  84. REQUIRE( testCase2.isComplete() == false );
  85. testCase2.close();
  86. REQUIRE( testCase2.isSuccessfullyCompleted() );
  87. }
  88. SECTION ("fail S2") {
  89. s2b.fail();
  90. REQUIRE( ctx.completedCycle() );
  91. REQUIRE( s2b.isComplete() );
  92. REQUIRE( s2b.isSuccessfullyCompleted() == false );
  93. testCase2.close();
  94. REQUIRE( testCase2.isSuccessfullyCompleted() == false );
  95. // Need a final cycle
  96. ctx.startCycle();
  97. ITracker& testCase3 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
  98. REQUIRE( testCase3.isOpen() );
  99. ITracker& s1c = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
  100. REQUIRE( s1c.isOpen() == false );
  101. ITracker& s2c = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
  102. REQUIRE( s2c.isOpen() == false );
  103. testCase3.close();
  104. REQUIRE( testCase3.isSuccessfullyCompleted() );
  105. }
  106. }
  107. }
  108. SECTION( "open a nested section" ) {
  109. ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
  110. REQUIRE( s2.isOpen() );
  111. s2.close();
  112. REQUIRE( s2.isComplete() );
  113. REQUIRE( s1.isComplete() == false );
  114. s1.close();
  115. REQUIRE( s1.isComplete() );
  116. REQUIRE( testCase.isComplete() == false );
  117. testCase.close();
  118. REQUIRE( testCase.isComplete() );
  119. }
  120. }
  121. static bool previouslyRun = false;
  122. static bool previouslyRunNested = false;
  123. TEST_CASE( "#1394", "[.][approvals][tracker]" ) {
  124. // -- Don't re-run after specified section is done
  125. REQUIRE(previouslyRun == false);
  126. SECTION( "RunSection" ) {
  127. previouslyRun = true;
  128. }
  129. SECTION( "SkipSection" ) {
  130. // cause an error if this section is called because it shouldn't be
  131. REQUIRE(1 == 0);
  132. }
  133. }
  134. TEST_CASE( "#1394 nested", "[.][approvals][tracker]" ) {
  135. REQUIRE(previouslyRunNested == false);
  136. SECTION( "NestedRunSection" ) {
  137. SECTION( "s1" ) {
  138. previouslyRunNested = true;
  139. }
  140. }
  141. SECTION( "NestedSkipSection" ) {
  142. // cause an error if this section is called because it shouldn't be
  143. REQUIRE(1 == 0);
  144. }
  145. }
  146. // Selecting a "not last" section inside a test case via -c "section" would
  147. // previously only run the first subsection, instead of running all of them.
  148. // This allows us to check that `"#1670 regression check" -c A` leads to
  149. // 2 successful assertions.
  150. TEST_CASE("#1670 regression check", "[.approvals][tracker]") {
  151. SECTION("A") {
  152. SECTION("1") SUCCEED();
  153. SECTION("2") SUCCEED();
  154. }
  155. SECTION("B") {
  156. SECTION("1") SUCCEED();
  157. SECTION("2") SUCCEED();
  158. }
  159. }
  160. // #1938 required a rework on how generator tracking works, so that `GENERATE`
  161. // supports being sandwiched between two `SECTION`s. The following tests check
  162. // various other scenarios through checking output in approval tests.
  163. TEST_CASE("#1938 - GENERATE after a section", "[.][regression][generators]") {
  164. SECTION("A") {
  165. SUCCEED("A");
  166. }
  167. auto m = GENERATE(1, 2, 3);
  168. SECTION("B") {
  169. REQUIRE(m);
  170. }
  171. }
  172. TEST_CASE("#1938 - flat generate", "[.][regression][generators]") {
  173. auto m = GENERATE(1, 2, 3);
  174. REQUIRE(m);
  175. }
  176. TEST_CASE("#1938 - nested generate", "[.][regression][generators]") {
  177. auto m = GENERATE(1, 2, 3);
  178. auto n = GENERATE(1, 2, 3);
  179. REQUIRE(m);
  180. REQUIRE(n);
  181. }
  182. TEST_CASE("#1938 - mixed sections and generates", "[.][regression][generators]") {
  183. auto i = GENERATE(1, 2);
  184. SECTION("A") {
  185. SUCCEED("A");
  186. }
  187. auto j = GENERATE(3, 4);
  188. SECTION("B") {
  189. SUCCEED("B");
  190. }
  191. auto k = GENERATE(5, 6);
  192. CAPTURE(i, j, k);
  193. SUCCEED();
  194. }
  195. TEST_CASE("#1938 - Section followed by flat generate", "[.][regression][generators]") {
  196. SECTION("A") {
  197. REQUIRE(1);
  198. }
  199. auto m = GENERATE(2, 3);
  200. REQUIRE(m);
  201. }