blob: de8f6fa3d7edd5721673ab4fd5504a60cf45b74e [file] [log] [blame]
Phil Nashd8026002010-11-09 23:24:00 +00001/*
Phil Nashd8026002010-11-09 23:24:00 +00002 * Created by Phil on 09/11/2010.
3 * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
4 *
5 * Distributed under the Boost Software License, Version 1.0. (See accompanying
6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Phil Nashd8026002010-11-09 23:24:00 +00007 */
8
Phil Nash823ea3e2011-04-26 08:32:40 +01009#include "catch.hpp"
Phil Nashd8026002010-11-09 23:24:00 +000010
11#include <string>
Phil Nash95627c42011-01-07 10:22:24 +000012#include <stdexcept>
Phil Nashd8026002010-11-09 23:24:00 +000013
14namespace
15{
Phil Nashbf37e682013-04-23 20:52:49 +010016 inline int thisThrows()
Phil Nashd8026002010-11-09 23:24:00 +000017 {
Martin Hořeňovskýc390c4c2017-01-26 23:13:12 +010018 if( Catch::alwaysTrue() )
19 throw std::domain_error( "expected exception" );
20 return 1;
Phil Nashd8026002010-11-09 23:24:00 +000021 }
22
23 int thisDoesntThrow()
24 {
25 return 0;
26 }
27}
28
Phil Nash7bcb4242017-01-23 17:56:41 +000029TEST_CASE( "When checked exceptions are thrown they can be expected or unexpected", "[!throws]" )
Phil Nashd8026002010-11-09 23:24:00 +000030{
Phil Nasha2d20952010-12-14 09:00:09 +000031 REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
32 REQUIRE_NOTHROW( thisDoesntThrow() );
33 REQUIRE_THROWS( thisThrows() );
Phil Nashd8026002010-11-09 23:24:00 +000034}
35
Phil Nash7bcb4242017-01-23 17:56:41 +000036TEST_CASE( "Expected exceptions that don't throw or unexpected exceptions fail the test", "[.][failing][!throws]" )
Phil Nashd8026002010-11-09 23:24:00 +000037{
38 CHECK_THROWS_AS( thisThrows(), std::string );
39 CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
40 CHECK_NOTHROW( thisThrows() );
41}
42
Phil Nash7bcb4242017-01-23 17:56:41 +000043TEST_CASE( "When unchecked exceptions are thrown directly they are always failures", "[.][failing][!throws]" )
Phil Nashd8026002010-11-09 23:24:00 +000044{
Martin Hořeňovskýc390c4c2017-01-26 23:13:12 +010045 if( Catch::alwaysTrue() )
46 throw std::domain_error( "unexpected exception" );
Phil Nashd8026002010-11-09 23:24:00 +000047}
48
Phil Nash7bcb4242017-01-23 17:56:41 +000049TEST_CASE( "An unchecked exception reports the line of the last assertion", "[.][failing][!throws]" )
Phil Nashd0cc33f2012-11-17 17:22:37 +000050{
51 CHECK( 1 == 1 );
Martin Hořeňovskýc390c4c2017-01-26 23:13:12 +010052 if( Catch::alwaysTrue() )
53 throw std::domain_error( "unexpected exception" );
Phil Nashd0cc33f2012-11-17 17:22:37 +000054}
Phil Nash5a2df712014-05-01 19:07:02 +010055
Phil Nash7bcb4242017-01-23 17:56:41 +000056TEST_CASE( "When unchecked exceptions are thrown from sections they are always failures", "[.][failing][!throws]" )
Phil Nash10ed1e02013-02-19 19:45:09 +000057{
58 SECTION( "section name", "" )
59 {
Martin Hořeňovskýc390c4c2017-01-26 23:13:12 +010060 if( Catch::alwaysTrue() )
61 throw std::domain_error( "unexpected exception" );
Phil Nash10ed1e02013-02-19 19:45:09 +000062 }
63}
Phil Nashd0cc33f2012-11-17 17:22:37 +000064
Phil Nash7bcb4242017-01-23 17:56:41 +000065TEST_CASE( "When unchecked exceptions are thrown from functions they are always failures", "[.][failing][!throws]" )
Phil Nash2666c962013-04-20 21:04:32 +010066{
67 CHECK( thisThrows() == 0 );
Phil Nash2666c962013-04-20 21:04:32 +010068}
69
Phil Nash7bcb4242017-01-23 17:56:41 +000070TEST_CASE( "When unchecked exceptions are thrown during a REQUIRE the test should abort fail", "[.][failing][!throws]" )
Phil Nasha176b932014-04-12 19:07:24 +010071{
72 REQUIRE( thisThrows() == 0 );
73 FAIL( "This should never happen" );
74}
75
Phil Nash6c23a652017-01-24 09:53:04 +000076TEST_CASE( "When unchecked exceptions are thrown during a CHECK the test should continue", "[.][failing][!throws]" )
Phil Nash1bccc032014-04-12 19:20:46 +010077{
Phil Nash7bcb4242017-01-23 17:56:41 +000078 try {
79 CHECK(thisThrows() == 0);
80 }
81 catch(...) {
82 FAIL( "This should never happen" );
83 }
Phil Nash1bccc032014-04-12 19:20:46 +010084}
85
Phil Nash7bcb4242017-01-23 17:56:41 +000086TEST_CASE( "When unchecked exceptions are thrown, but caught, they do not affect the test", "[!throws]" )
Phil Nashd8026002010-11-09 23:24:00 +000087{
88 try
89 {
90 throw std::domain_error( "unexpected exception" );
91 }
92 catch(...)
93 {
94 }
95}
Phil Nash9430a2c2011-04-20 15:40:40 +010096
97class CustomException
98{
99public:
100 CustomException( const std::string& msg )
101 : m_msg( msg )
102 {}
Phil Nashe9173812015-11-04 18:01:28 +0000103
Phil Nash9430a2c2011-04-20 15:40:40 +0100104 std::string getMessage() const
105 {
106 return m_msg;
107 }
Phil Nashe9173812015-11-04 18:01:28 +0000108
Phil Nash9430a2c2011-04-20 15:40:40 +0100109private:
110 std::string m_msg;
111};
112
Phil Nasha49f0882015-11-18 08:39:21 +0000113class CustomStdException : public std::exception
114{
115public:
116 CustomStdException( const std::string& msg )
117 : m_msg( msg )
118 {}
Phil Nashd274fc52015-11-20 17:09:48 +0000119 ~CustomStdException() CATCH_NOEXCEPT {}
Phil Nash722315a2015-12-04 10:20:33 +0000120
Phil Nasha49f0882015-11-18 08:39:21 +0000121 std::string getMessage() const
122 {
123 return m_msg;
124 }
Phil Nash722315a2015-12-04 10:20:33 +0000125
Phil Nasha49f0882015-11-18 08:39:21 +0000126private:
127 std::string m_msg;
128};
129
130
Phil Nash5d7b0542011-04-20 19:09:41 +0100131CATCH_TRANSLATE_EXCEPTION( CustomException& ex )
Phil Nash9430a2c2011-04-20 15:40:40 +0100132{
Phil Nash5d7b0542011-04-20 19:09:41 +0100133 return ex.getMessage();
Phil Nash9430a2c2011-04-20 15:40:40 +0100134}
135
Phil Nasha49f0882015-11-18 08:39:21 +0000136CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex )
137{
138 return ex.getMessage();
139}
140
Phil Nash5d7b0542011-04-20 19:09:41 +0100141CATCH_TRANSLATE_EXCEPTION( double& ex )
142{
143 return Catch::toString( ex );
144}
145
Phil Nash7bcb4242017-01-23 17:56:41 +0000146TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" )
Phil Nash9430a2c2011-04-20 15:40:40 +0100147{
Martin Hořeňovskýc390c4c2017-01-26 23:13:12 +0100148 if( Catch::alwaysTrue() )
149 throw CustomException( "custom exception" );
Phil Nash9430a2c2011-04-20 15:40:40 +0100150}
Phil Nashbf37e682013-04-23 20:52:49 +0100151
Phil Nash7bcb4242017-01-23 17:56:41 +0000152TEST_CASE("Custom std-exceptions can be custom translated", "[.][failing][!throws]" )
Phil Nasha49f0882015-11-18 08:39:21 +0000153{
154 if( Catch::alwaysTrue() )
155 throw CustomException( "custom std exception" );
156}
157
Phil Nashbf37e682013-04-23 20:52:49 +0100158inline void throwCustom() {
Martin Hořeňovskýc390c4c2017-01-26 23:13:12 +0100159 if( Catch::alwaysTrue() )
160 throw CustomException( "custom exception - not std" );
Phil Nashbf37e682013-04-23 20:52:49 +0100161}
Phil Nash5d7b0542011-04-20 19:09:41 +0100162
Phil Nash7bcb4242017-01-23 17:56:41 +0000163TEST_CASE( "Custom exceptions can be translated when testing for nothrow", "[.][failing][!throws]" )
Phil Nash5d7b0542011-04-20 19:09:41 +0100164{
Phil Nashbf37e682013-04-23 20:52:49 +0100165 REQUIRE_NOTHROW( throwCustom() );
Phil Nash5d7b0542011-04-20 19:09:41 +0100166}
167
Phil Nash7bcb4242017-01-23 17:56:41 +0000168TEST_CASE( "Custom exceptions can be translated when testing for throwing as something else", "[.][failing][!throws]" )
Phil Nash5d7b0542011-04-20 19:09:41 +0100169{
Phil Nashbf37e682013-04-23 20:52:49 +0100170 REQUIRE_THROWS_AS( throwCustom(), std::exception );
Phil Nash5d7b0542011-04-20 19:09:41 +0100171}
172
173
Phil Nash7bcb4242017-01-23 17:56:41 +0000174TEST_CASE( "Unexpected exceptions can be translated", "[.][failing][!throws]" )
Phil Nash5d7b0542011-04-20 19:09:41 +0100175{
Martin Hořeňovskýc390c4c2017-01-26 23:13:12 +0100176 if( Catch::alwaysTrue() )
177 throw double( 3.14 );
Phil Nash5d7b0542011-04-20 19:09:41 +0100178}
Phil Nash02e597c2011-04-21 19:43:55 +0100179
Phil Nashabf27162012-07-05 18:37:58 +0100180inline int thisFunctionNotImplemented( int ) {
181 CATCH_NOT_IMPLEMENTED;
182}
183
Phil Nash7bcb4242017-01-23 17:56:41 +0000184TEST_CASE( "NotImplemented exception", "[!throws]" )
Phil Nashabf27162012-07-05 18:37:58 +0100185{
186 REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) );
187}
Phil Nash85de7432015-07-13 06:34:41 +0100188
Phil Nash7bcb4242017-01-23 17:56:41 +0000189TEST_CASE( "Exception messages can be tested for", "[!throws]" ) {
Phil Nash72868922015-07-15 23:02:25 +0100190 using namespace Catch::Matchers;
Phil Nash2104ca22015-07-13 15:03:04 +0100191 SECTION( "exact match" )
192 REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
193 SECTION( "different case" )
Phil Nash72868922015-07-15 23:02:25 +0100194 REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) );
Phil Nash2104ca22015-07-13 15:03:04 +0100195 SECTION( "wildcarded" ) {
Phil Nash72868922015-07-15 23:02:25 +0100196 REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) );
197 REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) );
198 REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) );
199 REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) );
Phil Nash2104ca22015-07-13 15:03:04 +0100200 }
201}
202
Phil Nash7bcb4242017-01-23 17:56:41 +0000203TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!throws]" ) {
Phil Nash2104ca22015-07-13 15:03:04 +0100204 REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
Phil Nash85de7432015-07-13 06:34:41 +0100205 REQUIRE_THROWS_WITH( thisThrows(), "should fail" );
Phil Nash72868922015-07-15 23:02:25 +0100206 REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
Phil Nash85de7432015-07-13 06:34:41 +0100207}