symbols1.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <boost/spirit/include/karma_auxiliary.hpp>
  6. #include <boost/spirit/include/karma_char.hpp>
  7. #include <boost/spirit/include/karma_string.hpp>
  8. #include <boost/spirit/include/karma_operator.hpp>
  9. #include <boost/spirit/include/karma_directive.hpp>
  10. #include <boost/spirit/include/karma_generate.hpp>
  11. #include <boost/spirit/include/karma_nonterminal.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. #include <boost/core/lightweight_test_trait.hpp>
  14. #include "test.hpp"
  15. namespace fusion = boost::fusion;
  16. template <typename T>
  17. inline std::vector<T>
  18. make_vector(T const& t1, T const& t2)
  19. {
  20. std::vector<T> v;
  21. v.push_back(t1);
  22. v.push_back(t2);
  23. return v;
  24. }
  25. int main()
  26. {
  27. using spirit_test::test;
  28. using boost::spirit::karma::symbols;
  29. { // basics
  30. symbols<char, std::string> sym;
  31. sym.add
  32. ('j', "Joel")
  33. ('h', "Hartmut")
  34. ('t', "Tom")
  35. ('k', "Kim")
  36. ;
  37. BOOST_TEST_TRAIT_TRUE((
  38. boost::spirit::traits::is_generator<
  39. symbols<char, std::string> >));
  40. BOOST_TEST((test("Joel", sym, 'j')));
  41. BOOST_TEST((test("Hartmut", sym, 'h')));
  42. BOOST_TEST((test("Tom", sym, 't')));
  43. BOOST_TEST((test("Kim", sym, 'k')));
  44. BOOST_TEST((!test("", sym, 'x')));
  45. // test copy
  46. symbols<char, std::string> sym2;
  47. sym2 = sym;
  48. BOOST_TEST((test("Joel", sym2, 'j')));
  49. BOOST_TEST((test("Hartmut", sym2, 'h')));
  50. BOOST_TEST((test("Tom", sym2, 't')));
  51. BOOST_TEST((test("Kim", sym2, 'k')));
  52. BOOST_TEST((!test("", sym2, 'x')));
  53. // make sure it plays well with other generators
  54. BOOST_TEST((test("Joelyo", sym << "yo", 'j')));
  55. sym.remove
  56. ('j')
  57. ('h')
  58. ;
  59. BOOST_TEST((!test("", sym, 'j')));
  60. BOOST_TEST((!test("", sym, 'h')));
  61. }
  62. { // lower/upper handling
  63. using namespace boost::spirit::ascii;
  64. using boost::spirit::karma::lower;
  65. using boost::spirit::karma::upper;
  66. symbols<char, std::string> sym;
  67. sym.add
  68. ('j', "Joel")
  69. ('h', "Hartmut")
  70. ('t', "Tom")
  71. ('k', "Kim")
  72. ;
  73. BOOST_TEST((test("joel", lower[sym], 'j')));
  74. BOOST_TEST((test("hartmut", lower[sym], 'h')));
  75. BOOST_TEST((test("tom", lower[sym], 't')));
  76. BOOST_TEST((test("kim", lower[sym], 'k')));
  77. BOOST_TEST((test("JOEL", upper[sym], 'j')));
  78. BOOST_TEST((test("HARTMUT", upper[sym], 'h')));
  79. BOOST_TEST((test("TOM", upper[sym], 't')));
  80. BOOST_TEST((test("KIM", upper[sym], 'k')));
  81. // make sure it plays well with other generators
  82. BOOST_TEST((test("joelyo", lower[sym] << "yo", 'j')));
  83. BOOST_TEST((test("JOELyo", upper[sym] << "yo", 'j')));
  84. }
  85. return boost::report_errors();
  86. }