blob: 5787c6bed94b213d314a26d928030f4cedaa2247 [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 Nashcda21492012-08-16 18:47:41 +01009#ifdef __clang__
Phil Nasha695eb92012-08-13 07:46:10 +010010#pragma clang diagnostic ignored "-Wpadded"
Phil Nashcda21492012-08-16 18:47:41 +010011#endif
Phil Nasha695eb92012-08-13 07:46:10 +010012
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 Nasha695eb92012-08-13 07:46:10 +010022 CATCH_ATTRIBUTE_NORETURN
Phil Nashd1ee9642011-01-31 20:15:40 +000023 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 Nasha695eb92012-08-13 07:46:10 +010044CATCH_ATTRIBUTE_NORETURN
Phil Nash684baf12011-01-14 08:47:43 +000045TEST_CASE( "./failing/exceptions/explicit", "When checked exceptions are thrown they can be expected or unexpected" )
Phil Nashd8026002010-11-09 23:24:00 +000046{
47 CHECK_THROWS_AS( thisThrows(), std::string );
48 CHECK_THROWS_AS( thisDoesntThrow(), std::domain_error );
49 CHECK_NOTHROW( thisThrows() );
50}
51
Phil Nash4890a1d2011-03-14 19:21:35 +000052TEST_CASE_NORETURN( "./failing/exceptions/implicit", "When unchecked exceptions are thrown they are always failures" )
Phil Nashd8026002010-11-09 23:24:00 +000053{
54 throw std::domain_error( "unexpected exception" );
Phil Nashd1ee9642011-01-31 20:15:40 +000055 /*NOTREACHED*/
Phil Nashd8026002010-11-09 23:24:00 +000056}
57
Phil Nashd0cc33f2012-11-17 17:22:37 +000058TEST_CASE_NORETURN( "./failing/exceptions/implicit/2", "An unchecked exception reports the line of the last assertion" )
59{
60 CHECK( 1 == 1 );
61 throw std::domain_error( "unexpected exception" );
62 /*NOTREACHED*/
63}
Phil Nash10ed1e02013-02-19 19:45:09 +000064TEST_CASE( "./failing/exceptions/implicit/3", "When unchecked exceptions are thrown they are always failures" )
65{
66 SECTION( "section name", "" )
67 {
68 throw std::domain_error( "unexpected exception" );
69 /*NOTREACHED*/
70 }
71}
Phil Nashd0cc33f2012-11-17 17:22:37 +000072
Phil Nash684baf12011-01-14 08:47:43 +000073TEST_CASE( "./succeeding/exceptions/implicit", "When unchecked exceptions are thrown, but caught, they do not affect the test" )
Phil Nashd8026002010-11-09 23:24:00 +000074{
75 try
76 {
77 throw std::domain_error( "unexpected exception" );
78 }
79 catch(...)
80 {
81 }
82}
Phil Nash9430a2c2011-04-20 15:40:40 +010083
84class CustomException
85{
86public:
87 CustomException( const std::string& msg )
88 : m_msg( msg )
89 {}
90
91 std::string getMessage() const
92 {
93 return m_msg;
94 }
95
96private:
97 std::string m_msg;
98};
99
Phil Nash5d7b0542011-04-20 19:09:41 +0100100CATCH_TRANSLATE_EXCEPTION( CustomException& ex )
Phil Nash9430a2c2011-04-20 15:40:40 +0100101{
Phil Nash5d7b0542011-04-20 19:09:41 +0100102 return ex.getMessage();
Phil Nash9430a2c2011-04-20 15:40:40 +0100103}
104
Phil Nash5d7b0542011-04-20 19:09:41 +0100105CATCH_TRANSLATE_EXCEPTION( double& ex )
106{
107 return Catch::toString( ex );
108}
109
110TEST_CASE_NORETURN( "./failing/exceptions/custom", "Unexpected custom exceptions can be translated" )
Phil Nash9430a2c2011-04-20 15:40:40 +0100111{
112 throw CustomException( "custom exception" );
113}
Phil Nash5d7b0542011-04-20 19:09:41 +0100114
115TEST_CASE( "./failing/exceptions/custom/nothrow", "Custom exceptions can be translated when testing for nothrow" )
116{
117 REQUIRE_NOTHROW( throw CustomException( "unexpected custom exception" ) );
118}
119
120TEST_CASE( "./failing/exceptions/custom/throw", "Custom exceptions can be translated when testing for throwing as something else" )
121{
122 REQUIRE_THROWS_AS( throw CustomException( "custom exception - not std" ), std::exception );
123}
124
125
126TEST_CASE_NORETURN( "./failing/exceptions/custom/double", "Unexpected custom exceptions can be translated" )
127{
128 throw double( 3.14 );
129}
Phil Nash02e597c2011-04-21 19:43:55 +0100130
Phil Nashabf27162012-07-05 18:37:58 +0100131inline int thisFunctionNotImplemented( int ) {
132 CATCH_NOT_IMPLEMENTED;
133}
134
135TEST_CASE( "./succeeding/exceptions/notimplemented", "" )
136{
137 REQUIRE_THROWS( thisFunctionNotImplemented( 7 ) );
138}