test_not_regex.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. *
  3. * Copyright (c) 2004
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE test_not_regex.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares tests for invalid regexes.
  16. */
  17. #ifndef BOOST_REGEX_REGRESS_TEST_NOT_REGEX_HPP
  18. #define BOOST_REGEX_REGRESS_TEST_NOT_REGEX_HPP
  19. #include "info.hpp"
  20. //
  21. // this file implements a test for a regular expression that should not compile:
  22. //
  23. struct test_invalid_regex_tag{};
  24. template<class charT, class traits>
  25. void test_empty(boost::basic_regex<charT, traits>& r)
  26. {
  27. if(!r.empty())
  28. {
  29. BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::empty().", charT);
  30. }
  31. if(r.size())
  32. {
  33. BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::size().", charT);
  34. }
  35. if(r.str().size())
  36. {
  37. BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::str().", charT);
  38. }
  39. if(r.begin() != r.end())
  40. {
  41. BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::begin().", charT);
  42. }
  43. if(r.status() == 0)
  44. {
  45. BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::status().", charT);
  46. }
  47. if(r.begin() != r.end())
  48. {
  49. BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::begin().", charT);
  50. }
  51. }
  52. template<class charT, class traits>
  53. void test(boost::basic_regex<charT, traits>& r, const test_invalid_regex_tag&)
  54. {
  55. const std::basic_string<charT>& expression = test_info<charT>::expression();
  56. boost::regex_constants::syntax_option_type syntax_options = test_info<charT>::syntax_options();
  57. //
  58. // try it with exceptions disabled first:
  59. //
  60. #ifndef BOOST_NO_EXCEPTIONS
  61. try
  62. #endif
  63. {
  64. if(0 == r.assign(expression, syntax_options | boost::regex_constants::no_except).status())
  65. {
  66. BOOST_REGEX_TEST_ERROR("Expression compiled when it should not have done so.", charT);
  67. }
  68. test_empty(r);
  69. }
  70. #ifndef BOOST_NO_EXCEPTIONS
  71. catch(...)
  72. {
  73. BOOST_REGEX_TEST_ERROR("Unexpected exception thrown.", charT);
  74. }
  75. #endif
  76. //
  77. // now try again with exceptions:
  78. //
  79. bool have_catch = false;
  80. #ifndef BOOST_NO_EXCEPTIONS
  81. try
  82. #endif
  83. {
  84. r.assign(expression, syntax_options);
  85. #ifdef BOOST_NO_EXCEPTIONS
  86. if(r.status())
  87. have_catch = true;
  88. #endif
  89. }
  90. #ifndef BOOST_NO_EXCEPTIONS
  91. catch(const boost::bad_expression&)
  92. {
  93. have_catch = true;
  94. test_empty(r);
  95. }
  96. catch(const std::runtime_error& e)
  97. {
  98. have_catch = true;
  99. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << e.what(), charT);
  100. }
  101. catch(const std::exception& e)
  102. {
  103. have_catch = true;
  104. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << e.what(), charT);
  105. }
  106. catch(...)
  107. {
  108. have_catch = true;
  109. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", charT);
  110. }
  111. #endif
  112. if(!have_catch)
  113. {
  114. // oops expected exception was not thrown:
  115. BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", charT);
  116. }
  117. }
  118. #endif