Strings.matchAll().phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Strings::matchAll()
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Strings;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. Assert::same([], Strings::matchAll('hello world!', '#([E-L])+#'));
  10. Assert::same([
  11. ['hell', 'l'],
  12. ['l', 'l'],
  13. ], Strings::matchAll('hello world!', '#([e-l])+#'));
  14. Assert::same([
  15. ['hell'],
  16. ['l'],
  17. ], Strings::matchAll('hello world!', '#[e-l]+#'));
  18. Assert::same([
  19. [['lu', 2], ['l', 2], ['u', 3]],
  20. [['ou', 6], ['o', 6], ['u', 7]],
  21. [['k', 10], ['k', 10], ['', 11]],
  22. [['k', 14], ['k', 14], ['', 15]],
  23. ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE));
  24. Assert::same([
  25. [['lu', 2], ['l', 2], ['u', 3]],
  26. [['ou', 6], ['o', 6], ['u', 7]],
  27. [['k', 10], ['k', 10], ['', 11]],
  28. [['k', 14], ['k', 14], ['', 15]],
  29. ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true));
  30. Assert::same([
  31. [['lu', 2], ['ou', 6], ['k', 10], ['k', 14]],
  32. [['l', 2], ['o', 6], ['k', 10], ['k', 14]],
  33. [['u', 3], ['u', 7], ['', 11], ['', 15]],
  34. ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER));
  35. Assert::same([
  36. [['lu', 1], ['l', 1], ['u', 2]],
  37. [['ou', 4], ['o', 4], ['u', 5]],
  38. [['k', 7], ['k', 7], ['', 8]],
  39. [['k', 10], ['k', 10], ['', 11]],
  40. ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true, utf8: true));
  41. Assert::same([
  42. [['lu', 2], ['ou', 6], ['k', 10], ['k', 14]],
  43. [['l', 2], ['o', 6], ['k', 10], ['k', 14]],
  44. [['u', 3], ['u', 7], ['', 11], ['', 15]],
  45. ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true, patternOrder: true));
  46. Assert::same([
  47. [['lu', 1], ['ou', 4], ['k', 7], ['k', 10]],
  48. [['l', 1], ['o', 4], ['k', 7], ['k', 10]],
  49. [['u', 2], ['u', 5], ['', 8], ['', 11]],
  50. ], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true, patternOrder: true, utf8: true));
  51. Assert::same([['l'], ['k'], ['k']], Strings::matchAll('žluťoučký kůň', '#[e-l]+#u', offset: 2));
  52. Assert::same([['k'], ['k']], Strings::matchAll('žluťoučký kůň', '#[e-l]+#u', offset: 2, utf8: true));
  53. Assert::same([['žluťoučký'], ['kůň']], Strings::matchAll('žluťoučký kůň', '#\w+#', utf8: true)); // without modifier
  54. Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', PREG_PATTERN_ORDER, 2));
  55. Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', offset: 2, patternOrder: true));
  56. Assert::same([['e', null]], Strings::matchAll('hello world!', '#e(x)*#', unmatchedAsNull: true));
  57. Assert::same([], Strings::matchAll('hello world!', '', offset: 50));