mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-01 13:07:22 +00:00
Make round brackets optional for one argument tests like sameas
This commit is contained in:
committed by
Fabien Potencier
parent
c6927de9e7
commit
1ee72d9e15
@@ -711,6 +711,8 @@ class ExpressionParser
|
||||
$arguments = null;
|
||||
if ($stream->test(Token::PUNCTUATION_TYPE, '(')) {
|
||||
$arguments = $this->parseArguments(true);
|
||||
} elseif ($test->hasOneMandatoryArgument()) {
|
||||
$arguments = new Node([0 => $this->parsePrimaryExpression()]);
|
||||
}
|
||||
|
||||
return new $class($node, $name, $arguments, $this->parser->getCurrentToken()->getLine());
|
||||
|
||||
@@ -244,11 +244,11 @@ class CoreExtension extends AbstractExtension
|
||||
new TwigTest('odd', null, ['node_class' => '\Twig\Node\Expression\Test\OddTest']),
|
||||
new TwigTest('defined', null, ['node_class' => '\Twig\Node\Expression\Test\DefinedTest']),
|
||||
new TwigTest('sameas', null, ['node_class' => '\Twig\Node\Expression\Test\SameasTest', 'deprecated' => '1.21', 'alternative' => 'same as']),
|
||||
new TwigTest('same as', null, ['node_class' => '\Twig\Node\Expression\Test\SameasTest']),
|
||||
new TwigTest('same as', null, ['node_class' => '\Twig\Node\Expression\Test\SameasTest', 'one_mandatory_argument' => true]),
|
||||
new TwigTest('none', null, ['node_class' => '\Twig\Node\Expression\Test\NullTest']),
|
||||
new TwigTest('null', null, ['node_class' => '\Twig\Node\Expression\Test\NullTest']),
|
||||
new TwigTest('divisibleby', null, ['node_class' => '\Twig\Node\Expression\Test\DivisiblebyTest', 'deprecated' => '1.21', 'alternative' => 'divisible by']),
|
||||
new TwigTest('divisible by', null, ['node_class' => '\Twig\Node\Expression\Test\DivisiblebyTest']),
|
||||
new TwigTest('divisible by', null, ['node_class' => '\Twig\Node\Expression\Test\DivisiblebyTest', 'one_mandatory_argument' => true]),
|
||||
new TwigTest('constant', null, ['node_class' => '\Twig\Node\Expression\Test\ConstantTest']),
|
||||
new TwigTest('empty', 'twig_test_empty'),
|
||||
new TwigTest('iterable', 'twig_test_iterable'),
|
||||
|
||||
@@ -35,6 +35,7 @@ class TwigTest
|
||||
'node_class' => '\Twig\Node\Expression\TestExpression',
|
||||
'deprecated' => false,
|
||||
'alternative' => null,
|
||||
'one_mandatory_argument' => false,
|
||||
], $options);
|
||||
}
|
||||
|
||||
@@ -82,6 +83,11 @@ class TwigTest
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
|
||||
public function hasOneMandatoryArgument(): bool
|
||||
{
|
||||
return (bool) $this->options['one_mandatory_argument'];
|
||||
}
|
||||
}
|
||||
|
||||
class_alias('Twig\TwigTest', 'Twig_SimpleTest');
|
||||
|
||||
@@ -179,8 +179,11 @@ class SandboxTest extends \PHPUnit\Framework\TestCase
|
||||
'is_defined' => ['{{ obj.anotherFooObject is defined }}', '1'],
|
||||
'is_null' => ['{{ obj is null }}', ''],
|
||||
'is_sameas' => ['{{ obj is same as(obj) }}', '1'],
|
||||
'is_sameas_no_brackets' => ['{{ obj is same as obj }}', '1'],
|
||||
'is_sameas_from_array' => ['{{ arr.obj is same as(arr.obj) }}', '1'],
|
||||
'is_sameas_from_array_no_brackets' => ['{{ arr.obj is same as arr.obj }}', '1'],
|
||||
'is_sameas_from_another_method' => ['{{ obj.anotherFooObject is same as(obj.anotherFooObject) }}', ''],
|
||||
'is_sameas_from_another_method_no_brackets' => ['{{ obj.anotherFooObject is same as obj.anotherFooObject }}', ''],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
Twig supports the "divisible by" operator
|
||||
--TEMPLATE--
|
||||
{{ 8 is divisible by(2) ? 'OK' }}
|
||||
{{ 8 is divisible by 2 ? 'OK' }}
|
||||
{{ 8 is not divisible by(3) ? 'OK' }}
|
||||
{{ 8 is divisible by (2) ? 'OK' }}
|
||||
{{ 8 is divisible by 2 ? 'OK' }}
|
||||
{{ 8 is not
|
||||
divisible
|
||||
by
|
||||
@@ -15,3 +17,5 @@ OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
Twig supports the "same as" operator
|
||||
--TEMPLATE--
|
||||
{{ 1 is same as(1) ? 'OK' }}
|
||||
{{ 1 is same as 1 ? 'OK' }}
|
||||
{{ 1 is not same as(true) ? 'OK' }}
|
||||
{{ 1 is not same as true ? 'OK' }}
|
||||
{{ 1 is same as(1) ? 'OK' }}
|
||||
{{ 1 is not same as(true) ? 'OK' }}
|
||||
{{ 1 is same as (1) ? 'OK' }}
|
||||
{{ 1 is same as 1 ? 'OK' }}
|
||||
{{ 1 is not same as '1' ? 'OK' }}
|
||||
{{ 1 is not
|
||||
same
|
||||
as
|
||||
@@ -19,3 +23,7 @@ OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
|
||||
Reference in New Issue
Block a user