filt_matches_std_regex.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define BOOST_TEST_MODULE filt_matches_std_regex
  15. #include <boost/config.hpp>
  16. #if !defined(BOOST_NO_CXX11_HDR_REGEX)
  17. #include <string>
  18. #include <regex>
  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/std_regex.hpp>
  25. #include <boost/log/utility/string_literal.hpp>
  26. #include "char_definitions.hpp"
  27. namespace logging = boost::log;
  28. namespace attrs = logging::attributes;
  29. namespace expr = logging::expressions;
  30. // The test checks that string matching works
  31. BOOST_AUTO_TEST_CASE(matching_check)
  32. {
  33. typedef logging::attribute_set attr_set;
  34. typedef logging::attribute_value_set attr_values;
  35. typedef logging::filter filter;
  36. typedef test_data< char > data;
  37. typedef std::regex regex_type;
  38. attrs::constant< std::string > attr1("127.0.0.1");
  39. attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
  40. attrs::constant< std::string > attr3("Hello, world!");
  41. attr_set set1, set2, set3;
  42. set1[data::attr1()] = attr1;
  43. set1[data::attr2()] = attr2;
  44. set1[data::attr3()] = attr3;
  45. attr_values values1(set1, set2, set3);
  46. values1.freeze();
  47. filter f = expr::matches< std::string >(data::attr1(), regex_type("\\d+\\.\\d+\\.\\d+\\.\\d+"));
  48. BOOST_CHECK(f(values1));
  49. f = expr::matches< std::string >(data::attr1(), regex_type("[a-z]*"));
  50. BOOST_CHECK(!f(values1));
  51. f = expr::matches< logging::string_literal >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
  52. BOOST_CHECK(f(values1));
  53. f = expr::matches< std::string >(data::attr3(), regex_type("Hello, world!"));
  54. BOOST_CHECK(f(values1));
  55. // Attribute value not present
  56. f = expr::matches< std::string >(data::attr4(), regex_type(".*"));
  57. BOOST_CHECK(!f(values1));
  58. // Attribute value type does not match
  59. f = expr::matches< std::string >(data::attr2(), regex_type("[A-Z]* [a-z]* [A-Za-z]*"));
  60. BOOST_CHECK(!f(values1));
  61. }
  62. // The test checks that the filter composition works
  63. BOOST_AUTO_TEST_CASE(composition_check)
  64. {
  65. typedef logging::attribute_set attr_set;
  66. typedef logging::attribute_value_set attr_values;
  67. typedef logging::filter filter;
  68. typedef test_data< char > data;
  69. typedef std::regex regex_type;
  70. attrs::constant< std::string > attr1("127.0.0.1");
  71. attrs::constant< logging::string_literal > attr2(logging::str_literal("BIG brown FoX"));
  72. attrs::constant< std::string > attr3("Hello, world!");
  73. attr_set set1, set2, set3;
  74. attr_values values1(set1, set2, set3);
  75. values1.freeze();
  76. set1[data::attr2()] = attr2;
  77. attr_values values2(set1, set2, set3);
  78. values2.freeze();
  79. set1[data::attr3()] = attr3;
  80. set1[data::attr1()] = attr1;
  81. attr_values values3(set1, set2, set3);
  82. values3.freeze();
  83. 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]*"));
  84. BOOST_CHECK(!f(values1));
  85. BOOST_CHECK(f(values2));
  86. BOOST_CHECK(f(values3));
  87. 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]*"));
  88. BOOST_CHECK(!f(values1));
  89. BOOST_CHECK(!f(values2));
  90. BOOST_CHECK(f(values3));
  91. }
  92. #else // !defined(BOOST_NO_CXX11_HDR_REGEX)
  93. int main(int, char*[])
  94. {
  95. return 0;
  96. }
  97. #endif // !defined(BOOST_NO_CXX11_HDR_REGEX)