| 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 | { // more advanced |
| 38 | using boost::spirit::karma::rule; |
| 39 | using boost::spirit::karma::lit; |
| 40 | using boost::spirit::karma::char_; |
| 41 | |
| 42 | typedef spirit_test::output_iterator<char>::type output_iterator_type; |
| 43 | |
| 44 | symbols<char, rule<output_iterator_type, char()> > sym; |
| 45 | rule<output_iterator_type, char()> r1 = char_; |
| 46 | |
| 47 | sym.add |
| 48 | ('j', r1.alias()) |
| 49 | ('h', r1.alias()) |
| 50 | ('t', r1.alias()) |
| 51 | ('k', r1.alias()) |
| 52 | ; |
| 53 | |
| 54 | BOOST_TEST_TRAIT_TRUE(( |
| 55 | boost::spirit::traits::is_generator< |
| 56 | symbols<char, rule<output_iterator_type, char()> > >)); |
| 57 | |
| 58 | BOOST_TEST((test("J" , sym, make_vector('j', 'J')))); |
| 59 | BOOST_TEST((test("H" , sym, make_vector('h', 'H')))); |
| 60 | BOOST_TEST((test("T" , sym, make_vector('t', 'T')))); |
| 61 | BOOST_TEST((test("K" , sym, make_vector('k', 'K')))); |
| 62 | BOOST_TEST((!test("" , sym, 'x'))); |
| 63 | |
| 64 | // test copy |
| 65 | symbols<char, rule<output_iterator_type, char()> > sym2; |
| 66 | sym2 = sym; |
| 67 | BOOST_TEST((test("J" , sym2, make_vector('j', 'J')))); |
| 68 | BOOST_TEST((test("H" , sym2, make_vector('h', 'H')))); |
| 69 | BOOST_TEST((test("T" , sym2, make_vector('t', 'T')))); |
| 70 | BOOST_TEST((test("K" , sym2, make_vector('k', 'K')))); |
| 71 | BOOST_TEST((!test("" , sym2, 'x'))); |
| 72 | |
| 73 | // make sure it plays well with other generators |
| 74 | BOOST_TEST((test("Jyo" , sym << "yo" , make_vector('j', 'J')))); |
| 75 | |
| 76 | sym.remove |
| 77 | ('j') |
| 78 | ('h') |
| 79 | ; |
| 80 | |
| 81 | BOOST_TEST((!test("" , sym, 'j'))); |
| 82 | BOOST_TEST((!test("" , sym, 'h'))); |
| 83 | } |
| 84 | |
| 85 | { // basics |
| 86 | symbols<std::string> sym; |
| 87 | |
| 88 | sym.add |
| 89 | ("Joel" ) |
| 90 | ("Hartmut" ) |
| 91 | ("Tom" ) |
| 92 | ("Kim" ) |
| 93 | ; |
| 94 | |
| 95 | BOOST_TEST_TRAIT_TRUE(( |
| 96 | boost::spirit::traits::is_generator< |
| 97 | symbols<char, std::string> >)); |
| 98 | |
| 99 | BOOST_TEST((test("Joel" , sym, "Joel" ))); |
| 100 | BOOST_TEST((test("Hartmut" , sym, "Hartmut" ))); |
| 101 | BOOST_TEST((test("Tom" , sym, "Tom" ))); |
| 102 | BOOST_TEST((test("Kim" , sym, "Kim" ))); |
| 103 | BOOST_TEST((!test("" , sym, "X" ))); |
| 104 | |
| 105 | // test copy |
| 106 | symbols<std::string> sym2; |
| 107 | sym2 = sym; |
| 108 | BOOST_TEST((test("Joel" , sym2, "Joel" ))); |
| 109 | BOOST_TEST((test("Hartmut" , sym2, "Hartmut" ))); |
| 110 | BOOST_TEST((test("Tom" , sym2, "Tom" ))); |
| 111 | BOOST_TEST((test("Kim" , sym2, "Kim" ))); |
| 112 | BOOST_TEST((!test("" , sym2, "X" ))); |
| 113 | |
| 114 | // make sure it plays well with other generators |
| 115 | BOOST_TEST((test("Joelyo" , sym << "yo" , "Joel" ))); |
| 116 | |
| 117 | sym.remove |
| 118 | ("Joel" ) |
| 119 | ("Hartmut" ) |
| 120 | ; |
| 121 | |
| 122 | BOOST_TEST((!test("" , sym, "Joel" ))); |
| 123 | BOOST_TEST((!test("" , sym, "Hartmut" ))); |
| 124 | } |
| 125 | |
| 126 | { // name |
| 127 | symbols <std::string> sym("test1" ), sym2; |
| 128 | BOOST_TEST(sym.name() == "test1" ); |
| 129 | |
| 130 | sym.name(str: "test" ); |
| 131 | BOOST_TEST(sym.name() == "test" ); |
| 132 | sym2 = sym; |
| 133 | BOOST_TEST(sym2.name() == "test" ); |
| 134 | |
| 135 | symbols <std::string> sym3(sym); |
| 136 | BOOST_TEST(sym3.name() == "test" ); |
| 137 | } |
| 138 | |
| 139 | return boost::report_errors(); |
| 140 | } |
| 141 | |