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