From: fabien Date: Wed, 14 Oct 2009 07:41:24 +0000 (+0000) Subject: fixed look() method, added tests X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9fa9485df20e2e1ddd3093b5772ca263302f2a41;p=konrad%2Ftwig.git fixed look() method, added tests git-svn-id: http://svn.twig-project.org/trunk@41 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- diff --git a/lib/Twig/TokenStream.php b/lib/Twig/TokenStream.php index 9c63819..d4cae1e 100644 --- a/lib/Twig/TokenStream.php +++ b/lib/Twig/TokenStream.php @@ -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; diff --git a/test/unit/Twig/TokenStreamTest.php b/test/unit/Twig/TokenStreamTest.php new file mode 100644 index 0000000..335d719 --- /dev/null +++ b/test/unit/Twig/TokenStreamTest.php @@ -0,0 +1,68 @@ +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');