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
8 #include <map>
9 #include <string>
10 #include <boost/xpressive/xpressive.hpp>
11 #include <boost/xpressive/regex_actions.hpp>
12 #include <boost/test/unit_test.hpp>
13
14 namespace xpr = boost::xpressive;
15 using namespace xpr;
16
17 ///////////////////////////////////////////////////////////////////////////////
18 //
test_complement()19 void test_complement()
20 {
21 sregex rx1 = ~_n >> ~(set='a') >> ~(set='a','b') >> ~set['a'] >> ~_ln
22 >> ~before('a') >> ~after('a') >> ~alpha >> ~range('a','b') >> ~_b >> ~as_xpr('a');
23
24 #ifndef BOOST_XPRESSIVE_NO_WREGEX
25 wsregex rx2 = ~_n >> ~(set=L'a') >> ~(set=L'a',L'b') >> ~set[L'a'] >> ~_ln
26 >> ~before(L'a') >> ~after(L'a') >> ~alpha >> ~range(L'a',L'b') >> ~_b >> ~as_xpr(L'a');
27 #endif
28 }
29
30 ///////////////////////////////////////////////////////////////////////////////
31 //
test_static_actions_in_dynamic_keep()32 void test_static_actions_in_dynamic_keep()
33 {
34 std::string result;
35 std::string str("foo");
36
37 sregex_compiler compiler;
38 compiler["rx0"] = (s1="foo")[ xpr::ref(result) = s1 ];
39 sregex rx = compiler.compile("(?>(?$rx0))");
40
41 bool ok = regex_match(str, rx);
42 BOOST_CHECK(ok);
43 BOOST_CHECK_EQUAL(result, "foo");
44 }
45
46 ///////////////////////////////////////////////////////////////////////////////
47 //
test_static_actions_in_static_keep()48 void test_static_actions_in_static_keep()
49 {
50 std::string result;
51 std::string str("foo");
52
53 sregex rx0 = (s1="foo")[ xpr::ref(result) = s1 ];
54 sregex rx = keep(rx0);
55
56 bool ok = regex_match(str, rx);
57 BOOST_CHECK(ok);
58 BOOST_CHECK_EQUAL(result, "foo");
59 }
60
61 ///////////////////////////////////////////////////////////////////////////////
62 //
test_replace_with_lambda()63 void test_replace_with_lambda()
64 {
65 std::map<std::string, std::string> replacements;
66 replacements["X"] = "this";
67 replacements["Y"] = "that";
68
69 std::string input("\"$(X)\" has the value \"$(Y)\""), output;
70 std::string expected("\"this\" has the value \"that\"");
71 sregex rx = "$(" >> (s1= +~as_xpr(')')) >> ')';
72
73 output = regex_replace(input, rx, xpr::ref(replacements)[s1]);
74 BOOST_CHECK_EQUAL(output, expected);
75 }
76
77 using namespace boost::unit_test;
78 ///////////////////////////////////////////////////////////////////////////////
79 // init_unit_test_suite
80 //
init_unit_test_suite(int argc,char * argv[])81 test_suite* init_unit_test_suite( int argc, char* argv[] )
82 {
83 test_suite *test = BOOST_TEST_SUITE("miscelaneous tests");
84
85 test->add(BOOST_TEST_CASE(&test_complement));
86 test->add(BOOST_TEST_CASE(&test_static_actions_in_dynamic_keep));
87 test->add(BOOST_TEST_CASE(&test_static_actions_in_static_keep));
88 test->add(BOOST_TEST_CASE(&test_replace_with_lambda));
89
90 return test;
91 }
92