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_classic.cpp
9 * \author Andrey Semashev
12 * \brief This header contains tests for the \c matches filter with Boost.Spirit.Classic backend.
15 #define BOOST_TEST_MODULE filt_matches_spirit_classic
17 #include <boost/log/detail/config.hpp>
19 #if !defined(BOOST_LOG_NO_THREADS)
20 #define BOOST_SPIRIT_THREADSAFE 1
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"
34 namespace logging
= boost::log
;
35 namespace attrs
= logging::attributes
;
36 namespace expr
= logging::expressions
;
38 namespace spirit
= boost::spirit::classic
;
40 // The test checks that string matching works
41 BOOST_AUTO_TEST_CASE(matching_check
)
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
;
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!");
52 attr_set set1
, set2
, set3
;
53 set1
[data::attr1()] = attr1
;
54 set1
[data::attr2()] = attr2
;
55 set1
[data::attr3()] = attr3
;
57 attr_values
values1(set1
, set2
, set3
);
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
));
63 f
= expr::matches
< std::string
>(data::attr1(), *spirit::lower_p
);
64 BOOST_CHECK(!f(values1
));
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
));
69 f
= expr::matches
< std::string
>(data::attr3(), spirit::str_p("Hello, world!"));
70 BOOST_CHECK(f(values1
));
72 // Attribute value not present
73 f
= expr::matches
< std::string
>(data::attr4(), *spirit::anychar_p
);
74 BOOST_CHECK(!f(values1
));
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
));
81 // The test checks that the filter composition works
82 BOOST_AUTO_TEST_CASE(composition_check
)
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
;
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!");
93 attr_set set1
, set2
, set3
;
94 attr_values
values1(set1
, set2
, set3
);
96 set1
[data::attr2()] = attr2
;
97 attr_values
values2(set1
, set2
, set3
);
99 set1
[data::attr3()] = attr3
;
100 set1
[data::attr1()] = attr1
;
101 attr_values
values3(set1
, set2
, set3
);
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
));
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
));