• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #include "catch_assertionresult.h"
10 
11 namespace Catch {
AssertionResultData(ResultWas::OfType _resultType,LazyExpression const & _lazyExpression)12     AssertionResultData::AssertionResultData(ResultWas::OfType _resultType, LazyExpression const & _lazyExpression):
13         lazyExpression(_lazyExpression),
14         resultType(_resultType) {}
15 
reconstructExpression() const16     std::string AssertionResultData::reconstructExpression() const {
17 
18         if( reconstructedExpression.empty() ) {
19             if( lazyExpression ) {
20                 ReusableStringStream rss;
21                 rss << lazyExpression;
22                 reconstructedExpression = rss.str();
23             }
24         }
25         return reconstructedExpression;
26     }
27 
AssertionResult(AssertionInfo const & info,AssertionResultData const & data)28     AssertionResult::AssertionResult( AssertionInfo const& info, AssertionResultData const& data )
29     :   m_info( info ),
30         m_resultData( data )
31     {}
32 
33     // Result was a success
succeeded() const34     bool AssertionResult::succeeded() const {
35         return Catch::isOk( m_resultData.resultType );
36     }
37 
38     // Result was a success, or failure is suppressed
isOk() const39     bool AssertionResult::isOk() const {
40         return Catch::isOk( m_resultData.resultType ) || shouldSuppressFailure( m_info.resultDisposition );
41     }
42 
getResultType() const43     ResultWas::OfType AssertionResult::getResultType() const {
44         return m_resultData.resultType;
45     }
46 
hasExpression() const47     bool AssertionResult::hasExpression() const {
48         return !m_info.capturedExpression.empty();
49     }
50 
hasMessage() const51     bool AssertionResult::hasMessage() const {
52         return !m_resultData.message.empty();
53     }
54 
getExpression() const55     std::string AssertionResult::getExpression() const {
56         // Possibly overallocating by 3 characters should be basically free
57         std::string expr; expr.reserve(m_info.capturedExpression.size() + 3);
58         if (isFalseTest(m_info.resultDisposition)) {
59             expr += "!(";
60         }
61         expr += m_info.capturedExpression;
62         if (isFalseTest(m_info.resultDisposition)) {
63             expr += ')';
64         }
65         return expr;
66     }
67 
getExpressionInMacro() const68     std::string AssertionResult::getExpressionInMacro() const {
69         std::string expr;
70         if( m_info.macroName.empty() )
71             expr = static_cast<std::string>(m_info.capturedExpression);
72         else {
73             expr.reserve( m_info.macroName.size() + m_info.capturedExpression.size() + 4 );
74             expr += m_info.macroName;
75             expr += "( ";
76             expr += m_info.capturedExpression;
77             expr += " )";
78         }
79         return expr;
80     }
81 
hasExpandedExpression() const82     bool AssertionResult::hasExpandedExpression() const {
83         return hasExpression() && getExpandedExpression() != getExpression();
84     }
85 
getExpandedExpression() const86     std::string AssertionResult::getExpandedExpression() const {
87         std::string expr = m_resultData.reconstructExpression();
88         return expr.empty()
89                 ? getExpression()
90                 : expr;
91     }
92 
getMessage() const93     std::string AssertionResult::getMessage() const {
94         return m_resultData.message;
95     }
getSourceInfo() const96     SourceLineInfo AssertionResult::getSourceInfo() const {
97         return m_info.lineInfo;
98     }
99 
getTestMacroName() const100     StringRef AssertionResult::getTestMacroName() const {
101         return m_info.macroName;
102     }
103 
104 } // end namespace Catch
105