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     { // basics
37         symbols<char, std::string> sym;
38 
39         sym.add
40             ('j', "Joel")
41             ('h', "Hartmut")
42             ('t', "Tom")
43             ('k', "Kim")
44         ;
45 
46         boost::mpl::true_ f =
47             boost::mpl::bool_<boost::spirit::traits::is_generator<
48                 symbols<char, std::string> >::value>();
49 
50         // silence stupid compiler warnings
51         // i.e. MSVC warning C4189: 'f' : local variable is initialized but not referenced
52         BOOST_TEST((f.value));
53 
54         BOOST_TEST((test("Joel", sym, 'j')));
55         BOOST_TEST((test("Hartmut", sym, 'h')));
56         BOOST_TEST((test("Tom", sym, 't')));
57         BOOST_TEST((test("Kim", sym, 'k')));
58         BOOST_TEST((!test("", sym, 'x')));
59 
60         // test copy
61         symbols<char, std::string> sym2;
62         sym2 = sym;
63         BOOST_TEST((test("Joel", sym2, 'j')));
64         BOOST_TEST((test("Hartmut", sym2, 'h')));
65         BOOST_TEST((test("Tom", sym2, 't')));
66         BOOST_TEST((test("Kim", sym2, 'k')));
67         BOOST_TEST((!test("", sym2, 'x')));
68 
69         // make sure it plays well with other generators
70         BOOST_TEST((test("Joelyo", sym << "yo", 'j')));
71 
72         sym.remove
73             ('j')
74             ('h')
75         ;
76 
77         BOOST_TEST((!test("", sym, 'j')));
78         BOOST_TEST((!test("", sym, 'h')));
79     }
80 
81     { // lower/upper handling
82         using namespace boost::spirit::ascii;
83         using boost::spirit::karma::lower;
84         using boost::spirit::karma::upper;
85 
86         symbols<char, std::string> sym;
87         sym.add
88             ('j', "Joel")
89             ('h', "Hartmut")
90             ('t', "Tom")
91             ('k', "Kim")
92         ;
93 
94         BOOST_TEST((test("joel", lower[sym], 'j')));
95         BOOST_TEST((test("hartmut", lower[sym], 'h')));
96         BOOST_TEST((test("tom", lower[sym], 't')));
97         BOOST_TEST((test("kim", lower[sym], 'k')));
98 
99         BOOST_TEST((test("JOEL", upper[sym], 'j')));
100         BOOST_TEST((test("HARTMUT", upper[sym], 'h')));
101         BOOST_TEST((test("TOM", upper[sym], 't')));
102         BOOST_TEST((test("KIM", upper[sym], 'k')));
103 
104         // make sure it plays well with other generators
105         BOOST_TEST((test("joelyo", lower[sym] << "yo", 'j')));
106         BOOST_TEST((test("JOELyo", upper[sym] << "yo", 'j')));
107     }
108 
109     return boost::report_errors();
110 }
111