1 /*
2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
6 */
7 /*!
8 * \file util_manip_to_log.cpp
9 * \author Andrey Semashev
10 * \date 05.07.2015
11 *
12 * \brief This header contains tests for the \c to_log stream manipulator.
13 */
14
15 #define BOOST_TEST_MODULE util_manip_to_log
16
17 #include <string>
18 #include <sstream>
19 #include <algorithm>
20 #include <boost/test/unit_test.hpp>
21 #include <boost/log/utility/formatting_ostream.hpp>
22 #include <boost/log/utility/manipulators/to_log.hpp>
23 #include "char_definitions.hpp"
24
25 namespace logging = boost::log;
26
27 namespace tag {
28
29 struct a_my_class;
30
31 } // namespace tag
32
33 namespace {
34
35 struct my_class
36 {
37 int m_Data;
38
my_class__anone5322a880111::my_class39 explicit my_class(int data) : m_Data(data) {}
40 };
41
42 template< typename CharT, typename TraitsT >
43 inline std::basic_ostream< CharT, TraitsT >&
operator <<(std::basic_ostream<CharT,TraitsT> & strm,my_class const & obj)44 operator<< (std::basic_ostream< CharT, TraitsT >& strm, my_class const& obj)
45 {
46 strm << "my_class: [data: " << obj.m_Data << "]";
47 return strm;
48 }
49
50 template< typename StreamT >
51 inline StreamT&
operator <<(StreamT & strm,logging::to_log_manip<my_class> const & obj)52 operator<< (StreamT& strm, logging::to_log_manip< my_class > const& obj)
53 {
54 strm << "to_log(my_class: [data: " << obj.get().m_Data << "])";
55 return strm;
56 }
57
58 template< typename StreamT >
59 inline StreamT&
operator <<(StreamT & strm,logging::to_log_manip<my_class,tag::a_my_class> const & obj)60 operator<< (StreamT& strm, logging::to_log_manip< my_class, tag::a_my_class > const& obj)
61 {
62 strm << "to_log<a_my_class>(my_class: [data: " << obj.get().m_Data << "])";
63 return strm;
64 }
65
66 template< typename CharT, typename StreamT >
67 struct tests
68 {
69 typedef CharT char_type;
70 typedef StreamT stream_type;
71 typedef std::basic_string< char_type > string;
72 typedef std::basic_ostringstream< char_type > std_stream;
73
74 //! The test verifies that the default behavior of the manipulator is equivalent to the standard operator<<
default_operator__anone5322a880111::tests75 static void default_operator()
76 {
77 string str;
78 stream_type strm1(str);
79 strm1 << logging::to_log(10);
80
81 std_stream strm2;
82 strm2 << 10;
83 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
84 }
85
86 //! The test verifies that operator overrides work
operator_overrides__anone5322a880111::tests87 static void operator_overrides()
88 {
89 {
90 string str;
91 stream_type strm1(str);
92 strm1 << my_class(10);
93
94 std_stream strm2;
95 strm2 << "my_class: [data: " << 10 << "]";
96 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
97 }
98 {
99 string str;
100 stream_type strm1(str);
101 strm1 << logging::to_log(my_class(10));
102
103 std_stream strm2;
104 strm2 << "to_log(my_class: [data: " << 10 << "])";
105 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
106 }
107 {
108 string str;
109 stream_type strm1(str);
110 strm1 << logging::to_log< tag::a_my_class >(my_class(10));
111
112 std_stream strm2;
113 strm2 << "to_log<a_my_class>(my_class: [data: " << 10 << "])";
114 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
115 }
116 }
117 };
118
119 } // namespace
120
121
122 //! The test verifies that the default behavior of the manipulator is equivalent to the standard operator<<
BOOST_AUTO_TEST_CASE_TEMPLATE(default_operator,CharT,char_types)123 BOOST_AUTO_TEST_CASE_TEMPLATE(default_operator, CharT, char_types)
124 {
125 tests< CharT, std::basic_ostringstream< CharT > >::default_operator();
126 tests< CharT, logging::basic_formatting_ostream< CharT > >::default_operator();
127 }
128
129 //! The test verifies that operator overrides work
BOOST_AUTO_TEST_CASE_TEMPLATE(operator_overrides,CharT,char_types)130 BOOST_AUTO_TEST_CASE_TEMPLATE(operator_overrides, CharT, char_types)
131 {
132 tests< CharT, std::basic_ostringstream< CharT > >::operator_overrides();
133 tests< CharT, logging::basic_formatting_ostream< CharT > >::operator_overrides();
134 }
135