fixed look() method, added tests
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Wed, 14 Oct 2009 07:41:24 +0000 (07:41 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Wed, 14 Oct 2009 07:41:24 +0000 (07:41 +0000)
git-svn-id: http://svn.twig-project.org/trunk@41 93ef8e89-cb99-4229-a87c-7fa0fa45744b

lib/Twig/TokenStream.php
test/unit/Twig/TokenStreamTest.php [new file with mode: 0644]

index 9c63819..d4cae1e 100644 (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;
diff --git a/test/unit/Twig/TokenStreamTest.php b/test/unit/Twig/TokenStreamTest.php
new file mode 100644 (file)
index 0000000..335d719
--- /dev/null
@@ -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');