assertion_result.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. /// @file
  8. /// Enhanced result for test predicate that include message explaining failure
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_PREDICATE_RESULT_HPP_012705GER
  11. #define BOOST_TEST_PREDICATE_RESULT_HPP_012705GER
  12. // Boost.Test
  13. #include <boost/test/utils/class_properties.hpp>
  14. #include <boost/test/utils/wrap_stringstream.hpp>
  15. #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
  16. // Boost
  17. #include <boost/shared_ptr.hpp>
  18. #include <boost/detail/workaround.hpp>
  19. // STL
  20. #include <cstddef> // for std::size_t
  21. #include <boost/test/detail/suppress_warnings.hpp>
  22. //____________________________________________________________________________//
  23. namespace boost {
  24. namespace test_tools {
  25. // ************************************************************************** //
  26. // ************** assertion_result ************** //
  27. // ************************************************************************** //
  28. //!@brief Type used for storing the result of an assertion.
  29. class BOOST_TEST_DECL assertion_result {
  30. //!@internal
  31. typedef unit_test::const_string const_string;
  32. //!@internal
  33. struct dummy { void nonnull() {} };
  34. //!@internal
  35. typedef void (dummy::*safe_bool)();
  36. public:
  37. // Constructor
  38. assertion_result( bool pv_ )
  39. : p_predicate_value( pv_ )
  40. {}
  41. template<typename BoolConvertable>
  42. assertion_result( BoolConvertable const& pv_ ) : p_predicate_value( !!pv_ ) {}
  43. // Access methods
  44. bool operator!() const { return !p_predicate_value; }
  45. void operator=( bool pv_ ) { p_predicate_value.value = pv_; }
  46. operator safe_bool() const { return !!p_predicate_value ? &dummy::nonnull : 0; }
  47. // Public properties
  48. BOOST_READONLY_PROPERTY( bool, (assertion_result) ) p_predicate_value;
  49. // Access methods
  50. bool has_empty_message() const { return !m_message; }
  51. wrap_stringstream& message()
  52. {
  53. if( !m_message )
  54. m_message.reset( new wrap_stringstream );
  55. return *m_message;
  56. }
  57. const_string message() const { return !m_message ? const_string() : const_string( m_message->str() ); }
  58. private:
  59. // Data members
  60. shared_ptr<wrap_stringstream> m_message;
  61. };
  62. typedef assertion_result predicate_result;
  63. } // namespace test_tools
  64. } // namespace boost
  65. #include <boost/test/detail/enable_warnings.hpp>
  66. #endif // BOOST_TEST_PREDICATE_RESULT_HPP_012705GER