test_assert_with_placeholder.cpp 1.9 KB

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