summaryrefslogtreecommitdiff
path: root/projects/SelfTest/UsageTests/Exception.tests.cpp
blob: b13b93b0939884fa5135819cc8f842cb284680d4 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 *  Created by Phil on 09/11/2010.
 *  Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
 *
 *  Distributed under the Boost Software License, Version 1.0. (See accompanying
 *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 */

#include "catch.hpp"

#include <string>
#include <stdexcept>

#ifdef _MSC_VER
#pragma warning(disable:4702) // Unreachable code -- unconditional throws and so on
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wweak-vtables"
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#pragma clang diagnostic ignored "-Wunreachable-code"
#endif

#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
// Cannot use try/catch keywords with -fno-exceptions.
// Even if an exception was to be "thrown" it would just call std::terminate instead.

namespace { namespace ExceptionTests {

#ifndef EXCEPTION_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
#define EXCEPTION_TEST_HELPERS_INCLUDED

int thisThrows() {
    throw std::domain_error( "expected exception" );
    return 1;
}

int thisDoesntThrow() {
    return 0;
}

class CustomException {
public:
    explicit CustomException( const std::string& msg )
    : m_msg( msg )
    {}

    std::string getMessage() const {
        return m_msg;
    }

private:
    std::string m_msg;
};

class CustomStdException : public std::exception {
public:
    explicit CustomStdException( const std::string& msg )
    : m_msg( msg )
    {}
    ~CustomStdException() noexcept override {}

    std::string getMessage() const {
        return m_msg;
    }

private:
    std::string m_msg;
};

[[noreturn]] void throwCustom() {
    throw CustomException( "custom exception - not std" );
}

#endif

TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" ) {
    REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
    REQUIRE_NOTHROW( thisDoesntThrow() );
    REQUIRE_THROWS( thisThrows() );
}

TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" ) {
    CHECK_THROWS_AS( thisThrows(), std::string );
    CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
    CHECK_NOTHROW( thisThrows() );
}

TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" ) {
    throw std::domain_error( "unexpected exception" );
}

TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" ) {
    CHECK( 1 == 1 );
    throw std::domain_error( "unexpected exception" );
}

TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" ) {
    SECTION( "section name" ) {
        throw std::domain_error("unexpected exception");
    }
}

TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" ) {
    CHECK( thisThrows() == 0 );
}

TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" ) {
    REQUIRE( thisThrows() == 0 );
    FAIL( "This should never happen" );
}

TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" ) {
    try {
        CHECK(thisThrows() == 0);
    }
    catch(...) {
        FAIL( "This should never happen" );
    }
}

TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" ) {
    try {
        throw std::domain_error( "unexpected exception" );
    }
    catch(...) {}
}


CATCH_TRANSLATE_EXCEPTION( CustomException& ex ) {
    return ex.getMessage();
}

CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex ) {
    return ex.getMessage();
}

CATCH_TRANSLATE_EXCEPTION( double& ex ) {
    return Catch::Detail::stringify( ex );
}

TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) {
    throw CustomException( "custom exception" );
}

TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" ) {
    throw CustomException( "custom std exception" );
}

TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" ) {
    REQUIRE_NOTHROW( throwCustom() );
}

TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" ) {
    REQUIRE_THROWS_AS( throwCustom(), std::exception );
}

TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]"  ) {
    throw double( 3.14 );
}

TEST_CASE("Thrown string literals are translated", "[.][failing][!throws]") {
    throw "For some reason someone is throwing a string literal!";
}

TEST_CASE("thrown std::strings are translated", "[.][failing][!throws]") {
    throw std::string{ "Why would you throw a std::string?" };
}


#ifndef CATCH_CONFIG_DISABLE_MATCHERS

TEST_CASE( "Exception messages can be tested for", "[!throws]" ) {
    using namespace Catch::Matchers;
    SECTION( "exact match" )
        REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
    SECTION( "different case" )
    REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) );
    SECTION( "wildcarded" ) {
        REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) );
        REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) );
        REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) );
        REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) );
    }
}

#endif

TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) {
    REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
    REQUIRE_THROWS_WITH( thisThrows(), "should fail" );
    REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
}

TEST_CASE( "#748 - captures with unexpected exceptions", "[.][failing][!throws][!shouldfail]" ) {
    int answer = 42;
    CAPTURE( answer );
    // the message should be printed on the first two sections but not on the third
    SECTION( "outside assertions" ) {
        thisThrows();
    }
    SECTION( "inside REQUIRE_NOTHROW" ) {
        REQUIRE_NOTHROW( thisThrows() );
    }
    SECTION( "inside REQUIRE_THROWS" ) {
        REQUIRE_THROWS( thisThrows() );
    }
}

}} // namespace ExceptionTests

#endif // CATCH_CONFIG_USE_EXCEPTIONS

#ifdef __clang__
#pragma clang diagnostic pop
#endif