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