ElementPath_test.js 3.8 KB

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