fixed look() method, added tests

git-svn-id: http://svn.twig-project.org/trunk@41 93ef8e89-cb99-4229-a87c-7fa0fa45744b
This commit is contained in:
fabien
2009-10-14 07:41:24 +00:00
parent 84684d9d64
commit 9fa9485df2
2 changed files with 71 additions and 2 deletions
+3 -2
View File
@@ -52,12 +52,14 @@ class Twig_TokenStream
*/
public function next($fromStack = true)
{
if (!empty($this->pushed))
if ($fromStack && !empty($this->pushed))
{
$old = array_shift($this->pushed);
$token = array_shift($this->pushed);
}
else
{
$old = $this->current;
$token = array_shift($this->tokens);
}
@@ -80,7 +82,6 @@ class Twig_TokenStream
$token->setValue($value);
}
$old = $this->current;
$this->current = $token;
$this->eof = $token->getType() === Twig_Token::EOF_TYPE;
+68
View File
@@ -0,0 +1,68 @@
<?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(0);
$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');