SizeMatchExpression.js 2.5 KB

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