| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- "use strict";
- var assert = require("assert"),
- ElementPath = require("../../../../lib/pipeline/matcher/ElementPath.js");
- module.exports = {
- "ElementPath": {
- "Should find the item at the path": function() {
- var p = new ElementPath(),
- doc = {"x":4, "a":5},
- matchItems = [5];
- assert.ok(p.init("a").code, 'OK');
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should find the array at a path": function() {
- var p = new ElementPath(),
- doc = {"x":4, "a":[5, 6]},
- matchItems = [5,6, [5, 6]];
- assert.ok(p.init("a").code, 'OK');
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should find the array at a path without traversing the items in the array": function() {
- var p = new ElementPath(),
- doc = {"x":4, "a":[5, 6]},
- matchItems = [[5, 6]];
- assert.ok(p.init("a").code, 'OK');
- p.setTraverseLeafArray(false);
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should walk nested arrays": function() {
- var p = new ElementPath(),
- doc = {"a":[ {"b":5}, 3, {}, {"b":[9, 11]}, {"b":7}]},
- matchItems = [5, undefined, 9, 11, [9, 11], 7];
- assert.ok( p.init( "a.b" ).code, 'OK' );
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should walk nested arrays but not the array leaves": function() {
- var p = new ElementPath(),
- doc = {"a":[ {"b":5}, 3, {}, {"b":[9, 11]}, {"b":7}]},
- matchItems = [5, undefined, [9, 11], 7];
- assert.ok( p.init( "a.b" ).code, 'OK' );
- p.setTraverseLeafArray( false );
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should follow array indicies": function() {
- var p = new ElementPath(),
- doc = {"a":[5, 7, 3]},
- matchItems = [7];
- assert.ok( p.init( "a.1" ).code, 'OK' );
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should follow array indicies and get the sub array without checking leaves": function() {
- var p = new ElementPath(),
- doc = {"a":[5, [2, 4], 3]},
- matchItems = [[2,4]];
- assert.ok( p.init( "a.1" ).code, 'OK' );
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should follow array indicies and then into objects for leaves": function() {
- var p = new ElementPath(),
- doc = {"a":[5, {"1":4}, 3]},
- matchItems = [4, {"1":4}];
- assert.ok( p.init( "a.1" ).code, 'OK' );
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should follow an array index and check the non existent leaves, then pull the rest of the path from the object": function() {
- var p = new ElementPath(),
- doc = {"a":[5, {"b":4}, 3]},
- matchItems = [undefined, 4];
- assert.ok( p.init( "a.1.b" ).code, 'OK' );
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- },
- "should follow an array index into the leaves of a subarry, then get the remaining path inside the leaf object": function() {
- var p = new ElementPath(),
- doc = {"a":[5, [{"b":4}], 3]},
- matchItems = [4];
- assert.ok( p.init( "a.1.b" ).code, 'OK' );
- var checker = function(element) {
- assert.deepEqual(element, matchItems.shift());
- };
- p._matches(doc, null, checker);
- }
- }
- };
- if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
|