Coder Social home page Coder Social logo

CPPUNIT_ASSERT_PREDICATE about cppunit HOT 2 CLOSED

blytkerchan avatar blytkerchan commented on September 9, 2024
CPPUNIT_ASSERT_PREDICATE

from cppunit.

Comments (2)

blytkerchan avatar blytkerchan commented on September 9, 2024
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.

blytkerchan avatar blytkerchan commented on September 9, 2024

Patch applied

from cppunit.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.