output_test_stream.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /// @brief output_test_stream class definition
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_OUTPUT_TEST_STREAM_HPP_012705GER
  11. #define BOOST_TEST_OUTPUT_TEST_STREAM_HPP_012705GER
  12. // Boost.Test
  13. #include <boost/test/detail/global_typedef.hpp>
  14. #include <boost/test/utils/wrap_stringstream.hpp>
  15. #include <boost/test/tools/assertion_result.hpp>
  16. // STL
  17. #include <cstddef> // for std::size_t
  18. #include <boost/test/detail/suppress_warnings.hpp>
  19. //____________________________________________________________________________//
  20. // ************************************************************************** //
  21. // ************** output_test_stream ************** //
  22. // ************************************************************************** //
  23. namespace boost {
  24. namespace test_tools {
  25. //! Class to be used to simplify testing of ostream-based output operations
  26. class BOOST_TEST_DECL output_test_stream : public wrap_stringstream::wrapped_stream {
  27. typedef unit_test::const_string const_string;
  28. public:
  29. //! Constructor
  30. //!
  31. //!@param[in] pattern_file_name indicates the name of the file for matching. If the
  32. //! string is empty, the standard input or output streams are used instead
  33. //! (depending on match_or_save)
  34. //!@param[in] match_or_save if true, the pattern file will be read, otherwise it will be
  35. //! written
  36. //!@param[in] text_or_binary if false, opens the stream in binary mode. Otherwise the stream
  37. //! is opened with default flags and the carriage returns are ignored.
  38. explicit output_test_stream( const_string pattern_file_name = const_string(),
  39. bool match_or_save = true,
  40. bool text_or_binary = true );
  41. // Destructor
  42. virtual ~output_test_stream();
  43. //! Checks if the stream is empty
  44. //!
  45. //!@param[in] flush_stream if true, flushes the stream after the call
  46. virtual assertion_result is_empty( bool flush_stream = true );
  47. //! Checks the length of the stream
  48. //!
  49. //!@param[in] length target length
  50. //!@param[in] flush_stream if true, flushes the stream after the call. Set to false to call
  51. //! additional checks on the same content.
  52. virtual assertion_result check_length( std::size_t length, bool flush_stream = true );
  53. //! Checks the content of the stream against a string
  54. //!
  55. //!@param[in] arg_ the target stream
  56. //!@param[in] flush_stream if true, flushes the stream after the call.
  57. virtual assertion_result is_equal( const_string arg_, bool flush_stream = true );
  58. //! Checks the content of the stream against a pattern file
  59. //!
  60. //!@param[in] flush_stream if true, flushes/resets the stream after the call.
  61. virtual assertion_result match_pattern( bool flush_stream = true );
  62. //! Flushes the stream
  63. void flush();
  64. protected:
  65. //! Returns the string representation of the stream
  66. //!
  67. //! May be overriden in order to mutate the string before the matching operations.
  68. virtual std::string get_stream_string_representation() const;
  69. private:
  70. // helper functions
  71. //! Length of the stream
  72. std::size_t length();
  73. //! Synching the stream into an internal string representation
  74. virtual void sync();
  75. struct Impl;
  76. Impl* m_pimpl;
  77. };
  78. } // namespace test_tools
  79. } // namespace boost
  80. #include <boost/test/detail/enable_warnings.hpp>
  81. #endif // BOOST_TEST_OUTPUT_TEST_STREAM_HPP_012705GER