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)
8 * \file filt_matches_spirit_qi.cpp
9 * \author Andrey Semashev
12 * \brief This header contains tests for the \c matches filter with Boost.Spirit.Qi backend.
15 #define BOOST_TEST_MODULE filt_matches_spirit_qi
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"
29 namespace logging
= boost::log
;
30 namespace attrs
= logging::attributes
;
31 namespace expr
= logging::expressions
;
33 namespace spirit
= boost::spirit::qi
;
35 // The test checks that string matching works
36 BOOST_AUTO_TEST_CASE(matching_check
)
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
;
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!");
47 attr_set set1
, set2
, set3
;
48 set1
[data::attr1()] = attr1
;
49 set1
[data::attr2()] = attr2
;
50 set1
[data::attr3()] = attr3
;
52 attr_values
values1(set1
, set2
, set3
);
55 filter f
= expr::matches
< std::string
>(data::attr1(), spirit::uint_
>> '.' >> spirit::uint_
>> '.' >> spirit::uint_
>> '.' >> spirit::uint_
);
56 BOOST_CHECK(f(values1
));
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
));
62 f
= expr::matches
< std::string
>(data::attr1(), *spirit::lower
);
63 BOOST_CHECK(!f(values1
));
65 f
= expr::matches
< logging::string_literal
>(data::attr2(), *spirit::upper
>> spirit::space
>> *spirit::lower
>> spirit::space
>> *spirit::alpha
);
66 BOOST_CHECK(f(values1
));
68 f
= expr::matches
< std::string
>(data::attr3(), spirit::lit("Hello, world!"));
69 BOOST_CHECK(f(values1
));
71 // Attribute value not present
72 f
= expr::matches
< std::string
>(data::attr4(), *spirit::char_
);
73 BOOST_CHECK(!f(values1
));
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
));
80 // The test checks that the filter composition works
81 BOOST_AUTO_TEST_CASE(composition_check
)
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
;
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!");
92 attr_set set1
, set2
, set3
;
93 attr_values
values1(set1
, set2
, set3
);
95 set1
[data::attr2()] = attr2
;
96 attr_values
values2(set1
, set2
, set3
);
98 set1
[data::attr3()] = attr3
;
99 set1
[data::attr1()] = attr1
;
100 attr_values
values3(set1
, set2
, set3
);
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
));
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
));