* 0.9.6-DEV
+ * unit tests are now powered by PHPUnit
* added support for gettext via the `i18n` extension
* fixed twig_capitalize_string_filter() and fixed twig_length_filter() when used with UTF-8 values
* added a more useful exception if an if tag is not closed properly
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ colors="false"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ processIsolation="false"
+ stopOnFailure="false"
+ syntaxCheck="false"
+>
+ <testsuites>
+ <testsuite name="Twig Test Suite">
+ <directory>./test/Twig/</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <whitelist>
+ <directory suffix=".php">./lib/Twig/</directory>
+ </whitelist>
+ </filter>
+</phpunit>
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require_once dirname(__FILE__).'/bootstrap.php';
+
+class Twig_Tests_AutoloaderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testAutoload()
+ {
+ $this->assertFalse(class_exists('FooBarFoo'), '->autoload() does not try to load classes that does not begin with Twig');
+
+ $autoloader = new Twig_Autoloader();
+ $this->assertTrue($autoloader->autoload('Twig_Parser'), '->autoload() returns true if it is able to load a class');
+ $this->assertFalse($autoloader->autoload('Foo'), '->autoload() returns false if it is not able to load a class');
+ }
+}
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require_once dirname(__FILE__).'/../bootstrap.php';
+
+class Twig_Tests_Extension_SandboxTest extends \PHPUnit_Framework_TestCase
+{
+ static protected $params, $templates;
+
+ static public function setUpBeforeClass()
+ {
+ self::$params = array(
+ 'name' => 'Fabien',
+ 'obj' => new Object(),
+ );
+
+ self::$templates = array(
+ '1_basic1' => '{{ obj.foo }}',
+ '1_basic2' => '{{ name|upper }}',
+ '1_basic3' => '{% if name %}foo{% endif %}',
+ '1_basic4' => '{{ obj.bar }}',
+ '1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
+ );
+ }
+
+ public function testSandboxGloballySet()
+ {
+ $twig = $this->getEnvironment(false, self::$templates);
+ $this->assertEquals('FOO', $twig->loadTemplate('1_basic')->render(self::$params), 'Sandbox does nothing if it is disabled globally');
+
+ $twig = $this->getEnvironment(true, self::$templates);
+ try
+ {
+ $twig->loadTemplate('1_basic1')->render(self::$params);
+ $this->fail('Sandbox throws a SecurityError exception if an unallowed method is called');
+ }
+ catch (Twig_Sandbox_SecurityError $e)
+ {
+ }
+
+ $twig = $this->getEnvironment(true, self::$templates);
+ try
+ {
+ $twig->loadTemplate('1_basic2')->render(self::$params);
+ $this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
+ }
+ catch (Twig_Sandbox_SecurityError $e)
+ {
+ }
+
+ $twig = $this->getEnvironment(true, self::$templates);
+ try
+ {
+ $twig->loadTemplate('1_basic3')->render(self::$params);
+ $this->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template');
+ }
+ catch (Twig_Sandbox_SecurityError $e)
+ {
+ }
+
+ $twig = $this->getEnvironment(true, self::$templates);
+ try
+ {
+ $twig->loadTemplate('1_basic4')->render(self::$params);
+ $this->fail('Sandbox throws a SecurityError exception if an unallowed property is called in the template');
+ }
+ catch (Twig_Sandbox_SecurityError $e)
+ {
+ }
+
+ $twig = $this->getEnvironment(true, self::$templates, array(), array(), array('Object' => 'foo'));
+ $this->assertEquals('foo', $twig->loadTemplate('1_basic1')->render(self::$params), 'Sandbox allow some methods');
+
+ $twig = $this->getEnvironment(true, self::$templates, array(), array('upper'));
+ $this->assertEquals('FABIEN', $twig->loadTemplate('1_basic2')->render(self::$params), 'Sandbox allow some filters');
+
+ $twig = $this->getEnvironment(true, self::$templates, array('if'));
+ $this->assertEquals('foo', $twig->loadTemplate('1_basic3')->render(self::$params), 'Sandbox allow some tags');
+
+ $twig = $this->getEnvironment(true, self::$templates, array(), array(), array(), array('Object' => 'bar'));
+ $this->assertEquals('bar', $twig->loadTemplate('1_basic4')->render(self::$params), 'Sandbox allow some properties');
+ }
+
+ public function testSandboxLocallySetForAnInclude()
+ {
+ self::$templates = array(
+ '2_basic' => '{{ obj.foo }}{% include "2_included" %}{{ obj.foo }}',
+ '2_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
+ );
+
+ $twig = $this->getEnvironment(false, self::$templates);
+ $this->assertEquals('fooFOOfoo', $twig->loadTemplate('2_basic')->render(self::$params), 'Sandbox does nothing if disabled globally and sandboxed not used for the include');
+
+ self::$templates = array(
+ '3_basic' => '{{ obj.foo }}{% include "3_included" sandboxed %}{{ obj.foo }}',
+ '3_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
+ );
+
+ $twig = $this->getEnvironment(false, self::$templates);
+ $twig = $this->getEnvironment(true, self::$templates);
+ try
+ {
+ $twig->loadTemplate('3_basic')->render(self::$params);
+ $this->fail('Sandbox throws a SecurityError exception when the included file is sandboxed');
+ }
+ catch (Twig_Sandbox_SecurityError $e)
+ {
+ }
+ }
+
+ protected function getEnvironment($sandboxed, $templates, $tags = array(), $filters = array(), $methods = array(), $properties = array())
+ {
+ $loader = new Twig_Loader_Array($templates);
+ $twig = new Twig_Environment($loader, array('trim_blocks' => true, 'debug' => true));
+ $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties);
+ $twig->addExtension(new Twig_Extension_Sandbox($policy, $sandboxed));
+
+ return $twig;
+ }
+}
+
+class Object
+{
+ public $bar = 'bar';
+
+ public function foo()
+ {
+ return 'foo';
+ }
+}
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require_once dirname(__FILE__).'/bootstrap.php';
+
+class Twig_Tests_TokenStreamTest extends \PHPUnit_Framework_TestCase
+{
+ static protected $tokens;
+
+ static public function setupBeforeClass()
+ {
+ self::$tokens = array(
+ new Twig_Token(Twig_Token::TEXT_TYPE, 1, 0),
+ new Twig_Token(Twig_Token::TEXT_TYPE, 2, 0),
+ new Twig_Token(Twig_Token::TEXT_TYPE, 3, 0),
+ new Twig_Token(Twig_Token::TEXT_TYPE, 4, 0),
+ new Twig_Token(Twig_Token::TEXT_TYPE, 5, 0),
+ new Twig_Token(Twig_Token::TEXT_TYPE, 6, 0),
+ new Twig_Token(Twig_Token::TEXT_TYPE, 7, 0),
+ new Twig_Token(Twig_Token::EOF_TYPE, 0, 0),
+ );
+ }
+
+ public function testNext()
+ {
+ $stream = new Twig_TokenStream(self::$tokens, '', false);
+ $repr = array();
+ while (!$stream->isEOF())
+ {
+ $token = $stream->next();
+
+ $repr[] = $token->getValue();
+ }
+ $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->next() returns the next token in the stream');
+ }
+
+ public function testLook()
+ {
+ $stream = new Twig_TokenStream(self::$tokens, '', false);
+ $this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
+ $repr = array();
+ while (!$stream->isEOF())
+ {
+ $token = $stream->next();
+
+ $repr[] = $token->getValue();
+ }
+ $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->look() pushes the token to the stack');
+
+ $stream = new Twig_TokenStream(self::$tokens, '', false);
+ $this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
+ $this->assertEquals(3, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
+ $this->assertEquals(4, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
+ $this->assertEquals(5, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
+ $repr = array();
+ while (!$stream->isEOF())
+ {
+ $token = $stream->next();
+
+ $repr[] = $token->getValue();
+ }
+ $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->look() pushes the token to the stack');
+ }
+
+ public function testRewind()
+ {
+ $stream = new Twig_TokenStream(self::$tokens, '', false);
+ $this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
+ $this->assertEquals(3, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
+ $this->assertEquals(4, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
+ $this->assertEquals(5, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
+ $stream->rewind();
+ $repr = array();
+ while (!$stream->isEOF())
+ {
+ $token = $stream->next(false);
+
+ $repr[] = $token->getValue();
+ }
+ $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->rewind() pushes all pushed tokens to the token array');
+ }
+}
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require_once dirname(__FILE__).'/../../../lib/Twig/Autoloader.php';
+Twig_Autoloader::register();
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require_once dirname(__FILE__).'/bootstrap.php';
+
+class Twig_Tests_IntegrationTest extends \PHPUnit_Framework_TestCase
+{
+ static protected $fixturesDir;
+
+ static public function setUpBeforeClass()
+ {
+ self::$fixturesDir = realpath(dirname(__FILE__).'/../../fixtures/');
+ }
+
+ public function testIntegration()
+ {
+ foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::$fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
+ {
+ if (!preg_match('/\.test$/', $file))
+ {
+ continue;
+ }
+
+ $test = file_get_contents($file->getRealpath());
+
+ if (!preg_match('/--TEST--\s*(.*?)\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match))
+ {
+ throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace(self::$fixturesDir.'/', '', $file)));
+ }
+
+ $message = $match[1];
+ $templates = array();
+ preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $match[2], $matches, PREG_SET_ORDER);
+ foreach ($matches as $match)
+ {
+ $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
+ }
+
+ $loader = new Twig_Loader_Array($templates);
+ $twig = new Twig_Environment($loader, array('trim_blocks' => true, 'cache' => false));
+ $twig->addExtension(new Twig_Extension_Escaper());
+ $twig->addExtension(new TestExtension());
+
+ $template = $twig->loadTemplate('index.twig');
+
+ preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $matches, PREG_SET_ORDER);
+ foreach ($matches as $match)
+ {
+ $output = trim($template->render(eval($match[1].';')), "\n ");
+ $expected = trim($match[2], "\n ");
+
+ $this->assertEquals($expected, $output, $message);
+ if ($output != $expected)
+ {
+ echo 'Compiled template that failed:';
+
+ foreach (array_keys($templates) as $name)
+ {
+ $source = $loader->getSource($name);
+ echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
+ }
+ }
+ }
+ }
+ }
+}
+
+class Foo
+{
+ public function bar($param1 = null, $param2 = null)
+ {
+ return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
+ }
+
+ public function getFoo()
+ {
+ return 'foo';
+ }
+
+ public function getSelf()
+ {
+ return $this;
+ }
+}
+
+class TestExtension extends Twig_Extension
+{
+ public function getFilters()
+ {
+ return array('nl2br' => new Twig_Filter_Method($this, 'nl2br'));
+ }
+
+ public function nl2br($value, $sep = '<br />')
+ {
+ return str_replace("\n", $sep."\n", $value);
+ }
+
+ public function getName()
+ {
+ return 'test';
+ }
+}
+++ /dev/null
-<?php
-
-/*
- * This file is part of Twig.
- *
- * (c) Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-require_once(dirname(__FILE__).'/../lib/lime/LimeAutoloader.php');
-LimeAutoloader::register();
-
-$suite = new LimeTestSuite(array(
- 'force_colors' => isset($argv) && in_array('--color', $argv),
- 'base_dir' => realpath(dirname(__FILE__).'/..'),
-));
-
-foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__).'/../unit'), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
-{
- if (preg_match('/Test\.php$/', $file))
- {
- $suite->register($file->getRealPath());
- }
-}
-
-$coverage = new LimeCoverage($suite, array(
- 'base_dir' => realpath(dirname(__FILE__).'/../../lib'),
- 'extension' => '.php',
- 'verbose' => true,
-));
-
-$files = array();
-foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__).'/../../lib'), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
-{
- if (preg_match('/\.php$/', $file))
- {
- $files[] = $file->getRealPath();
- }
-}
-$coverage->setFiles($files);
-
-$coverage->run();
+++ /dev/null
-<?php
-
-/*
- * This file is part of Twig.
- *
- * (c) Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-require_once(dirname(__FILE__).'/../lib/lime/LimeAutoloader.php');
-LimeAutoloader::register();
-
-$suite = new LimeTestSuite(array(
- 'force_colors' => isset($argv) && in_array('--color', $argv),
- 'base_dir' => realpath(dirname(__FILE__).'/..'),
-));
-
-foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__).'/../unit'), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
-{
- if (preg_match('/Test\.php$/', $file))
- {
- $suite->register($file->getRealPath());
- }
-}
-
-exit($suite->run() ? 0 : 1);
+++ /dev/null
-<?php
-
-/*
- * This file is part of Twig.
- *
- * (c) Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-require_once(dirname(__FILE__).'/../../lib/lime/LimeAutoloader.php');
-LimeAutoloader::register();
-
-require_once dirname(__FILE__).'/../../../lib/Twig/Autoloader.php';
-Twig_Autoloader::register();
-
-$t = new LimeTest(3);
-
-// ->autoload()
-$t->diag('->autoload()');
-
-$t->ok(!class_exists('Foo'), '->autoload() does not try to load classes that does not begin with Twig');
-
-$autoloader = new Twig_Autoloader();
-$t->is($autoloader->autoload('Twig_Parser'), true, '->autoload() returns true if it is able to load a class');
-$t->is($autoloader->autoload('Foo'), false, '->autoload() returns false if it is not able to load a class');
+++ /dev/null
-<?php
-
-/*
- * This file is part of Twig.
- *
- * (c) Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-require_once(dirname(__FILE__).'/../../../lib/lime/LimeAutoloader.php');
-LimeAutoloader::register();
-
-require_once dirname(__FILE__).'/../../../../lib/Twig/Autoloader.php';
-Twig_Autoloader::register();
-
-class Object
-{
- public $bar = 'bar';
-
- public function foo()
- {
- return 'foo';
- }
-}
-
-$params = array(
- 'name' => 'Fabien',
- 'obj' => new Object(),
-);
-$templates = array(
- '1_basic1' => '{{ obj.foo }}',
- '1_basic2' => '{{ name|upper }}',
- '1_basic3' => '{% if name %}foo{% endif %}',
- '1_basic4' => '{{ obj.bar }}',
- '1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
-);
-
-$t = new LimeTest(11);
-
-$t->diag('Sandbox globally set');
-$twig = get_environment(false, $templates);
-$t->is($twig->loadTemplate('1_basic')->render($params), 'FOO', 'Sandbox does nothing if it is disabled globally');
-
-$twig = get_environment(true, $templates);
-try
-{
- $twig->loadTemplate('1_basic1')->render($params);
- $t->fail('Sandbox throws a SecurityError exception if an unallowed method is called');
-}
-catch (Twig_Sandbox_SecurityError $e)
-{
- $t->pass('Sandbox throws a SecurityError exception if an unallowed method is called');
-}
-
-$twig = get_environment(true, $templates);
-try
-{
- $twig->loadTemplate('1_basic2')->render($params);
- $t->fail('Sandbox throws a SecurityError exception if an unallowed filter is called');
-}
-catch (Twig_Sandbox_SecurityError $e)
-{
- $t->pass('Sandbox throws a SecurityError exception if an unallowed filter is called');
-}
-
-$twig = get_environment(true, $templates);
-try
-{
- $twig->loadTemplate('1_basic3')->render($params);
- $t->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template');
-}
-catch (Twig_Sandbox_SecurityError $e)
-{
- $t->pass('Sandbox throws a SecurityError exception if an unallowed tag is used in the template');
-}
-
-$twig = get_environment(true, $templates);
-try
-{
- $twig->loadTemplate('1_basic4')->render($params);
- $t->fail('Sandbox throws a SecurityError exception if an unallowed property is called in the template');
-}
-catch (Twig_Sandbox_SecurityError $e)
-{
- $t->pass('Sandbox throws a SecurityError exception if an unallowed property is called in the template');
-}
-
-$twig = get_environment(true, $templates, array(), array(), array('Object' => 'foo'));
-$t->is($twig->loadTemplate('1_basic1')->render($params), 'foo', 'Sandbox allow some methods');
-
-$twig = get_environment(true, $templates, array(), array('upper'));
-$t->is($twig->loadTemplate('1_basic2')->render($params), 'FABIEN', 'Sandbox allow some filters');
-
-$twig = get_environment(true, $templates, array('if'));
-$t->is($twig->loadTemplate('1_basic3')->render($params), 'foo', 'Sandbox allow some tags');
-
-$twig = get_environment(true, $templates, array(), array(), array(), array('Object' => 'bar'));
-$t->is($twig->loadTemplate('1_basic4')->render($params), 'bar', 'Sandbox allow some properties');
-
-$t->diag('Sandbox locally set for an include');
-
-$templates = array(
- '2_basic' => '{{ obj.foo }}{% include "2_included" %}{{ obj.foo }}',
- '2_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
-);
-
-$twig = get_environment(false, $templates);
-$t->is($twig->loadTemplate('2_basic')->render($params), 'fooFOOfoo', 'Sandbox does nothing if disabled globally and sandboxed not used for the include');
-
-$templates = array(
- '3_basic' => '{{ obj.foo }}{% include "3_included" sandboxed %}{{ obj.foo }}',
- '3_included' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
-);
-
-$twig = get_environment(false, $templates);
-$twig = get_environment(true, $templates);
-try
-{
- $twig->loadTemplate('3_basic')->render($params);
- $t->fail('Sandbox throws a SecurityError exception when the included file is sandboxed');
-}
-catch (Twig_Sandbox_SecurityError $e)
-{
- $t->pass('Sandbox throws a SecurityError exception when the included file is sandboxed');
-}
-
-
-function get_environment($sandboxed, $templates, $tags = array(), $filters = array(), $methods = array(), $properties = array())
-{
- $loader = new Twig_Loader_Array($templates);
- $twig = new Twig_Environment($loader, array('trim_blocks' => true, 'debug' => true));
- $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties);
- $twig->addExtension(new Twig_Extension_Sandbox($policy, $sandboxed));
-
- return $twig;
-}
+++ /dev/null
-<?php
-
-/*
- * This file is part of Twig.
- *
- * (c) Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-require_once(dirname(__FILE__).'/../../lib/lime/LimeAutoloader.php');
-LimeAutoloader::register();
-
-require_once dirname(__FILE__).'/../../../lib/Twig/Autoloader.php';
-Twig_Autoloader::register();
-
-$t = new LimeTest(13);
-
-$tokens = array(
- new Twig_Token(Twig_Token::TEXT_TYPE, 1, 0),
- new Twig_Token(Twig_Token::TEXT_TYPE, 2, 0),
- new Twig_Token(Twig_Token::TEXT_TYPE, 3, 0),
- new Twig_Token(Twig_Token::TEXT_TYPE, 4, 0),
- new Twig_Token(Twig_Token::TEXT_TYPE, 5, 0),
- new Twig_Token(Twig_Token::TEXT_TYPE, 6, 0),
- new Twig_Token(Twig_Token::TEXT_TYPE, 7, 0),
- new Twig_Token(Twig_Token::EOF_TYPE, 0, 0),
-);
-
-// ->next()
-$t->diag('->next()');
-$stream = new Twig_TokenStream($tokens, '', false);
-$repr = array();
-while (!$stream->isEOF())
-{
- $token = $stream->next();
-
- $repr[] = $token->getValue();
-}
-$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->next() returns the next token in the stream');
-
-// ->look()
-$t->diag('->look()');
-$stream = new Twig_TokenStream($tokens, '', false);
-$t->is($stream->look()->getValue(), 2, '->look() returns the next token');
-$repr = array();
-while (!$stream->isEOF())
-{
- $token = $stream->next();
-
- $repr[] = $token->getValue();
-}
-$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->look() pushes the token to the stack');
-
-$stream = new Twig_TokenStream($tokens, '', false);
-$t->is($stream->look()->getValue(), 2, '->look() returns the next token');
-$t->is($stream->look()->getValue(), 3, '->look() can be called several times to look more than one upcoming token');
-$t->is($stream->look()->getValue(), 4, '->look() can be called several times to look more than one upcoming token');
-$t->is($stream->look()->getValue(), 5, '->look() can be called several times to look more than one upcoming token');
-$repr = array();
-while (!$stream->isEOF())
-{
- $token = $stream->next();
-
- $repr[] = $token->getValue();
-}
-$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->look() pushes the token to the stack');
-
-// ->rewind()
-$t->diag('->rewind()');
-$stream = new Twig_TokenStream($tokens, '', false);
-$t->is($stream->look()->getValue(), 2, '->look() returns the next token');
-$t->is($stream->look()->getValue(), 3, '->look() can be called several times to look more than one upcoming token');
-$t->is($stream->look()->getValue(), 4, '->look() can be called several times to look more than one upcoming token');
-$t->is($stream->look()->getValue(), 5, '->look() can be called several times to look more than one upcoming token');
-$stream->rewind();
-$repr = array();
-while (!$stream->isEOF())
-{
- $token = $stream->next(false);
-
- $repr[] = $token->getValue();
-}
-$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->rewind() pushes all pushed tokens to the token array');
+++ /dev/null
-<?php
-
-/*
- * This file is part of Twig.
- *
- * (c) Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-require_once(dirname(__FILE__).'/../lib/lime/LimeAutoloader.php');
-LimeAutoloader::register();
-
-require_once dirname(__FILE__).'/../../lib/Twig/Autoloader.php';
-Twig_Autoloader::register();
-
-class Foo
-{
- public function bar($param1 = null, $param2 = null)
- {
- return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
- }
-
- public function getFoo()
- {
- return 'foo';
- }
-
- public function getSelf()
- {
- return $this;
- }
-}
-
-class TestExtension extends Twig_Extension
-{
- public function getFilters()
- {
- return array('nl2br' => new Twig_Filter_Method($this, 'nl2br'));
- }
-
- public function nl2br($value, $sep = '<br />')
- {
- return str_replace("\n", $sep."\n", $value);
- }
-
- public function getName()
- {
- return 'test';
- }
-}
-
-$t = new LimeTest(62);
-$fixturesDir = realpath(dirname(__FILE__).'/../fixtures/');
-
-foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
-{
- if (!preg_match('/\.test$/', $file))
- {
- continue;
- }
-
- $test = file_get_contents($file->getRealpath());
-
- if (!preg_match('/--TEST--\s*(.*?)\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match))
- {
- throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
- }
-
- $message = $match[1];
- $templates = array();
- preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $match[2], $matches, PREG_SET_ORDER);
- foreach ($matches as $match)
- {
- $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
- }
-
- $loader = new Twig_Loader_Array($templates);
- $twig = new Twig_Environment($loader, array('trim_blocks' => true, 'cache' => false));
- $twig->addExtension(new Twig_Extension_Escaper());
- $twig->addExtension(new TestExtension());
-
- $template = $twig->loadTemplate('index.twig');
-
- preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $matches, PREG_SET_ORDER);
- foreach ($matches as $match)
- {
- $output = trim($template->render(eval($match[1].';')), "\n ");
- $expected = trim($match[2], "\n ");
-
- $t->is($output, $expected, $message);
- if ($output != $expected)
- {
- $t->comment('Compiled template that failed:');
-
- foreach (array_keys($templates) as $name)
- {
- $source = $loader->getSource($name);
- $t->comment($twig->compile($twig->parse($twig->tokenize($source, $name))));
- }
- }
- }
-}