expectd.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 Joel de Guzman
  3. Copyright (c) 2016 Frank Hein, maxence business consulting gmbh
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/include/qi_operator.hpp>
  9. #include <boost/spirit/include/qi_char.hpp>
  10. #include <boost/spirit/include/qi_string.hpp>
  11. #include <boost/spirit/include/qi_numeric.hpp>
  12. #include <boost/spirit/include/qi_directive.hpp>
  13. #include <boost/spirit/include/qi_action.hpp>
  14. #include <boost/spirit/include/qi_nonterminal.hpp>
  15. #include <boost/spirit/include/qi_auxiliary.hpp>
  16. #include <boost/spirit/include/support_argument.hpp>
  17. #include <boost/fusion/include/vector.hpp>
  18. #include <boost/fusion/include/at.hpp>
  19. #include <boost/spirit/include/phoenix_core.hpp>
  20. #include <boost/spirit/include/phoenix_operator.hpp>
  21. #include <boost/spirit/include/phoenix_statement.hpp>
  22. #include <string>
  23. #include <iostream>
  24. #include "test.hpp"
  25. int
  26. main()
  27. {
  28. using namespace boost::spirit;
  29. using namespace boost::spirit::ascii;
  30. using spirit_test::test;
  31. using spirit_test::print_info;
  32. using boost::spirit::qi::expectation_failure;
  33. {
  34. try
  35. {
  36. BOOST_TEST((test("aa", expect[char_ >> char_])));
  37. BOOST_TEST((test("aaa", expect[char_ >> char_ >> char_('a')])));
  38. BOOST_TEST((test("xi", expect[char_('x') >> char_('i')])));
  39. BOOST_TEST((test("xin", expect[char_('x') >> char_('i') >> char_('n')])));
  40. BOOST_TEST((!test("xi", expect[char_('y')]))); // should throw!
  41. }
  42. catch (expectation_failure<char const*> const& x)
  43. {
  44. std::cout << "expected: "; print_info(x.what_);
  45. std::cout << "got: \"" << x.first << '"' << std::endl;
  46. BOOST_TEST(boost::get<std::string>(x.what_.value) == "y");
  47. BOOST_TEST(std::string(x.first, x.last) == "xi");
  48. }
  49. }
  50. {
  51. try
  52. {
  53. BOOST_TEST((!test("xi", expect[char_('x') >> char_('o')])));
  54. }
  55. catch (expectation_failure<char const*> const& x)
  56. {
  57. std::cout << "expected: "; print_info(x.what_);
  58. std::cout << "got: \"" << x.first << '"' << std::endl;
  59. BOOST_TEST(std::string(x.first, x.last) == "xi");
  60. BOOST_TEST(x.what_.tag == "sequence");
  61. }
  62. }
  63. {
  64. try
  65. {
  66. BOOST_TEST((!test(" x i", expect[char_('x') >> char_('o')], space)));
  67. }
  68. catch (expectation_failure<char const*> const& x)
  69. {
  70. std::cout << "expected: "; print_info(x.what_);
  71. std::cout << "got: \"" << x.first << '"' << std::endl;
  72. BOOST_TEST(std::string(x.first, x.last) == " x i");
  73. BOOST_TEST(x.what_.tag == "sequence");
  74. }
  75. }
  76. {
  77. try
  78. {
  79. BOOST_TEST((test(" a a", expect[char_ >> char_], space)));
  80. BOOST_TEST((test(" x i", expect[char_('x') >> char_('i')], space)));
  81. BOOST_TEST((!test(" x i", expect[char_('y')], space)));
  82. }
  83. catch (expectation_failure<char const*> const& x)
  84. {
  85. std::cout << "expected: "; print_info(x.what_);
  86. std::cout << "got: \"" << x.first << '"' << std::endl;
  87. BOOST_TEST(boost::get<std::string>(x.what_.value) == "y");
  88. BOOST_TEST(std::string(x.first, x.last) == "x i");
  89. }
  90. }
  91. {
  92. try
  93. {
  94. BOOST_TEST((test("aA", expect[no_case[char_('a') >> 'a']])));
  95. BOOST_TEST((test("BEGIN END", expect[no_case[lit("begin") >> "end"]], space)));
  96. BOOST_TEST((!test("BEGIN END", expect[no_case[lit("begin") >> "nend"]], space)));
  97. }
  98. catch (expectation_failure<char const*> const& x)
  99. {
  100. std::cout << "expected: "; print_info(x.what_);
  101. std::cout << "got: \"" << x.first << '"' << std::endl;
  102. BOOST_TEST(x.what_.tag == "sequence");
  103. BOOST_TEST(std::string(x.first, x.last) == "BEGIN END");
  104. }
  105. }
  106. {
  107. using boost::spirit::qi::rule;
  108. using boost::spirit::eps;
  109. rule<const wchar_t*, void(int)> r;
  110. r = expect[eps(_r1)];
  111. }
  112. return boost::report_errors();
  113. }