]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2011 Joel de Guzman | |
3 | ||
4 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | =============================================================================*/ | |
7 | #include <boost/detail/lightweight_test.hpp> | |
8 | #include <boost/spirit/include/qi_operator.hpp> | |
9 | #include <boost/spirit/include/qi_char.hpp> | |
10 | #include <boost/spirit/include/qi_string.hpp> | |
11 | #include <boost/spirit/include/qi_numeric.hpp> | |
12 | #include <boost/spirit/include/qi_action.hpp> | |
13 | #include <boost/spirit/include/support_argument.hpp> | |
14 | #include <boost/fusion/include/vector.hpp> | |
15 | #include <boost/fusion/include/at.hpp> | |
16 | #include <boost/spirit/include/phoenix_core.hpp> | |
17 | #include <boost/spirit/include/phoenix_operator.hpp> | |
7c673cae FG |
18 | #include <boost/optional.hpp> |
19 | ||
20 | #include <string> | |
21 | #include <iostream> | |
22 | #include "test.hpp" | |
23 | ||
24 | int | |
25 | main() | |
26 | { | |
27 | using spirit_test::test; | |
28 | using spirit_test::test_attr; | |
29 | ||
30 | using boost::spirit::qi::int_; | |
31 | using boost::spirit::qi::_1; | |
32 | using boost::spirit::qi::_2; | |
33 | using boost::spirit::ascii::char_; | |
34 | using boost::spirit::ascii::alpha; | |
35 | using boost::fusion::vector; | |
36 | using boost::fusion::at_c; | |
37 | using boost::optional; | |
38 | ||
39 | { | |
40 | BOOST_TEST((test("a", char_('a') || char_('b')))); | |
41 | BOOST_TEST((test("b", char_('a') || char_('b')))); | |
42 | BOOST_TEST((test("ab", char_('a') || char_('b')))); | |
43 | } | |
44 | ||
45 | { | |
46 | vector<optional<int>, optional<char> > attr; | |
47 | ||
48 | BOOST_TEST((test_attr("a", int_ || alpha, attr))); | |
49 | BOOST_TEST((!at_c<0>(attr))); | |
50 | BOOST_TEST((at_c<1>(attr).get() == 'a')); | |
51 | ||
52 | at_c<1>(attr) = optional<char>(); // clear the optional | |
53 | BOOST_TEST((test_attr("123", int_ || alpha, attr))); | |
54 | BOOST_TEST((at_c<0>(attr).get() == 123)); | |
55 | BOOST_TEST((!at_c<1>(attr))); | |
56 | ||
57 | at_c<0>(attr) = optional<int>(); // clear the optional | |
58 | BOOST_TEST((test_attr("123a", int_ || alpha, attr))); | |
59 | BOOST_TEST((at_c<0>(attr).get() == 123)); | |
60 | BOOST_TEST((at_c<1>(attr).get() == 'a')); | |
61 | ||
62 | BOOST_TEST((!test("a123", int_ || alpha))); | |
63 | } | |
64 | ||
65 | { // test whether optional<optional<>> gets properly handled | |
66 | vector<optional<int>, optional<int> > attr1; | |
67 | BOOST_TEST((test_attr("123", int_ || '[' >> -int_ >> ']', attr1))); | |
68 | BOOST_TEST((at_c<0>(attr1) && at_c<0>(attr1).get() == 123)); | |
69 | BOOST_TEST((!at_c<1>(attr1))); | |
70 | ||
71 | vector<optional<int>, optional<int> > attr2; | |
72 | BOOST_TEST((test_attr("[123]", int_ || '[' >> -int_ >> ']', attr2))); | |
73 | BOOST_TEST((!at_c<0>(attr2))); | |
74 | BOOST_TEST((at_c<1>(attr2) && at_c<1>(attr2).get() == 123)); | |
75 | ||
76 | vector<optional<int>, optional<optional<int> > > attr3; | |
77 | BOOST_TEST((test_attr("[]", int_ || '[' >> -int_ >> ']', attr3))); | |
78 | BOOST_TEST((!at_c<0>(attr3))); | |
79 | BOOST_TEST((at_c<1>(attr3) && !at_c<1>(attr3).get())); | |
80 | } | |
81 | ||
82 | { // test unused attribute handling | |
83 | ||
84 | vector<optional<int>, optional<char> > attr; | |
85 | BOOST_TEST((test_attr("123abc", int_ || ("ab" >> char_), attr))); | |
86 | BOOST_TEST((at_c<0>(attr).get() == 123)); | |
87 | BOOST_TEST((at_c<1>(attr).get() == 'c')); | |
88 | } | |
89 | ||
90 | { // test unused attribute handling | |
91 | ||
92 | optional<int> attr; | |
93 | BOOST_TEST((test_attr("123ab", int_ || "ab", attr))); | |
94 | BOOST_TEST((attr == 123)); | |
95 | } | |
96 | ||
97 | { // test action | |
98 | namespace phx = boost::phoenix; | |
99 | ||
100 | optional<int> i; | |
101 | optional<char> c; | |
102 | ||
103 | BOOST_TEST((test("123a", (int_ || alpha)[phx::ref(i) = _1, phx::ref(c) = _2]))); | |
104 | BOOST_TEST((i.get() == 123)); | |
105 | BOOST_TEST((c.get() == 'a')); | |
106 | } | |
107 | ||
108 | return boost::report_errors(); | |
109 | } | |
110 |