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