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
15 #define BOOST_TEST_MODULE util_manip_auto_newline
16
17 #include <string>
18 #include <boost/test/unit_test.hpp>
19 #include <boost/log/utility/manipulators/auto_newline.hpp>
20 #include <boost/log/utility/formatting_ostream.hpp>
21 #include "char_definitions.hpp"
22
23 namespace logging = boost::log;
24
25 // Test appending a newline to a non-empty string
BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_non_empty_string,CharT,char_types)26 BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_non_empty_string, CharT, char_types)
27 {
28 typedef CharT char_type;
29 typedef std::basic_ostringstream< char_type > ostream_type;
30 typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
31 typedef typename formatting_ostream_type::string_type string_type;
32
33 string_type str_fmt;
34 formatting_ostream_type strm_fmt(str_fmt);
35
36 strm_fmt << "Hello" << logging::auto_newline;
37 strm_fmt.flush();
38
39 ostream_type strm_correct;
40 strm_correct << "Hello\n";
41
42 BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
43 }
44
45 // Test appending a newline to an empty string
BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_empty_string,CharT,char_types)46 BOOST_AUTO_TEST_CASE_TEMPLATE(append_to_empty_string, CharT, char_types)
47 {
48 typedef CharT char_type;
49 typedef std::basic_ostringstream< char_type > ostream_type;
50 typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
51 typedef typename formatting_ostream_type::string_type string_type;
52
53 string_type str_fmt;
54 formatting_ostream_type strm_fmt(str_fmt);
55
56 strm_fmt << logging::auto_newline;
57 strm_fmt.flush();
58
59 ostream_type strm_correct;
60 strm_correct << "\n";
61
62 BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
63 }
64
65 // Test not appending a newline to a non-empty string which already ends with a newline
BOOST_AUTO_TEST_CASE_TEMPLATE(not_append_if_ends_with_a_newline,CharT,char_types)66 BOOST_AUTO_TEST_CASE_TEMPLATE(not_append_if_ends_with_a_newline, CharT, char_types)
67 {
68 typedef CharT char_type;
69 typedef std::basic_ostringstream< char_type > ostream_type;
70 typedef logging::basic_formatting_ostream< char_type > formatting_ostream_type;
71 typedef typename formatting_ostream_type::string_type string_type;
72
73 string_type str_fmt;
74 formatting_ostream_type strm_fmt(str_fmt);
75
76 strm_fmt << "Hello\n" << logging::auto_newline;
77 strm_fmt.flush();
78
79 ostream_type strm_correct;
80 strm_correct << "Hello\n";
81
82 BOOST_CHECK(equal_strings(strm_fmt.str(), strm_correct.str()));
83 }
84