LexerTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function testNameLabelForTag()
  13. {
  14. $template = '{% ☃ %}';
  15. $lexer = new Twig_Lexer(new Twig_Environment());
  16. $stream = $lexer->tokenize($template);
  17. $stream->expect(Twig_Token::BLOCK_START_TYPE);
  18. $this->assertSame('☃', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
  19. }
  20. public function testNameLabelForFunction()
  21. {
  22. $template = '{{ ☃() }}';
  23. $lexer = new Twig_Lexer(new Twig_Environment());
  24. $stream = $lexer->tokenize($template);
  25. $stream->expect(Twig_Token::VAR_START_TYPE);
  26. $this->assertSame('☃', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
  27. }
  28. public function testBracketsNesting()
  29. {
  30. $template = '{{ {"a":{"b":"c"}} }}';
  31. $this->assertEquals(2, $this->countToken($template, Twig_Token::PUNCTUATION_TYPE, '{'));
  32. $this->assertEquals(2, $this->countToken($template, Twig_Token::PUNCTUATION_TYPE, '}'));
  33. }
  34. protected function countToken($template, $type, $value = null)
  35. {
  36. $lexer = new Twig_Lexer(new Twig_Environment());
  37. $stream = $lexer->tokenize($template);
  38. $count = 0;
  39. $tokens = array();
  40. while (!$stream->isEOF()) {
  41. $token = $stream->next();
  42. if ($type === $token->getType()) {
  43. if (null === $value || $value === $token->getValue()) {
  44. ++$count;
  45. }
  46. }
  47. }
  48. return $count;
  49. }
  50. public function testLineDirective()
  51. {
  52. $template = "foo\n"
  53. . "bar\n"
  54. . "{% line 10 %}\n"
  55. . "{{\n"
  56. . "baz\n"
  57. . "}}\n";
  58. $lexer = new Twig_Lexer(new Twig_Environment());
  59. $stream = $lexer->tokenize($template);
  60. // foo\nbar\n
  61. $this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
  62. // \n (after {% line %})
  63. $this->assertSame(10, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
  64. // {{
  65. $this->assertSame(11, $stream->expect(Twig_Token::VAR_START_TYPE)->getLine());
  66. // baz
  67. $this->assertSame(12, $stream->expect(Twig_Token::NAME_TYPE)->getLine());
  68. }
  69. public function testLineDirectiveInline()
  70. {
  71. $template = "foo\n"
  72. . "bar{% line 10 %}{{\n"
  73. . "baz\n"
  74. . "}}\n";
  75. $lexer = new Twig_Lexer(new Twig_Environment());
  76. $stream = $lexer->tokenize($template);
  77. // foo\nbar
  78. $this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
  79. // {{
  80. $this->assertSame(10, $stream->expect(Twig_Token::VAR_START_TYPE)->getLine());
  81. // baz
  82. $this->assertSame(11, $stream->expect(Twig_Token::NAME_TYPE)->getLine());
  83. }
  84. public function testLongComments()
  85. {
  86. $template = '{# '.str_repeat('*', 100000).' #}';
  87. $lexer = new Twig_Lexer(new Twig_Environment());
  88. $lexer->tokenize($template);
  89. // should not throw an exception
  90. }
  91. public function testLongRaw()
  92. {
  93. $template = '{% raw %}'.str_repeat('*', 100000).'{% endraw %}';
  94. $lexer = new Twig_Lexer(new Twig_Environment());
  95. $stream = $lexer->tokenize($template);
  96. // should not throw an exception
  97. }
  98. public function testLongVar()
  99. {
  100. $template = '{{ '.str_repeat('x', 100000).' }}';
  101. $lexer = new Twig_Lexer(new Twig_Environment());
  102. $stream = $lexer->tokenize($template);
  103. // should not throw an exception
  104. }
  105. public function testLongBlock()
  106. {
  107. $template = '{% '.str_repeat('x', 100000).' %}';
  108. $lexer = new Twig_Lexer(new Twig_Environment());
  109. $stream = $lexer->tokenize($template);
  110. // should not throw an exception
  111. }
  112. public function testBigNumbers()
  113. {
  114. $template = '{{ 922337203685477580700 }}';
  115. $lexer = new Twig_Lexer(new Twig_Environment());
  116. $stream = $lexer->tokenize($template);
  117. $node = $stream->next();
  118. $node = $stream->next();
  119. $this->assertEquals(922337203685477580700, $node->getValue());
  120. }
  121. }