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)
8 * \file filt_matches_boost_regex.cpp
9 * \author Andrey Semashev
12 * \brief This header contains tests for the \c matches filter with Boost.Regex backend.
15 #define BOOST_TEST_MODULE filt_matches_boost_regex
18 #include <boost/regex.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 <boost/log/support/regex.hpp>
25 #include <boost/log/utility/string_literal.hpp>
26 #include "char_definitions.hpp"
28 namespace logging = boost::log;
29 namespace attrs = logging::attributes;
30 namespace expr = logging::expressions;
32 // The test checks that string matching works
33 BOOST_AUTO_TEST_CASE(matching_check)
35 typedef logging::attribute_set attr_set;
36 typedef logging::attribute_value_set attr_values;
37 typedef logging::filter filter;
38 typedef test_data< char > data;
39 typedef boost::regex regex_type;
41 attrs::constant< std::string > attr1("127.0.0.1");
42 attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
43 attrs::constant< std::string > attr3("Hello, world!");
45 attr_set set1, set2, set3;
46 set1[data::attr1()] = attr1;
47 set1[data::attr2()] = attr2;
48 set1[data::attr3()] = attr3;
50 attr_values values1(set1, set2, set3);
53 filter f = expr::matches< std::string >(data::attr1(), regex_type("\\d+\\.\\d+\\.\\d+\\.\\d+"));
54 BOOST_CHECK(f(values1));
56 f = expr::matches< std::string >(data::attr1(), regex_type("[a-z]*"));
57 BOOST_CHECK(!f(values1));
59 f = expr::matches< logging::string_literal >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
60 BOOST_CHECK(f(values1));
62 f = expr::matches< std::string >(data::attr3(), regex_type("Hello, world!"));
63 BOOST_CHECK(f(values1));
65 // Attribute value not present
66 f = expr::matches< std::string >(data::attr4(), regex_type(".*"));
67 BOOST_CHECK(!f(values1));
69 // Attribute value type does not match
70 f = expr::matches< std::string >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
71 BOOST_CHECK(!f(values1));
74 // The test checks that the filter composition works
75 BOOST_AUTO_TEST_CASE(composition_check)
77 typedef logging::attribute_set attr_set;
78 typedef logging::attribute_value_set attr_values;
79 typedef logging::filter filter;
80 typedef test_data< char > data;
81 typedef boost::regex regex_type;
83 attrs::constant< std::string > attr1("127.0.0.1");
84 attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
85 attrs::constant< std::string > attr3("Hello, world!");
87 attr_set set1, set2, set3;
88 attr_values values1(set1, set2, set3);
90 set1[data::attr2()] = attr2;
91 attr_values values2(set1, set2, set3);
93 set1[data::attr3()] = attr3;
94 set1[data::attr1()] = attr1;
95 attr_values values3(set1, set2, set3);
98 filter f = expr::matches< std::string >(data::attr1(), regex_type("\\d+\\.\\d+\\.\\d+\\.\\d+")) || expr::matches< logging::string_literal >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
99 BOOST_CHECK(!f(values1));
100 BOOST_CHECK(f(values2));
101 BOOST_CHECK(f(values3));
103 f = expr::matches< std::string >(data::attr1(), regex_type("\\d+\\.\\d+\\.\\d+\\.\\d+")) && expr::matches< logging::string_literal >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
104 BOOST_CHECK(!f(values1));
105 BOOST_CHECK(!f(values2));
106 BOOST_CHECK(f(values3));