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_matches_spirit_qi.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.Qi backend.
13  */
14 
15 #define BOOST_TEST_MODULE filt_matches_spirit_qi
16 
17 #include <string>
18 #include <boost/spirit/include/qi_core.hpp>
19 #include <boost/spirit/include/qi_rule.hpp>
20 #include <boost/test/unit_test.hpp>
21 #include <boost/log/attributes/constant.hpp>
22 #include <boost/log/attributes/attribute_set.hpp>
23 #include <boost/log/attributes/attribute_value_set.hpp>
24 #include <boost/log/expressions.hpp>
25 #include <boost/log/support/spirit_qi.hpp>
26 #include <boost/log/utility/string_literal.hpp>
27 #include "char_definitions.hpp"
28 
29 namespace logging = boost::log;
30 namespace attrs = logging::attributes;
31 namespace expr = logging::expressions;
32 
33 namespace spirit = boost::spirit::qi;
34 
35 // The test checks that string matching works
BOOST_AUTO_TEST_CASE(matching_check)36 BOOST_AUTO_TEST_CASE(matching_check)
37 {
38     typedef logging::attribute_set attr_set;
39     typedef logging::attribute_value_set attr_values;
40     typedef logging::filter filter;
41     typedef test_data< char > data;
42 
43     attrs::constant< std::string > attr1("127.0.0.1");
44     attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
45     attrs::constant< std::string > attr3("Hello, world!");
46 
47     attr_set set1, set2, set3;
48     set1[data::attr1()] = attr1;
49     set1[data::attr2()] = attr2;
50     set1[data::attr3()] = attr3;
51 
52     attr_values values1(set1, set2, set3);
53     values1.freeze();
54 
55     filter f = expr::matches< std::string >(data::attr1(), spirit::uint_ >> '.' >> spirit::uint_ >> '.' >> spirit::uint_ >> '.' >> spirit::uint_);
56     BOOST_CHECK(f(values1));
57 
58     // Make sure rules also work
59     f = expr::matches< std::string >(data::attr1(), spirit::rule< std::string::const_iterator, void() >(spirit::uint_ >> '.' >> spirit::uint_ >> '.' >> spirit::uint_ >> '.' >> spirit::uint_));
60     BOOST_CHECK(f(values1));
61 
62     f = expr::matches< std::string >(data::attr1(), *spirit::lower);
63     BOOST_CHECK(!f(values1));
64 
65     f = expr::matches< logging::string_literal >(data::attr2(), *spirit::upper >> spirit::space >> *spirit::lower >> spirit::space >> *spirit::alpha);
66     BOOST_CHECK(f(values1));
67 
68     f = expr::matches< std::string >(data::attr3(), spirit::lit("Hello, world!"));
69     BOOST_CHECK(f(values1));
70 
71     // Attribute value not present
72     f = expr::matches< std::string >(data::attr4(), *spirit::char_);
73     BOOST_CHECK(!f(values1));
74 
75     // Attribute value type does not match
76     f = expr::matches< std::string >(data::attr2(), *spirit::upper >> spirit::space >> *spirit::lower >> spirit::space >> *spirit::alpha);
77     BOOST_CHECK(!f(values1));
78 }
79 
80 // The test checks that the filter composition works
BOOST_AUTO_TEST_CASE(composition_check)81 BOOST_AUTO_TEST_CASE(composition_check)
82 {
83     typedef logging::attribute_set attr_set;
84     typedef logging::attribute_value_set attr_values;
85     typedef logging::filter filter;
86     typedef test_data< char > data;
87 
88     attrs::constant< std::string > attr1("127.0.0.1");
89     attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
90     attrs::constant< std::string > attr3("Hello, world!");
91 
92     attr_set set1, set2, set3;
93     attr_values values1(set1, set2, set3);
94     values1.freeze();
95     set1[data::attr2()] = attr2;
96     attr_values values2(set1, set2, set3);
97     values2.freeze();
98     set1[data::attr3()] = attr3;
99     set1[data::attr1()] = attr1;
100     attr_values values3(set1, set2, set3);
101     values3.freeze();
102 
103     filter f = expr::matches< std::string >(data::attr1(), +spirit::digit >> '.' >> +spirit::digit >> '.' >> +spirit::digit >> '.' >> +spirit::digit)
104             || expr::matches< logging::string_literal >(data::attr2(), *spirit::upper >> spirit::space >> *spirit::lower >> spirit::space >> *spirit::alpha);
105     BOOST_CHECK(!f(values1));
106     BOOST_CHECK(f(values2));
107     BOOST_CHECK(f(values3));
108 
109     f = expr::matches< std::string >(data::attr1(), +spirit::digit >> '.' >> +spirit::digit >> '.' >> +spirit::digit >> '.' >> +spirit::digit)
110             && expr::matches< logging::string_literal >(data::attr2(), *spirit::upper >> spirit::space >> *spirit::lower >> spirit::space >> *spirit::alpha);
111     BOOST_CHECK(!f(values1));
112     BOOST_CHECK(!f(values2));
113     BOOST_CHECK(f(values3));
114 }
115