Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / log / test / run / filt_matches_spirit_classic.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   filt_matches_spirit_classic.cpp
9  * \author Andrey Semashev
10  * \date   30.03.2014
11  *
12  * \brief  This header contains tests for the \c matches filter with Boost.Spirit.Classic backend.
13  */
14
15 #define BOOST_TEST_MODULE filt_matches_spirit_classic
16
17 #include <boost/log/detail/config.hpp>
18
19 #if !defined(BOOST_LOG_NO_THREADS)
20 #define BOOST_SPIRIT_THREADSAFE 1
21 #endif
22
23 #include <string>
24 #include <boost/spirit/include/classic_core.hpp>
25 #include <boost/test/unit_test.hpp>
26 #include <boost/log/attributes/constant.hpp>
27 #include <boost/log/attributes/attribute_set.hpp>
28 #include <boost/log/attributes/attribute_value_set.hpp>
29 #include <boost/log/expressions.hpp>
30 #include <boost/log/support/spirit_classic.hpp>
31 #include <boost/log/utility/string_literal.hpp>
32 #include "char_definitions.hpp"
33
34 namespace logging = boost::log;
35 namespace attrs = logging::attributes;
36 namespace expr = logging::expressions;
37
38 namespace spirit = boost::spirit::classic;
39
40 // The test checks that string matching works
41 BOOST_AUTO_TEST_CASE(matching_check)
42 {
43     typedef logging::attribute_set attr_set;
44     typedef logging::attribute_value_set attr_values;
45     typedef logging::filter filter;
46     typedef test_data< char > data;
47
48     attrs::constant< std::string > attr1("127.0.0.1");
49     attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
50     attrs::constant< std::string > attr3("Hello, world!");
51
52     attr_set set1, set2, set3;
53     set1[data::attr1()] = attr1;
54     set1[data::attr2()] = attr2;
55     set1[data::attr3()] = attr3;
56
57     attr_values values1(set1, set2, set3);
58     values1.freeze();
59
60     filter f = expr::matches< std::string >(data::attr1(), spirit::uint_p >> '.' >> spirit::uint_p >> '.' >> spirit::uint_p >> '.' >> spirit::uint_p);
61     BOOST_CHECK(f(values1));
62
63     f = expr::matches< std::string >(data::attr1(), *spirit::lower_p);
64     BOOST_CHECK(!f(values1));
65
66     f = expr::matches< logging::string_literal >(data::attr2(), *spirit::upper_p >> spirit::space_p >> *spirit::lower_p >> spirit::space_p >> *spirit::alpha_p);
67     BOOST_CHECK(f(values1));
68
69     f = expr::matches< std::string >(data::attr3(), spirit::str_p("Hello, world!"));
70     BOOST_CHECK(f(values1));
71
72     // Attribute value not present
73     f = expr::matches< std::string >(data::attr4(), *spirit::anychar_p);
74     BOOST_CHECK(!f(values1));
75
76     // Attribute value type does not match
77     f = expr::matches< std::string >(data::attr2(), *spirit::upper_p >> spirit::space_p >> *spirit::lower_p >> spirit::space_p >> *spirit::alpha_p);
78     BOOST_CHECK(!f(values1));
79 }
80
81 // The test checks that the filter composition works
82 BOOST_AUTO_TEST_CASE(composition_check)
83 {
84     typedef logging::attribute_set attr_set;
85     typedef logging::attribute_value_set attr_values;
86     typedef logging::filter filter;
87     typedef test_data< char > data;
88
89     attrs::constant< std::string > attr1("127.0.0.1");
90     attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
91     attrs::constant< std::string > attr3("Hello, world!");
92
93     attr_set set1, set2, set3;
94     attr_values values1(set1, set2, set3);
95     values1.freeze();
96     set1[data::attr2()] = attr2;
97     attr_values values2(set1, set2, set3);
98     values2.freeze();
99     set1[data::attr3()] = attr3;
100     set1[data::attr1()] = attr1;
101     attr_values values3(set1, set2, set3);
102     values3.freeze();
103
104     filter f = expr::matches< std::string >(data::attr1(), +spirit::digit_p >> '.' >> +spirit::digit_p >> '.' >> +spirit::digit_p >> '.' >> +spirit::digit_p)
105             || expr::matches< logging::string_literal >(data::attr2(), *spirit::upper_p >> spirit::space_p >> *spirit::lower_p >> spirit::space_p >> *spirit::alpha_p);
106     BOOST_CHECK(!f(values1));
107     BOOST_CHECK(f(values2));
108     BOOST_CHECK(f(values3));
109
110     f = expr::matches< std::string >(data::attr1(), +spirit::digit_p >> '.' >> +spirit::digit_p >> '.' >> +spirit::digit_p >> '.' >> +spirit::digit_p)
111             && expr::matches< logging::string_literal >(data::attr2(), *spirit::upper_p >> spirit::space_p >> *spirit::lower_p >> spirit::space_p >>*spirit::alpha_p);
112     BOOST_CHECK(!f(values1));
113     BOOST_CHECK(!f(values2));
114     BOOST_CHECK(f(values3));
115 }