ElementPath.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. var assert = require("assert"),
  3. ElementPath = require("../../../../lib/pipeline/matcher/ElementPath.js");
  4. module.exports = {
  5. "ElementPath": {
  6. "Should find the item at the path": function() {
  7. var p = new ElementPath(),
  8. doc = {"x":4, "a":5},
  9. matchItems = [5];
  10. assert.ok(p.init("a").code, 'OK');
  11. var checker = function(element) {
  12. assert.deepEqual(element, matchItems.shift());
  13. };
  14. p._matches(doc, null, checker);
  15. },
  16. "should find the array at a path": function() {
  17. var p = new ElementPath(),
  18. doc = {"x":4, "a":[5, 6]},
  19. matchItems = [5,6, [5, 6]];
  20. assert.ok(p.init("a").code, 'OK');
  21. var checker = function(element) {
  22. assert.deepEqual(element, matchItems.shift());
  23. };
  24. p._matches(doc, null, checker);
  25. },
  26. "should find the array at a path without traversing the items in the array": function() {
  27. var p = new ElementPath(),
  28. doc = {"x":4, "a":[5, 6]},
  29. matchItems = [[5, 6]];
  30. assert.ok(p.init("a").code, 'OK');
  31. p.setTraverseLeafArray(false);
  32. var checker = function(element) {
  33. assert.deepEqual(element, matchItems.shift());
  34. };
  35. p._matches(doc, null, checker);
  36. },
  37. "should walk nested arrays": function() {
  38. var p = new ElementPath(),
  39. doc = {"a":[ {"b":5}, 3, {}, {"b":[9, 11]}, {"b":7}]},
  40. matchItems = [5, undefined, 9, 11, [9, 11], 7];
  41. assert.ok( p.init( "a.b" ).code, 'OK' );
  42. var checker = function(element) {
  43. assert.deepEqual(element, matchItems.shift());
  44. };
  45. p._matches(doc, null, checker);
  46. },
  47. "should walk nested arrays but not the array leaves": function() {
  48. var p = new ElementPath(),
  49. doc = {"a":[ {"b":5}, 3, {}, {"b":[9, 11]}, {"b":7}]},
  50. matchItems = [5, undefined, [9, 11], 7];
  51. assert.ok( p.init( "a.b" ).code, 'OK' );
  52. p.setTraverseLeafArray( false );
  53. var checker = function(element) {
  54. assert.deepEqual(element, matchItems.shift());
  55. };
  56. p._matches(doc, null, checker);
  57. },
  58. "should follow array indicies": function() {
  59. var p = new ElementPath(),
  60. doc = {"a":[5, 7, 3]},
  61. matchItems = [7];
  62. assert.ok( p.init( "a.1" ).code, 'OK' );
  63. var checker = function(element) {
  64. assert.deepEqual(element, matchItems.shift());
  65. };
  66. p._matches(doc, null, checker);
  67. },
  68. "should follow array indicies and get the sub array without checking leaves": function() {
  69. var p = new ElementPath(),
  70. doc = {"a":[5, [2, 4], 3]},
  71. matchItems = [[2,4]];
  72. assert.ok( p.init( "a.1" ).code, 'OK' );
  73. var checker = function(element) {
  74. assert.deepEqual(element, matchItems.shift());
  75. };
  76. p._matches(doc, null, checker);
  77. },
  78. "should follow array indicies and then into objects for leaves": function() {
  79. var p = new ElementPath(),
  80. doc = {"a":[5, {"1":4}, 3]},
  81. matchItems = [4, {"1":4}];
  82. assert.ok( p.init( "a.1" ).code, 'OK' );
  83. var checker = function(element) {
  84. assert.deepEqual(element, matchItems.shift());
  85. };
  86. p._matches(doc, null, checker);
  87. },
  88. "should follow an array index and check the non existent leaves, then pull the rest of the path from the object": function() {
  89. var p = new ElementPath(),
  90. doc = {"a":[5, {"b":4}, 3]},
  91. matchItems = [undefined, 4];
  92. assert.ok( p.init( "a.1.b" ).code, 'OK' );
  93. var checker = function(element) {
  94. assert.deepEqual(element, matchItems.shift());
  95. };
  96. p._matches(doc, null, checker);
  97. },
  98. "should follow an array index into the leaves of a subarry, then get the remaining path inside the leaf object": function() {
  99. var p = new ElementPath(),
  100. doc = {"a":[5, [{"b":4}], 3]},
  101. matchItems = [4];
  102. assert.ok( p.init( "a.1.b" ).code, 'OK' );
  103. var checker = function(element) {
  104. assert.deepEqual(element, matchItems.shift());
  105. };
  106. p._matches(doc, null, checker);
  107. }
  108. }
  109. };
  110. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);