1 ///////////////////////////////////////////////////////////////////////////////
2 // test_skip.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 <iostream>
10 #include <boost/xpressive/xpressive.hpp>
11 #include <boost/xpressive/regex_actions.hpp>
12 #include <boost/test/unit_test.hpp>
13
14 using namespace boost::unit_test;
15 using namespace boost::xpressive;
16
test1()17 void test1()
18 {
19 std::string s = "a a b b c c";
20
21 sregex rx =
22 "a a" >>
23 skip(_s)
24 (
25 (s1= as_xpr('b')) >>
26 as_xpr('b') >>
27 *as_xpr('c') // causes backtracking
28 ) >>
29 "c c";
30
31 smatch what;
32 BOOST_CHECK( regex_match(s, what, rx) );
33
34 s = "123,456,789";
35 sregex rx2 = skip(',')(+_d);
36 BOOST_CHECK( regex_match(s, what, rx2) );
37
38 s = "foo";
39 sregex rx3 = skip(_s)(after("fo") >> 'o');
40 BOOST_CHECK( regex_search(s, what, rx3) );
41 }
42
43 template<typename Expr>
test_skip_aux(Expr const & expr)44 void test_skip_aux(Expr const &expr)
45 {
46 sregex rx = skip(_s)(expr);
47 }
48
test_skip()49 void test_skip()
50 {
51 int i=0;
52 std::map<std::string, int> syms;
53 std::locale loc;
54
55 test_skip_aux( 'a' );
56 test_skip_aux( _ );
57 test_skip_aux( +_ );
58 test_skip_aux( -+_ );
59 test_skip_aux( !_ );
60 test_skip_aux( -!_ );
61 test_skip_aux( repeat<0,42>(_) );
62 test_skip_aux( -repeat<0,42>(_) );
63 test_skip_aux( _ >> 'a' );
64 test_skip_aux( _ >> 'a' | _ );
65 test_skip_aux( _ >> 'a' | _ >> 'b' );
66 test_skip_aux( s1= _ >> 'a' | _ >> 'b' );
67 test_skip_aux( icase(_ >> 'a' | _ >> 'b') );
68 test_skip_aux( imbue(loc)(_ >> 'a' | _ >> 'b') );
69 test_skip_aux( (set='a') );
70 test_skip_aux( (set='a','b') );
71 test_skip_aux( ~(set='a') );
72 test_skip_aux( ~(set='a','b') );
73 test_skip_aux( range('a','b') );
74 test_skip_aux( ~range('a','b') );
75 test_skip_aux( set['a' | alpha] );
76 test_skip_aux( ~set['a' | alpha] );
77 test_skip_aux( before(_) );
78 test_skip_aux( ~before(_) );
79 test_skip_aux( after(_) );
80 test_skip_aux( ~after(_) );
81 test_skip_aux( keep(*_) );
82 test_skip_aux( (*_)[ref(i) = as<int>(_) + 1] );
83 test_skip_aux( (a1= syms)[ref(i) = a1 + 1] );
84 }
85
86 ///////////////////////////////////////////////////////////////////////////////
87 // init_unit_test_suite
88 //
init_unit_test_suite(int argc,char * argv[])89 test_suite* init_unit_test_suite( int argc, char* argv[] )
90 {
91 test_suite *test = BOOST_TEST_SUITE("test skip()");
92
93 test->add(BOOST_TEST_CASE(&test1));
94
95 return test;
96 }
97