blob: 7bc013876cd6ad9f23e61b580edfba24a2c16cb4 [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
115TEST_CASE( "./failing/exceptions/in-section", "Exceptions thrown from sections report file/ line or section" )
116{
117 SECTION( "the section", "" )
118 {
Phil Nash7b449f72012-02-15 08:20:06 +0000119 CATCH_REGISTER_LINE_INFO( "the section2" ) SECTION( "the section2", "" )
Phil Nash02e597c2011-04-21 19:43:55 +0100120 {
121 throw std::domain_error( "Exception from section" );
122 }
123 }
124}
Phil Nash7b449f72012-02-15 08:20:06 +0000125
126TEST_CASE( "./succeeding/exceptions/error messages", "The error messages produced by exceptions caught by Catch matched the expected form" )
127{
128 Catch::EmbeddedRunner runner;
129
130 SECTION( "custom, unexpected", "" )
131 {
132 runner.runMatching( "./failing/exceptions/custom" );
133 INFO( runner.getOutput() );
134 CHECK( runner.getOutput().find( "Unexpected exception" ) != std::string::npos );
135 CHECK( runner.getOutput().find( "custom exception" ) != std::string::npos );
136 }
137
138 SECTION( "in section", "" )
139 {
140 runner.runMatching( "./failing/exceptions/in-section" );
141 INFO( runner.getOutput() );
142 CHECK( runner.getOutput().find( "Unexpected exception" ) != std::string::npos );
143 CHECK( runner.getOutput().find( "Exception from section" ) != std::string::npos );
144 CHECK( runner.getOutput().find( CATCH_GET_LINE_INFO( "the section2" ) ) != std::string::npos );
145 }
146
147}