Make round brackets optional for one argument tests like sameas

This commit is contained in:
Andreas
2020-02-04 11:25:54 +01:00
committed by Fabien Potencier
parent c6927de9e7
commit 1ee72d9e15
6 changed files with 25 additions and 2 deletions
+2
View File
@@ -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());
+2 -2
View File
@@ -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'),
+6
View File
@@ -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');
+3
View File
@@ -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
+8
View File
@@ -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