]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
1e59de90 | 8 | #include <boost/spirit/include/karma_as.hpp> |
7c673cae FG |
9 | |
10 | #include <boost/spirit/include/karma.hpp> | |
11 | #include <boost/spirit/include/support_utree.hpp> | |
12 | ||
13 | #include <sstream> | |
14 | ||
15 | #include "test.hpp" | |
16 | ||
17 | int main() | |
18 | { | |
19 | using spirit_test::test; | |
20 | using spirit_test::test_delimited; | |
21 | using boost::spirit::utree; | |
22 | using boost::spirit::utree_type; | |
23 | using boost::spirit::utf8_string_range_type; | |
24 | using boost::spirit::utf8_string_type; | |
25 | using boost::spirit::utf8_symbol_type; | |
26 | ||
27 | using boost::spirit::karma::char_; | |
28 | using boost::spirit::karma::bool_; | |
29 | using boost::spirit::karma::int_; | |
30 | using boost::spirit::karma::double_; | |
31 | using boost::spirit::karma::string; | |
32 | using boost::spirit::karma::space; | |
33 | using boost::spirit::karma::rule; | |
34 | ||
35 | typedef spirit_test::output_iterator<char>::type output_iterator; | |
36 | ||
37 | // as_string | |
38 | { | |
39 | using boost::spirit::karma::digit; | |
40 | using boost::spirit::karma::as_string; | |
41 | ||
42 | utree ut("xy"); | |
43 | BOOST_TEST(test("xy", string, ut)); | |
44 | BOOST_TEST(test("xy", as_string[*char_], ut)); | |
45 | BOOST_TEST(test("x,y", as_string[char_ << ',' << char_], ut)); | |
46 | ||
47 | ut.clear(); | |
48 | ut.push_back("ab"); | |
49 | ut.push_back(1.2); | |
50 | BOOST_TEST(test("ab1.2", as_string[*~digit] << double_, ut)); | |
51 | BOOST_TEST(test("a,b1.2", as_string[~digit % ','] << double_, ut)); | |
52 | } | |
53 | ||
54 | // as | |
55 | { | |
56 | using boost::spirit::karma::digit; | |
57 | using boost::spirit::karma::as; | |
58 | ||
59 | typedef as<std::string> as_string_type; | |
60 | as_string_type const as_string = as_string_type(); | |
61 | ||
62 | typedef as<utf8_symbol_type> as_symbol_type; | |
63 | as_symbol_type const as_symbol = as_symbol_type(); | |
64 | ||
65 | utree ut("xy"); | |
66 | BOOST_TEST(test("xy", string, ut)); | |
67 | BOOST_TEST(test("xy", as_string[*char_], ut)); | |
68 | BOOST_TEST(test("x,y", as_string[char_ << ',' << char_], ut)); | |
69 | ||
70 | ut.clear(); | |
71 | ut.push_back("ab"); | |
72 | ut.push_back(1.2); | |
73 | BOOST_TEST(test("ab1.2", as_string[*~digit] << double_, ut)); | |
74 | BOOST_TEST(test("a,b1.2", as_string[~digit % ','] << double_, ut)); | |
75 | ||
76 | ut = utf8_symbol_type("xy"); | |
77 | BOOST_TEST(test("xy", string, ut)); | |
78 | BOOST_TEST(test("xy", as_symbol[*char_], ut)); | |
79 | BOOST_TEST(test("x,y", as_symbol[char_ << ',' << char_], ut)); | |
80 | ||
81 | ut.clear(); | |
82 | ut.push_back(utf8_symbol_type("ab")); | |
83 | ut.push_back(1.2); | |
84 | BOOST_TEST(test("ab1.2", as_symbol[*~digit] << double_, ut)); | |
85 | BOOST_TEST(test("a,b1.2", as_symbol[~digit % ','] << double_, ut)); | |
86 | } | |
87 | ||
88 | // typed basic_string rules | |
89 | { | |
90 | utree ut("buzz"); | |
91 | ||
92 | rule<output_iterator, utf8_string_type()> r1 = string; | |
93 | rule<output_iterator, utf8_symbol_type()> r2 = string; | |
94 | ||
95 | BOOST_TEST(test("buzz", r1, ut)); | |
96 | ||
97 | ut = utf8_symbol_type("bar"); | |
98 | BOOST_TEST(test("bar", r2, ut)); | |
99 | } | |
100 | ||
101 | // parameterized karma::string | |
102 | { | |
103 | utree ut("foo"); | |
104 | ||
105 | rule<output_iterator, utf8_string_type()> r1 = string("foo"); | |
106 | BOOST_TEST(test("foo", string("foo"), ut)); | |
107 | BOOST_TEST(test("foo", r1, ut)); | |
108 | } | |
109 | ||
110 | { | |
111 | using boost::spirit::karma::verbatim; | |
112 | using boost::spirit::karma::repeat; | |
113 | using boost::spirit::karma::space; | |
114 | using boost::spirit::karma::digit; | |
115 | ||
116 | utree ut; | |
117 | ut.push_back('x'); | |
118 | ut.push_back('y'); | |
119 | ut.push_back('c'); | |
120 | BOOST_TEST(test_delimited("xy c ", verbatim[repeat(2)[char_]] << char_, ut, space)); | |
121 | BOOST_TEST(test_delimited("x yc ", char_ << verbatim[*char_], ut, space)); | |
122 | ||
123 | ut.clear(); | |
124 | ut.push_back('a'); | |
125 | ut.push_back('b'); | |
126 | ut.push_back(1.2); | |
127 | BOOST_TEST(test_delimited("ab 1.2 ", verbatim[repeat(2)[~digit]] << double_, ut, space)); | |
128 | } | |
129 | ||
130 | return boost::report_errors(); | |
131 | } |