Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / log / test / run / filt_matches_boost_regex.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_boost_regex.cpp
9  * \author Andrey Semashev
10  * \date   30.03.2014
11  *
12  * \brief  This header contains tests for the \c matches filter with Boost.Regex backend.
13  */
14
15 #define BOOST_TEST_MODULE filt_matches_boost_regex
16
17 #include <string>
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"
27
28 namespace logging = boost::log;
29 namespace attrs = logging::attributes;
30 namespace expr = logging::expressions;
31
32 // The test checks that string matching works
33 BOOST_AUTO_TEST_CASE(matching_check)
34 {
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;
40
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!");
44
45     attr_set set1, set2, set3;
46     set1[data::attr1()] = attr1;
47     set1[data::attr2()] = attr2;
48     set1[data::attr3()] = attr3;
49
50     attr_values values1(set1, set2, set3);
51     values1.freeze();
52
53     filter f = expr::matches< std::string >(data::attr1(), regex_type("\\d+\\.\\d+\\.\\d+\\.\\d+"));
54     BOOST_CHECK(f(values1));
55
56     f = expr::matches< std::string >(data::attr1(), regex_type("[a-z]*"));
57     BOOST_CHECK(!f(values1));
58
59     f = expr::matches< logging::string_literal >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
60     BOOST_CHECK(f(values1));
61
62     f = expr::matches< std::string >(data::attr3(), regex_type("Hello, world!"));
63     BOOST_CHECK(f(values1));
64
65     // Attribute value not present
66     f = expr::matches< std::string >(data::attr4(), regex_type(".*"));
67     BOOST_CHECK(!f(values1));
68
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));
72 }
73
74 // The test checks that the filter composition works
75 BOOST_AUTO_TEST_CASE(composition_check)
76 {
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;
82
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!");
86
87     attr_set set1, set2, set3;
88     attr_values values1(set1, set2, set3);
89     values1.freeze();
90     set1[data::attr2()] = attr2;
91     attr_values values2(set1, set2, set3);
92     values2.freeze();
93     set1[data::attr3()] = attr3;
94     set1[data::attr1()] = attr1;
95     attr_values values3(set1, set2, set3);
96     values3.freeze();
97
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));
102
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));
107 }