catch_assertionhandler.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Created by Phil on 8/8/2017.
  3. * Copyright 2017 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)
  7. */
  8. #include "catch_assertionhandler.h"
  9. #include "catch_assertionresult.h"
  10. #include "catch_interfaces_runner.h"
  11. #include "catch_interfaces_config.h"
  12. #include "catch_context.h"
  13. #include "catch_debugger.h"
  14. #include "catch_interfaces_registry_hub.h"
  15. #include "catch_capture_matchers.h"
  16. #include "catch_run_context.h"
  17. #include "catch_enforce.h"
  18. namespace Catch {
  19. namespace {
  20. auto operator <<( std::ostream& os, ITransientExpression const& expr ) -> std::ostream& {
  21. expr.streamReconstructedExpression( os );
  22. return os;
  23. }
  24. }
  25. LazyExpression::LazyExpression( bool isNegated )
  26. : m_isNegated( isNegated )
  27. {}
  28. LazyExpression::LazyExpression( LazyExpression const& other ) : m_isNegated( other.m_isNegated ) {}
  29. LazyExpression::operator bool() const {
  30. return m_transientExpression != nullptr;
  31. }
  32. auto operator << ( std::ostream& os, LazyExpression const& lazyExpr ) -> std::ostream& {
  33. if( lazyExpr.m_isNegated )
  34. os << "!";
  35. if( lazyExpr ) {
  36. if( lazyExpr.m_isNegated && lazyExpr.m_transientExpression->isBinaryExpression() )
  37. os << "(" << *lazyExpr.m_transientExpression << ")";
  38. else
  39. os << *lazyExpr.m_transientExpression;
  40. }
  41. else {
  42. os << "{** error - unchecked empty expression requested **}";
  43. }
  44. return os;
  45. }
  46. AssertionHandler::AssertionHandler
  47. ( StringRef const& macroName,
  48. SourceLineInfo const& lineInfo,
  49. StringRef capturedExpression,
  50. ResultDisposition::Flags resultDisposition )
  51. : m_assertionInfo{ macroName, lineInfo, capturedExpression, resultDisposition },
  52. m_resultCapture( getResultCapture() )
  53. {}
  54. void AssertionHandler::handleExpr( ITransientExpression const& expr ) {
  55. m_resultCapture.handleExpr( m_assertionInfo, expr, m_reaction );
  56. }
  57. void AssertionHandler::handleMessage(ResultWas::OfType resultType, StringRef const& message) {
  58. m_resultCapture.handleMessage( m_assertionInfo, resultType, message, m_reaction );
  59. }
  60. auto AssertionHandler::allowThrows() const -> bool {
  61. return getCurrentContext().getConfig()->allowThrows();
  62. }
  63. void AssertionHandler::complete() {
  64. setCompleted();
  65. if( m_reaction.shouldDebugBreak ) {
  66. // If you find your debugger stopping you here then go one level up on the
  67. // call-stack for the code that caused it (typically a failed assertion)
  68. // (To go back to the test and change execution, jump over the throw, next)
  69. CATCH_BREAK_INTO_DEBUGGER();
  70. }
  71. if (m_reaction.shouldThrow) {
  72. #if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
  73. throw Catch::TestFailureException();
  74. #else
  75. CATCH_ERROR( "Test failure requires aborting test!" );
  76. #endif
  77. }
  78. }
  79. void AssertionHandler::setCompleted() {
  80. m_completed = true;
  81. }
  82. void AssertionHandler::handleUnexpectedInflightException() {
  83. m_resultCapture.handleUnexpectedInflightException( m_assertionInfo, Catch::translateActiveException(), m_reaction );
  84. }
  85. void AssertionHandler::handleExceptionThrownAsExpected() {
  86. m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction);
  87. }
  88. void AssertionHandler::handleExceptionNotThrownAsExpected() {
  89. m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction);
  90. }
  91. void AssertionHandler::handleUnexpectedExceptionNotThrown() {
  92. m_resultCapture.handleUnexpectedExceptionNotThrown( m_assertionInfo, m_reaction );
  93. }
  94. void AssertionHandler::handleThrowingCallSkipped() {
  95. m_resultCapture.handleNonExpr(m_assertionInfo, ResultWas::Ok, m_reaction);
  96. }
  97. // This is the overload that takes a string and infers the Equals matcher from it
  98. // The more general overload, that takes any string matcher, is in catch_capture_matchers.cpp
  99. void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef const& matcherString ) {
  100. handleExceptionMatchExpr( handler, Matchers::Equals( str ), matcherString );
  101. }
  102. } // namespace Catch