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/config/warning_disable.hpp>
7 #include <boost/detail/lightweight_test.hpp>
8 
9 #include <boost/spirit/include/karma_auxiliary.hpp>
10 #include <boost/spirit/include/karma_char.hpp>
11 #include <boost/spirit/include/karma_string.hpp>
12 #include <boost/spirit/include/karma_operator.hpp>
13 #include <boost/spirit/include/karma_directive.hpp>
14 #include <boost/spirit/include/karma_generate.hpp>
15 #include <boost/spirit/include/karma_nonterminal.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::mpl::true_ f =
54             boost::mpl::bool_<boost::spirit::traits::is_generator<
55                 symbols<char, rule<output_iterator_type, char()> > >::value>();
56 
57         // silence stupid compiler warnings
58         // i.e. MSVC warning C4189: 'f' : local variable is initialized but not referenced
59         BOOST_TEST((f.value));
60 
61         BOOST_TEST((test("J", sym, make_vector('j', 'J'))));
62         BOOST_TEST((test("H", sym, make_vector('h', 'H'))));
63         BOOST_TEST((test("T", sym, make_vector('t', 'T'))));
64         BOOST_TEST((test("K", sym, make_vector('k', 'K'))));
65         BOOST_TEST((!test("", sym, 'x')));
66 
67         // test copy
68         symbols<char, rule<output_iterator_type, char()> > sym2;
69         sym2 = sym;
70         BOOST_TEST((test("J", sym2, make_vector('j', 'J'))));
71         BOOST_TEST((test("H", sym2, make_vector('h', 'H'))));
72         BOOST_TEST((test("T", sym2, make_vector('t', 'T'))));
73         BOOST_TEST((test("K", sym2, make_vector('k', 'K'))));
74         BOOST_TEST((!test("", sym2, 'x')));
75 
76         // make sure it plays well with other generators
77         BOOST_TEST((test("Jyo", sym << "yo", make_vector('j', '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     { // basics
89         symbols<std::string> sym;
90 
91         sym.add
92             ("Joel")
93             ("Hartmut")
94             ("Tom")
95             ("Kim")
96         ;
97 
98         boost::mpl::true_ f =
99             boost::mpl::bool_<boost::spirit::traits::is_generator<
100                 symbols<char, std::string> >::value>();
101 
102         // silence stupid compiler warnings
103         // i.e. MSVC warning C4189: 'f' : local variable is initialized but not referenced
104         BOOST_TEST((f.value));
105 
106         BOOST_TEST((test("Joel", sym, "Joel")));
107         BOOST_TEST((test("Hartmut", sym, "Hartmut")));
108         BOOST_TEST((test("Tom", sym, "Tom")));
109         BOOST_TEST((test("Kim", sym, "Kim")));
110         BOOST_TEST((!test("", sym, "X")));
111 
112         // test copy
113         symbols<std::string> sym2;
114         sym2 = sym;
115         BOOST_TEST((test("Joel", sym2, "Joel")));
116         BOOST_TEST((test("Hartmut", sym2, "Hartmut")));
117         BOOST_TEST((test("Tom", sym2, "Tom")));
118         BOOST_TEST((test("Kim", sym2, "Kim")));
119         BOOST_TEST((!test("", sym2, "X")));
120 
121         // make sure it plays well with other generators
122         BOOST_TEST((test("Joelyo", sym << "yo", "Joel")));
123 
124         sym.remove
125             ("Joel")
126             ("Hartmut")
127         ;
128 
129         BOOST_TEST((!test("", sym, "Joel")));
130         BOOST_TEST((!test("", sym, "Hartmut")));
131     }
132 
133     { // name
134         symbols <std::string> sym("test1"), sym2;
135         BOOST_TEST(sym.name() == "test1");
136 
137         sym.name("test");
138         BOOST_TEST(sym.name() == "test");
139         sym2 = sym;
140         BOOST_TEST(sym2.name() == "test");
141 
142         symbols <std::string> sym3(sym);
143         BOOST_TEST(sym3.name() == "test");
144     }
145 
146     return boost::report_errors();
147 }
148