Skip to content

Commit

Permalink
test cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Afsin Ustundag committed Jun 24, 2015
1 parent 3cc7dd2 commit b43d947
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/jsonpath/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var re = exports.re = {
qq_string: new RegExp('^\"(?:\\\\[\"bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\"\\\\])*\"')
};

var types = exports.types = {
var types = {
ROOT: {
type: 'root'
},
Expand Down
100 changes: 53 additions & 47 deletions test/jsonpath/test-normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,64 @@

var chai = require('chai');

var jsonpath = require('../../lib/jsonpath/parser');
var parser = require('../../lib/jsonpath/parser');

var expect = chai.expect;

describe('jsonpath normalization', function () {
var toNode = function (property) {
describe('parser.normalize', function () {
var _p = function (property) {
return {
type: 'property',
parameter: property
};
};

var _n = function (type) {
return {
type: type
};
};

it('$.store.book[*].author', function () {
var actual = jsonpath.normalize('$.store.book[*].author');
var actual = parser.normalize('$.store.book[*].author');
var expected = [
jsonpath.types.ROOT,
toNode('store'),
toNode('book'),
jsonpath.types.WILDCARD,
toNode('author')
_n('root'),
_p('store'),
_p('book'),
_n('wildcard'),
_p('author')
];
expect(actual).to.deep.equal(expected);
});

it('$..author', function () {
var actual = jsonpath.normalize('$..author');
var actual = parser.normalize('$..author');
var expected = [
jsonpath.types.ROOT,
jsonpath.types.RECURSIVE_DESCENT,
toNode('author')
_n('root'),
_n('recursive_descent'),
_p('author')
];
expect(actual).to.deep.equal(expected);
});

it('$.store.*', function () {
var actual = jsonpath.normalize('$.store.*');
var expected = [jsonpath.types.ROOT, toNode('store'), jsonpath.types.WILDCARD];
var actual = parser.normalize('$.store.*');
var expected = [_n('root'), _p('store'), _n('wildcard')];
expect(actual).to.deep.equal(expected);
});

it('$.store..price', function () {
var actual = jsonpath.normalize('$.store..price');
var expected = [jsonpath.types.ROOT, toNode('store'), jsonpath.types.RECURSIVE_DESCENT, toNode('price')];
var actual = parser.normalize('$.store..price');
var expected = [_n('root'), _p('store'), _n('recursive_descent'), _p('price')];
expect(actual).to.deep.equal(expected);
});

it('$..book[2]', function () {
var actual = jsonpath.normalize('$..book[2]');
var actual = parser.normalize('$..book[2]');
var expected = [
jsonpath.types.ROOT,
jsonpath.types.RECURSIVE_DESCENT,
toNode('book'), {
_n('root'),
_n('recursive_descent'),
_p('book'), {
type: 'properties',
parameter: [2]
}
Expand All @@ -62,17 +68,17 @@ describe('jsonpath normalization', function () {
});

it('$..book[(@.length-1)]', function () {
var actual = jsonpath.normalize('$..book[(@.length-1)]');
var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), {
var actual = parser.normalize('$..book[(@.length-1)]');
var expected = [_n('root'), _n('recursive_descent'), _p('book'), {
type: 'script',
parameter: '@.length-1'
}];
expect(actual).to.deep.equal(expected);
});

it('$..book[-1:]', function () {
var actual = jsonpath.normalize('$..book[-1:]');
var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), {
var actual = parser.normalize('$..book[-1:]');
var expected = [_n('root'), _n('recursive_descent'), _p('book'), {
type: 'properties',
parameter: [{
start: -1,
Expand All @@ -84,17 +90,17 @@ describe('jsonpath normalization', function () {
});

it('$..book[1,2]', function () {
var actual = jsonpath.normalize('$..book[1,2]');
var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), {
var actual = parser.normalize('$..book[1,2]');
var expected = [_n('root'), _n('recursive_descent'), _p('book'), {
type: 'properties',
parameter: [1, 2]
}];
expect(actual).to.deep.equal(expected);
});

it('$..book[:2]', function () {
var actual = jsonpath.normalize('$..book[:2]');
var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), {
var actual = parser.normalize('$..book[:2]');
var expected = [_n('root'), _n('recursive_descent'), _p('book'), {
type: 'properties',
parameter: [{
start: 0,
Expand All @@ -106,71 +112,71 @@ describe('jsonpath normalization', function () {
});

it('$..book[*][category,author]', function () {
var actual = jsonpath.normalize('$..book[*][category,author]');
var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), jsonpath.types.WILDCARD, {
var actual = parser.normalize('$..book[*][category,author]');
var expected = [_n('root'), _n('recursive_descent'), _p('book'), _n('wildcard'), {
type: 'properties',
parameter: ['category', 'author']
}];
expect(actual).to.deep.equal(expected);
});

it('$..book[?(@.isbn)]', function () {
var actual = jsonpath.normalize('$..book[?(@.isbn)]');
var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), {
var actual = parser.normalize('$..book[?(@.isbn)]');
var expected = [_n('root'), _n('recursive_descent'), _p('book'), {
type: 'filter_script',
parameter: '@.isbn'
}];
expect(actual).to.deep.equal(expected);
});

it('$..[?(@.price>19)]^', function () {
var actual = jsonpath.normalize('$..[?(@.price>19)]^');
var actual = parser.normalize('$..[?(@.price>19)]^');
var filterScriptNode = {
type: 'filter_script',
parameter: '@.price>19'
};
expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, filterScriptNode, jsonpath.types.PARENT]);
expect(actual).to.deep.equal([_n('root'), _n('recursive_descent'), filterScriptNode, _n('parent')]);
});

it('$..*', function () {
var actual = jsonpath.normalize('$..*');
expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, jsonpath.types.WILDCARD]);
var actual = parser.normalize('$..*');
expect(actual).to.deep.equal([_n('root'), _n('recursive_descent'), _n('wildcard')]);
});

it('$.store.book[?(@path !== "$[\'store\'][\'book\'][0]")]', function () {
var actual = jsonpath.normalize('$.store.book[?(@path !== "$[\'store\'][\'book\'][0]")]');
var expected = [jsonpath.types.ROOT, toNode('store'), toNode('book'), {
var actual = parser.normalize('$.store.book[?(@path !== "$[\'store\'][\'book\'][0]")]');
var expected = [_n('root'), _p('store'), _p('book'), {
type: 'filter_script',
parameter: '@path !== "$[\'store\'][\'book\'][0]"'
}];
expect(actual).to.deep.equal(expected);
});

it('$.store..price.round()', function () {
var actual = jsonpath.normalize('$.store..price.round()');
var expected = [jsonpath.types.ROOT, toNode('store'), jsonpath.types.RECURSIVE_DESCENT, toNode('price'), {
var actual = parser.normalize('$.store..price.round()');
var expected = [_n('root'), _p('store'), _n('recursive_descent'), _p('price'), {
type: 'fn',
parameter: 'round'
}];
expect(actual).to.deep.equal(expected);
});

it('$.link[$.obj.library.books[0].references[*]].title', function () {
var actual = jsonpath.normalize('$.link[$.obj.library.books[0].references[*]].title');
var expected = [jsonpath.types.ROOT, toNode('link'), {
var actual = parser.normalize('$.link[$.obj.library.books[0].references[*]].title');
var expected = [_n('root'), _p('link'), {
type: 'subpath',
parameter: '$.obj.library.books[0].references[*]'
}, toNode('title')];
}, _p('title')];
expect(actual).to.deep.equal(expected);
});

it('$', function () {
var actual = jsonpath.normalize('$');
expect(actual).to.deep.equal([jsonpath.types.ROOT]);
var actual = parser.normalize('$');
expect(actual).to.deep.equal([_n('root')]);
});

xit('$..book[*].category^', function () {
var fn = jsonpath.instance.bind(null, '$..book[*].category^');
var fn = parser.instance.bind(null, '$..book[*].category^');
expect(fn).to.throw(Error);
});
});
File renamed without changes.

0 comments on commit b43d947

Please sign in to comment.