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_std_regex.cpp
9 * \author Andrey Semashev
10 * \date 30.03.2014
11 *
12 * \brief This header contains tests for the \c matches filter with \c std::regex backend.
13 */
14
15 #define BOOST_TEST_MODULE filt_matches_std_regex
16
17 #include <boost/config.hpp>
18
19 #if !defined(BOOST_NO_CXX11_HDR_REGEX)
20
21 #include <string>
22 #include <regex>
23 #include <boost/test/unit_test.hpp>
24 #include <boost/log/attributes/constant.hpp>
25 #include <boost/log/attributes/attribute_set.hpp>
26 #include <boost/log/attributes/attribute_value_set.hpp>
27 #include <boost/log/expressions.hpp>
28 #include <boost/log/support/std_regex.hpp>
29 #include <boost/log/utility/string_literal.hpp>
30 #include "char_definitions.hpp"
31
32 namespace logging = boost::log;
33 namespace attrs = logging::attributes;
34 namespace expr = logging::expressions;
35
36 // The test checks that string matching works
BOOST_AUTO_TEST_CASE(matching_check)37 BOOST_AUTO_TEST_CASE(matching_check)
38 {
39 typedef logging::attribute_set attr_set;
40 typedef logging::attribute_value_set attr_values;
41 typedef logging::filter filter;
42 typedef test_data< char > data;
43 typedef std::regex regex_type;
44
45 attrs::constant< std::string > attr1("127.0.0.1");
46 attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
47 attrs::constant< std::string > attr3("Hello, world!");
48
49 attr_set set1, set2, set3;
50 set1[data::attr1()] = attr1;
51 set1[data::attr2()] = attr2;
52 set1[data::attr3()] = attr3;
53
54 attr_values values1(set1, set2, set3);
55 values1.freeze();
56
57 filter f = expr::matches< std::string >(data::attr1(), regex_type("\\d+\\.\\d+\\.\\d+\\.\\d+"));
58 BOOST_CHECK(f(values1));
59
60 f = expr::matches< std::string >(data::attr1(), regex_type("[a-z]*"));
61 BOOST_CHECK(!f(values1));
62
63 f = expr::matches< logging::string_literal >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
64 BOOST_CHECK(f(values1));
65
66 f = expr::matches< std::string >(data::attr3(), regex_type("Hello, world!"));
67 BOOST_CHECK(f(values1));
68
69 // Attribute value not present
70 f = expr::matches< std::string >(data::attr4(), regex_type(".*"));
71 BOOST_CHECK(!f(values1));
72
73 // Attribute value type does not match
74 f = expr::matches< std::string >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
75 BOOST_CHECK(!f(values1));
76 }
77
78 // The test checks that the filter composition works
BOOST_AUTO_TEST_CASE(composition_check)79 BOOST_AUTO_TEST_CASE(composition_check)
80 {
81 typedef logging::attribute_set attr_set;
82 typedef logging::attribute_value_set attr_values;
83 typedef logging::filter filter;
84 typedef test_data< char > data;
85 typedef std::regex regex_type;
86
87 attrs::constant< std::string > attr1("127.0.0.1");
88 attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
89 attrs::constant< std::string > attr3("Hello, world!");
90
91 attr_set set1, set2, set3;
92 attr_values values1(set1, set2, set3);
93 values1.freeze();
94 set1[data::attr2()] = attr2;
95 attr_values values2(set1, set2, set3);
96 values2.freeze();
97 set1[data::attr3()] = attr3;
98 set1[data::attr1()] = attr1;
99 attr_values values3(set1, set2, set3);
100 values3.freeze();
101
102 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]*"));
103 BOOST_CHECK(!f(values1));
104 BOOST_CHECK(f(values2));
105 BOOST_CHECK(f(values3));
106
107 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]*"));
108 BOOST_CHECK(!f(values1));
109 BOOST_CHECK(!f(values2));
110 BOOST_CHECK(f(values3));
111 }
112
113 #else // !defined(BOOST_NO_CXX11_HDR_REGEX)
114
main(int,char * [])115 int main(int, char*[])
116 {
117 return 0;
118 }
119
120 #endif // !defined(BOOST_NO_CXX11_HDR_REGEX)
121