1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 // Copyright (c) 2001-2011 Joel de Guzman
3 // Copyright (c) 2010 Bryce Lelbach
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #include <boost/config/warning_disable.hpp>
9 #include <boost/detail/lightweight_test.hpp>
10
11 #include <boost/mpl/print.hpp>
12
13 #include <boost/spirit/include/karma.hpp>
14 #include <boost/spirit/include/support_utree.hpp>
15
16 #include <sstream>
17
18 #include "test.hpp"
19
main()20 int main()
21 {
22 using spirit_test::test;
23 using spirit_test::test_delimited;
24 using boost::spirit::utree;
25 using boost::spirit::utree_type;
26 using boost::spirit::utf8_string_range_type;
27 using boost::spirit::utf8_string_type;
28 using boost::spirit::utf8_symbol_type;
29
30 using boost::spirit::karma::char_;
31 using boost::spirit::karma::bool_;
32 using boost::spirit::karma::int_;
33 using boost::spirit::karma::double_;
34 using boost::spirit::karma::string;
35 using boost::spirit::karma::space;
36 using boost::spirit::karma::rule;
37
38 typedef spirit_test::output_iterator<char>::type output_iterator;
39
40 // as_string
41 {
42 using boost::spirit::karma::digit;
43 using boost::spirit::karma::as_string;
44
45 utree ut("xy");
46 BOOST_TEST(test("xy", string, ut));
47 BOOST_TEST(test("xy", as_string[*char_], ut));
48 BOOST_TEST(test("x,y", as_string[char_ << ',' << char_], ut));
49
50 ut.clear();
51 ut.push_back("ab");
52 ut.push_back(1.2);
53 BOOST_TEST(test("ab1.2", as_string[*~digit] << double_, ut));
54 BOOST_TEST(test("a,b1.2", as_string[~digit % ','] << double_, ut));
55 }
56
57 // as
58 {
59 using boost::spirit::karma::digit;
60 using boost::spirit::karma::as;
61
62 typedef as<std::string> as_string_type;
63 as_string_type const as_string = as_string_type();
64
65 typedef as<utf8_symbol_type> as_symbol_type;
66 as_symbol_type const as_symbol = as_symbol_type();
67
68 utree ut("xy");
69 BOOST_TEST(test("xy", string, ut));
70 BOOST_TEST(test("xy", as_string[*char_], ut));
71 BOOST_TEST(test("x,y", as_string[char_ << ',' << char_], ut));
72
73 ut.clear();
74 ut.push_back("ab");
75 ut.push_back(1.2);
76 BOOST_TEST(test("ab1.2", as_string[*~digit] << double_, ut));
77 BOOST_TEST(test("a,b1.2", as_string[~digit % ','] << double_, ut));
78
79 ut = utf8_symbol_type("xy");
80 BOOST_TEST(test("xy", string, ut));
81 BOOST_TEST(test("xy", as_symbol[*char_], ut));
82 BOOST_TEST(test("x,y", as_symbol[char_ << ',' << char_], ut));
83
84 ut.clear();
85 ut.push_back(utf8_symbol_type("ab"));
86 ut.push_back(1.2);
87 BOOST_TEST(test("ab1.2", as_symbol[*~digit] << double_, ut));
88 BOOST_TEST(test("a,b1.2", as_symbol[~digit % ','] << double_, ut));
89 }
90
91 // typed basic_string rules
92 {
93 utree ut("buzz");
94
95 rule<output_iterator, utf8_string_type()> r1 = string;
96 rule<output_iterator, utf8_symbol_type()> r2 = string;
97
98 BOOST_TEST(test("buzz", r1, ut));
99
100 ut = utf8_symbol_type("bar");
101 BOOST_TEST(test("bar", r2, ut));
102 }
103
104 // parameterized karma::string
105 {
106 utree ut("foo");
107
108 rule<output_iterator, utf8_string_type()> r1 = string("foo");
109 BOOST_TEST(test("foo", string("foo"), ut));
110 BOOST_TEST(test("foo", r1, ut));
111 }
112
113 {
114 using boost::spirit::karma::verbatim;
115 using boost::spirit::karma::repeat;
116 using boost::spirit::karma::space;
117 using boost::spirit::karma::digit;
118
119 utree ut;
120 ut.push_back('x');
121 ut.push_back('y');
122 ut.push_back('c');
123 BOOST_TEST(test_delimited("xy c ", verbatim[repeat(2)[char_]] << char_, ut, space));
124 BOOST_TEST(test_delimited("x yc ", char_ << verbatim[*char_], ut, space));
125
126 ut.clear();
127 ut.push_back('a');
128 ut.push_back('b');
129 ut.push_back(1.2);
130 BOOST_TEST(test_delimited("ab 1.2 ", verbatim[repeat(2)[~digit]] << double_, ut, space));
131 }
132
133 return boost::report_errors();
134 }
135