-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathX08-StaticAnalysisSupport.cpp
More file actions
102 lines (78 loc) · 2.58 KB
/
Copy pathX08-StaticAnalysisSupport.cpp
File metadata and controls
102 lines (78 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
/**\file
* Test that the macros compile with
* `CATCH_CONFIG_EXPERIMENTAL_STATIC_ANALYSIS_SUPPORT`.
*
* The expansions in this mode are meant to be scanned, not run, and use
* symbols that are left undefined on purpose.
*
* The compilation itself is the test.
*/
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_exception.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <stdexcept>
#include <string>
// Left undefined on purpose. Their results are opaque to the compiler.
int const* make_pointer();
std::string make_string();
namespace {
[[noreturn]]
void throws() {
throw std::runtime_error( "some exception" );
}
void doesnt_throw() {}
} // namespace
TEST_CASE( "REQUIRE and CHECK" ) {
int const* const ptr = make_pointer();
REQUIRE( ptr );
CHECK( *ptr == 42 );
REQUIRE_FALSE( ptr == nullptr );
CHECK_FALSE( ptr == nullptr );
CHECK_NOFAIL( *ptr == 42 );
}
TEST_CASE( "CHECKED_IF and CHECKED_ELSE" ) {
int const* const ptr = make_pointer();
CHECKED_IF( ptr ) {
CHECK( *ptr == 42 );
}
CHECKED_ELSE( ptr ) {
CHECK_FALSE( ptr );
}
}
TEST_CASE( "Exception assertions" ) {
REQUIRE_NOTHROW( doesnt_throw() );
CHECK_NOTHROW( doesnt_throw() );
REQUIRE_THROWS( throws() );
CHECK_THROWS( throws() );
REQUIRE_THROWS_AS( throws(), std::runtime_error );
CHECK_THROWS_AS( throws(), std::runtime_error );
}
// The matcher-based assertions are not modelled for static analysis atm,
// but they still have to compile in this mode.
TEST_CASE( "Matcher assertions" ) {
using namespace Catch::Matchers;
REQUIRE_THAT( make_string(), StartsWith( "some" ) );
CHECK_THAT( make_string(), Equals( "some string" ) );
REQUIRE_THROWS_WITH( throws(), "some exception" );
CHECK_THROWS_WITH( throws(), ContainsSubstring( "exception" ) );
REQUIRE_THROWS_MATCHES(
throws(), std::runtime_error, Message( "some exception" ) );
CHECK_THROWS_MATCHES(
throws(), std::runtime_error, Message( "some exception" ) );
}
TEST_CASE( "Non-assertion macros" ) {
STATIC_REQUIRE( 1 + 1 == 2 );
STATIC_REQUIRE_FALSE( 1 + 1 == 3 );
STATIC_CHECK( 1 + 1 == 2 );
STATIC_CHECK_FALSE( 1 + 1 == 3 );
SUCCEED( "succeeded" );
if ( make_pointer() ) {
FAIL( "failed" );
}
SKIP( "skipped" );
}