Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 27, 2022
1 parent 61672c4 commit e3422e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Extension/CoreExtension.php
Expand Up @@ -310,7 +310,6 @@ public function getOperators(): array
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Extension\CoreExtension;
use Twig\Extension\SandboxExtension;
use Twig\Markup;
Expand Down Expand Up @@ -1020,18 +1019,18 @@ function twig_compare($a, $b)
return $a <=> $b;
}

/**
* @param string $pattern
* @param string $subject
*
* @return int
*
* @throws SyntaxError When an invalid pattern is used
*/
/**
* @param string $pattern
* @param string $subject
*
* @return int
*
* @throws RuntimeError When an invalid pattern is used
*/
function twig_matches(string $regexp, string $str)
{
set_error_handler(function ($t, $m) use ($regexp) {
throw new SyntaxError(sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12));
throw new RuntimeError(sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12));
});
try {
return preg_match($regexp, $str);
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/expressions/matches_error.test
@@ -0,0 +1,8 @@
--TEST--
Twig supports the "matches" operator with a great error message
--TEMPLATE--
{{ 'foo' matches '/o' }}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Regexp "/o" passed to "matches" is not valid: No ending delimiter '/' found in "index.twig" at line 2

0 comments on commit e3422e5

Please sign in to comment.