distinct_tests.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*=============================================================================
  2. Copyright (c) 2003 Vaclav Vesely
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #include <boost/spirit/include/classic_core.hpp>
  9. #include <boost/spirit/include/classic_distinct.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. using namespace boost;
  12. using namespace BOOST_SPIRIT_CLASSIC_NS;
  13. typedef
  14. scanner<char const*, scanner_policies<skipper_iteration_policy<> > >
  15. scanner_t;
  16. typedef
  17. rule<scanner_t>
  18. rule_t;
  19. void distinct_parser_test()
  20. {
  21. // distinct_parser()
  22. {
  23. distinct_parser<> distinct_p;
  24. // operator()(CharT const* str) const
  25. rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
  26. BOOST_TEST(parse("keyword123", r, space_p).full);
  27. }
  28. // distinct_parser(CharT const* letters)
  29. {
  30. distinct_parser<> distinct_p("0-9a-zA-Z_");
  31. // operator()(CharT const* str) const
  32. rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
  33. BOOST_TEST(parse("keyword 123", r, space_p).full);
  34. BOOST_TEST(parse("keyword-123", r, space_p).full);
  35. BOOST_TEST(!parse("keyword123", r, space_p).hit);
  36. }
  37. // distinct_parser(parser<TailT> const & tail_)
  38. {
  39. distinct_parser<
  40. char,
  41. alternative<
  42. alnum_parser,
  43. sequence<
  44. chlit<>,
  45. negated_char_parser<chlit<> >
  46. >
  47. >
  48. >
  49. distinct_p(alnum_p | ('-' >> ~ch_p('-')));
  50. // operator()(CharT const* str) const
  51. rule_t r = distinct_p("keyword") >> !str_p("--") >> int_p;
  52. BOOST_TEST(parse("keyword 123", r, space_p).full);
  53. BOOST_TEST(parse("keyword--123", r, space_p).full);
  54. BOOST_TEST(!parse("keyword-123", r, space_p).hit);
  55. }
  56. }
  57. void distinct_directive_test()
  58. {
  59. // distinct_directive()
  60. {
  61. distinct_directive<> distinct_d;
  62. // operator[](CharT const* str) const
  63. {
  64. rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
  65. BOOST_TEST(parse("keyword123", r, space_p).full);
  66. }
  67. // operator[](parser<ParserT> const &subject) const
  68. {
  69. rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
  70. BOOST_TEST(parse("keyword123", r, space_p).full);
  71. }
  72. }
  73. // distinct_directive(CharT const* letters)
  74. {
  75. distinct_directive<> distinct_d("0-9a-zA-Z_");
  76. // operator[](CharT const* str) const
  77. {
  78. rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
  79. BOOST_TEST(parse("keyword 123", r, space_p).full);
  80. BOOST_TEST(parse("keyword-123", r, space_p).full);
  81. BOOST_TEST(!parse("keyword123", r, space_p).hit);
  82. }
  83. // operator[](parser<ParserT> const &subject) const
  84. {
  85. rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
  86. BOOST_TEST(parse("keyword 123", r, space_p).full);
  87. BOOST_TEST(parse("keyword-123", r, space_p).full);
  88. BOOST_TEST(!parse("keyword123", r, space_p).hit);
  89. }
  90. }
  91. // distinct_directive(parser<TailT> const & tail_)
  92. {
  93. distinct_directive<
  94. char,
  95. alternative<
  96. alnum_parser,
  97. sequence<
  98. chlit<>,
  99. negated_char_parser<chlit<> >
  100. >
  101. >
  102. >
  103. distinct_d(alnum_p | ('-' >> ~ch_p('-')));
  104. // operator[](CharT const* str) const
  105. {
  106. rule_t r = distinct_d["keyword"] >> !str_p("--") >> int_p;
  107. BOOST_TEST(parse("keyword 123", r, space_p).full);
  108. BOOST_TEST(parse("keyword--123", r, space_p).full);
  109. BOOST_TEST(!parse("keyword-123", r, space_p).hit);
  110. }
  111. // operator[](parser<ParserT> const &subject) const
  112. {
  113. rule_t r = distinct_d[str_p("keyword")] >> !str_p("--") >> int_p;
  114. BOOST_TEST(parse("keyword 123", r, space_p).full);
  115. BOOST_TEST(parse("keyword--123", r, space_p).full);
  116. BOOST_TEST(!parse("keyword-123", r, space_p).hit);
  117. }
  118. }
  119. }
  120. void dynamic_distinct_parser_test()
  121. {
  122. // dynamic_distinct_parser()
  123. {
  124. dynamic_distinct_parser<scanner_t> distinct_p;
  125. // operator()(CharT const* str) const
  126. rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
  127. BOOST_TEST(parse("keyword123", r, space_p).full);
  128. }
  129. // dynamic_distinct_parser(CharT const* letters)
  130. {
  131. dynamic_distinct_parser<scanner_t> distinct_p("0-9a-zA-Z_");
  132. // operator()(CharT const* str) const
  133. rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
  134. BOOST_TEST(parse("keyword 123", r, space_p).full);
  135. BOOST_TEST(parse("keyword-123", r, space_p).full);
  136. BOOST_TEST(!parse("keyword123", r, space_p).hit);
  137. }
  138. // dynamic_distinct_parser(parser<TailT> const & tail_)
  139. {
  140. dynamic_distinct_parser<scanner_t>
  141. distinct_p(alnum_p | ('-' >> ~ch_p('-')));
  142. // operator()(CharT const* str) const
  143. rule_t r = distinct_p("keyword") >> !str_p("--") >> int_p;
  144. BOOST_TEST(parse("keyword 123", r, space_p).full);
  145. BOOST_TEST(parse("keyword--123", r, space_p).full);
  146. BOOST_TEST(!parse("keyword-123", r, space_p).hit);
  147. }
  148. }
  149. void dynamic_distinct_directive_test()
  150. {
  151. // dynamic_distinct_directive()
  152. {
  153. dynamic_distinct_directive<scanner_t> distinct_d;
  154. // operator[](CharT const* str) const
  155. {
  156. rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
  157. BOOST_TEST(parse("keyword123", r, space_p).full);
  158. }
  159. // operator[](parser<ParserT> const &subject) const
  160. {
  161. rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
  162. BOOST_TEST(parse("keyword123", r, space_p).full);
  163. }
  164. }
  165. // dynamic_distinct_directive(CharT const* letters)
  166. {
  167. dynamic_distinct_directive<scanner_t> distinct_d("0-9a-zA-Z_");
  168. // operator[](CharT const* str) const
  169. {
  170. rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
  171. BOOST_TEST(parse("keyword 123", r, space_p).full);
  172. BOOST_TEST(parse("keyword-123", r, space_p).full);
  173. BOOST_TEST(!parse("keyword123", r, space_p).hit);
  174. }
  175. // operator[](parser<ParserT> const &subject) const
  176. {
  177. rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
  178. BOOST_TEST(parse("keyword 123", r, space_p).full);
  179. BOOST_TEST(parse("keyword-123", r, space_p).full);
  180. BOOST_TEST(!parse("keyword123", r, space_p).hit);
  181. }
  182. }
  183. // dynamic_distinct_directive(parser<TailT> const & tail_)
  184. {
  185. dynamic_distinct_directive<scanner_t>
  186. distinct_d(alnum_p | ('-' >> ~ch_p('-')));
  187. // operator[](CharT const* str) const
  188. {
  189. rule_t r = distinct_d["keyword"] >> !str_p("--") >> int_p;
  190. BOOST_TEST(parse("keyword 123", r, space_p).full);
  191. BOOST_TEST(parse("keyword--123", r, space_p).full);
  192. BOOST_TEST(!parse("keyword-123", r, space_p).hit);
  193. }
  194. // operator[](parser<ParserT> const &subject) const
  195. {
  196. rule_t r = distinct_d[str_p("keyword")] >> !str_p("--") >> int_p;
  197. BOOST_TEST(parse("keyword 123", r, space_p).full);
  198. BOOST_TEST(parse("keyword--123", r, space_p).full);
  199. BOOST_TEST(!parse("keyword-123", r, space_p).hit);
  200. }
  201. }
  202. }
  203. int
  204. main()
  205. {
  206. distinct_parser_test();
  207. distinct_directive_test();
  208. dynamic_distinct_parser_test();
  209. dynamic_distinct_directive_test();
  210. return boost::report_errors();
  211. }