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