1 // 030-Asn-Require-Check.cpp 2 3 // Catch has two natural expression assertion macro's: 4 // - REQUIRE() stops at first failure. 5 // - CHECK() continues after failure. 6 7 // There are two variants to support decomposing negated expressions: 8 // - REQUIRE_FALSE() stops at first failure. 9 // - CHECK_FALSE() continues after failure. 10 11 // main() provided in 000-CatchMain.cpp 12 13 #include <catch2/catch.hpp> 14 one()15std::string one() { 16 return "1"; 17 } 18 19 TEST_CASE( "Assert that something is true (pass)", "[require]" ) { 20 REQUIRE( one() == "1" ); 21 } 22 23 TEST_CASE( "Assert that something is true (fail)", "[require]" ) { 24 REQUIRE( one() == "x" ); 25 } 26 27 TEST_CASE( "Assert that something is true (stop at first failure)", "[require]" ) { 28 WARN( "REQUIRE stops at first failure:" ); 29 30 REQUIRE( one() == "x" ); 31 REQUIRE( one() == "1" ); 32 } 33 34 TEST_CASE( "Assert that something is true (continue after failure)", "[check]" ) { 35 WARN( "CHECK continues after failure:" ); 36 37 CHECK( one() == "x" ); 38 REQUIRE( one() == "1" ); 39 } 40 41 TEST_CASE( "Assert that something is false (stops at first failure)", "[require-false]" ) { 42 WARN( "REQUIRE_FALSE stops at first failure:" ); 43 44 REQUIRE_FALSE( one() == "1" ); 45 REQUIRE_FALSE( one() != "1" ); 46 } 47 48 TEST_CASE( "Assert that something is false (continue after failure)", "[check-false]" ) { 49 WARN( "CHECK_FALSE continues after failure:" ); 50 51 CHECK_FALSE( one() == "1" ); 52 REQUIRE_FALSE( one() != "1" ); 53 } 54 55 // Compile & run: 56 // - g++ -std=c++11 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 030-Asn-Require-Check 030-Asn-Require-Check.cpp 000-CatchMain.o && 030-Asn-Require-Check --success 57 // - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 030-Asn-Require-Check.cpp 000-CatchMain.obj && 030-Asn-Require-Check --success 58 59 // Expected compact output (all assertions): 60 // 61 // prompt> 030-Asn-Require-Check.exe --reporter compact --success 62 // 030-Asn-Require-Check.cpp:20: passed: one() == "1" for: "1" == "1" 63 // 030-Asn-Require-Check.cpp:24: failed: one() == "x" for: "1" == "x" 64 // 030-Asn-Require-Check.cpp:28: warning: 'REQUIRE stops at first failure:' 65 // 030-Asn-Require-Check.cpp:30: failed: one() == "x" for: "1" == "x" 66 // 030-Asn-Require-Check.cpp:35: warning: 'CHECK continues after failure:' 67 // 030-Asn-Require-Check.cpp:37: failed: one() == "x" for: "1" == "x" 68 // 030-Asn-Require-Check.cpp:38: passed: one() == "1" for: "1" == "1" 69 // 030-Asn-Require-Check.cpp:42: warning: 'REQUIRE_FALSE stops at first failure:' 70 // 030-Asn-Require-Check.cpp:44: failed: !(one() == "1") for: !("1" == "1") 71 // 030-Asn-Require-Check.cpp:49: warning: 'CHECK_FALSE continues after failure:' 72 // 030-Asn-Require-Check.cpp:51: failed: !(one() == "1") for: !("1" == "1") 73 // 030-Asn-Require-Check.cpp:52: passed: !(one() != "1") for: !("1" != "1") 74 // Failed 5 test cases, failed 5 assertions. 75