stringify.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var assert = require('assert');
  2. var jp = require('../');
  3. suite('stringify', function() {
  4. test('simple path stringifies', function() {
  5. var string = jp.stringify(['$', 'a', 'b', 'c']);
  6. assert.equal(string, '$.a.b.c');
  7. });
  8. test('numeric literals end up as subscript numbers', function() {
  9. var string = jp.stringify(['$', 'store', 'book', 0, 'author']);
  10. assert.equal(string, '$.store.book[0].author');
  11. });
  12. test('simple path with no leading root stringifies', function() {
  13. var string = jp.stringify(['a', 'b', 'c']);
  14. assert.equal(string, '$.a.b.c');
  15. });
  16. test('simple parsed path stringifies', function() {
  17. var path = [
  18. { scope: 'child', operation: 'member', expression: { type: 'identifier', value: 'a' } },
  19. { scope: 'child', operation: 'member', expression: { type: 'identifier', value: 'b' } },
  20. { scope: 'child', operation: 'member', expression: { type: 'identifier', value: 'c' } }
  21. ];
  22. var string = jp.stringify(path);
  23. assert.equal(string, '$.a.b.c');
  24. });
  25. test('keys with hyphens get subscripted', function() {
  26. var string = jp.stringify(['$', 'member-search']);
  27. assert.equal(string, '$["member-search"]');
  28. });
  29. test('complicated path round trips', function() {
  30. var pathExpression = '$..*[0:2].member["string-xyz"]';
  31. var path = jp.parse(pathExpression);
  32. var string = jp.stringify(path);
  33. assert.equal(string, pathExpression);
  34. });
  35. test('complicated path with filter exp round trips', function() {
  36. var pathExpression = '$..*[0:2].member[?(@.val > 10)]';
  37. var path = jp.parse(pathExpression);
  38. var string = jp.stringify(path);
  39. assert.equal(string, pathExpression);
  40. });
  41. test('throws for no input', function() {
  42. assert.throws(function() { jp.stringify() }, /we need a path/);
  43. });
  44. });