util_manip_auto_newline.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright Andrey Semashev 2019.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file util_manip_auto_newline.cpp
  9. * \author Andrey Semashev
  10. * \date 23.06.2019
  11. *
  12. * \brief This header contains tests for the auto_newline manipulator.
  13. */
  14. #define BOOST_TEST_MODULE util_manip_auto_newline
  15. #include <string>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/log/utility/manipulators/auto_newline.hpp>
  18. #include <boost/log/utility/formatting_ostream.hpp>
  19. #include "char_definitions.hpp"
  20. namespace logging = boost::log;
  21. // Test appending a newline to a non-empty string
  22. BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_non_empty_string, CharT, char_types)
  23. {
  24. typedef CharT char_type;
  25. typedef std::basic_ostringstream< char_type > ostream_type;
  26. typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
  27. typedef typename formatting_ostream_type::string_type string_type;
  28. string_type str_fmt;
  29. formatting_ostream_type strm_fmt(str_fmt);
  30. strm_fmt << "Hello" << logging::auto_newline;
  31. strm_fmt.flush();
  32. ostream_type strm_correct;
  33. strm_correct << "Hello\n";
  34. BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
  35. }
  36. // Test appending a newline to an empty string
  37. BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_empty_string, CharT, char_types)
  38. {
  39. typedef CharT char_type;
  40. typedef std::basic_ostringstream< char_type > ostream_type;
  41. typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
  42. typedef typename formatting_ostream_type::string_type string_type;
  43. string_type str_fmt;
  44. formatting_ostream_type strm_fmt(str_fmt);
  45. strm_fmt << logging::auto_newline;
  46. strm_fmt.flush();
  47. ostream_type strm_correct;
  48. strm_correct << "\n";
  49. BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
  50. }
  51. // Test not appending a newline to a non-empty string which already ends with a newline
  52. BOOST_AUTO_TEST_CASE_TEMPLATE(not_append_if_ends_with_a_newline, CharT, char_types)
  53. {
  54. typedef CharT char_type;
  55. typedef std::basic_ostringstream< char_type > ostream_type;
  56. typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
  57. typedef typename formatting_ostream_type::string_type string_type;
  58. string_type str_fmt;
  59. formatting_ostream_type strm_fmt(str_fmt);
  60. strm_fmt << "Hello\n" << logging::auto_newline;
  61. strm_fmt.flush();
  62. ostream_type strm_correct;
  63. strm_correct << "Hello\n";
  64. BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
  65. }