test_format.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // test_format.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. //
  8. // Test all the different ways to call regex_replace with a formatter.
  9. #include <map>
  10. #include <string>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/xpressive/xpressive.hpp>
  13. using namespace boost::xpressive;
  14. std::map<std::string, std::string> replacements;
  15. struct format1
  16. {
  17. template<typename BidiIter>
  18. std::string operator()(match_results<BidiIter> const &what) const
  19. {
  20. return replacements[what[1].str()];
  21. }
  22. };
  23. struct format2
  24. {
  25. template<typename BidiIter, typename OutIter>
  26. OutIter operator()(match_results<BidiIter> const &what, OutIter out) const
  27. {
  28. std::map<std::string, std::string>::const_iterator where = replacements.find(what[1].str());
  29. if(where != replacements.end())
  30. out = std::copy((*where).second.begin(), (*where).second.end(), out);
  31. return out;
  32. }
  33. };
  34. struct format3
  35. {
  36. template<typename BidiIter, typename OutIter>
  37. OutIter operator()(match_results<BidiIter> const &what, OutIter out, regex_constants::match_flag_type) const
  38. {
  39. std::map<std::string, std::string>::const_iterator where = replacements.find(what[1].str());
  40. if(where != replacements.end())
  41. out = std::copy((*where).second.begin(), (*where).second.end(), out);
  42. return out;
  43. }
  44. };
  45. std::string format_fun(smatch const &what)
  46. {
  47. return replacements[what[1].str()];
  48. }
  49. std::string c_format_fun(cmatch const &what)
  50. {
  51. return replacements[what[1].str()];
  52. }
  53. void test_main()
  54. {
  55. replacements["X"] = "this";
  56. replacements["Y"] = "that";
  57. std::string input("\"$(X)\" has the value \"$(Y)\""), output;
  58. sregex rx = sregex::compile("\\$\\(([^\\)]+)\\)");
  59. cregex crx = cregex::compile("\\$\\(([^\\)]+)\\)");
  60. std::string result("\"this\" has the value \"that\"");
  61. output = regex_replace(input, rx, format1());
  62. BOOST_CHECK_EQUAL(output, result);
  63. output = regex_replace(input.c_str(), crx, format1());
  64. BOOST_CHECK_EQUAL(output, result);
  65. output = regex_replace(input, rx, format2());
  66. BOOST_CHECK_EQUAL(output, result);
  67. output = regex_replace(input.c_str(), crx, format2());
  68. BOOST_CHECK_EQUAL(output, result);
  69. output = regex_replace(input, rx, format3());
  70. BOOST_CHECK_EQUAL(output, result);
  71. output = regex_replace(input.c_str(), crx, format3());
  72. BOOST_CHECK_EQUAL(output, result);
  73. output = regex_replace(input, rx, format_fun);
  74. BOOST_CHECK_EQUAL(output, result);
  75. output = regex_replace(input.c_str(), crx, c_format_fun);
  76. BOOST_CHECK_EQUAL(output, result);
  77. output = regex_replace(input, rx, &format_fun);
  78. BOOST_CHECK_EQUAL(output, result);
  79. output = regex_replace(input.c_str(), crx, &c_format_fun);
  80. BOOST_CHECK_EQUAL(output, result);
  81. output.clear();
  82. regex_replace(std::back_inserter(output), input.begin(), input.end(), rx, format1());
  83. BOOST_CHECK_EQUAL(output, result);
  84. output.clear();
  85. regex_replace(std::back_inserter(output), input.begin(), input.end(), rx, format2());
  86. BOOST_CHECK_EQUAL(output, result);
  87. output.clear();
  88. regex_replace(std::back_inserter(output), input.begin(), input.end(), rx, format2());
  89. BOOST_CHECK_EQUAL(output, result);
  90. output.clear();
  91. regex_replace(std::back_inserter(output), input.begin(), input.end(), rx, format_fun);
  92. BOOST_CHECK_EQUAL(output, result);
  93. output.clear();
  94. regex_replace(std::back_inserter(output), input.begin(), input.end(), rx, &format_fun);
  95. BOOST_CHECK_EQUAL(output, result);
  96. }
  97. using namespace boost::unit_test;
  98. ///////////////////////////////////////////////////////////////////////////////
  99. // init_unit_test_suite
  100. //
  101. test_suite* init_unit_test_suite( int argc, char* argv[] )
  102. {
  103. test_suite *test = BOOST_TEST_SUITE("test_format");
  104. test->add(BOOST_TEST_CASE(&test_main));
  105. return test;
  106. }