Add support for dynamic tests

This commit is contained in:
Fabien Potencier
2018-07-31 09:55:27 +02:00
parent 7a3d825969
commit 4beb7bfb0b
9 changed files with 48 additions and 10 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ before_script:
script: | script: |
if [[ $TRAVIS_PHP_VERSION = 7.* || $TRAVIS_PHP_VERSION = nightly ]]; then if [[ $TRAVIS_PHP_VERSION = 7.* || $TRAVIS_PHP_VERSION = nightly ]]; then
SYMFONY_PHPUNIT_VERSION=6.1 ./vendor/bin/simple-phpunit SYMFONY_PHPUNIT_VERSION=6.5 ./vendor/bin/simple-phpunit
else else
./vendor/bin/simple-phpunit ./vendor/bin/simple-phpunit
fi fi
+2 -1
View File
@@ -1,5 +1,6 @@
* 1.35.5 (2018-XX-XX) * 1.36.0 (2018-XX-XX)
* added support for dynamically named tests
* fixed filesystem loader throwing an exception instead of returning false * fixed filesystem loader throwing an exception instead of returning false
* 1.35.4 (2018-07-13) * 1.35.4 (2018-07-13)
+1 -1
View File
@@ -45,7 +45,7 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "1.35-dev" "dev-master": "1.36-dev"
} }
} }
} }
+5 -2
View File
@@ -371,9 +371,12 @@ The ``node`` sub-node will contain an expression of ``my_value``. Node-based
tests also have access to the ``arguments`` node. This node will contain the tests also have access to the ``arguments`` node. This node will contain the
various other arguments that have been provided to your test. various other arguments that have been provided to your test.
.. versionadded:: 1.36
Dynamic tests support was added in Twig 1.36.
If you want to pass a variable number of positional or named arguments to the If you want to pass a variable number of positional or named arguments to the
test, set the ``is_variadic`` option to ``true``. Tests also support dynamic test, set the ``is_variadic`` option to ``true``. Tests support dynamic
name feature as filters and functions. names (see dynamic filters and functions for the syntax).
Tags Tags
---- ----
+1 -1
View File
@@ -15,7 +15,7 @@
#ifndef PHP_TWIG_H #ifndef PHP_TWIG_H
#define PHP_TWIG_H #define PHP_TWIG_H
#define PHP_TWIG_VERSION "1.35.5-DEV" #define PHP_TWIG_VERSION "1.36.0-DEV"
#include "php.h" #include "php.h"
+17 -4
View File
@@ -16,11 +16,11 @@
*/ */
class Twig_Environment class Twig_Environment
{ {
const VERSION = '1.35.5'; const VERSION = '1.36.0';
const VERSION_ID = 13505; const VERSION_ID = 13600;
const MAJOR_VERSION = 1; const MAJOR_VERSION = 1;
const MINOR_VERSION = 35; const MINOR_VERSION = 36;
const RELEASE_VERSION = 5; const RELEASE_VERSION = 0;
const EXTRA_VERSION = 'DEV'; const EXTRA_VERSION = 'DEV';
protected $charset; protected $charset;
@@ -1240,6 +1240,19 @@ class Twig_Environment
return $this->tests[$name]; return $this->tests[$name];
} }
foreach ($this->tests as $pattern => $test) {
$pattern = str_replace('\\*', '(.*?)', preg_quote($pattern, '#'), $count);
if ($count) {
if (preg_match('#^'.$pattern.'$#', $name, $matches)) {
array_shift($matches);
$test->setArguments($matches);
return $test;
}
}
}
return false; return false;
} }
+3
View File
@@ -28,6 +28,9 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression_Call
$this->setAttribute('name', $name); $this->setAttribute('name', $name);
$this->setAttribute('type', 'test'); $this->setAttribute('type', 'test');
$this->setAttribute('thing', $test); $this->setAttribute('thing', $test);
if ($test instanceof Twig_SimpleTest) {
$this->setAttribute('arguments', $test->getArguments());
}
if ($test instanceof Twig_TestCallableInterface || $test instanceof Twig_SimpleTest) { if ($test instanceof Twig_TestCallableInterface || $test instanceof Twig_SimpleTest) {
$this->setAttribute('callable', $test->getCallable()); $this->setAttribute('callable', $test->getCallable());
} }
+12
View File
@@ -22,6 +22,8 @@ class Twig_SimpleTest
protected $callable; protected $callable;
protected $options; protected $options;
private $arguments = array();
public function __construct($name, $callable, array $options = array()) public function __construct($name, $callable, array $options = array())
{ {
$this->name = $name; $this->name = $name;
@@ -68,6 +70,16 @@ class Twig_SimpleTest
{ {
return $this->options['alternative']; return $this->options['alternative'];
} }
public function setArguments($arguments)
{
$this->arguments = $arguments;
}
public function getArguments()
{
return $this->arguments;
}
} }
class_alias('Twig_SimpleTest', 'Twig\TwigTest', false); class_alias('Twig_SimpleTest', 'Twig\TwigTest', false);
+6
View File
@@ -168,6 +168,7 @@ class TwigTestExtension extends Twig_Extension
{ {
return array( return array(
new Twig_SimpleTest('multi word', array($this, 'is_multi_word')), new Twig_SimpleTest('multi word', array($this, 'is_multi_word')),
new Twig_SimpleTest('test_*', array($this, 'dynamic_test')),
); );
} }
@@ -209,6 +210,11 @@ class TwigTestExtension extends Twig_Extension
return $foo.'/'.$bar.'/'.$item; return $foo.'/'.$bar.'/'.$item;
} }
public function dynamic_test($element, $item)
{
return $element === $item;
}
public function escape_something($value) public function escape_something($value)
{ {
return strtoupper($value); return strtoupper($value);