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 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::seek['x']);
31
32 // test eoi
33 {
34 BOOST_TEST(test("", x3::seek[x3::eoi]));
35 BOOST_TEST(test(" ", x3::seek[x3::eoi], x3::space));
36 BOOST_TEST(test("a", x3::seek[x3::eoi]));
37 BOOST_TEST(test(" a", x3::seek[x3::eoi], x3::space));
38 }
39
40 // test literal finding
41 {
42 int i = 0;
43
44 BOOST_TEST(
45 test_attr("!@#$%^&*KEY:123", x3::seek["KEY:"] >> x3::int_, i)
46 && i == 123
47 );
48 }
49 // test sequence finding
50 {
51 int i = 0;
52
53 BOOST_TEST(
54 test_attr("!@#$%^&* KEY : 123", x3::seek[x3::lit("KEY") >> ':'] >> x3::int_, i, x3::space)
55 && i == 123
56 );
57 }
58
59 // test attr finding
60 {
61 std::vector<int> v;
62
63 BOOST_TEST( // expect partial match
64 test_attr("a06b78c3d", +x3::seek[x3::int_], v, false)
65 && v.size() == 3 && v[0] == 6 && v[1] == 78 && v[2] == 3
66 );
67 }
68
69 // test action
70 {
71
72 bool b = false;
73 auto const action = [&b]() { b = true; };
74
75 BOOST_TEST( // expect partial match
76 test("abcdefg", x3::seek["def"][action], false)
77 && b
78 );
79 }
80
81 // test container
82 {
83 std::vector<int> v;
84
85 BOOST_TEST(
86 test_attr("abcInt:100Int:95Int:44", x3::seek[+("Int:" >> x3::int_)], v)
87 && v.size() == 3 && v[0] == 100 && v[1] == 95 && v[2] == 44
88 );
89 }
90
91 // test failure rollback
92 {
93 BOOST_TEST(test_failure("abcdefg", x3::seek[x3::int_]));
94 }
95
96 return boost::report_errors();
97 }
98