test_regex_replace.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_regex_replace.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares tests for regex search and replace.
  16. */
  17. #ifndef BOOST_REGEX_REGRESS_REGEX_REPLACE_HPP
  18. #define BOOST_REGEX_REGRESS_REGEX_REPLACE_HPP
  19. #include "info.hpp"
  20. template<class charT, class traits>
  21. void test_regex_replace(boost::basic_regex<charT, traits>& r)
  22. {
  23. typedef std::basic_string<charT> string_type;
  24. const string_type& search_text = test_info<charT>::search_text();
  25. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  26. const string_type& format_string = test_info<charT>::format_string();
  27. const string_type& result_string = test_info<charT>::result_string();
  28. string_type result = boost::regex_replace(search_text, r, format_string, opts);
  29. if(result != result_string)
  30. {
  31. BOOST_REGEX_TEST_ERROR("regex_replace generated an incorrect string result", charT);
  32. }
  33. }
  34. struct test_regex_replace_tag{};
  35. template<class charT, class traits>
  36. void test(boost::basic_regex<charT, traits>& r, const test_regex_replace_tag&)
  37. {
  38. const std::basic_string<charT>& expression = test_info<charT>::expression();
  39. boost::regex_constants::syntax_option_type syntax_options = test_info<charT>::syntax_options();
  40. #ifndef BOOST_NO_EXCEPTIONS
  41. try
  42. #endif
  43. {
  44. r.assign(expression, syntax_options);
  45. if(r.status())
  46. {
  47. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r.status(), charT);
  48. }
  49. test_regex_replace(r);
  50. }
  51. #ifndef BOOST_NO_EXCEPTIONS
  52. catch(const boost::bad_expression& e)
  53. {
  54. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e.what(), charT);
  55. }
  56. catch(const std::runtime_error& e)
  57. {
  58. BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << e.what(), charT);
  59. }
  60. catch(const std::exception& e)
  61. {
  62. BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << e.what(), charT);
  63. }
  64. catch(...)
  65. {
  66. BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", charT);
  67. }
  68. #endif
  69. }
  70. #endif