one_of.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2010.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/metaparse/one_of.hpp>
  6. #include <boost/metaparse/one_char.hpp>
  7. #include <boost/metaparse/fail.hpp>
  8. #include <boost/metaparse/is_error.hpp>
  9. #include <boost/metaparse/start.hpp>
  10. #include <boost/metaparse/get_result.hpp>
  11. #include <boost/metaparse/sequence.hpp>
  12. #include <boost/metaparse/get_position.hpp>
  13. #include <boost/metaparse/next_char.hpp>
  14. #include "common.hpp"
  15. #include <boost/mpl/equal_to.hpp>
  16. #include <boost/mpl/apply_wrap.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include "test_case.hpp"
  19. BOOST_METAPARSE_TEST_CASE(one_of)
  20. {
  21. using boost::metaparse::is_error;
  22. using boost::metaparse::one_of;
  23. using boost::metaparse::start;
  24. using boost::metaparse::get_result;
  25. using boost::metaparse::one_char;
  26. using boost::metaparse::fail;
  27. using boost::metaparse::sequence;
  28. using boost::metaparse::get_position;
  29. using boost::metaparse::next_char;
  30. using boost::mpl::apply_wrap2;
  31. using boost::mpl::equal_to;
  32. typedef fail<test_failure> test_fail;
  33. typedef sequence<one_char, test_fail> test_fail_later;
  34. // test_1_with_good
  35. BOOST_MPL_ASSERT((
  36. equal_to<
  37. get_result<apply_wrap2<one_of<one_char>, str_hello, start> >::type,
  38. char_h
  39. >
  40. ));
  41. // test_1_with_bad
  42. BOOST_MPL_ASSERT((
  43. is_error<apply_wrap2<one_of<test_fail>, str_hello, start> >
  44. ));
  45. // test_2_with_two_good
  46. BOOST_MPL_ASSERT((
  47. equal_to<
  48. get_result<
  49. apply_wrap2<one_of<one_char, one_char>, str_hello, start>
  50. >::type,
  51. char_h
  52. >
  53. ));
  54. // test_2_with_first_good
  55. BOOST_MPL_ASSERT((
  56. equal_to<
  57. get_result<
  58. apply_wrap2<one_of<one_char, test_fail>, str_hello, start>
  59. >::type,
  60. char_h
  61. >
  62. ));
  63. // test_2_with_second_good
  64. BOOST_MPL_ASSERT((
  65. equal_to<
  66. get_result<
  67. apply_wrap2<one_of<test_fail, one_char>, str_hello, start>
  68. >::type,
  69. char_h
  70. >
  71. ));
  72. // test_2_with_two_bad
  73. BOOST_MPL_ASSERT((
  74. is_error<apply_wrap2<one_of<test_fail, test_fail>, str_hello, start> >
  75. ));
  76. // test
  77. BOOST_MPL_ASSERT((is_error<apply_wrap2<one_of< >, str_hello, start> >));
  78. // test_with_good
  79. BOOST_MPL_ASSERT((
  80. equal_to<
  81. get_result<apply_wrap2<one_of<one_char>, str_hello, start> >::type,
  82. char_h
  83. >
  84. ));
  85. // test_with_bad
  86. BOOST_MPL_ASSERT((is_error<apply_wrap2<one_of<test_fail>,str_hello,start> >));
  87. // test_with_two_good
  88. BOOST_MPL_ASSERT((
  89. equal_to<
  90. get_result<
  91. apply_wrap2<one_of<one_char, one_char>, str_hello, start>
  92. >::type,
  93. char_h
  94. >
  95. ));
  96. // test_with_first_good
  97. BOOST_MPL_ASSERT((
  98. equal_to<
  99. get_result<
  100. apply_wrap2<one_of<one_char, test_fail>, str_hello, start>
  101. >::type,
  102. char_h
  103. >
  104. ));
  105. // test_with_second_good
  106. BOOST_MPL_ASSERT((
  107. equal_to<
  108. get_result<
  109. apply_wrap2<one_of<test_fail, one_char>, str_hello, start>
  110. >::type,
  111. char_h
  112. >
  113. ));
  114. // test_with_two_bad
  115. BOOST_MPL_ASSERT((
  116. is_error<apply_wrap2<one_of<test_fail, test_fail>, str_hello, start> >
  117. ));
  118. // test_error_is_the_last_error
  119. BOOST_MPL_ASSERT((
  120. equal_to<
  121. next_char<start, char_h>::type,
  122. get_position<
  123. apply_wrap2<
  124. one_of<test_fail, test_fail_later>,
  125. str_hello,
  126. start
  127. >
  128. >::type
  129. >
  130. ));
  131. }