AbstractLexerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. public function testUtf8Mismatch()
  124. {
  125. $this->concreteLexer->setInput("\xE9=10");
  126. $this->assertTrue($this->concreteLexer->moveNext());
  127. $this->assertEquals(
  128. array(
  129. 'value' => "\xE9=10",
  130. 'type' => 'string',
  131. 'position' => 0,
  132. ),
  133. $this->concreteLexer->lookahead
  134. );
  135. }
  136. /**
  137. * @dataProvider dataProvider
  138. *
  139. * @param $input
  140. * @param $expectedTokens
  141. */
  142. public function testPeek($input, $expectedTokens)
  143. {
  144. $this->concreteLexer->setInput($input);
  145. foreach ($expectedTokens as $expectedToken) {
  146. $this->assertEquals($expectedToken, $this->concreteLexer->peek());
  147. }
  148. $this->assertNull($this->concreteLexer->peek());
  149. }
  150. /**
  151. * @dataProvider dataProvider
  152. *
  153. * @param $input
  154. * @param $expectedTokens
  155. */
  156. public function testGlimpse($input, $expectedTokens)
  157. {
  158. $this->concreteLexer->setInput($input);
  159. foreach ($expectedTokens as $expectedToken) {
  160. $this->assertEquals($expectedToken, $this->concreteLexer->glimpse());
  161. $this->concreteLexer->moveNext();
  162. }
  163. $this->assertNull($this->concreteLexer->peek());
  164. }
  165. public function inputUntilPositionDataProvider()
  166. {
  167. return array(
  168. array('price=10', 5, 'price'),
  169. );
  170. }
  171. /**
  172. * @dataProvider inputUntilPositionDataProvider
  173. *
  174. * @param $input
  175. * @param $position
  176. * @param $expectedInput
  177. */
  178. public function testGetInputUntilPosition($input, $position, $expectedInput)
  179. {
  180. $this->concreteLexer->setInput($input);
  181. $this->assertSame($expectedInput, $this->concreteLexer->getInputUntilPosition($position));
  182. }
  183. /**
  184. * @dataProvider dataProvider
  185. *
  186. * @param $input
  187. * @param $expectedTokens
  188. */
  189. public function testIsNextToken($input, $expectedTokens)
  190. {
  191. $this->concreteLexer->setInput($input);
  192. $this->concreteLexer->moveNext();
  193. for ($i = 0; $i < count($expectedTokens); $i++) {
  194. $this->assertTrue($this->concreteLexer->isNextToken($expectedTokens[$i]['type']));
  195. $this->concreteLexer->moveNext();
  196. }
  197. }
  198. /**
  199. * @dataProvider dataProvider
  200. *
  201. * @param $input
  202. * @param $expectedTokens
  203. */
  204. public function testIsNextTokenAny($input, $expectedTokens)
  205. {
  206. $allTokenTypes = array_map(function ($token) {
  207. return $token['type'];
  208. }, $expectedTokens);
  209. $this->concreteLexer->setInput($input);
  210. $this->concreteLexer->moveNext();
  211. for ($i = 0; $i < count($expectedTokens); $i++) {
  212. $this->assertTrue($this->concreteLexer->isNextTokenAny(array($expectedTokens[$i]['type'])));
  213. $this->assertTrue($this->concreteLexer->isNextTokenAny($allTokenTypes));
  214. $this->concreteLexer->moveNext();
  215. }
  216. }
  217. public function testGetLiteral()
  218. {
  219. $this->assertSame('Doctrine\Tests\Common\Lexer\ConcreteLexer::INT', $this->concreteLexer->getLiteral('int'));
  220. $this->assertSame('fake_token', $this->concreteLexer->getLiteral('fake_token'));
  221. }
  222. public function testIsA()
  223. {
  224. $this->assertTrue($this->concreteLexer->isA(11, 'int'));
  225. $this->assertTrue($this->concreteLexer->isA(1.1, 'int'));
  226. $this->assertTrue($this->concreteLexer->isA('=', 'operator'));
  227. $this->assertTrue($this->concreteLexer->isA('>', 'operator'));
  228. $this->assertTrue($this->concreteLexer->isA('<', 'operator'));
  229. $this->assertTrue($this->concreteLexer->isA('fake_text', 'string'));
  230. }
  231. }