SimpleTokenParserTest.php 4.5KB

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