Strings.before().phpt 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Strings::before()
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Strings;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. test('', function () {
  10. $foo = '0123456789a123456789b123456789c';
  11. Assert::same('', Strings::before($foo, '0', 1));
  12. Assert::same('012345678', Strings::before($foo, '9', 1));
  13. Assert::same('0123456', Strings::before($foo, '789', 1));
  14. Assert::same('', Strings::before($foo, '', 1));
  15. Assert::same('0123456789a123456789b123456789c', Strings::before($foo, '', -1));
  16. Assert::same('0123456789a123456789b123456789', Strings::before($foo, 'c', -1));
  17. Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', -1));
  18. Assert::same('0123456789a123456789b12345678', Strings::before($foo, '9', 3));
  19. Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', 3));
  20. Assert::same('012345678', Strings::before($foo, '9', -3));
  21. Assert::same('0123456', Strings::before($foo, '789', -3));
  22. Assert::null(Strings::before($foo, '9', 0));
  23. Assert::null(Strings::before($foo, 'not-in-string'));
  24. Assert::null(Strings::before($foo, 'b', -2));
  25. Assert::null(Strings::before($foo, 'b', 2));
  26. });
  27. test('', function () {
  28. $foo = "I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n"; // Iñtërnâtiônàlizætiøn
  29. Assert::same("I\u{F1}t\u{EB}rn\u{E2}", Strings::before($foo, 'ti', 1));
  30. Assert::same("I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}", Strings::before($foo, 'ti', 2));
  31. });