Integrated INTERNAL_CATCH_THROWS_STR_MATCHES with new AssertionHandler

This commit is contained in:
Phil Nash 2017-08-09 00:44:30 +01:00
parent ef4fa56b71
commit 27fd8f80bd
9 changed files with 51 additions and 20 deletions

View File

@ -13,6 +13,7 @@
#include "catch_context.h" #include "catch_context.h"
#include "catch_debugger.h" #include "catch_debugger.h"
#include "catch_interfaces_registry_hub.h" #include "catch_interfaces_registry_hub.h"
#include "catch_matchers_string.h"
#include <cassert> #include <cassert>
@ -140,5 +141,14 @@ namespace Catch {
m_inExceptionGuard = false; m_inExceptionGuard = false;
} }
using StringMatcher = Matchers::Impl::MatcherBase<std::string>;
void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef matcherString ) {
MatchExpr<std::string, StringMatcher const&> expr( Catch::translateActiveException(), matcher, matcherString );
handler.handle( expr );
}
void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef matcherString ) {
handleExceptionMatchExpr( handler, Matchers::Equals( str ), matcherString );
}
} // namespace Catch } // namespace Catch

View File

@ -10,6 +10,7 @@
#include "catch_decomposer.h" #include "catch_decomposer.h"
#include "catch_assertioninfo.h" #include "catch_assertioninfo.h"
#include "catch_matchers_string.h" // !TBD: for exception matchers
namespace Catch { namespace Catch {
@ -67,6 +68,11 @@ namespace Catch {
void unsetExceptionGuard(); void unsetExceptionGuard();
}; };
using StringMatcher = Matchers::Impl::MatcherBase<std::string>;
void handleExceptionMatchExpr( AssertionHandler& handler, StringMatcher const& matcher, StringRef matcherString );
void handleExceptionMatchExpr( AssertionHandler& handler, std::string const& str, StringRef matcherString );
} // namespace Catch } // namespace Catch
#endif // TWOBLUECUBES_CATCH_ASSERTIONHANDLER_H_INCLUDED #endif // TWOBLUECUBES_CATCH_ASSERTIONHANDLER_H_INCLUDED

View File

@ -159,18 +159,18 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \ #define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \
do { \ do { \
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #__VA_ARGS__ ", " #matcher, resultDisposition); \ Catch::AssertionHandler catchAssertionHandler( macroName, CATCH_INTERNAL_LINEINFO, #__VA_ARGS__ ", " #matcher, resultDisposition ); \
if( __catchResult.allowThrows() ) \ if( catchAssertionHandler.allowThrows() ) \
try { \ try { \
static_cast<void>(__VA_ARGS__); \ static_cast<void>(__VA_ARGS__); \
__catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \ catchAssertionHandler.handle( Catch::ResultWas::DidntThrowException ); \
} \ } \
catch( ... ) { \ catch( ... ) { \
__catchResult.captureExpectedException( matcher ); \ handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher ); \
} \ } \
else \ else \
__catchResult.captureResult( Catch::ResultWas::Ok ); \ catchAssertionHandler.handle( Catch::ResultWas::Ok ); \
INTERNAL_CATCH_REACT( __catchResult ) \ INTERNAL_CATCH_REACT2( catchAssertionHandler ) \
} while( Catch::alwaysFalse() ) } while( Catch::alwaysFalse() )

View File

@ -12,7 +12,6 @@
namespace Catch { namespace Catch {
void formatReconstructedExpression( std::ostream &os, std::string const& lhs, std::string const& op, std::string const& rhs ) { void formatReconstructedExpression( std::ostream &os, std::string const& lhs, std::string const& op, std::string const& rhs ) {
if( lhs.size() + rhs.size() < 40 && if( lhs.size() + rhs.size() < 40 &&
lhs.find('\n') == std::string::npos && lhs.find('\n') == std::string::npos &&
rhs.find('\n') == std::string::npos ) rhs.find('\n') == std::string::npos )

View File

@ -10,6 +10,7 @@
#include "catch_tostring.h" #include "catch_tostring.h"
#include "catch_stringref.h" #include "catch_stringref.h"
#include "catch_matchers.hpp" // !TBD: for exception matchers - move this
#include <ostream> #include <ostream>
@ -148,7 +149,7 @@ namespace Catch {
template<typename ArgT, typename MatcherT> template<typename ArgT, typename MatcherT>
class MatchExpr : public ITransientExpression { class MatchExpr : public ITransientExpression {
ArgT const& m_arg; ArgT const& m_arg;
MatcherT const& m_matcher; MatcherT m_matcher;
StringRef m_matcherString; StringRef m_matcherString;
bool m_result; bool m_result;
public: public:
@ -171,6 +172,7 @@ namespace Catch {
os << matcherAsString; os << matcherAsString;
} }
}; };
template<typename ArgT, typename MatcherT> template<typename ArgT, typename MatcherT>
auto makeMatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString ) -> MatchExpr<ArgT, MatcherT> { auto makeMatchExpr( ArgT const& arg, MatcherT const& matcher, StringRef matcherString ) -> MatchExpr<ArgT, MatcherT> {
return MatchExpr<ArgT, MatcherT>( arg, matcher, matcherString ); return MatchExpr<ArgT, MatcherT>( arg, matcher, matcherString );

View File

@ -485,7 +485,7 @@ ExceptionTests.cpp:<line number>
ExceptionTests.cpp:<line number>: FAILED: ExceptionTests.cpp:<line number>: FAILED:
REQUIRE_THROWS_WITH( thisThrows(), "should fail" ) REQUIRE_THROWS_WITH( thisThrows(), "should fail" )
with expansion: with expansion:
expected exception "expected exception" equals: "should fail"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Nice descriptive name Nice descriptive name

View File

@ -1428,6 +1428,8 @@ ExceptionTests.cpp:<line number>
ExceptionTests.cpp:<line number>: ExceptionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ) REQUIRE_THROWS_WITH( thisThrows(), "expected exception" )
with expansion:
"expected exception" equals: "expected exception"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Exception messages can be tested for Exception messages can be tested for
@ -1439,6 +1441,8 @@ ExceptionTests.cpp:<line number>
ExceptionTests.cpp:<line number>: ExceptionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) ) REQUIRE_THROWS_WITH( thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) )
with expansion:
"expected exception" equals: "expected exception" (case insensitive)
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Exception messages can be tested for Exception messages can be tested for
@ -1450,18 +1454,26 @@ ExceptionTests.cpp:<line number>
ExceptionTests.cpp:<line number>: ExceptionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) ) REQUIRE_THROWS_WITH( thisThrows(), StartsWith( "expected" ) )
with expansion:
"expected exception" starts with: "expected"
ExceptionTests.cpp:<line number>: ExceptionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) ) REQUIRE_THROWS_WITH( thisThrows(), EndsWith( "exception" ) )
with expansion:
"expected exception" ends with: "exception"
ExceptionTests.cpp:<line number>: ExceptionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) ) REQUIRE_THROWS_WITH( thisThrows(), Contains( "except" ) )
with expansion:
"expected exception" contains: "except"
ExceptionTests.cpp:<line number>: ExceptionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) ) REQUIRE_THROWS_WITH( thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) )
with expansion:
"expected exception" contains: "except" (case insensitive)
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Expected exceptions that don't throw or unexpected exceptions fail the test Expected exceptions that don't throw or unexpected exceptions fail the test
@ -1882,11 +1894,13 @@ ExceptionTests.cpp:<line number>
ExceptionTests.cpp:<line number>: ExceptionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE_THROWS_WITH( thisThrows(), "expected exception" ) REQUIRE_THROWS_WITH( thisThrows(), "expected exception" )
with expansion:
"expected exception" equals: "expected exception"
ExceptionTests.cpp:<line number>: FAILED: ExceptionTests.cpp:<line number>: FAILED:
REQUIRE_THROWS_WITH( thisThrows(), "should fail" ) REQUIRE_THROWS_WITH( thisThrows(), "should fail" )
with expansion: with expansion:
expected exception "expected exception" equals: "should fail"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Nice descriptive name Nice descriptive name

View File

@ -311,7 +311,7 @@ MatchersTests.cpp:<line number>
</failure> </failure>
</testcase> </testcase>
<testcase classname="<exe-name>.global" name="Mismatching exception messages failing the test" time="{duration}"> <testcase classname="<exe-name>.global" name="Mismatching exception messages failing the test" time="{duration}">
<failure message="expected exception" type="REQUIRE_THROWS_WITH"> <failure message="&quot;expected exception&quot; equals: &quot;should fail&quot;" type="REQUIRE_THROWS_WITH">
ExceptionTests.cpp:<line number> ExceptionTests.cpp:<line number>
</failure> </failure>
</testcase> </testcase>

View File

@ -1646,7 +1646,7 @@
thisThrows(), "expected exception" thisThrows(), "expected exception"
</Original> </Original>
<Expanded> <Expanded>
thisThrows(), "expected exception" "expected exception" equals: "expected exception"
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/> <OverallResults successes="1" failures="0" expectedFailures="0"/>
@ -1657,7 +1657,7 @@
thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No )
</Original> </Original>
<Expanded> <Expanded>
thisThrows(), Equals( "expecteD Exception", Catch::CaseSensitive::No ) "expected exception" equals: "expected exception" (case insensitive)
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/> <OverallResults successes="1" failures="0" expectedFailures="0"/>
@ -1668,7 +1668,7 @@
thisThrows(), StartsWith( "expected" ) thisThrows(), StartsWith( "expected" )
</Original> </Original>
<Expanded> <Expanded>
thisThrows(), StartsWith( "expected" ) "expected exception" starts with: "expected"
</Expanded> </Expanded>
</Expression> </Expression>
<Expression success="true" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" > <Expression success="true" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" >
@ -1676,7 +1676,7 @@
thisThrows(), EndsWith( "exception" ) thisThrows(), EndsWith( "exception" )
</Original> </Original>
<Expanded> <Expanded>
thisThrows(), EndsWith( "exception" ) "expected exception" ends with: "exception"
</Expanded> </Expanded>
</Expression> </Expression>
<Expression success="true" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" > <Expression success="true" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" >
@ -1684,7 +1684,7 @@
thisThrows(), Contains( "except" ) thisThrows(), Contains( "except" )
</Original> </Original>
<Expanded> <Expanded>
thisThrows(), Contains( "except" ) "expected exception" contains: "except"
</Expanded> </Expanded>
</Expression> </Expression>
<Expression success="true" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" > <Expression success="true" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" >
@ -1692,7 +1692,7 @@
thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) thisThrows(), Contains( "exCept", Catch::CaseSensitive::No )
</Original> </Original>
<Expanded> <Expanded>
thisThrows(), Contains( "exCept", Catch::CaseSensitive::No ) "expected exception" contains: "except" (case insensitive)
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResults successes="4" failures="0" expectedFailures="0"/> <OverallResults successes="4" failures="0" expectedFailures="0"/>
@ -2172,7 +2172,7 @@
thisThrows(), "expected exception" thisThrows(), "expected exception"
</Original> </Original>
<Expanded> <Expanded>
thisThrows(), "expected exception" "expected exception" equals: "expected exception"
</Expanded> </Expanded>
</Expression> </Expression>
<Expression success="false" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" > <Expression success="false" type="REQUIRE_THROWS_WITH" filename="projects/<exe-name>/ExceptionTests.cpp" >
@ -2180,7 +2180,7 @@
thisThrows(), "should fail" thisThrows(), "should fail"
</Original> </Original>
<Expanded> <Expanded>
expected exception "expected exception" equals: "should fail"
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResult success="false"/> <OverallResult success="false"/>