SizeMatchExpression_test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. SizeMatchExpression = require("../../../../lib/pipeline/matcher/SizeMatchExpression.js"),
  6. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails.js");
  7. module.exports = {
  8. "SizeMatchExpression": {
  9. "Should match an element": function() {
  10. var match={"a":[5,6]},
  11. notMatch={"a":[5]},
  12. size = new SizeMatchExpression();
  13. assert.strictEqual(size.init("", 2).code, ErrorCodes.OK);
  14. assert.ok(size.matchesSingleElement(match.a));
  15. assert.ok(!size.matchesSingleElement(notMatch.a));
  16. },
  17. "Should not match non array": function() {
  18. // Non arrays do not match.
  19. var stringValue={"a":"z"},
  20. numberValue={"a":0},
  21. arrayValue={"a":[]},
  22. size = new SizeMatchExpression();
  23. assert.strictEqual(size.init("", 0).code, ErrorCodes.OK);
  24. assert.ok(!size.matchesSingleElement(stringValue.a));
  25. assert.ok(!size.matchesSingleElement(numberValue.a));
  26. assert.ok(size.matchesSingleElement(arrayValue.a));
  27. },
  28. "Should match an array": function() {
  29. var size = new SizeMatchExpression();
  30. assert.strictEqual(size.init("a", 2).code, ErrorCodes.OK);
  31. assert.ok(size.matches({"a":[4, 5.5]}, null));
  32. // Arrays are not unwound to look for matching subarrays.
  33. assert.ok(!size.matches({"a":[4, 5.5, [1,2]]}, null));
  34. },
  35. "Should match a nested array": function() {
  36. var size = new SizeMatchExpression();
  37. assert.strictEqual(size.init("a.2", 2).code, ErrorCodes.OK);
  38. // A numerically referenced nested array is matched.
  39. assert.ok(size.matches({"a":[4, 5.5, [1, 2]]}, null));
  40. },
  41. "ElemMatchKey should return the appropriate results": function() {
  42. var size = new SizeMatchExpression(),
  43. details = new MatchDetails();
  44. assert.strictEqual(size.init("a.b", 3).code, ErrorCodes.OK);
  45. details.requestElemMatchKey();
  46. assert.ok(!size.matches({"a":1}, details));
  47. assert.ok(!details.hasElemMatchKey());
  48. assert.ok(size.matches({"a":{"b":[1, 2, 3]}}, details));
  49. assert.ok(!details.hasElemMatchKey());
  50. assert.ok(size.matches({"a":[2, {"b":[1, 2, 3]}]}, details));
  51. assert.ok(details.hasElemMatchKey());
  52. assert.strictEqual("1", details.elemMatchKey());
  53. },
  54. "Should return equivalency": function() {
  55. var e1 = new SizeMatchExpression(),
  56. e2 = new SizeMatchExpression(),
  57. e3 = new SizeMatchExpression();
  58. e1.init("a", 5);
  59. e2.init("a", 6);
  60. e3.init("v", 5);
  61. assert.ok(e1.equivalent(e1));
  62. assert.ok(!e1.equivalent(e2));
  63. assert.ok(!e1.equivalent(e3));
  64. }
  65. }
  66. };