AbstractLexerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace JMS\Parser;
  3. class AbstractLexerTest extends \PHPUnit_Framework_TestCase
  4. {
  5. const T_UNKNOWN = 0;
  6. const T_STRING = 1;
  7. const T_INTEGER = 2;
  8. const T_COMMA = 100;
  9. private $lexer;
  10. public function testTokenization()
  11. {
  12. $this->lexer->setInput('"foo" 1234');
  13. $this->assertNull($this->lexer->token);
  14. $this->assertNotNull($this->lexer->next);
  15. $this->assertAttributeEquals(array(
  16. array('foo', 0, self::T_STRING),
  17. array(1234, 7, self::T_INTEGER),
  18. ), 'tokens', $this->lexer);
  19. }
  20. public function testMoveNext()
  21. {
  22. $this->lexer->setInput('1 2 3');
  23. $this->assertNull($this->lexer->token);
  24. $this->assertTrue($this->lexer->moveNext());
  25. $this->assertValue(1, $this->lexer->token);
  26. $this->assertTrue($this->lexer->moveNext());
  27. $this->assertValue(2, $this->lexer->token);
  28. $this->assertFalse($this->lexer->moveNext());
  29. $this->assertValue(3, $this->lexer->token);
  30. }
  31. /**
  32. * @expectedException \InvalidArgumentException
  33. */
  34. public function testSkipUntilWithNonExistent()
  35. {
  36. $this->lexer->setInput('1 2 3');
  37. $this->lexer->skipUntil(self::T_STRING);
  38. }
  39. public function testSkipUntil()
  40. {
  41. $this->lexer->setInput('1 "foo"');
  42. $this->assertNull($this->lexer->skipUntil(self::T_STRING));
  43. $this->assertValue(1, $this->lexer->token);
  44. $this->assertValue('foo', $this->lexer->next);
  45. }
  46. public function testIsNext()
  47. {
  48. $this->lexer->setInput('1');
  49. $this->assertTrue($this->lexer->isNext(self::T_INTEGER));
  50. $this->assertFalse($this->lexer->isNext(self::T_COMMA));
  51. }
  52. public function testIsNextAny()
  53. {
  54. $this->lexer->setInput('1');
  55. $this->assertTrue($this->lexer->isNextAny(array(self::T_COMMA, self::T_INTEGER)));
  56. $this->assertFalse($this->lexer->isNextAny(array(self::T_COMMA, self::T_STRING)));
  57. }
  58. public function testPeek()
  59. {
  60. $this->lexer->setInput('1 2 3');
  61. $this->assertValue(1, $this->lexer->next);
  62. $this->assertValue(2, $this->lexer->peek()->get());
  63. $this->assertValue(1, $this->lexer->next);
  64. $this->assertValue(3, $this->lexer->peek()->get());
  65. $this->assertValue(1, $this->lexer->next);
  66. $this->assertTrue($this->lexer->moveNext());
  67. $this->assertValue(2, $this->lexer->next);
  68. $this->assertValue(3, $this->lexer->peek()->get());
  69. $this->assertValue(2, $this->lexer->next);
  70. }
  71. private function assertValue($expected, $actualToken)
  72. {
  73. $this->assertNotNull($actualToken);
  74. $this->assertSame($expected, $actualToken[0]);
  75. }
  76. protected function setUp()
  77. {
  78. $this->lexer = $this->getMockForAbstractClass('JMS\Parser\AbstractLexer');
  79. $this->lexer->expects($this->any())
  80. ->method('getRegex')
  81. ->will($this->returnValue('/("(?:[^"]*|(?<=\\)"))*")|([0-9]+)|\s+|(.)/i'));
  82. $this->lexer->expects($this->any())
  83. ->method('determineTypeAndValue')
  84. ->will($this->returnCallback(function($value) {
  85. if (',' === $value) {
  86. return array(AbstractLexerTest::T_COMMA, $value);
  87. }
  88. if ('"' === $value[0]) {
  89. return array(AbstractLexerTest::T_STRING, substr($value, 1, -1));
  90. }
  91. if (preg_match('/^[0-9]+$/', $value)) {
  92. return array(AbstractLexerTest::T_INTEGER, (integer) $value);
  93. }
  94. return array(AbstractLexerTest::T_UNKNOWN, $value);
  95. }));
  96. }
  97. }