DocLexerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Doctrine\Tests\Common\Annotations;
  3. use Doctrine\Common\Annotations\DocLexer;
  4. class DocLexerTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testMarkerAnnotation()
  7. {
  8. $lexer = new DocLexer;
  9. $lexer->setInput("@Name");
  10. $this->assertNull($lexer->token);
  11. $this->assertNull($lexer->lookahead);
  12. $this->assertTrue($lexer->moveNext());
  13. $this->assertNull($lexer->token);
  14. $this->assertEquals('@', $lexer->lookahead['value']);
  15. $this->assertTrue($lexer->moveNext());
  16. $this->assertEquals('@', $lexer->token['value']);
  17. $this->assertEquals('Name', $lexer->lookahead['value']);
  18. $this->assertFalse($lexer->moveNext());
  19. }
  20. public function testScannerTokenizesDocBlockWhitConstants()
  21. {
  22. $lexer = new DocLexer();
  23. $docblock = '@AnnotationWithConstants(PHP_EOL, ClassWithConstants::SOME_VALUE, \Doctrine\Tests\Common\Annotations\Fixtures\IntefaceWithConstants::SOME_VALUE)';
  24. $tokens = array (
  25. array(
  26. 'value' => '@',
  27. 'position' => 0,
  28. 'type' => DocLexer::T_AT,
  29. ),
  30. array(
  31. 'value' => 'AnnotationWithConstants',
  32. 'position' => 1,
  33. 'type' => DocLexer::T_IDENTIFIER,
  34. ),
  35. array(
  36. 'value' => '(',
  37. 'position' => 24,
  38. 'type' => DocLexer::T_OPEN_PARENTHESIS,
  39. ),
  40. array(
  41. 'value' => 'PHP_EOL',
  42. 'position' => 25,
  43. 'type' => DocLexer::T_IDENTIFIER,
  44. ),
  45. array(
  46. 'value' => ',',
  47. 'position' => 32,
  48. 'type' => DocLexer::T_COMMA,
  49. ),
  50. array(
  51. 'value' => 'ClassWithConstants::SOME_VALUE',
  52. 'position' => 34,
  53. 'type' => DocLexer::T_IDENTIFIER,
  54. ),
  55. array(
  56. 'value' => ',',
  57. 'position' => 64,
  58. 'type' => DocLexer::T_COMMA,
  59. ),
  60. array(
  61. 'value' => '\\Doctrine\\Tests\\Common\\Annotations\\Fixtures\\IntefaceWithConstants::SOME_VALUE',
  62. 'position' => 66,
  63. 'type' => DocLexer::T_IDENTIFIER,
  64. ),
  65. array(
  66. 'value' => ')',
  67. 'position' => 143,
  68. 'type' => DocLexer::T_CLOSE_PARENTHESIS,
  69. )
  70. );
  71. $lexer->setInput($docblock);
  72. foreach ($tokens as $expected) {
  73. $lexer->moveNext();
  74. $lookahead = $lexer->lookahead;
  75. $this->assertEquals($expected['value'], $lookahead['value']);
  76. $this->assertEquals($expected['type'], $lookahead['type']);
  77. $this->assertEquals($expected['position'], $lookahead['position']);
  78. }
  79. $this->assertFalse($lexer->moveNext());
  80. }
  81. public function testScannerTokenizesDocBlockWhitInvalidIdentifier()
  82. {
  83. $lexer = new DocLexer();
  84. $docblock = '@Foo\3.42';
  85. $tokens = array (
  86. array(
  87. 'value' => '@',
  88. 'position' => 0,
  89. 'type' => DocLexer::T_AT,
  90. ),
  91. array(
  92. 'value' => 'Foo',
  93. 'position' => 1,
  94. 'type' => DocLexer::T_IDENTIFIER,
  95. ),
  96. array(
  97. 'value' => '\\',
  98. 'position' => 4,
  99. 'type' => DocLexer::T_NAMESPACE_SEPARATOR,
  100. ),
  101. array(
  102. 'value' => 3.42,
  103. 'position' => 5,
  104. 'type' => DocLexer::T_FLOAT,
  105. )
  106. );
  107. $lexer->setInput($docblock);
  108. foreach ($tokens as $expected) {
  109. $lexer->moveNext();
  110. $lookahead = $lexer->lookahead;
  111. $this->assertEquals($expected['value'], $lookahead['value']);
  112. $this->assertEquals($expected['type'], $lookahead['type']);
  113. $this->assertEquals($expected['position'], $lookahead['position']);
  114. }
  115. $this->assertFalse($lexer->moveNext());
  116. }
  117. }