filt_matches_boost_regex.cpp 3.8 KB

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