• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   filt_has_attr.cpp
9  * \author Andrey Semashev
10  * \date   31.01.2009
11  *
12  * \brief  This header contains tests for the \c has_attr filter.
13  */
14 
15 #define BOOST_TEST_MODULE filt_has_attr
16 
17 #include <string>
18 #include <boost/mpl/vector.hpp>
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/attributes/attribute_value_set.hpp>
23 #include <boost/log/expressions.hpp>
24 #include "char_definitions.hpp"
25 
26 namespace logging = boost::log;
27 namespace attrs = logging::attributes;
28 namespace expr = logging::expressions;
29 
30 // The test checks that the filter detects the attribute value presence
BOOST_AUTO_TEST_CASE(presence_check)31 BOOST_AUTO_TEST_CASE(presence_check)
32 {
33     typedef logging::attribute_set attr_set;
34     typedef logging::attribute_value_set attr_values;
35     typedef logging::filter filter;
36     typedef test_data< char > data;
37 
38     attrs::constant< int > attr1(10);
39     attrs::constant< double > attr2(5.5);
40     attrs::constant< std::string > attr3("Hello, world!");
41 
42     attr_set set1, set2, set3;
43     set1[data::attr1()] = attr1;
44     set1[data::attr2()] = attr2;
45     set1[data::attr3()] = attr3;
46 
47     attr_values values1(set1, set2, set3);
48     values1.freeze();
49 
50     filter f = expr::has_attr(data::attr1());
51     BOOST_CHECK(f(values1));
52 
53     f = expr::has_attr(data::attr4());
54     BOOST_CHECK(!f(values1));
55 
56     f = expr::has_attr< double >(data::attr2());
57     BOOST_CHECK(f(values1));
58 
59     f = expr::has_attr< double >(data::attr3());
60     BOOST_CHECK(!f(values1));
61 
62     typedef mpl::vector< unsigned int, char, std::string >::type value_types;
63     f = expr::has_attr< value_types >(data::attr3());
64     BOOST_CHECK(f(values1));
65 
66     f = expr::has_attr< value_types >(data::attr1());
67     BOOST_CHECK(!f(values1));
68 }
69 
70 // The test checks that the filter composition works
BOOST_AUTO_TEST_CASE(composition_check)71 BOOST_AUTO_TEST_CASE(composition_check)
72 {
73     typedef logging::attribute_set attr_set;
74     typedef logging::attribute_value_set attr_values;
75     typedef logging::filter filter;
76     typedef test_data< char > data;
77 
78     attrs::constant< int > attr1(10);
79     attrs::constant< double > attr2(5.5);
80     attrs::constant< std::string > attr3("Hello, world!");
81 
82     attr_set set1, set2, set3;
83     attr_values values1(set1, set2, set3);
84     values1.freeze();
85     set1[data::attr2()] = attr2;
86     attr_values values2(set1, set2, set3);
87     values2.freeze();
88     set1[data::attr3()] = attr3;
89     set1[data::attr1()] = attr1;
90     attr_values values3(set1, set2, set3);
91     values3.freeze();
92 
93     filter f = expr::has_attr(data::attr1()) || expr::has_attr< double >(data::attr2());
94     BOOST_CHECK(!f(values1));
95     BOOST_CHECK(f(values2));
96     BOOST_CHECK(f(values3));
97 
98     f = expr::has_attr(data::attr1()) && expr::has_attr< double >(data::attr2());
99     BOOST_CHECK(!f(values1));
100     BOOST_CHECK(!f(values2));
101     BOOST_CHECK(f(values3));
102 }
103