misc2.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // misc2.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include <map>
  8. #include <string>
  9. #include <boost/xpressive/xpressive.hpp>
  10. #include <boost/xpressive/regex_actions.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. namespace xpr = boost::xpressive;
  13. using namespace xpr;
  14. ///////////////////////////////////////////////////////////////////////////////
  15. //
  16. void test_complement()
  17. {
  18. sregex rx1 = ~_n >> ~(set='a') >> ~(set='a','b') >> ~set['a'] >> ~_ln
  19. >> ~before('a') >> ~after('a') >> ~alpha >> ~range('a','b') >> ~_b >> ~as_xpr('a');
  20. #ifndef BOOST_XPRESSIVE_NO_WREGEX
  21. wsregex rx2 = ~_n >> ~(set=L'a') >> ~(set=L'a',L'b') >> ~set[L'a'] >> ~_ln
  22. >> ~before(L'a') >> ~after(L'a') >> ~alpha >> ~range(L'a',L'b') >> ~_b >> ~as_xpr(L'a');
  23. #endif
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////
  26. //
  27. void test_static_actions_in_dynamic_keep()
  28. {
  29. std::string result;
  30. std::string str("foo");
  31. sregex_compiler compiler;
  32. compiler["rx0"] = (s1="foo")[ xpr::ref(result) = s1 ];
  33. sregex rx = compiler.compile("(?>(?$rx0))");
  34. bool ok = regex_match(str, rx);
  35. BOOST_CHECK(ok);
  36. BOOST_CHECK_EQUAL(result, "foo");
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////
  39. //
  40. void test_static_actions_in_static_keep()
  41. {
  42. std::string result;
  43. std::string str("foo");
  44. sregex rx0 = (s1="foo")[ xpr::ref(result) = s1 ];
  45. sregex rx = keep(rx0);
  46. bool ok = regex_match(str, rx);
  47. BOOST_CHECK(ok);
  48. BOOST_CHECK_EQUAL(result, "foo");
  49. }
  50. ///////////////////////////////////////////////////////////////////////////////
  51. //
  52. void test_replace_with_lambda()
  53. {
  54. std::map<std::string, std::string> replacements;
  55. replacements["X"] = "this";
  56. replacements["Y"] = "that";
  57. std::string input("\"$(X)\" has the value \"$(Y)\""), output;
  58. std::string expected("\"this\" has the value \"that\"");
  59. sregex rx = "$(" >> (s1= +~as_xpr(')')) >> ')';
  60. output = regex_replace(input, rx, xpr::ref(replacements)[s1]);
  61. BOOST_CHECK_EQUAL(output, expected);
  62. }
  63. using namespace boost::unit_test;
  64. ///////////////////////////////////////////////////////////////////////////////
  65. // init_unit_test_suite
  66. //
  67. test_suite* init_unit_test_suite( int argc, char* argv[] )
  68. {
  69. test_suite *test = BOOST_TEST_SUITE("miscelaneous tests");
  70. test->add(BOOST_TEST_CASE(&test_complement));
  71. test->add(BOOST_TEST_CASE(&test_static_actions_in_dynamic_keep));
  72. test->add(BOOST_TEST_CASE(&test_static_actions_in_static_keep));
  73. test->add(BOOST_TEST_CASE(&test_replace_with_lambda));
  74. return test;
  75. }