inline_ut.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * inline_ut.cpp --
  3. *
  4. * Copyright (c) 2007-2010, Dmitry Prokoptsev <dprokoptsev@gmail.com>,
  5. * Alexander Gololobov <agololobov@gmail.com>
  6. *
  7. * This file is part of Pire, the Perl Incompatible
  8. * Regular Expressions library.
  9. *
  10. * Pire is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Pire is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser Public License for more details.
  19. * You should have received a copy of the GNU Lesser Public License
  20. * along with Pire. If not, see <http://www.gnu.org/licenses>.
  21. */
  22. #include <stub/hacks.h>
  23. #include "stub/cppunit.h"
  24. #include <pire.h>
  25. #include <iostream>
  26. #include <string.h>
  27. Y_UNIT_TEST_SUITE(TestPireInline) {
  28. template<class Scanner>
  29. typename Scanner::State RunRegexp(const Scanner& scanner, const char* str)
  30. {
  31. typename Scanner::State state;
  32. scanner.Initialize(state);
  33. Step(scanner, state, Pire::BeginMark);
  34. Run(scanner, state, str, str + strlen(str));
  35. Step(scanner, state, Pire::EndMark);
  36. return state;
  37. }
  38. template<class Scanner>
  39. bool Matches(const Scanner& scanner, const char* str)
  40. {
  41. return scanner.Final(RunRegexp(scanner, str));
  42. }
  43. template<class Scanner>
  44. bool Matches2(const Scanner& scanner, const char* str)
  45. {
  46. return Pire::Matches(scanner, str);
  47. }
  48. bool ParticularMatch(Pire::Scanner& sc, Pire::Scanner::State st, size_t idx)
  49. {
  50. std::pair<const size_t*, const size_t*> p = sc.AcceptedRegexps(st);
  51. return std::distance(p.first, p.second) == 1 && *p.first == idx;
  52. }
  53. Y_UNIT_TEST(Inline)
  54. {
  55. Pire::Scanner scanner = PIRE_REGEXP("http://([a-z0-9]+\\.)+[a-z]{2,4}/?", "is");
  56. UNIT_ASSERT(Matches(scanner, "http://domain.vasya.ru/"));
  57. UNIT_ASSERT(Matches(scanner, "prefix http://domain.vasya.ru/"));
  58. UNIT_ASSERT(!Matches(scanner, "http://127.0.0.1/"));
  59. Pire::Scanner scanner2 = PIRE_REGEXP("http://([a-z0-9]+\\.)+[a-z]{2,4}/?", "i");
  60. UNIT_ASSERT(Matches2(scanner2, "http://domain.vasya.ru/"));
  61. UNIT_ASSERT(!Matches2(scanner2, "prefix http://domain.vasya.ru/"));
  62. UNIT_ASSERT(!Matches2(scanner2, "http://127.0.0.1/"));
  63. }
  64. Y_UNIT_TEST(InlineGlue)
  65. {
  66. // Check whether pire_inline handles comments as well:
  67. /* - a C-style comment outside a regexp; */
  68. Pire::Scanner sc = PIRE_REGEXP(
  69. "foo", "", /* - a C-style comment inside a regexp; */
  70. "bar", "", // - a C++-style comment inside a regexp;
  71. "baz", ""
  72. );
  73. // - a C++-style comment outside a regexp.
  74. UNIT_ASSERT(ParticularMatch(sc, Pire::Runner(sc).Run("foo").State(), 0));
  75. UNIT_ASSERT(ParticularMatch(sc, Pire::Runner(sc).Run("bar").State(), 1));
  76. UNIT_ASSERT(ParticularMatch(sc, Pire::Runner(sc).Run("baz").State(), 2));
  77. UNIT_ASSERT(!Matches2(sc, "xxx"));
  78. }
  79. }