AbstractLexerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace Doctrine\Tests\Common\Lexer;
  3. class AbstractLexerTest extends \PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * @var ConcreteLexer
  7. */
  8. private $concreteLexer;
  9. public function setUp()
  10. {
  11. $this->concreteLexer = new ConcreteLexer();
  12. }
  13. public function dataProvider()
  14. {
  15. return array(
  16. array(
  17. 'price=10',
  18. array(
  19. array(
  20. 'value' => 'price',
  21. 'type' => 'string',
  22. 'position' => 0,
  23. ),
  24. array(
  25. 'value' => '=',
  26. 'type' => 'operator',
  27. 'position' => 5,
  28. ),
  29. array(
  30. 'value' => 10,
  31. 'type' => 'int',
  32. 'position' => 6,
  33. ),
  34. ),
  35. ),
  36. );
  37. }
  38. public function testResetPeek()
  39. {
  40. $expectedTokens = array(
  41. array(
  42. 'value' => 'price',
  43. 'type' => 'string',
  44. 'position' => 0,
  45. ),
  46. array(
  47. 'value' => '=',
  48. 'type' => 'operator',
  49. 'position' => 5,
  50. ),
  51. array(
  52. 'value' => 10,
  53. 'type' => 'int',
  54. 'position' => 6,
  55. ),
  56. );
  57. $this->concreteLexer->setInput('price=10');
  58. $this->assertEquals($expectedTokens[0], $this->concreteLexer->peek());
  59. $this->assertEquals($expectedTokens[1], $this->concreteLexer->peek());
  60. $this->concreteLexer->resetPeek();
  61. $this->assertEquals($expectedTokens[0], $this->concreteLexer->peek());
  62. }
  63. public function testResetPosition()
  64. {
  65. $expectedTokens = array(
  66. array(
  67. 'value' => 'price',
  68. 'type' => 'string',
  69. 'position' => 0,
  70. ),
  71. array(
  72. 'value' => '=',
  73. 'type' => 'operator',
  74. 'position' => 5,
  75. ),
  76. array(
  77. 'value' => 10,
  78. 'type' => 'int',
  79. 'position' => 6,
  80. ),
  81. );
  82. $this->concreteLexer->setInput('price=10');
  83. $this->assertNull($this->concreteLexer->lookahead);
  84. $this->assertTrue($this->concreteLexer->moveNext());
  85. $this->assertEquals($expectedTokens[0], $this->concreteLexer->lookahead);
  86. $this->assertTrue($this->concreteLexer->moveNext());
  87. $this->assertEquals($expectedTokens[1], $this->concreteLexer->lookahead);
  88. $this->concreteLexer->resetPosition(0);
  89. $this->assertTrue($this->concreteLexer->moveNext());
  90. $this->assertEquals($expectedTokens[0], $this->concreteLexer->lookahead);
  91. }
  92. /**
  93. * @dataProvider dataProvider
  94. *
  95. * @param $input
  96. * @param $expectedTokens
  97. */
  98. public function testMoveNext($input, $expectedTokens)
  99. {
  100. $this->concreteLexer->setInput($input);
  101. $this->assertNull($this->concreteLexer->lookahead);
  102. for ($i = 0; $i < count($expectedTokens); $i++) {
  103. $this->assertTrue($this->concreteLexer->moveNext());
  104. $this->assertEquals($expectedTokens[$i], $this->concreteLexer->lookahead);
  105. }
  106. $this->assertFalse($this->concreteLexer->moveNext());
  107. $this->assertNull($this->concreteLexer->lookahead);
  108. }
  109. public function testSkipUntil()
  110. {
  111. $this->concreteLexer->setInput('price=10');
  112. $this->assertTrue($this->concreteLexer->moveNext());
  113. $this->concreteLexer->skipUntil('operator');
  114. $this->assertEquals(
  115. array(
  116. 'value' => '=',
  117. 'type' => 'operator',
  118. 'position' => 5,
  119. ),
  120. $this->concreteLexer->lookahead
  121. );
  122. }
  123. /**
  124. * @dataProvider dataProvider
  125. *
  126. * @param $input
  127. * @param $expectedTokens
  128. */
  129. public function testPeek($input, $expectedTokens)
  130. {
  131. $this->concreteLexer->setInput($input);
  132. foreach ($expectedTokens as $expectedToken) {
  133. $this->assertEquals($expectedToken, $this->concreteLexer->peek());
  134. }
  135. $this->assertNull($this->concreteLexer->peek());
  136. }
  137. /**
  138. * @dataProvider dataProvider
  139. *
  140. * @param $input
  141. * @param $expectedTokens
  142. */
  143. public function testGlimpse($input, $expectedTokens)
  144. {
  145. $this->concreteLexer->setInput($input);
  146. foreach ($expectedTokens as $expectedToken) {
  147. $this->assertEquals($expectedToken, $this->concreteLexer->glimpse());
  148. $this->concreteLexer->moveNext();
  149. }
  150. $this->assertNull($this->concreteLexer->peek());
  151. }
  152. public function inputUntilPositionDataProvider()
  153. {
  154. return array(
  155. array('price=10', 5, 'price'),
  156. );
  157. }
  158. /**
  159. * @dataProvider inputUntilPositionDataProvider
  160. *
  161. * @param $input
  162. * @param $position
  163. * @param $expectedInput
  164. */
  165. public function testGetInputUntilPosition($input, $position, $expectedInput)
  166. {
  167. $this->concreteLexer->setInput($input);
  168. $this->assertSame($expectedInput, $this->concreteLexer->getInputUntilPosition($position));
  169. }
  170. /**
  171. * @dataProvider dataProvider
  172. *
  173. * @param $input
  174. * @param $expectedTokens
  175. */
  176. public function testIsNextToken($input, $expectedTokens)
  177. {
  178. $this->concreteLexer->setInput($input);
  179. $this->concreteLexer->moveNext();
  180. for ($i = 0; $i < count($expectedTokens); $i++) {
  181. $this->assertTrue($this->concreteLexer->isNextToken($expectedTokens[$i]['type']));
  182. $this->concreteLexer->moveNext();
  183. }
  184. }
  185. /**
  186. * @dataProvider dataProvider
  187. *
  188. * @param $input
  189. * @param $expectedTokens
  190. */
  191. public function testIsNextTokenAny($input, $expectedTokens)
  192. {
  193. $allTokenTypes = array_map(function ($token) {
  194. return $token['type'];
  195. }, $expectedTokens);
  196. $this->concreteLexer->setInput($input);
  197. $this->concreteLexer->moveNext();
  198. for ($i = 0; $i < count($expectedTokens); $i++) {
  199. $this->assertTrue($this->concreteLexer->isNextTokenAny(array($expectedTokens[$i]['type'])));
  200. $this->assertTrue($this->concreteLexer->isNextTokenAny($allTokenTypes));
  201. $this->concreteLexer->moveNext();
  202. }
  203. }
  204. public function testGetLiteral()
  205. {
  206. $this->assertSame('Doctrine\Tests\Common\Lexer\ConcreteLexer::INT', $this->concreteLexer->getLiteral('int'));
  207. $this->assertSame('fake_token', $this->concreteLexer->getLiteral('fake_token'));
  208. }
  209. public function testIsA()
  210. {
  211. $this->assertTrue($this->concreteLexer->isA(11, 'int'));
  212. $this->assertTrue($this->concreteLexer->isA(1.1, 'int'));
  213. $this->assertTrue($this->concreteLexer->isA('=', 'operator'));
  214. $this->assertTrue($this->concreteLexer->isA('>', 'operator'));
  215. $this->assertTrue($this->concreteLexer->isA('<', 'operator'));
  216. $this->assertTrue($this->concreteLexer->isA('fake_text', 'string'));
  217. }
  218. }