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