030-Asn-Require-Check.cpp 2.7 KB

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