catch_assertion_handler.cpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 #include <catch2/internal/catch_assertion_handler.hpp> 9 #include <catch2/interfaces/catch_interfaces_config.hpp> 10 #include <catch2/internal/catch_context.hpp> 11 #include <catch2/internal/catch_enforce.hpp> 12 #include <catch2/internal/catch_debugger.hpp> 13 #include <catch2/internal/catch_test_failure_exception.hpp> 14 #include <catch2/interfaces/catch_interfaces_registry_hub.hpp> 15 #include <catch2/matchers/catch_matchers_string.hpp> 16 17 namespace Catch { 18 19 AssertionHandler::AssertionHandler 20 ( StringRef macroName, 21 SourceLineInfo const& lineInfo, 22 StringRef capturedExpression, 23 ResultDisposition::Flags resultDisposition ) 24 : m_assertionInfo{ macroName, lineInfo, capturedExpression, resultDisposition }, 25 m_resultCapture( getResultCapture() ) 26 { 27 m_resultCapture.notifyAssertionStarted( m_assertionInfo ); 28 } 29 30 void AssertionHandler::handleExpr( ITransientExpression const& expr ) { 31 m_resultCapture.handleExpr( m_assertionInfo, expr, m_reaction ); 32 } 33 void AssertionHandler::handleMessage(ResultWas::OfType resultType, StringRef message) { 34 m_resultCapture.handleMessage( m_assertionInfo, resultType, message, m_reaction ); 35 } 36 37 auto AssertionHandler::allowThrows() const -> bool { 38 return getCurrentContext().getConfig()->allowThrows(); 39 } 40 41 void AssertionHandler::complete() { 42 m_completed = true; 43 if( m_reaction.shouldDebugBreak ) { 44 45 // If you find your debugger stopping you here then go one level up on the 46 // call-stack for the code that caused it (typically a failed assertion) 47 48 // (To go back to the test and change execution, jump over the throw, next) 49 CATCH_BREAK_INTO_DEBUGGER(); 50 } 51 if (m_reaction.shouldThrow) { 52 throw_test_failure_exception(); 53 } 54 if ( m_reaction.shouldSkip ) { 55 throw_test_skip_exception(); 56 } 57 } 58 59 void AssertionHandler::handleUnexpectedInflightException() { 60 m_resultCapture.handleUnexpectedInflightException( m_assertionInfo, Catch::translateActiveException(), m_reaction ); 61 } 62 63 void AssertionHandler::handleExceptionThrownAsExpected() { 64 m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); 65 } 66 void AssertionHandler::handleExceptionNotThrownAsExpected() { 67 m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); 68 } 69 70 void AssertionHandler::handleUnexpectedExceptionNotThrown() { 71 m_resultCapture.handleUnexpectedExceptionNotThrown( m_assertionInfo, m_reaction ); 72 } 73 74 void AssertionHandler::handleThrowingCallSkipped() { 75 m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction); 76 } 77 78 // This is the overload that takes a string and infers the Equals matcher from it 79 // The more general overload, that takes any string matcher, is in catch_capture_matchers.cpp 80 void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str ) { 81 handleExceptionMatchExpr( handler, Matchers::Equals( str ) ); 82 } 83 84 } // namespace Catch