1 ///////////////////////////////////////////////////////////////////////////////
2 // test_assert_with_placeholder.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 <boost/xpressive/xpressive.hpp>
9 #include <boost/xpressive/regex_actions.hpp>
10 #include <boost/test/unit_test.hpp>
11
12 using namespace boost::xpressive;
13
14 placeholder<int> const _cnt = {{}};
15
16 struct count_a_impl
17 {
18 typedef void result_type;
19
operator ()count_a_impl20 void operator()(int &cnt) const
21 {
22 ++cnt;
23 }
24 };
25
26 boost::xpressive::function<count_a_impl>::type const count_a = {{}};
27
28 struct check_a_impl
29 {
30 typedef bool result_type;
31
operator ()check_a_impl32 bool operator()(int &cnt, int val) const
33 {
34 if(cnt < val)
35 {
36 ++cnt;
37 return true;
38 }
39 return false;
40 }
41 };
42
43 boost::xpressive::function<check_a_impl>::type const check_a = {{}};
44
test_assert_with_placeholder()45 void test_assert_with_placeholder()
46 {
47 int cnt = 0;
48 std::string a_str("a_aaaaa___a_aa_aaa_");
49 const sregex expr1(*(as_xpr('a')[count_a(_cnt)] | '_'));
50 const sregex expr2(*(as_xpr('a')[check(check_a(_cnt, 5))] | '_'));
51
52 sregex_iterator iter1(a_str.begin(), a_str.end(), expr1,
53 let(_cnt = cnt));
54 BOOST_CHECK_EQUAL(iter1->str(0), a_str);
55 BOOST_CHECK_EQUAL(cnt, 12);
56
57 cnt = 0;
58 sregex_iterator iter2(a_str.begin(), a_str.end(), expr2,
59 let(_cnt = cnt));
60
61 BOOST_CHECK_EQUAL(iter2->str(0), std::string("a_aaaa"));
62 BOOST_CHECK_EQUAL(cnt, 5);
63 }
64
65 using namespace boost::unit_test;
66 ///////////////////////////////////////////////////////////////////////////////
67 // init_unit_test_suite
68 //
init_unit_test_suite(int argc,char * argv[])69 test_suite* init_unit_test_suite( int argc, char* argv[] )
70 {
71 test_suite *test = BOOST_TEST_SUITE("test that custom assertions can use argument placeholders");
72
73 test->add(BOOST_TEST_CASE(&test_assert_with_placeholder));
74
75 return test;
76 }
77