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
14namespace xpr = boost::xpressive;
15using namespace xpr;
16
17///////////////////////////////////////////////////////////////////////////////
18//
19void test_complement()
20{
21 sregex rx1 = ~_n >> ~(set='a') >> ~(set='a','b') >> ~set['a'] >> ~_ln
22 >> ~before(expr: 'a') >> ~after(expr: 'a') >> ~alpha >> ~range(ch_min: 'a',ch_max: '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(expr: L'a') >> ~after(expr: L'a') >> ~alpha >> ~range(ch_min: L'a',ch_max: L'b') >> ~_b >> ~as_xpr(L'a');
27#endif
28}
29
30///////////////////////////////////////////////////////////////////////////////
31//
32void 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(t&: result) = s1 ];
39 sregex rx = compiler.compile(begin: "(?>(?$rx0))");
40
41 bool ok = regex_match(rng&: str, re: rx);
42 BOOST_CHECK(ok);
43 BOOST_CHECK_EQUAL(result, "foo");
44}
45
46///////////////////////////////////////////////////////////////////////////////
47//
48void test_static_actions_in_static_keep()
49{
50 std::string result;
51 std::string str("foo");
52
53 sregex rx0 = (s1="foo")[ xpr::ref(t&: result) = s1 ];
54 sregex rx = keep(expr: rx0);
55
56 bool ok = regex_match(rng&: str, re: rx);
57 BOOST_CHECK(ok);
58 BOOST_CHECK_EQUAL(result, "foo");
59}
60
61///////////////////////////////////////////////////////////////////////////////
62//
63void 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(str&: input, re: rx, format: xpr::ref(t&: replacements)[s1]);
74 BOOST_CHECK_EQUAL(output, expected);
75}
76
77using namespace boost::unit_test;
78///////////////////////////////////////////////////////////////////////////////
79// init_unit_test_suite
80//
81test_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

source code of boost/libs/xpressive/test/misc2.cpp