catch_assertionresult.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Created by Phil on 8/8/12
  3. * Copyright 2012 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_assertionresult.h"
  9. namespace Catch {
  10. AssertionResultData::AssertionResultData(ResultWas::OfType _resultType, LazyExpression const & _lazyExpression):
  11. lazyExpression(_lazyExpression),
  12. resultType(_resultType) {}
  13. std::string AssertionResultData::reconstructExpression() const {
  14. if( reconstructedExpression.empty() ) {
  15. if( lazyExpression ) {
  16. ReusableStringStream rss;
  17. rss << lazyExpression;
  18. reconstructedExpression = rss.str();
  19. }
  20. }
  21. return reconstructedExpression;
  22. }
  23. AssertionResult::AssertionResult( AssertionInfo const& info, AssertionResultData const& data )
  24. : m_info( info ),
  25. m_resultData( data )
  26. {}
  27. // Result was a success
  28. bool AssertionResult::succeeded() const {
  29. return Catch::isOk( m_resultData.resultType );
  30. }
  31. // Result was a success, or failure is suppressed
  32. bool AssertionResult::isOk() const {
  33. return Catch::isOk( m_resultData.resultType ) || shouldSuppressFailure( m_info.resultDisposition );
  34. }
  35. ResultWas::OfType AssertionResult::getResultType() const {
  36. return m_resultData.resultType;
  37. }
  38. bool AssertionResult::hasExpression() const {
  39. return !m_info.capturedExpression.empty();
  40. }
  41. bool AssertionResult::hasMessage() const {
  42. return !m_resultData.message.empty();
  43. }
  44. std::string AssertionResult::getExpression() const {
  45. // Possibly overallocating by 3 characters should be basically free
  46. std::string expr; expr.reserve(m_info.capturedExpression.size() + 3);
  47. if (isFalseTest(m_info.resultDisposition)) {
  48. expr += "!(";
  49. }
  50. expr += m_info.capturedExpression;
  51. if (isFalseTest(m_info.resultDisposition)) {
  52. expr += ')';
  53. }
  54. return expr;
  55. }
  56. std::string AssertionResult::getExpressionInMacro() const {
  57. std::string expr;
  58. if( m_info.macroName.empty() )
  59. expr = static_cast<std::string>(m_info.capturedExpression);
  60. else {
  61. expr.reserve( m_info.macroName.size() + m_info.capturedExpression.size() + 4 );
  62. expr += m_info.macroName;
  63. expr += "( ";
  64. expr += m_info.capturedExpression;
  65. expr += " )";
  66. }
  67. return expr;
  68. }
  69. bool AssertionResult::hasExpandedExpression() const {
  70. return hasExpression() && getExpandedExpression() != getExpression();
  71. }
  72. std::string AssertionResult::getExpandedExpression() const {
  73. std::string expr = m_resultData.reconstructExpression();
  74. return expr.empty()
  75. ? getExpression()
  76. : expr;
  77. }
  78. std::string AssertionResult::getMessage() const {
  79. return m_resultData.message;
  80. }
  81. SourceLineInfo AssertionResult::getSourceInfo() const {
  82. return m_info.lineInfo;
  83. }
  84. StringRef AssertionResult::getTestMacroName() const {
  85. return m_info.macroName;
  86. }
  87. } // end namespace Catch