| 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/spirit/include/karma_symbols.hpp> |
| 7 | |
| 8 | #include <boost/spirit/include/karma_auxiliary.hpp> |
| 9 | #include <boost/spirit/include/karma_char.hpp> |
| 10 | #include <boost/spirit/include/karma_string.hpp> |
| 11 | #include <boost/spirit/include/karma_operator.hpp> |
| 12 | #include <boost/spirit/include/karma_directive.hpp> |
| 13 | #include <boost/spirit/include/karma_generate.hpp> |
| 14 | #include <boost/spirit/include/karma_nonterminal.hpp> |
| 15 | |
| 16 | #include <boost/core/lightweight_test_trait.hpp> |
| 17 | |
| 18 | #include "test.hpp" |
| 19 | |
| 20 | namespace fusion = boost::fusion; |
| 21 | |
| 22 | template <typename T> |
| 23 | inline std::vector<T> |
| 24 | make_vector(T const& t1, T const& t2) |
| 25 | { |
| 26 | std::vector<T> v; |
| 27 | v.push_back(t1); |
| 28 | v.push_back(t2); |
| 29 | return v; |
| 30 | } |
| 31 | |
| 32 | int main() |
| 33 | { |
| 34 | using spirit_test::test; |
| 35 | using boost::spirit::karma::symbols; |
| 36 | |
| 37 | { // advanced |
| 38 | using boost::spirit::karma::rule; |
| 39 | using boost::spirit::karma::lit; |
| 40 | |
| 41 | typedef spirit_test::output_iterator<char>::type output_iterator_type; |
| 42 | |
| 43 | symbols<char, rule<output_iterator_type> > sym; |
| 44 | |
| 45 | rule<output_iterator_type> r1 = lit("Joel" ); |
| 46 | rule<output_iterator_type> r2 = lit("Hartmut" ); |
| 47 | rule<output_iterator_type> r3 = lit("Tom" ); |
| 48 | rule<output_iterator_type> r4 = lit("Kim" ); |
| 49 | |
| 50 | sym.add |
| 51 | ('j', r1.alias()) |
| 52 | ('h', r2.alias()) |
| 53 | ('t', r3.alias()) |
| 54 | ('k', r4.alias()) |
| 55 | ; |
| 56 | |
| 57 | BOOST_TEST_TRAIT_TRUE(( |
| 58 | boost::spirit::traits::is_generator< |
| 59 | symbols<char, rule<output_iterator_type> > >)); |
| 60 | |
| 61 | BOOST_TEST((test("Joel" , sym, 'j'))); |
| 62 | BOOST_TEST((test("Hartmut" , sym, 'h'))); |
| 63 | BOOST_TEST((test("Tom" , sym, 't'))); |
| 64 | BOOST_TEST((test("Kim" , sym, 'k'))); |
| 65 | BOOST_TEST((!test("" , sym, 'x'))); |
| 66 | |
| 67 | // test copy |
| 68 | symbols<char, rule<output_iterator_type> > sym2; |
| 69 | sym2 = sym; |
| 70 | BOOST_TEST((test("Joel" , sym2, 'j'))); |
| 71 | BOOST_TEST((test("Hartmut" , sym2, 'h'))); |
| 72 | BOOST_TEST((test("Tom" , sym2, 't'))); |
| 73 | BOOST_TEST((test("Kim" , sym2, 'k'))); |
| 74 | BOOST_TEST((!test("" , sym2, 'x'))); |
| 75 | |
| 76 | // make sure it plays well with other generators |
| 77 | BOOST_TEST((test("Joelyo" , sym << "yo" , 'j'))); |
| 78 | |
| 79 | sym.remove |
| 80 | ('j') |
| 81 | ('h') |
| 82 | ; |
| 83 | |
| 84 | BOOST_TEST((!test("" , sym, 'j'))); |
| 85 | BOOST_TEST((!test("" , sym, 'h'))); |
| 86 | } |
| 87 | |
| 88 | { // more advanced |
| 89 | using boost::spirit::karma::rule; |
| 90 | using boost::spirit::karma::lit; |
| 91 | using boost::spirit::karma::string; |
| 92 | |
| 93 | typedef spirit_test::output_iterator<char>::type output_iterator_type; |
| 94 | |
| 95 | symbols<char, rule<output_iterator_type, std::string()> > sym; |
| 96 | rule<output_iterator_type, std::string()> r1 = string; |
| 97 | |
| 98 | sym.add |
| 99 | ('j', r1.alias()) |
| 100 | ('h', r1.alias()) |
| 101 | ('t', r1.alias()) |
| 102 | ('k', r1.alias()) |
| 103 | ; |
| 104 | |
| 105 | BOOST_TEST_TRAIT_TRUE(( |
| 106 | boost::spirit::traits::is_generator< |
| 107 | symbols<char, std::string> >)); |
| 108 | |
| 109 | BOOST_TEST((test("Joel" , sym, fusion::make_vector('j', "Joel" )))); |
| 110 | BOOST_TEST((test("Hartmut" , sym, fusion::make_vector('h', "Hartmut" )))); |
| 111 | BOOST_TEST((test("Tom" , sym, fusion::make_vector('t', "Tom" )))); |
| 112 | BOOST_TEST((test("Kim" , sym, fusion::make_vector('k', "Kim" )))); |
| 113 | BOOST_TEST((!test("" , sym, 'x'))); |
| 114 | |
| 115 | // test copy |
| 116 | symbols<char, rule<output_iterator_type, std::string()> > sym2; |
| 117 | sym2 = sym; |
| 118 | BOOST_TEST((test("Joel" , sym2, fusion::make_vector('j', "Joel" )))); |
| 119 | BOOST_TEST((test("Hartmut" , sym2, fusion::make_vector('h', "Hartmut" )))); |
| 120 | BOOST_TEST((test("Tom" , sym2, fusion::make_vector('t', "Tom" )))); |
| 121 | BOOST_TEST((test("Kim" , sym2, fusion::make_vector('k', "Kim" )))); |
| 122 | BOOST_TEST((!test("" , sym2, 'x'))); |
| 123 | |
| 124 | // make sure it plays well with other generators |
| 125 | BOOST_TEST((test("Joelyo" , sym << "yo" , fusion::make_vector('j', "Joel" )))); |
| 126 | |
| 127 | sym.remove |
| 128 | ('j') |
| 129 | ('h') |
| 130 | ; |
| 131 | |
| 132 | BOOST_TEST((!test("" , sym, 'j'))); |
| 133 | BOOST_TEST((!test("" , sym, 'h'))); |
| 134 | } |
| 135 | |
| 136 | { // test for proto problem with rvalue references (10-11-2011) |
| 137 | symbols<char, std::string> sym; |
| 138 | |
| 139 | sym += std::make_pair(x: 'j', y: "Joel" ); |
| 140 | sym += std::make_pair(x: 'h', y: "Hartmut" ); |
| 141 | |
| 142 | BOOST_TEST((test("Joel" , sym, 'j'))); |
| 143 | BOOST_TEST((test("Hartmut" , sym, 'h'))); |
| 144 | } |
| 145 | |
| 146 | return boost::report_errors(); |
| 147 | } |
| 148 | |