|
| 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 | + * Test that the macros compile with |
| 11 | + * `CATCH_CONFIG_EXPERIMENTAL_STATIC_ANALYSIS_SUPPORT`. |
| 12 | + * |
| 13 | + * The expansions in this mode are meant to be scanned, not run, and use |
| 14 | + * symbols that are left undefined on purpose. |
| 15 | + * |
| 16 | + * The compilation itself is the test. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <catch2/catch_test_macros.hpp> |
| 20 | +#include <catch2/matchers/catch_matchers_exception.hpp> |
| 21 | +#include <catch2/matchers/catch_matchers_string.hpp> |
| 22 | + |
| 23 | +#include <stdexcept> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +// Left undefined on purpose. Their results are opaque to the compiler. |
| 27 | +int const* make_pointer(); |
| 28 | +std::string make_string(); |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | + [[noreturn]] |
| 33 | + void throws() { |
| 34 | + throw std::runtime_error( "some exception" ); |
| 35 | + } |
| 36 | + void doesnt_throw() {} |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +TEST_CASE( "REQUIRE and CHECK" ) { |
| 41 | + int const* const ptr = make_pointer(); |
| 42 | + |
| 43 | + REQUIRE( ptr ); |
| 44 | + CHECK( *ptr == 42 ); |
| 45 | + |
| 46 | + REQUIRE_FALSE( ptr == nullptr ); |
| 47 | + CHECK_FALSE( ptr == nullptr ); |
| 48 | + CHECK_NOFAIL( *ptr == 42 ); |
| 49 | +} |
| 50 | + |
| 51 | +TEST_CASE( "CHECKED_IF and CHECKED_ELSE" ) { |
| 52 | + int const* const ptr = make_pointer(); |
| 53 | + |
| 54 | + CHECKED_IF( ptr ) { |
| 55 | + CHECK( *ptr == 42 ); |
| 56 | + } |
| 57 | + CHECKED_ELSE( ptr ) { |
| 58 | + CHECK_FALSE( ptr ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +TEST_CASE( "Exception assertions" ) { |
| 63 | + REQUIRE_NOTHROW( doesnt_throw() ); |
| 64 | + CHECK_NOTHROW( doesnt_throw() ); |
| 65 | + |
| 66 | + REQUIRE_THROWS( throws() ); |
| 67 | + CHECK_THROWS( throws() ); |
| 68 | + |
| 69 | + REQUIRE_THROWS_AS( throws(), std::runtime_error ); |
| 70 | + CHECK_THROWS_AS( throws(), std::runtime_error ); |
| 71 | +} |
| 72 | + |
| 73 | +// The matcher-based assertions are not modelled for static analysis atm, |
| 74 | +// but they still have to compile in this mode. |
| 75 | +TEST_CASE( "Matcher assertions" ) { |
| 76 | + using namespace Catch::Matchers; |
| 77 | + |
| 78 | + REQUIRE_THAT( make_string(), StartsWith( "some" ) ); |
| 79 | + CHECK_THAT( make_string(), Equals( "some string" ) ); |
| 80 | + |
| 81 | + REQUIRE_THROWS_WITH( throws(), "some exception" ); |
| 82 | + CHECK_THROWS_WITH( throws(), ContainsSubstring( "exception" ) ); |
| 83 | + |
| 84 | + REQUIRE_THROWS_MATCHES( |
| 85 | + throws(), std::runtime_error, Message( "some exception" ) ); |
| 86 | + CHECK_THROWS_MATCHES( |
| 87 | + throws(), std::runtime_error, Message( "some exception" ) ); |
| 88 | +} |
| 89 | + |
| 90 | +TEST_CASE( "Non-assertion macros" ) { |
| 91 | + STATIC_REQUIRE( 1 + 1 == 2 ); |
| 92 | + STATIC_REQUIRE_FALSE( 1 + 1 == 3 ); |
| 93 | + STATIC_CHECK( 1 + 1 == 2 ); |
| 94 | + STATIC_CHECK_FALSE( 1 + 1 == 3 ); |
| 95 | + |
| 96 | + SUCCEED( "succeeded" ); |
| 97 | + |
| 98 | + if ( make_pointer() ) { |
| 99 | + FAIL( "failed" ); |
| 100 | + } |
| 101 | + SKIP( "skipped" ); |
| 102 | +} |
0 commit comments