Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / log / test / run / form_format.cpp
1 /*
2  *          Copyright Andrey Semashev 2007 - 2014.
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   form_format.cpp
9  * \author Andrey Semashev
10  * \date   07.02.2009
11  *
12  * \brief  This header contains tests for the Boost.Format-style formatting.
13  */
14
15 #define BOOST_TEST_MODULE form_format
16
17 #include <string>
18 #include <boost/test/unit_test.hpp>
19 #include <boost/log/attributes/constant.hpp>
20 #include <boost/log/attributes/attribute_set.hpp>
21 #include <boost/log/expressions.hpp>
22 #include <boost/log/core/record.hpp>
23 #include <boost/log/utility/formatting_ostream.hpp>
24 #include "char_definitions.hpp"
25 #include "make_record.hpp"
26
27 namespace logging = boost::log;
28 namespace attrs = logging::attributes;
29 namespace expr = logging::expressions;
30
31 namespace {
32
33     template< typename >
34     struct format_test_data;
35
36 #ifdef BOOST_LOG_USE_CHAR
37     template< >
38     struct format_test_data< char > :
39         public test_data< char >
40     {
41         static const char* format1() { return "%1%, %2%"; }
42     };
43 #endif // BOOST_LOG_USE_CHAR
44
45 #ifdef BOOST_LOG_USE_WCHAR_T
46     template< >
47     struct format_test_data< wchar_t > :
48         public test_data< wchar_t >
49     {
50         static const wchar_t* format1() { return L"%1%, %2%"; }
51     };
52 #endif // BOOST_LOG_USE_WCHAR_T
53
54 } // namespace
55
56 // The test checks that Boost.Format formatting works
57 BOOST_AUTO_TEST_CASE_TEMPLATE(formatting, CharT, char_types)
58 {
59     typedef logging::attribute_set attr_set;
60     typedef std::basic_string< CharT > string;
61     typedef logging::basic_formatting_ostream< CharT > osstream;
62     typedef logging::record_view record_view;
63     typedef logging::basic_formatter< CharT > formatter;
64     typedef format_test_data< CharT > data;
65
66     attrs::constant< int > attr1(10);
67     attrs::constant< double > attr2(5.5);
68
69     attr_set set1;
70     set1[data::attr1()] = attr1;
71     set1[data::attr2()] = attr2;
72
73     record_view rec = make_record_view(set1);
74
75     {
76         string str1, str2;
77         osstream strm1(str1), strm2(str2);
78         formatter f = expr::format(data::format1()) % expr::attr< int >(data::attr1()) % expr::attr< double >(data::attr2());
79         f(rec, strm1);
80         strm2 << 10 << ", " << 5.5;
81         BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
82     }
83 }