Message.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef CPPUNIT_MESSAGE_H
  2. #define CPPUNIT_MESSAGE_H
  3. #include <cppunit/Portability.h>
  4. #if CPPUNIT_NEED_DLL_DECL
  5. #pragma warning( push )
  6. #pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z
  7. #endif
  8. #include <cppunit/portability/CppUnitDeque.h>
  9. #include <string>
  10. CPPUNIT_NS_BEGIN
  11. #if CPPUNIT_NEED_DLL_DECL
  12. // template class CPPUNIT_API std::deque<std::string>;
  13. #endif
  14. /*! \brief Message associated to an Exception.
  15. * \ingroup CreatingNewAssertions
  16. * A message is composed of two items:
  17. * - a short description (~20/30 characters)
  18. * - a list of detail strings
  19. *
  20. * The short description is used to indicate how the detail strings should be
  21. * interpreted. It usually indicates the failure types, such as
  22. * "assertion failed", "forced failure", "unexpected exception caught",
  23. * "equality assertion failed"... It should not contains new line character (\n).
  24. *
  25. * Detail strings are used to provide more information about the failure. It
  26. * can contains the asserted expression, the expected and actual values in an
  27. * equality assertion, some addional messages... Detail strings can contains
  28. * new line characters (\n).
  29. */
  30. class CPPUNIT_API Message
  31. {
  32. public:
  33. Message();
  34. // Ensure thread-safe copy by detaching the string.
  35. Message( const Message &other );
  36. explicit Message( const std::string &shortDescription );
  37. Message( const std::string &shortDescription,
  38. const std::string &detail1 );
  39. Message( const std::string &shortDescription,
  40. const std::string &detail1,
  41. const std::string &detail2 );
  42. Message( const std::string &shortDescription,
  43. const std::string &detail1,
  44. const std::string &detail2,
  45. const std::string &detail3 );
  46. Message &operator =( const Message &other );
  47. /*! \brief Returns the short description.
  48. * \return Short description.
  49. */
  50. const std::string &shortDescription() const;
  51. /*! \brief Returns the number of detail string.
  52. * \return Number of detail string.
  53. */
  54. int detailCount() const;
  55. /*! \brief Returns the detail at the specified index.
  56. * \param index Zero based index of the detail string to return.
  57. * \returns Detail string at the specified index.
  58. * \exception std::invalid_argument if \a index < 0 or index >= detailCount().
  59. */
  60. std::string detailAt( int index ) const;
  61. /*! \brief Returns a string that represents a list of the detail strings.
  62. *
  63. * Example:
  64. * \code
  65. * Message message( "not equal", "Expected: 3", "Actual: 7" );
  66. * std::string details = message.details();
  67. * // details contains:
  68. * // "- Expected: 3\n- Actual: 7\n" \endcode
  69. *
  70. * \return A string that is a concatenation of all the detail strings. Each detail
  71. * string is prefixed with '- ' and suffixed with '\n' before being
  72. * concatenated to the other.
  73. */
  74. std::string details() const;
  75. /*! \brief Removes all detail strings.
  76. */
  77. void clearDetails();
  78. /*! \brief Adds a single detail string.
  79. * \param detail Detail string to add.
  80. */
  81. void addDetail( const std::string &detail );
  82. /*! \brief Adds two detail strings.
  83. * \param detail1 Detail string to add.
  84. * \param detail2 Detail string to add.
  85. */
  86. void addDetail( const std::string &detail1,
  87. const std::string &detail2 );
  88. /*! \brief Adds three detail strings.
  89. * \param detail1 Detail string to add.
  90. * \param detail2 Detail string to add.
  91. * \param detail3 Detail string to add.
  92. */
  93. void addDetail( const std::string &detail1,
  94. const std::string &detail2,
  95. const std::string &detail3 );
  96. /*! \brief Adds the detail strings of the specified message.
  97. * \param message All the detail strings of this message are added to this one.
  98. */
  99. void addDetail( const Message &message );
  100. /*! \brief Sets the short description.
  101. * \param shortDescription New short description.
  102. */
  103. void setShortDescription( const std::string &shortDescription );
  104. /*! \brief Tests if a message is identical to another one.
  105. * \param other Message this message is compared to.
  106. * \return \c true if the two message are identical, \c false otherwise.
  107. */
  108. bool operator ==( const Message &other ) const;
  109. /*! \brief Tests if a message is different from another one.
  110. * \param other Message this message is compared to.
  111. * \return \c true if the two message are not identical, \c false otherwise.
  112. */
  113. bool operator !=( const Message &other ) const;
  114. private:
  115. std::string m_shortDescription;
  116. typedef CppUnitDeque<std::string> Details;
  117. Details m_details;
  118. };
  119. CPPUNIT_NS_END
  120. #if CPPUNIT_NEED_DLL_DECL
  121. #pragma warning( pop )
  122. #endif
  123. #endif // CPPUNIT_MESSAGE_H