1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 // #define KARMA_TEST_COMPILE_FAIL
7 
8 #include <boost/config/warning_disable.hpp>
9 #include <boost/detail/lightweight_test.hpp>
10 
11 #include <boost/spirit/include/karma_char.hpp>
12 #include <boost/spirit/include/karma_string.hpp>
13 #include <boost/spirit/include/karma_numeric.hpp>
14 #include <boost/spirit/include/karma_generate.hpp>
15 #include <boost/spirit/include/karma_operator.hpp>
16 #include <boost/spirit/include/karma_directive.hpp>
17 #include <boost/spirit/include/karma_action.hpp>
18 #include <boost/spirit/include/karma_nonterminal.hpp>
19 #include <boost/spirit/include/karma_auxiliary.hpp>
20 #include <boost/spirit/include/karma_directive.hpp>
21 #include <boost/spirit/include/support_unused.hpp>
22 #include <boost/spirit/include/phoenix_core.hpp>
23 #include <boost/spirit/include/phoenix_operator.hpp>
24 #include <boost/spirit/include/phoenix_statement.hpp>
25 #include <boost/spirit/include/phoenix_stl.hpp>
26 #include <boost/fusion/include/vector.hpp>
27 
28 #include "test.hpp"
29 
30 using namespace spirit_test;
31 
32 ///////////////////////////////////////////////////////////////////////////////
main()33 int main()
34 {
35     using namespace boost::spirit;
36     using namespace boost::spirit::ascii;
37     namespace fusion = boost::fusion;
38 
39     {
40         BOOST_TEST(test("xi", char_('x') << char_('i')));
41         BOOST_TEST(!test("xi", char_('x') << char_('o')));
42     }
43 
44     {
45         BOOST_TEST(test_delimited("x i ", char_('x') << 'i', char(' ')));
46         BOOST_TEST(!test_delimited("x i ",
47             char_('x') << char_('o'), char(' ')));
48     }
49 
50     {
51         BOOST_TEST(test_delimited("Hello , World ",
52             lit("Hello") << ',' << "World", char(' ')));
53     }
54 
55     {
56         // a single element
57         char attr = 'a';
58         BOOST_TEST((test("ab", char_ << 'b', attr)));
59     }
60 
61     {
62         // a single element fusion sequence
63         fusion::vector<char> attr('a');
64         BOOST_TEST((test("ab", char_ << 'b', attr)));
65     }
66 
67     {
68         fusion::vector<char, char, std::string> p ('a', 'b', "cdefg");
69         BOOST_TEST(test("abcdefg", char_ << char_ << string, p));
70         BOOST_TEST(test_delimited("a b cdefg ",
71             char_ << char_ << string, p, char(' ')));
72     }
73 
74     {
75         fusion::vector<char, int, char> p ('a', 12, 'c');
76         BOOST_TEST(test("a12c", char_ << int_ << char_, p));
77         BOOST_TEST(test_delimited("a 12 c ",
78             char_ << int_ << char_, p, char(' ')));
79     }
80 
81     {
82         // element sequence can be shorter and longer than the attribute
83         // sequence
84         using boost::spirit::karma::strict;
85         using boost::spirit::karma::relaxed;
86 
87         fusion::vector<char, int, char> p ('a', 12, 'c');
88         BOOST_TEST(test("a12", char_ << int_, p));
89         BOOST_TEST(test_delimited("a 12 ", char_ << int_, p, char(' ')));
90 
91         BOOST_TEST(test("a12", relaxed[char_ << int_], p));
92         BOOST_TEST(test_delimited("a 12 ", relaxed[char_ << int_], p, char(' ')));
93 
94         BOOST_TEST(!test("", strict[char_ << int_], p));
95         BOOST_TEST(!test_delimited("", strict[char_ << int_], p, char(' ')));
96 
97         fusion::vector<char, int> p1 ('a', 12);
98         BOOST_TEST(test("a12c", char_ << int_ << char_('c'), p1));
99         BOOST_TEST(test_delimited("a 12 c ", char_ << int_ << char_('c'),
100             p1, char(' ')));
101 
102         BOOST_TEST(test("a12c", relaxed[char_ << int_ << char_('c')], p1));
103         BOOST_TEST(test_delimited("a 12 c ",
104             relaxed[char_ << int_ << char_('c')], p1, char(' ')));
105 
106         BOOST_TEST(!test("", strict[char_ << int_ << char_('c')], p1));
107         BOOST_TEST(!test_delimited("", strict[char_ << int_ << char_('c')],
108             p1, char(' ')));
109 
110         BOOST_TEST(test("a12", strict[char_ << int_], p1));
111         BOOST_TEST(test_delimited("a 12 ", strict[char_ << int_], p1, char(' ')));
112 
113         std::string value("foo ' bar");
114         BOOST_TEST(test("\"foo ' bar\"", '"' << strict[*(~char_('*'))] << '"', value));
115         BOOST_TEST(test("\"foo ' bar\"", strict['"' << *(~char_('*')) << '"'], value));
116     }
117 
118     {
119         // if all elements of a sequence have unused parameters, the whole
120         // sequence has an unused parameter as well
121         fusion::vector<char, char> p ('a', 'e');
122         BOOST_TEST(test("abcde",
123             char_ << (lit('b') << 'c' << 'd') << char_, p));
124         BOOST_TEST(test_delimited("a b c d e ",
125             char_ << (lit('b') << 'c' << 'd') << char_, p, char(' ')));
126     }
127 
128     {
129         // literal generators do not need an attribute
130         fusion::vector<char, char> p('a', 'c');
131         BOOST_TEST(test("abc", char_ << 'b' << char_, p));
132         BOOST_TEST(test_delimited("a b c ",
133             char_ << 'b' << char_, p, char(' ')));
134     }
135 
136     {
137         // literal generators do not need an attribute, not even at the end
138         fusion::vector<char, char> p('a', 'c');
139         BOOST_TEST(test("acb", char_ << char_ << 'b', p));
140         BOOST_TEST(test_delimited("a c b ",
141             char_ << char_ << 'b', p, char(' ')));
142     }
143 
144     return boost::report_errors();
145 }
146 
147