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_char.hpp>
9 #include <boost/spirit/include/qi_numeric.hpp>
10 #include <boost/spirit/include/qi_operator.hpp>
11 #include <boost/spirit/include/qi_action.hpp>
12 #include <boost/spirit/include/qi_directive.hpp>
13 #include <boost/spirit/include/support_argument.hpp>
14 #include <boost/spirit/include/phoenix_core.hpp>
15 #include <boost/spirit/include/phoenix_operator.hpp>
16 #include <boost/fusion/adapted/struct.hpp>
17 
18 #include <iostream>
19 #include "test.hpp"
20 
21 struct adata
22 {
23     int a;
24     boost::optional<int> b;
25 };
26 
27 BOOST_FUSION_ADAPT_STRUCT(
28     adata,
29     (int, a)
30     (boost::optional<int>, b)
31 )
32 
33 struct test_attribute_type
34 {
35     template <typename Attribute, typename Context>
operator ()test_attribute_type36     void operator()(Attribute&, Context&, bool&) const
37     {
38         BOOST_TEST(typeid(Attribute).name() == typeid(boost::optional<int>).name());
39     }
40 };
41 
42 int
main()43 main()
44 {
45     using spirit_test::test;
46     using spirit_test::test_attr;
47 
48     using boost::spirit::qi::_1;
49     using boost::spirit::qi::int_;
50     using boost::spirit::qi::omit;
51     using boost::spirit::ascii::char_;
52 
53     {
54         BOOST_TEST((test("1234", -int_)));
55         BOOST_TEST((test("abcd", -int_, false)));
56     }
57 
58     {   // test propagation of unused
59         using boost::fusion::at_c;
60         using boost::fusion::vector;
61 
62         vector<char, char> v;
63         BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v)));
64         BOOST_TEST((at_c<0>(v) == 'a'));
65         BOOST_TEST((at_c<1>(v) == 'c'));
66 
67         v = boost::fusion::vector<char, char>();
68         BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v)));
69         BOOST_TEST((at_c<0>(v) == 'a'));
70         BOOST_TEST((at_c<1>(v) == 'c'));
71 
72         char ch;
73         BOOST_TEST((test_attr(",c", -(',' >> char_), ch)));
74         BOOST_TEST((ch == 'c'));
75     }
76 
77     {   // test action
78         boost::optional<int> n = 0;
79         BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n)));
80         BOOST_TEST((n.get() == 1234));
81     }
82 
83     {
84         std::string s;
85         BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s)));
86         BOOST_TEST(s == "abc");
87     }
88 
89     {
90         namespace phx = boost::phoenix;
91 
92         boost::optional<int> n = 0;
93         BOOST_TEST((test("1234", (-int_)[phx::ref(n) = _1])));
94         BOOST_TEST(n.get() == 1234);
95 
96         n = boost::optional<int>();
97         BOOST_TEST((test("abcd", (-int_)[phx::ref(n) = _1], false)));
98         BOOST_TEST(!n);
99     }
100 
101     {
102         std::vector<adata> v;
103         BOOST_TEST((test_attr("a 1 2 a 2", *("a" >> int_ >> -int_), v
104           , char_(' '))));
105         BOOST_TEST(2 == v.size() &&
106             1 == v[0].a && v[0].b && 2 == *(v[0].b) &&
107             2 == v[1].a && !v[1].b);
108     }
109 
110     return boost::report_errors();
111 }
112