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