1 /*//////////////////////////////////////////////////////////////////////////////
2 Copyright (c) 2011 Jamboree
3 Copyright (c) 2014 Lee Clagett
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //////////////////////////////////////////////////////////////////////////////*/
8 #include <vector>
9
10 #include <boost/detail/lightweight_test.hpp>
11 #include <boost/spirit/home/x3/auxiliary/eoi.hpp>
12 #include <boost/spirit/home/x3/core.hpp>
13 #include <boost/spirit/home/x3/char.hpp>
14 #include <boost/spirit/home/x3/string.hpp>
15 #include <boost/spirit/home/x3/numeric.hpp>
16 #include <boost/spirit/home/x3/operator/plus.hpp>
17 #include <boost/spirit/home/x3/operator/sequence.hpp>
18
19 #include <boost/spirit/home/x3/directive/seek.hpp>
20
21 #include "test.hpp"
22
23
24 ///////////////////////////////////////////////////////////////////////////////
main()25 int main()
26 {
27 using namespace spirit_test;
28 namespace x3 = boost::spirit::x3;
29
30 // test eoi
31 {
32 BOOST_TEST(test("", x3::seek[x3::eoi]));
33 BOOST_TEST(test(" ", x3::seek[x3::eoi], x3::space));
34 BOOST_TEST(test("a", x3::seek[x3::eoi]));
35 BOOST_TEST(test(" a", x3::seek[x3::eoi], x3::space));
36 }
37
38 // test literal finding
39 {
40 int i = 0;
41
42 BOOST_TEST(
43 test_attr("!@#$%^&*KEY:123", x3::seek["KEY:"] >> x3::int_, i)
44 && i == 123
45 );
46 }
47 // test sequence finding
48 {
49 int i = 0;
50
51 BOOST_TEST(
52 test_attr("!@#$%^&* KEY : 123", x3::seek[x3::lit("KEY") >> ':'] >> x3::int_, i, x3::space)
53 && i == 123
54 );
55 }
56
57 // test attr finding
58 {
59 std::vector<int> v;
60
61 BOOST_TEST( // expect partial match
62 test_attr("a06b78c3d", +x3::seek[x3::int_], v, false)
63 && v.size() == 3 && v[0] == 6 && v[1] == 78 && v[2] == 3
64 );
65 }
66
67 // test action
68 {
69
70 bool b = false;
71 auto const action = [&b]() { b = true; };
72
73 BOOST_TEST( // expect partial match
74 test("abcdefg", x3::seek["def"][action], false)
75 && b
76 );
77 }
78
79 // test container
80 {
81 std::vector<int> v;
82
83 BOOST_TEST(
84 test_attr("abcInt:100Int:95Int:44", x3::seek[+("Int:" >> x3::int_)], v)
85 && v.size() == 3 && v[0] == 100 && v[1] == 95 && v[2] == 44
86 );
87 }
88
89 // test failure rollback
90 {
91 BOOST_TEST(test_failure("abcdefg", x3::seek[x3::int_]));
92 }
93
94 return boost::report_errors();
95 }
96