LexerTest.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 testBracketsNesting()
  13. {
  14. $template = '{{ {"a":{"b":"c"}} }}';
  15. $this->assertEquals(2, $this->countToken($template, Twig_Token::PUNCTUATION_TYPE, '{'));
  16. $this->assertEquals(2, $this->countToken($template, Twig_Token::PUNCTUATION_TYPE, '}'));
  17. }
  18. protected function countToken($template, $type, $value = null)
  19. {
  20. $lexer = new Twig_Lexer(new Twig_Environment());
  21. $stream = $lexer->tokenize($template);
  22. $count = 0;
  23. $tokens = array();
  24. while (!$stream->isEOF()) {
  25. $token = $stream->next();
  26. if ($type === $token->getType()) {
  27. if (null === $value || $value === $token->getValue()) {
  28. ++$count;
  29. }
  30. }
  31. }
  32. return $count;
  33. }
  34. public function testLineDirective()
  35. {
  36. $template = "foo\n"
  37. . "bar\n"
  38. . "{% line 10 %}\n"
  39. . "{{\n"
  40. . "baz\n"
  41. . "}}\n";
  42. $lexer = new Twig_Lexer(new Twig_Environment());
  43. $stream = $lexer->tokenize($template);
  44. // foo\nbar\n
  45. $this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
  46. // \n (after {% line %})
  47. $this->assertSame(10, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
  48. // {{
  49. $this->assertSame(11, $stream->expect(Twig_Token::VAR_START_TYPE)->getLine());
  50. // baz
  51. $this->assertSame(12, $stream->expect(Twig_Token::NAME_TYPE)->getLine());
  52. }
  53. public function testLineDirectiveInline()
  54. {
  55. $template = "foo\n"
  56. . "bar{% line 10 %}{{\n"
  57. . "baz\n"
  58. . "}}\n";
  59. $lexer = new Twig_Lexer(new Twig_Environment());
  60. $stream = $lexer->tokenize($template);
  61. // foo\nbar
  62. $this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
  63. // {{
  64. $this->assertSame(10, $stream->expect(Twig_Token::VAR_START_TYPE)->getLine());
  65. // baz
  66. $this->assertSame(11, $stream->expect(Twig_Token::NAME_TYPE)->getLine());
  67. }
  68. public function testLongComments()
  69. {
  70. $template = '{# '.str_repeat('*', 100000).' #}';
  71. $lexer = new Twig_Lexer(new Twig_Environment());
  72. $lexer->tokenize($template);
  73. // should not throw an exception
  74. }
  75. public function testLongRaw()
  76. {
  77. $template = '{% raw %}'.str_repeat('*', 100000).'{% endraw %}';
  78. $lexer = new Twig_Lexer(new Twig_Environment());
  79. $stream = $lexer->tokenize($template);
  80. // should not throw an exception
  81. }
  82. public function testLongBlock()
  83. {
  84. $template = '{{ '.str_repeat('*', 100000).' }}';
  85. $lexer = new Twig_Lexer(new Twig_Environment());
  86. $stream = $lexer->tokenize($template);
  87. // should not throw an exception
  88. }
  89. public function testLongBlock1()
  90. {
  91. $template = '{% '.str_repeat('*', 100000).' %}';
  92. $lexer = new Twig_Lexer(new Twig_Environment());
  93. $stream = $lexer->tokenize($template);
  94. // should not throw an exception
  95. }
  96. }