Asserter.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef CPPUNIT_ASSERTER_H
  2. #define CPPUNIT_ASSERTER_H
  3. #include <cppunit/AdditionalMessage.h>
  4. #include <cppunit/SourceLine.h>
  5. #include <string>
  6. CPPUNIT_NS_BEGIN
  7. class Message;
  8. /*! \brief A set of functions to help writing assertion macros.
  9. * \ingroup CreatingNewAssertions
  10. *
  11. * Here is an example of assertion, a simplified version of the
  12. * actual assertion implemented in examples/cppunittest/XmlUniformiser.h:
  13. * \code
  14. * #include <cppunit/SourceLine.h>
  15. * #include <cppunit/TestAssert.h>
  16. *
  17. * void
  18. * checkXmlEqual( std::string expectedXml,
  19. * std::string actualXml,
  20. * CppUnit::SourceLine sourceLine )
  21. * {
  22. * std::string expected = XmlUniformiser( expectedXml ).stripped();
  23. * std::string actual = XmlUniformiser( actualXml ).stripped();
  24. *
  25. * if ( expected == actual )
  26. * return;
  27. *
  28. * ::CppUnit::Asserter::failNotEqual( expected,
  29. * actual,
  30. * sourceLine );
  31. * }
  32. *
  33. * /// Asserts that two XML strings are equivalent.
  34. * #define CPPUNITTEST_ASSERT_XML_EQUAL( expected, actual ) \
  35. * checkXmlEqual( expected, actual, \
  36. * CPPUNIT_SOURCELINE() )
  37. * \endcode
  38. */
  39. struct Asserter
  40. {
  41. /*! \brief Throws a Exception with the specified message and location.
  42. */
  43. static void CPPUNIT_API fail( const Message &message,
  44. const SourceLine &sourceLine = SourceLine() );
  45. /*! \brief Throws a Exception with the specified message and location.
  46. * \deprecated Use fail( Message, SourceLine ) instead.
  47. */
  48. static void CPPUNIT_API fail( std::string message,
  49. const SourceLine &sourceLine = SourceLine() );
  50. /*! \brief Throws a Exception with the specified message and location.
  51. * \param shouldFail if \c true then the exception is thrown. Otherwise
  52. * nothing happen.
  53. * \param message Message explaining the assertion failiure.
  54. * \param sourceLine Location of the assertion.
  55. */
  56. static void CPPUNIT_API failIf( bool shouldFail,
  57. const Message &message,
  58. const SourceLine &sourceLine = SourceLine() );
  59. /*! \brief Throws a Exception with the specified message and location.
  60. * \deprecated Use failIf( bool, Message, SourceLine ) instead.
  61. * \param shouldFail if \c true then the exception is thrown. Otherwise
  62. * nothing happen.
  63. * \param message Message explaining the assertion failiure.
  64. * \param sourceLine Location of the assertion.
  65. */
  66. static void CPPUNIT_API failIf( bool shouldFail,
  67. std::string message,
  68. const SourceLine &sourceLine = SourceLine() );
  69. /*! \brief Returns a expected value string for a message.
  70. * Typically used to create 'not equal' message, or to check that a message
  71. * contains the expected content when writing unit tests for your custom
  72. * assertions.
  73. *
  74. * \param expectedValue String that represents the expected value.
  75. * \return \a expectedValue prefixed with "Expected: ".
  76. * \see makeActual().
  77. */
  78. static std::string CPPUNIT_API makeExpected( const std::string &expectedValue );
  79. /*! \brief Returns an actual value string for a message.
  80. * Typically used to create 'not equal' message, or to check that a message
  81. * contains the expected content when writing unit tests for your custom
  82. * assertions.
  83. *
  84. * \param actualValue String that represents the actual value.
  85. * \return \a actualValue prefixed with "Actual : ".
  86. * \see makeExpected().
  87. */
  88. static std::string CPPUNIT_API makeActual( const std::string &actualValue );
  89. static Message CPPUNIT_API makeNotEqualMessage( const std::string &expectedValue,
  90. const std::string &actualValue,
  91. const AdditionalMessage &additionalMessage = AdditionalMessage(),
  92. const std::string &shortDescription = "equality assertion failed");
  93. /*! \brief Throws an Exception with the specified message and location.
  94. * \param expected Text describing the expected value.
  95. * \param actual Text describing the actual value.
  96. * \param sourceLine Location of the assertion.
  97. * \param additionalMessage Additional message. Usually used to report
  98. * what are the differences between the expected and actual value.
  99. * \param shortDescription Short description for the failure message.
  100. */
  101. static void CPPUNIT_API failNotEqual( std::string expected,
  102. std::string actual,
  103. const SourceLine &sourceLine,
  104. const AdditionalMessage &additionalMessage = AdditionalMessage(),
  105. std::string shortDescription = "equality assertion failed" );
  106. /*! \brief Throws an Exception with the specified message and location.
  107. * \param shouldFail if \c true then the exception is thrown. Otherwise
  108. * nothing happen.
  109. * \param expected Text describing the expected value.
  110. * \param actual Text describing the actual value.
  111. * \param sourceLine Location of the assertion.
  112. * \param additionalMessage Additional message. Usually used to report
  113. * where the "difference" is located.
  114. * \param shortDescription Short description for the failure message.
  115. */
  116. static void CPPUNIT_API failNotEqualIf( bool shouldFail,
  117. std::string expected,
  118. std::string actual,
  119. const SourceLine &sourceLine,
  120. const AdditionalMessage &additionalMessage = AdditionalMessage(),
  121. std::string shortDescription = "equality assertion failed" );
  122. };
  123. CPPUNIT_NS_END
  124. #endif // CPPUNIT_ASSERTER_H