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