Debian Codegraph @

3rdparty/catch2/tests/ExtraTests/X20-AssertionStartingEventGoesBeforeAssertionIsEvaluated.cpp

raw symbols SCIP
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 /**\file
10 * Registers an event listener to increments counter of assertionStarting events.
11 *
12 * Different assertion macros then check that the counter is at expected
13 * value when they are evaluated.
14 */
15
16 #include <catch2/catch_test_macros.hpp>
17 #include <catch2/reporters/catch_reporter_event_listener.hpp>
18 #include <catch2/reporters/catch_reporter_registrars.hpp>
19 #include <catch2/matchers/catch_matchers_predicate.hpp>
20
21 namespace {
22
23 static size_t assertion_starting_events_seen = 0;
24
25 class AssertionStartingListener : public Catch::EventListenerBase {
26 public:
27 AssertionStartingListener( Catch::IConfig const* config ):
28 EventListenerBase( config ) {}
29
30 void assertionStarting( Catch::AssertionInfo const& ) override {
31 ++assertion_starting_events_seen;
32 }
33 };
34
35 static bool f1() {
36 return assertion_starting_events_seen == 1;
37 }
38
39 static void f2() {
40 if ( assertion_starting_events_seen != 2 ) { throw 1; }
41 }
42
43 static void f3() {
44 if ( assertion_starting_events_seen == 3 ) { throw 1; }
45 }
46
47 static bool f4() { return assertion_starting_events_seen == 4; }
48
49 static void f5() { throw assertion_starting_events_seen; }
50
51 } // anonymous namespace
52
53 CATCH_REGISTER_LISTENER( AssertionStartingListener )
54
55 TEST_CASE() {
56 // **IMPORTANT**
57 // The order of assertions below matters.
58 REQUIRE( f1() );
59 REQUIRE_NOTHROW( f2() );
60 REQUIRE_THROWS( f3() );
61 REQUIRE_THAT( f4(),
62 Catch::Matchers::Predicate<bool>( []( bool b ) { return b; } ) );
63 REQUIRE_THROWS_MATCHES(
64 f5(), size_t, Catch::Matchers::Predicate<size_t>( []( size_t i ) {
65 return i == 5;
66 } ) );
67
68 CAPTURE( assertion_starting_events_seen ); // **not** an assertion
69 INFO( "some info msg" ); // **not** an assertion
70 WARN( "warning! warning!" ); // assertion-like message
71 SUCCEED(); // assertion-like message
72
73 // We skip FAIL/SKIP and so on, which fail the test.
74
75 // This require will also increment the count once
76 REQUIRE( assertion_starting_events_seen == 8 );
77 }