• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2015 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/home/x3.hpp>
9 #include <boost/fusion/adapted/struct.hpp>
10 #include <boost/fusion/include/vector.hpp>
11 
12 #include <iostream>
13 #include "test.hpp"
14 #include "utils.hpp"
15 
16 struct adata
17 {
18     int a;
19     boost::optional<int> b;
20 };
21 
22 BOOST_FUSION_ADAPT_STRUCT(adata,
23     a, b
24 )
25 
26 struct test_attribute_type
27 {
28     template <typename Context>
operator ()test_attribute_type29     void operator()(Context& ctx) const
30     {
31         BOOST_TEST(typeid(decltype(_attr(ctx))).name() == typeid(boost::optional<int>).name());
32     }
33 };
34 
35 int
main()36 main()
37 {
38     using boost::spirit::x3::traits::is_optional;
39 
40     static_assert(is_optional<boost::optional<int>>(), "is_optional problem");
41 
42     using spirit_test::test;
43     using spirit_test::test_attr;
44 
45     using boost::spirit::x3::int_;
46     using boost::spirit::x3::omit;
47     using boost::spirit::x3::ascii::char_;
48 
49     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(-int_);
50 
51     {
52         BOOST_TEST((test("1234", -int_)));
53         BOOST_TEST((test("abcd", -int_, false)));
54 
55         boost::optional<int> n;
56         BOOST_TEST(test_attr("", -int_, n))
57             && BOOST_TEST(!n);
58         BOOST_TEST(test_attr("123", -int_, n))
59             && BOOST_TEST(n) && BOOST_TEST_EQ(*n, 123);
60 
61         boost::optional<std::string> s;
62         BOOST_TEST(test_attr("", -+char_, s))
63             && BOOST_TEST(!s);
64         BOOST_TEST(test_attr("abc", -+char_, s))
65             && BOOST_TEST(s) && BOOST_TEST_EQ(*s, "abc");
66     }
67 
68     {   // test propagation of unused
69         using boost::fusion::at_c;
70         using boost::fusion::vector;
71 
72         vector<char, char> v;
73         BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v)));
74         BOOST_TEST((at_c<0>(v) == 'a'));
75         BOOST_TEST((at_c<1>(v) == 'c'));
76 
77         v = boost::fusion::vector<char, char>();
78         BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v)));
79         BOOST_TEST((at_c<0>(v) == 'a'));
80         BOOST_TEST((at_c<1>(v) == 'c'));
81 
82         char ch;
83         BOOST_TEST((test_attr(",c", -(',' >> char_), ch)));
84         BOOST_TEST((ch == 'c'));
85     }
86 
87     {   // test action
88         boost::optional<int> n = 0;
89         BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n)));
90         BOOST_TEST((n.get() == 1234));
91     }
92 
93     {
94         std::string s;
95         BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s)));
96         BOOST_TEST(s == "abc");
97     }
98 
99     {
100         boost::optional<int> n = 0;
101         auto f = [&](auto& ctx){ n = _attr(ctx); };
102 
103         BOOST_TEST((test("1234", (-int_)[f])));
104         BOOST_TEST(n.get() == 1234);
105 
106         n = boost::optional<int>();
107         BOOST_TEST((test("abcd", (-int_)[f], false)));
108         BOOST_TEST(!n);
109     }
110 
111     {
112         std::vector<adata> v;
113         BOOST_TEST((test_attr("a 1 2 a 2", *('a' >> int_ >> -int_), v
114           , char_(' '))));
115         BOOST_TEST(2 == v.size() &&
116             1 == v[0].a && v[0].b && 2 == *(v[0].b) &&
117             2 == v[1].a && !v[1].b);
118     }
119 
120     { // test move only types
121         boost::optional<move_only> o;
122         BOOST_TEST(test_attr("s", -synth_move_only, o));
123         BOOST_TEST(o);
124     }
125 
126     return boost::report_errors();
127 }
128