LexerTest.php 2.6 KB

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