LexerTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace JmesPath\Tests;
  3. use JmesPath\Lexer;
  4. use JmesPath\SyntaxErrorException;
  5. /**
  6. * @covers JmesPath\Lexer
  7. */
  8. class LexerTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function inputProvider()
  11. {
  12. return array(
  13. array('0', 'number'),
  14. array('1', 'number'),
  15. array('2', 'number'),
  16. array('3', 'number'),
  17. array('4', 'number'),
  18. array('5', 'number'),
  19. array('6', 'number'),
  20. array('7', 'number'),
  21. array('8', 'number'),
  22. array('9', 'number'),
  23. array('-1', 'number'),
  24. array('-1.5', 'number'),
  25. array('109.5', 'number'),
  26. array('.', 'dot'),
  27. array('{', 'lbrace'),
  28. array('}', 'rbrace'),
  29. array('[', 'lbracket'),
  30. array(']', 'rbracket'),
  31. array(':', 'colon'),
  32. array(',', 'comma'),
  33. array('||', 'or'),
  34. array('*', 'star'),
  35. array('foo', 'identifier'),
  36. array('"foo"', 'quoted_identifier'),
  37. array('`true`', 'literal'),
  38. array('`false`', 'literal'),
  39. array('`null`', 'literal'),
  40. array('`"true"`', 'literal')
  41. );
  42. }
  43. /**
  44. * @dataProvider inputProvider
  45. */
  46. public function testTokenizesInput($input, $type)
  47. {
  48. $l = new Lexer();
  49. $tokens = $l->tokenize($input);
  50. $this->assertEquals($tokens[0]['type'], $type);
  51. }
  52. public function testTokenizesJsonLiterals()
  53. {
  54. $l = new Lexer();
  55. $tokens = $l->tokenize('`null`, `false`, `true`, `"abc"`, `"ab\\"c"`,'
  56. . '`0`, `0.45`, `-0.5`');
  57. $this->assertNull($tokens[0]['value']);
  58. $this->assertFalse($tokens[2]['value']);
  59. $this->assertTrue($tokens[4]['value']);
  60. $this->assertEquals('abc', $tokens[6]['value']);
  61. $this->assertEquals('ab"c', $tokens[8]['value']);
  62. $this->assertSame(0, $tokens[10]['value']);
  63. $this->assertSame(0.45, $tokens[12]['value']);
  64. $this->assertSame(-0.5, $tokens[14]['value']);
  65. }
  66. public function testTokenizesJsonNumbers()
  67. {
  68. $l = new Lexer();
  69. $tokens = $l->tokenize('`10`, `1.2`, `-10.20e-10`, `1.2E+2`');
  70. $this->assertEquals(10, $tokens[0]['value']);
  71. $this->assertEquals(1.2, $tokens[2]['value']);
  72. $this->assertEquals(-1.02E-9, $tokens[4]['value']);
  73. $this->assertEquals(120, $tokens[6]['value']);
  74. }
  75. public function testCanWorkWithElidedJsonLiterals()
  76. {
  77. $l = new Lexer();
  78. $tokens = $l->tokenize('`foo`');
  79. $this->assertEquals('foo', $tokens[0]['value']);
  80. $this->assertEquals('literal', $tokens[0]['type']);
  81. }
  82. }