Comments (2)
Patch to allow application of predicates in assertions (and get a useful message on failure)
--- cppunit-1.12.1/include/cppunit/Asserter.h
+++ cppunit-1.12.1/include/cppunit/Asserter.h
@@ -103,6 +103,10 @@
const AdditionalMessage &additionalMessage = AdditionalMessage(),
const std::string &shortDescription = "equality assertion failed");
+ static Message CPPUNIT_API makePredicateFailMessage( const std::string &expectedValue,
+ const std::string &actualValue,
+ const AdditionalMessage &additionalMessage = AdditionalMessage(),
+ const std::string &shortDescription = "predicate failed");
/*! \brief Throws an Exception with the specified message and location.
* \param expected Text describing the expected value.
* \param actual Text describing the actual value.
@@ -134,6 +138,20 @@
const AdditionalMessage &additionalMessage = AdditionalMessage(),
std::string shortDescription = "equality assertion failed" );
+
+ /*! \brief Throws an Exception with the specified message and location.
+ * \param expected Text describing the expected value.
+ * \param actual Text describing the actual value.
+ * \param sourceLine Location of the assertion.
+ * \param additionalMessage Additional message. Usually used to report
+ * where the "failed predicate" is located.
+ * \param shortDescription Short description for the failure message.
+ */
+ static void CPPUNIT_API failPredicate( std::string expected,
+ std::string actual,
+ const SourceLine &sourceLine,
+ const AdditionalMessage &additionalMessage = AdditionalMessage(),
+ std::string shortDescription = "predicate failed" );
};
--- cppunit-1.12.1/include/cppunit/TestAssert.h
+++ cppunit-1.12.1/include/cppunit/TestAssert.h
@@ -105,6 +105,25 @@
}
}
+/*! \brief (Implementation) Applies a given predicate to two values of the same type and asserts the predicate returns true.
+ * Use CPPUNIT_ASSERT_PREDICATE instead of this function.
+ * \sa assertion_traits, Asserter::failPredicate().
+ */
+template < class T, class P >
+void assertPredicate(const T& expected,
+ const T& actual,
+ const P& predicate,
+ SourceLine sourceLine,
+ const std::string &message)
+{
+ if (!predicate(actual, expected))
+ {
+ Asserter::failPredicate( assertion_traits<T>::toString(expected),
+ assertion_traits<T>::toString(actual),
+ sourceLine,
+ message );
+ }
+}
/*! \brief (Implementation) Asserts that two double are equals given a tolerance.
* Use CPPUNIT_ASSERT_DOUBLES_EQUAL instead of this function.
@@ -247,7 +266,12 @@
CPPUNIT_SOURCELINE(), \
(message) ) )
-
+#define CPPUNIT_ASSERT_PREDICATE(message, expected, actual, predicate) \
+ ( CPPUNIT_NS::assertPredicate( (expected), \
+ (actual), \
+ (predicate), \
+ CPPUNIT_SOURCELINE(), \
+ (message) ) )
/** Asserts that the given expression throws an exception of the specified type.
* \ingroup Assertions
* Example of usage:
--- cppunit-1.12.1/src/cppunit/Asserter.cpp
+++ cppunit-1.12.1/src/cppunit/Asserter.cpp
@@ -69,7 +69,21 @@
return message;
}
+Message
+Asserter::makePredicateFailMessage( const std::string &expectedValue,
+ const std::string &actualValue,
+ const AdditionalMessage &additionalMessage,
+ const std::string &shortDescription)
+{
+ Message message( shortDescription,
+ makeExpected( expectedValue ),
+ makeActual( actualValue ) );
+ message.addDetail( additionalMessage );
+ return message;
+}
+
+
void
Asserter::failNotEqual( std::string expected,
std::string actual,
@@ -97,5 +111,18 @@
failNotEqual( expected, actual, sourceLine, additionalMessage, shortDescription );
}
+void Asserter::failPredicate( std::string expected,
+ std::string actual,
+ const SourceLine &sourceLine,
+ const AdditionalMessage &additionalMessage,
+ std::string shortDescription )
+{
+ fail( makePredicateFailMessage( expected,
+ actual,
+ additionalMessage,
+ shortDescription ),
+ sourceLine );
+}
+
CPPUNIT_NS_END
from cppunit.
Patch applied
from cppunit.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppunit.