catch2/projects/SelfTest/IntrospectiveTests/PartTracker.tests.cpp

357 lines
11 KiB
C++
Raw Normal View History

/*
* Created by Phil on 1/10/2015.
* Copyright 2015 Two Blue Cubes Ltd
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "internal/catch_suppress_warnings.h"
#include "internal/catch_test_case_tracker.h"
namespace Catch
{
2015-10-23 19:44:48 +02:00
class LocalContext {
2015-10-23 19:44:48 +02:00
public:
TrackerContext& operator()() const {
return TrackerContext::instance();
}
};
} // namespace Catch
// -------------------
#include "catch.hpp"
using namespace Catch;
namespace {
Catch::TestCaseTracking::NameAndLocation makeNAL( std::string const& name ) {
return Catch::TestCaseTracking::NameAndLocation( name, Catch::SourceLineInfo("",0) );
}
}
TEST_CASE( "Tracker" ) {
TrackerContext ctx;
ctx.startRun();
ctx.startCycle();
ITracker& testCase = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase.isOpen() );
ITracker& s1 = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s1.isOpen() );
SECTION( "successfully close one section" ) {
s1.close();
2015-10-29 09:17:55 +01:00
REQUIRE( s1.isSuccessfullyCompleted() );
2015-10-29 09:33:50 +01:00
REQUIRE( testCase.isComplete() == false );
testCase.close();
2015-10-29 09:17:55 +01:00
REQUIRE( ctx.completedCycle() );
REQUIRE( testCase.isSuccessfullyCompleted() );
}
SECTION( "fail one section" ) {
s1.fail();
2015-10-29 09:33:50 +01:00
REQUIRE( s1.isComplete() );
2015-10-23 19:44:48 +02:00
REQUIRE( s1.isSuccessfullyCompleted() == false );
2015-10-29 09:33:50 +01:00
REQUIRE( testCase.isComplete() == false );
testCase.close();
2015-10-29 09:17:55 +01:00
REQUIRE( ctx.completedCycle() );
2015-10-23 19:44:48 +02:00
REQUIRE( testCase.isSuccessfullyCompleted() == false );
SECTION( "re-enter after failed section" ) {
ctx.startCycle();
ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase2.isOpen() );
ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
REQUIRE( s1b.isOpen() == false );
testCase2.close();
2015-10-29 09:17:55 +01:00
REQUIRE( ctx.completedCycle() );
2015-10-29 09:33:50 +01:00
REQUIRE( testCase.isComplete() );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase.isSuccessfullyCompleted() );
}
SECTION( "re-enter after failed section and find next section" ) {
ctx.startCycle();
ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase2.isOpen() );
ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s1b.isOpen() == false );
ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
REQUIRE( s2.isOpen() );
2015-10-29 09:17:55 +01:00
s2.close();
2015-10-29 09:17:55 +01:00
REQUIRE( ctx.completedCycle() );
testCase2.close();
2015-10-29 09:33:50 +01:00
REQUIRE( testCase.isComplete() );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase.isSuccessfullyCompleted() );
}
}
SECTION( "successfully close one section, then find another" ) {
s1.close();
ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
REQUIRE( s2.isOpen() == false );
testCase.close();
2015-10-29 09:33:50 +01:00
REQUIRE( testCase.isComplete() == false );
SECTION( "Re-enter - skips S1 and enters S2" ) {
ctx.startCycle();
ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase2.isOpen() );
ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
2015-10-23 19:44:48 +02:00
REQUIRE( s1b.isOpen() == false );
ITracker& s2b = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
REQUIRE( s2b.isOpen() );
REQUIRE( ctx.completedCycle() == false );
SECTION ("Successfully close S2") {
s2b.close();
2015-10-29 09:17:55 +01:00
REQUIRE( ctx.completedCycle() );
2015-10-29 09:17:55 +01:00
REQUIRE( s2b.isSuccessfullyCompleted() );
2015-10-29 09:33:50 +01:00
REQUIRE( testCase2.isComplete() == false );
testCase2.close();
2015-10-29 09:17:55 +01:00
REQUIRE( testCase2.isSuccessfullyCompleted() );
}
SECTION ("fail S2") {
s2b.fail();
2015-10-29 09:17:55 +01:00
REQUIRE( ctx.completedCycle() );
2015-10-29 09:33:50 +01:00
REQUIRE( s2b.isComplete() );
2015-10-23 19:44:48 +02:00
REQUIRE( s2b.isSuccessfullyCompleted() == false );
testCase2.close();
2015-10-23 19:44:48 +02:00
REQUIRE( testCase2.isSuccessfullyCompleted() == false );
2015-10-29 09:17:55 +01:00
// Need a final cycle
ctx.startCycle();
ITracker& testCase3 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase3.isOpen() );
ITracker& s1c = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s1c.isOpen() == false );
ITracker& s2c = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s2c.isOpen() == false );
2015-10-29 09:17:55 +01:00
testCase3.close();
REQUIRE( testCase3.isSuccessfullyCompleted() );
}
}
}
SECTION( "open a nested section" ) {
ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s2.isOpen() );
s2.close();
REQUIRE( s2.isComplete() );
REQUIRE( s1.isComplete() == false );
s1.close();
REQUIRE( s1.isComplete() );
REQUIRE( testCase.isComplete() == false );
testCase.close();
REQUIRE( testCase.isComplete() );
}
SECTION( "start a generator" ) {
IndexTracker& g1 = IndexTracker::acquire( ctx, makeNAL( "G1" ), 2 );
2015-10-29 09:17:55 +01:00
REQUIRE( g1.isOpen() );
REQUIRE( g1.index() == 0 );
REQUIRE( g1.isComplete() == false );
REQUIRE( s1.isComplete() == false );
SECTION( "close outer section" )
{
s1.close();
REQUIRE( s1.isComplete() == false );
testCase.close();
REQUIRE( testCase.isSuccessfullyCompleted() == false );
SECTION( "Re-enter for second generation" ) {
ctx.startCycle();
ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase2.isOpen() );
ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s1b.isOpen() );
IndexTracker& g1b = IndexTracker::acquire( ctx, makeNAL( "G1" ), 2 );
2015-10-29 09:17:55 +01:00
REQUIRE( g1b.isOpen() );
REQUIRE( g1b.index() == 1 );
REQUIRE( s1.isComplete() == false );
s1b.close();
REQUIRE( s1b.isComplete() );
REQUIRE( g1b.isComplete() );
testCase2.close();
REQUIRE( testCase2.isComplete() );
}
}
SECTION( "Start a new inner section" ) {
ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s2.isOpen() );
s2.close();
REQUIRE( s2.isComplete() );
s1.close();
REQUIRE( s1.isComplete() == false );
testCase.close();
REQUIRE( testCase.isComplete() == false );
SECTION( "Re-enter for second generation" ) {
ctx.startCycle();
ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( testCase2.isOpen() );
ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s1b.isOpen() );
// generator - next value
IndexTracker& g1b = IndexTracker::acquire( ctx, makeNAL( "G1" ), 2 );
2015-10-29 09:17:55 +01:00
REQUIRE( g1b.isOpen() );
REQUIRE( g1b.index() == 1 );
// inner section again
ITracker& s2b = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
2015-10-29 09:17:55 +01:00
REQUIRE( s2b.isOpen() );
s2b.close();
REQUIRE( s2b.isComplete() );
s1b.close();
REQUIRE( g1b.isComplete() );
REQUIRE( s1b.isComplete() );
testCase2.close();
REQUIRE( testCase2.isComplete() );
}
}
SECTION( "Fail an inner section" ) {
ITracker& s2 = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
REQUIRE( s2.isOpen() );
s2.fail();
REQUIRE( s2.isComplete() );
REQUIRE( s2.isSuccessfullyCompleted() == false );
s1.close();
REQUIRE( s1.isComplete() == false );
testCase.close();
REQUIRE( testCase.isComplete() == false );
SECTION( "Re-enter for second generation" ) {
ctx.startCycle();
ITracker& testCase2 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
REQUIRE( testCase2.isOpen() );
ITracker& s1b = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
REQUIRE( s1b.isOpen() );
// generator - still same value
IndexTracker& g1b = IndexTracker::acquire( ctx, makeNAL( "G1" ), 2 );
REQUIRE( g1b.isOpen() );
REQUIRE( g1b.index() == 0 );
// inner section again - this time won't open
ITracker& s2b = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
REQUIRE( s2b.isOpen() == false );
s1b.close();
REQUIRE( g1b.isComplete() == false );
REQUIRE( s1b.isComplete() == false );
testCase2.close();
REQUIRE( testCase2.isComplete() == false );
// Another cycle - now should complete
ctx.startCycle();
ITracker& testCase3 = SectionTracker::acquire( ctx, makeNAL( "Testcase" ) );
REQUIRE( testCase3.isOpen() );
ITracker& s1c = SectionTracker::acquire( ctx, makeNAL( "S1" ) );
REQUIRE( s1c.isOpen() );
// generator - now next value
IndexTracker& g1c = IndexTracker::acquire( ctx, makeNAL( "G1" ), 2 );
REQUIRE( g1c.isOpen() );
REQUIRE( g1c.index() == 1 );
// inner section - now should open again
ITracker& s2c = SectionTracker::acquire( ctx, makeNAL( "S2" ) );
REQUIRE( s2c.isOpen() );
s2c.close();
REQUIRE( s2c.isComplete() );
s1c.close();
REQUIRE( g1c.isComplete() );
REQUIRE( s1c.isComplete() );
testCase3.close();
REQUIRE( testCase3.isComplete() );
}
}
// !TBD"
// nested generator
// two sections within a generator
}
}
static bool previouslyRun = false;
static bool previouslyRunNested = false;
TEST_CASE( "#1394", "[.][approvals][tracker]" ) {
// -- Don't re-run after specified section is done
REQUIRE(previouslyRun == false);
SECTION( "RunSection" ) {
previouslyRun = true;
}
SECTION( "SkipSection" ) {
// cause an error if this section is called because it shouldn't be
REQUIRE(1 == 0);
}
}
TEST_CASE( "#1394 nested", "[.][approvals][tracker]" ) {
REQUIRE(previouslyRunNested == false);
SECTION( "NestedRunSection" ) {
SECTION( "s1" ) {
previouslyRunNested = true;
}
}
SECTION( "NestedSkipSection" ) {
// cause an error if this section is called because it shouldn't be
REQUIRE(1 == 0);
}
}