form_auto_newline.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 form_auto_newline.cpp
  9. * \author Andrey Semashev
  10. * \date 23.06.2019
  11. *
  12. * \brief This header contains tests for the auto_newline formatter.
  13. */
  14. #define BOOST_TEST_MODULE form_auto_newline
  15. #include <string>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/log/expressions.hpp>
  18. #include <boost/log/utility/formatting_ostream.hpp>
  19. #include "char_definitions.hpp"
  20. #include "make_record.hpp"
  21. namespace logging = boost::log;
  22. namespace expr = logging::expressions;
  23. // Test appending a newline to a non-empty string
  24. BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_non_empty_string, CharT, char_types)
  25. {
  26. typedef CharT char_type;
  27. typedef std::basic_ostringstream< char_type > ostream_type;
  28. typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
  29. typedef typename formatting_ostream_type::string_type string_type;
  30. typedef logging::record_view record_view;
  31. typedef logging::basic_formatter< char_type > formatter;
  32. record_view rec = make_record_view();
  33. string_type str_fmt;
  34. formatting_ostream_type strm_fmt(str_fmt);
  35. formatter f = expr::stream << "Hello" << expr::auto_newline;
  36. f(rec, strm_fmt);
  37. ostream_type strm_correct;
  38. strm_correct << "Hello\n";
  39. BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
  40. }
  41. // Test appending a newline to an empty string
  42. BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_empty_string, CharT, char_types)
  43. {
  44. typedef CharT char_type;
  45. typedef std::basic_ostringstream< char_type > ostream_type;
  46. typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
  47. typedef typename formatting_ostream_type::string_type string_type;
  48. typedef logging::record_view record_view;
  49. typedef logging::basic_formatter< char_type > formatter;
  50. record_view rec = make_record_view();
  51. string_type str_fmt;
  52. formatting_ostream_type strm_fmt(str_fmt);
  53. formatter f = expr::stream << expr::auto_newline;
  54. f(rec, strm_fmt);
  55. ostream_type strm_correct;
  56. strm_correct << "\n";
  57. BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
  58. }
  59. // Test not appending a newline to a non-empty string which already ends with a newline
  60. BOOST_AUTO_TEST_CASE_TEMPLATE(not_append_if_ends_with_a_newline, CharT, char_types)
  61. {
  62. typedef CharT char_type;
  63. typedef std::basic_ostringstream< char_type > ostream_type;
  64. typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
  65. typedef typename formatting_ostream_type::string_type string_type;
  66. typedef logging::record_view record_view;
  67. typedef logging::basic_formatter< char_type > formatter;
  68. record_view rec = make_record_view();
  69. string_type str_fmt;
  70. formatting_ostream_type strm_fmt(str_fmt);
  71. formatter f = expr::stream << "Hello\n" << expr::auto_newline;
  72. f(rec, strm_fmt);
  73. ostream_type strm_correct;
  74. strm_correct << "Hello\n";
  75. BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
  76. }