Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / spirit / test / x3 / omit.cpp
1 /*=============================================================================
2     Copyright (c) 2001-2013 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/include/vector.hpp>
10 #include <boost/fusion/include/at.hpp>
11
12 #include <string>
13 #include <iostream>
14 #include "test.hpp"
15
16 int
17 main()
18 {
19     using namespace boost::spirit::x3::ascii;
20     using boost::spirit::x3::omit;
21     using boost::spirit::x3::unused_type;
22     using boost::spirit::x3::unused;
23     using boost::spirit::x3::int_;
24
25     using boost::fusion::vector;
26     using boost::fusion::at_c;
27
28     using spirit_test::test;
29     using spirit_test::test_attr;
30
31     {
32         BOOST_TEST(test("a", omit['a']));
33     }
34
35     {
36         // omit[] means we don't receive the attribute
37         char attr;
38         BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> char_, attr)));
39         BOOST_TEST((attr == 'c'));
40     }
41
42     {
43         // If all elements except 1 is omitted, the attribute is
44         // a single-element sequence. For this case alone, we allow
45         // naked attributes (unwrapped in a fusion sequence).
46         char attr;
47         BOOST_TEST((test_attr("abc", omit[char_] >> 'b' >> char_, attr)));
48         BOOST_TEST((attr == 'c'));
49     }
50
51     {
52         // omit[] means we don't receive the attribute
53         vector<> attr;
54         BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> omit[char_], attr)));
55     }
56
57     {
58         // omit[] means we don't receive the attribute
59         // this test is merely a compile test, because using a unused as the
60         // explicit attribute doesn't make any sense
61         unused_type attr;
62         BOOST_TEST((test_attr("abc", omit[char_ >> 'b' >> char_], attr)));
63     }
64
65     {
66         // omit[] means we don't receive the attribute, if all elements of a
67         // sequence have unused attributes, the whole sequence has an unused
68         // attribute as well
69         vector<char, char> attr;
70         BOOST_TEST((test_attr("abcde",
71             char_ >> (omit[char_] >> omit['c'] >> omit[char_]) >> char_, attr)));
72         BOOST_TEST((at_c<0>(attr) == 'a'));
73         BOOST_TEST((at_c<1>(attr) == 'e'));
74     }
75
76     {
77         // "hello" has an unused_type. unused attrubutes are not part of the sequence
78         vector<char, char> attr;
79         BOOST_TEST((test_attr("a hello c", char_ >> "hello" >> char_, attr, space)));
80         BOOST_TEST((at_c<0>(attr) == 'a'));
81         BOOST_TEST((at_c<1>(attr) == 'c'));
82     }
83
84     {
85         // if only one node in a sequence is left (all the others are omitted),
86         // then we need "naked" attributes (not wraped in a tuple)
87         int attr;
88         BOOST_TEST((test_attr("a 123 c", omit['a'] >> int_ >> omit['c'], attr, space)));
89         BOOST_TEST((attr == 123));
90     }
91
92     {
93         // unused means we don't care about the attribute
94         BOOST_TEST((test_attr("abc", char_ >> 'b' >> char_, unused)));
95     }
96
97     // $$$ Not yet implemented $$$
98     //~ {   // test action with omitted attribute
99         //~ char c = 0;
100
101         //~ using boost::phoenix::ref;
102
103         //~ BOOST_TEST(test("x123\"a string\"", (char_ >> omit[int_] >> "\"a string\"")
104             //~ [ref(c) = _1]));
105         //~ BOOST_TEST(c == 'x');
106     //~ }
107
108     // $$$ Not yet implemented $$$
109     //~ {   // test action with omitted attribute
110         //~ int n = 0;
111
112         //~ using boost::phoenix::ref;
113
114         //~ BOOST_TEST(test("x 123 \"a string\"",
115             //~ (omit[char_] >> int_ >> "\"a string\"")[ref(n) = _1], space));
116         //~ BOOST_TEST(n == 123);
117     //~ }
118
119     return boost::report_errors();
120 }
121