SimpleTokenParserTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /**
  11. * @group legacy
  12. */
  13. class Twig_Tests_SimpleTokenParserTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @dataProvider getTests
  17. */
  18. public function testParseGrammar($str, $grammar)
  19. {
  20. $this->assertEquals($grammar, Twig_Extensions_SimpleTokenParser::parseGrammar($str), '::parseGrammar() parses a grammar');
  21. }
  22. public function testParseGrammarExceptions()
  23. {
  24. try {
  25. Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo>');
  26. $this->fail();
  27. } catch (Exception $e) {
  28. $this->assertEquals('Twig_Error_Runtime', get_class($e));
  29. }
  30. try {
  31. Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo');
  32. $this->fail();
  33. } catch (Exception $e) {
  34. $this->assertEquals('Twig_Error_Runtime', get_class($e));
  35. }
  36. try {
  37. Twig_Extensions_SimpleTokenParser::parseGrammar('<foo:foo> (with');
  38. $this->fail();
  39. } catch (Exception $e) {
  40. $this->assertEquals('Twig_Error_Runtime', get_class($e));
  41. }
  42. }
  43. public function getTests()
  44. {
  45. return array(
  46. array('', new Twig_Extensions_Grammar_Tag()),
  47. array('const', new Twig_Extensions_Grammar_Tag(
  48. new Twig_Extensions_Grammar_Constant('const')
  49. )),
  50. array(' const ', new Twig_Extensions_Grammar_Tag(
  51. new Twig_Extensions_Grammar_Constant('const')
  52. )),
  53. array('<expr>', new Twig_Extensions_Grammar_Tag(
  54. new Twig_Extensions_Grammar_Expression('expr')
  55. )),
  56. array('<expr:expression>', new Twig_Extensions_Grammar_Tag(
  57. new Twig_Extensions_Grammar_Expression('expr')
  58. )),
  59. array(' <expr:expression> ', new Twig_Extensions_Grammar_Tag(
  60. new Twig_Extensions_Grammar_Expression('expr')
  61. )),
  62. array('<nb:number>', new Twig_Extensions_Grammar_Tag(
  63. new Twig_Extensions_Grammar_Number('nb')
  64. )),
  65. array('<bool:boolean>', new Twig_Extensions_Grammar_Tag(
  66. new Twig_Extensions_Grammar_Boolean('bool')
  67. )),
  68. array('<content:body>', new Twig_Extensions_Grammar_Tag(
  69. new Twig_Extensions_Grammar_Body('content')
  70. )),
  71. array('<expr:expression> [with <arguments:array>]', new Twig_Extensions_Grammar_Tag(
  72. new Twig_Extensions_Grammar_Expression('expr'),
  73. new Twig_Extensions_Grammar_Optional(
  74. new Twig_Extensions_Grammar_Constant('with'),
  75. new Twig_Extensions_Grammar_Array('arguments')
  76. )
  77. )),
  78. array(' <expr:expression> [ with <arguments:array> ] ', new Twig_Extensions_Grammar_Tag(
  79. new Twig_Extensions_Grammar_Expression('expr'),
  80. new Twig_Extensions_Grammar_Optional(
  81. new Twig_Extensions_Grammar_Constant('with'),
  82. new Twig_Extensions_Grammar_Array('arguments')
  83. )
  84. )),
  85. array('<expr:expression> [with <arguments:array> [or <optional:expression>]]', new Twig_Extensions_Grammar_Tag(
  86. new Twig_Extensions_Grammar_Expression('expr'),
  87. new Twig_Extensions_Grammar_Optional(
  88. new Twig_Extensions_Grammar_Constant('with'),
  89. new Twig_Extensions_Grammar_Array('arguments'),
  90. new Twig_Extensions_Grammar_Optional(
  91. new Twig_Extensions_Grammar_Constant('or'),
  92. new Twig_Extensions_Grammar_Expression('optional')
  93. )
  94. )
  95. )),
  96. array('<expr:expression> [with <arguments:array> [, <optional:expression>]]', new Twig_Extensions_Grammar_Tag(
  97. new Twig_Extensions_Grammar_Expression('expr'),
  98. new Twig_Extensions_Grammar_Optional(
  99. new Twig_Extensions_Grammar_Constant('with'),
  100. new Twig_Extensions_Grammar_Array('arguments'),
  101. new Twig_Extensions_Grammar_Optional(
  102. new Twig_Extensions_Grammar_Constant(',', Twig_Token::PUNCTUATION_TYPE),
  103. new Twig_Extensions_Grammar_Expression('optional')
  104. )
  105. )
  106. )),
  107. );
  108. }
  109. }