blob: 8d8eb552bd7857b87844a956041a82d2c96f59f6 [file] [log] [blame]
Phil Nashd8026002010-11-09 23:24:00 +00001/*
2 * ExceptionTests.cpp
3 * Catch - Test
4 *
5 * Created by Phil on 09/11/2010.
6 * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
7 *
8 * Distributed under the Boost Software License, Version 1.0. (See accompanying
9 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 *
11 */
12
Phil Nash823ea3e2011-04-26 08:32:40 +010013#include "catch.hpp"
Phil Nashd8026002010-11-09 23:24:00 +000014
15#include <string>
Phil Nash95627c42011-01-07 10:22:24 +000016#include <stdexcept>
Phil Nashd8026002010-11-09 23:24:00 +000017
Phil Nash7b449f72012-02-15 08:20:06 +000018#include "catch_self_test.hpp"
19
Phil Nashd8026002010-11-09 23:24:00 +000020namespace
21{
Phil Nashd1ee9642011-01-31 20:15:40 +000022 ATTRIBUTE_NORETURN
23 int thisThrows();
24
Phil Nashd8026002010-11-09 23:24:00 +000025 int thisThrows()
26 {
27 throw std::domain_error( "expected exception" );
Phil Nashd1ee9642011-01-31 20:15:40 +000028 /*NOTREACHED*/
Phil Nashd8026002010-11-09 23:24:00 +000029 }
30
31 int thisDoesntThrow()
32 {
33 return 0;
34 }
35}
36
Phil Nash684baf12011-01-14 08:47:43 +000037TEST_CASE( "./succeeding/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
Phil Nashd8026002010-11-09 23:24:00 +000038{
Phil Nasha2d20952010-12-14 09:00:09 +000039 REQUIRE_THROWS_AS( thisThrows(), std::domain_error );
40 REQUIRE_NOTHROW( thisDoesntThrow() );
41 REQUIRE_THROWS( thisThrows() );
Phil Nashd8026002010-11-09 23:24:00 +000042}
43
Phil Nash684baf12011-01-14 08:47:43 +000044TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
Phil Nashd8026002010-11-09 23:24:00 +000045{
46 CHECK_THROWS_AS( thisThrows(), std::string );
47 CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
48 CHECK_NOTHROW( thisThrows() );
49}
50
Phil Nash4890a1d2011-03-14 19:21:35 +000051TEST_CASE_NORETURN( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
Phil Nashd8026002010-11-09 23:24:00 +000052{
53 throw std::domain_error( "unexpected exception" );
Phil Nashd1ee9642011-01-31 20:15:40 +000054 /*NOTREACHED*/
Phil Nashd8026002010-11-09 23:24:00 +000055}
56
Phil Nash684baf12011-01-14 08:47:43 +000057TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" )
Phil Nashd8026002010-11-09 23:24:00 +000058{
59 try
60 {
61 throw std::domain_error( "unexpected exception" );
62 }
63 catch(...)
64 {
65 }
66}
Phil Nash9430a2c2011-04-20 15:40:40 +010067
68class CustomException
69{
70public:
71 CustomException( const std::string& msg )
72 : m_msg( msg )
73 {}
74
75 std::string getMessage() const
76 {
77 return m_msg;
78 }
79
80private:
81 std::string m_msg;
82};
83
Phil Nash5d7b0542011-04-20 19:09:41 +010084CATCH_TRANSLATE_EXCEPTION( CustomException& ex )
Phil Nash9430a2c2011-04-20 15:40:40 +010085{
Phil Nash5d7b0542011-04-20 19:09:41 +010086 return ex.getMessage();
Phil Nash9430a2c2011-04-20 15:40:40 +010087}
88
Phil Nash5d7b0542011-04-20 19:09:41 +010089CATCH_TRANSLATE_EXCEPTION( double& ex )
90{
91 return Catch::toString( ex );
92}
93
94TEST_CASE_NORETURN( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" )
Phil Nash9430a2c2011-04-20 15:40:40 +010095{
96 throw CustomException( "custom exception" );
97}
Phil Nash5d7b0542011-04-20 19:09:41 +010098
99TEST_CASE( "./failing/exceptions/custom/nothrow", "Custom exceptions can be translated when testing for nothrow" )
100{
101 REQUIRE_NOTHROW( throw CustomException( "unexpected custom exception" ) );
102}
103
104TEST_CASE( "./failing/exceptions/custom/throw", "Custom exceptions can be translated when testing for throwing as something else" )
105{
106 REQUIRE_THROWS_AS( throw CustomException( "custom exception - not std" ), std::exception );
107}
108
109
110TEST_CASE_NORETURN( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" )
111{
112 throw double( 3.14 );
113}
Phil Nash02e597c2011-04-21 19:43:55 +0100114
Phil Nash5824b562012-06-05 10:38:18 +0100115#pragma GCC diagnostic push
116#pragma GCC diagnostic ignored "-Wunused-variable"
117
Phil Nash02e597c2011-04-21 19:43:55 +0100118TEST_CASE( "./failing/exceptions/in-section", "Exceptions thrown from sections report file/ line or section" )
119{
120 SECTION( "the section", "" )
121 {
Phil Nash7b449f72012-02-15 08:20:06 +0000122 CATCH_REGISTER_LINE_INFO( "the section2" ) SECTION( "the section2", "" )
Phil Nash02e597c2011-04-21 19:43:55 +0100123 {
124 throw std::domain_error( "Exception from section" );
125 }
126 }
127}
Phil Nash7b449f72012-02-15 08:20:06 +0000128
Phil Nash5824b562012-06-05 10:38:18 +0100129#pragma GCC diagnostic pop
130
Phil Nash7b449f72012-02-15 08:20:06 +0000131TEST_CASE( "./succeeding/exceptions/error messages", "The error messages produced by exceptions caught by Catch matched the expected form" )
132{
133 Catch::EmbeddedRunner runner;
Phil Nashb2132022012-05-04 07:55:11 +0100134 using namespace Catch::Matchers;
Phil Nash7b449f72012-02-15 08:20:06 +0000135
136 SECTION( "custom, unexpected", "" )
137 {
138 runner.runMatching( "./failing/exceptions/custom" );
Phil Nashb2132022012-05-04 07:55:11 +0100139// CHECK_THAT( runner.getLog(), Contains( "Unexpected exception" ) ); // Mock reporter doesn't say this
140 CHECK_THAT( runner.getLog(), Contains( "custom exception" ) );
Phil Nash7b449f72012-02-15 08:20:06 +0000141 }
142
143 SECTION( "in section", "" )
144 {
145 runner.runMatching( "./failing/exceptions/in-section" );
Phil Nashb2132022012-05-04 07:55:11 +0100146 INFO( runner.getLog() );
147// CHECK( runner.getLog().find( "Unexpected exception" ) != std::string::npos ); // Mock reporter doesn't say this
148 CHECK_THAT( runner.getLog(), Contains( "Exception from section" ) );
149// CHECK( runner.getLog().find( CATCH_GET_LINE_INFO( "the section2" ) ) != std::string::npos ); // Mock reporter doesn't say this
Phil Nash7b449f72012-02-15 08:20:06 +0000150 }
151
152}
Phil Nashabf27162012-07-05 18:37:58 +0100153
154inline int thisFunctionNotImplemented( int ) {
155 CATCH_NOT_IMPLEMENTED;
156}
157
158TEST_CASE( "./succeeding/exceptions/notimplemented", "" )
159{
160 REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) );
161}