expect.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 Joel de Guzman
  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/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/qi_operator.hpp>
  8. #include <boost/spirit/include/qi_char.hpp>
  9. #include <boost/spirit/include/qi_string.hpp>
  10. #include <boost/spirit/include/qi_numeric.hpp>
  11. #include <boost/spirit/include/qi_directive.hpp>
  12. #include <boost/spirit/include/qi_action.hpp>
  13. #include <boost/spirit/include/qi_nonterminal.hpp>
  14. #include <boost/spirit/include/qi_auxiliary.hpp>
  15. #include <boost/spirit/include/support_argument.hpp>
  16. #include <boost/fusion/include/vector.hpp>
  17. #include <boost/fusion/include/at.hpp>
  18. #include <boost/spirit/include/phoenix_core.hpp>
  19. #include <boost/spirit/include/phoenix_operator.hpp>
  20. #include <boost/spirit/include/phoenix_statement.hpp>
  21. #include <string>
  22. #include <iostream>
  23. #include "test.hpp"
  24. int
  25. main()
  26. {
  27. using namespace boost::spirit;
  28. using namespace boost::spirit::ascii;
  29. using spirit_test::test;
  30. using spirit_test::print_info;
  31. using boost::spirit::qi::expectation_failure;
  32. {
  33. try
  34. {
  35. BOOST_TEST((test("aa", char_ > char_)));
  36. BOOST_TEST((test("aaa", char_ > char_ > char_('a'))));
  37. BOOST_TEST((test("xi", char_('x') > char_('i'))));
  38. BOOST_TEST((!test("xi", char_('y') > char_('o')))); // should not throw!
  39. BOOST_TEST((test("xin", char_('x') > char_('i') > char_('n'))));
  40. BOOST_TEST((!test("xi", char_('x') > char_('o'))));
  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) == "o");
  47. BOOST_TEST(std::string(x.first, x.last) == "i");
  48. }
  49. }
  50. {
  51. try
  52. {
  53. BOOST_TEST((test(" a a", char_ > char_, space)));
  54. BOOST_TEST((test(" x i", char_('x') > char_('i'), space)));
  55. BOOST_TEST((!test(" x i", char_('x') > char_('o'), space)));
  56. }
  57. catch (expectation_failure<char const*> const& x)
  58. {
  59. std::cout << "expected: "; print_info(x.what_);
  60. std::cout << "got: \"" << x.first << '"' << std::endl;
  61. BOOST_TEST(boost::get<std::string>(x.what_.value) == "o");
  62. BOOST_TEST(std::string(x.first, x.last) == "i");
  63. }
  64. }
  65. {
  66. try
  67. {
  68. BOOST_TEST((test("aA", no_case[char_('a') > 'a'])));
  69. BOOST_TEST((test("BEGIN END", no_case[lit("begin") > "end"], space)));
  70. BOOST_TEST((!test("BEGIN END", no_case[lit("begin") > "nend"], space)));
  71. }
  72. catch (expectation_failure<char const*> const& x)
  73. {
  74. std::cout << "expected: "; print_info(x.what_);
  75. std::cout << "got: \"" << x.first << '"' << std::endl;
  76. BOOST_TEST(x.what_.tag == "no-case-literal-string");
  77. BOOST_TEST(boost::get<std::string>(x.what_.value) == "nend");
  78. BOOST_TEST(std::string(x.first, x.last) == "END");
  79. }
  80. }
  81. {
  82. using boost::spirit::qi::rule;
  83. using boost::spirit::eps;
  84. rule<const wchar_t*, void(int)> r;
  85. r = eps > eps(_r1);
  86. }
  87. return boost::report_errors();
  88. }