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 form_if.cpp
9 * \author Andrey Semashev
10 * \date 05.02.2009
11 *
12 * \brief This header contains tests for the \c if_ formatter.
13 */
14
15 #define BOOST_TEST_MODULE form_if
16
17 #include <string>
18 #include <ostream>
19 #include <boost/test/unit_test.hpp>
20 #include <boost/log/attributes/constant.hpp>
21 #include <boost/log/attributes/attribute_set.hpp>
22 #include <boost/log/expressions.hpp>
23 #include <boost/log/core/record.hpp>
24 #include <boost/log/utility/formatting_ostream.hpp>
25 #include "char_definitions.hpp"
26 #include "make_record.hpp"
27
28 namespace logging = boost::log;
29 namespace attrs = logging::attributes;
30 namespace expr = logging::expressions;
31
32 // The test checks that conditional formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(conditional_formatting,CharT,char_types)33 BOOST_AUTO_TEST_CASE_TEMPLATE(conditional_formatting, CharT, char_types)
34 {
35 typedef logging::attribute_set attr_set;
36 typedef std::basic_string< CharT > string;
37 typedef logging::basic_formatting_ostream< CharT > osstream;
38 typedef logging::record_view record_view;
39 typedef logging::basic_formatter< CharT > formatter;
40 typedef test_data< CharT > data;
41
42 attrs::constant< int > attr1(10);
43 attrs::constant< double > attr2(5.5);
44
45 attr_set set1;
46 set1[data::attr1()] = attr1;
47 set1[data::attr2()] = attr2;
48
49 record_view rec = make_record_view(set1);
50
51 // Check for various modes of attribute value type specification
52 {
53 string str1, str2;
54 osstream strm1(str1), strm2(str2);
55 formatter f = expr::stream <<
56 expr::if_(expr::has_attr< int >(data::attr1()))
57 [
58 expr::stream << expr::attr< int >(data::attr1())
59 ];
60 f(rec, strm1);
61 strm2 << 10;
62 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
63 }
64 {
65 string str1;
66 osstream strm1(str1);
67 formatter f = expr::stream <<
68 expr::if_(expr::has_attr< int >(data::attr2()))
69 [
70 expr::stream << expr::attr< int >(data::attr2())
71 ];
72 f(rec, strm1);
73 BOOST_CHECK(equal_strings(strm1.str(), string()));
74 }
75 {
76 string str1, str2;
77 osstream strm1(str1), strm2(str2);
78 formatter f = expr::stream <<
79 expr::if_(expr::has_attr< int >(data::attr1()))
80 [
81 expr::stream << expr::attr< int >(data::attr1())
82 ]
83 .else_
84 [
85 expr::stream << expr::attr< double >(data::attr2())
86 ];
87 f(rec, strm1);
88 strm2 << 10;
89 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
90 }
91 {
92 string str1, str2;
93 osstream strm1(str1), strm2(str2);
94 formatter f = expr::stream <<
95 expr::if_(expr::has_attr< int >(data::attr2()))
96 [
97 expr::stream << expr::attr< int >(data::attr1())
98 ]
99 .else_
100 [
101 expr::stream << expr::attr< double >(data::attr2())
102 ];
103 f(rec, strm1);
104 strm2 << 5.5;
105 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
106 }
107 }
108