AndMatchExpression_test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. matcher = require("../../../lib/matcher/"),
  6. AndMatchExpression = matcher.AndMatchExpression,
  7. EqualityMatchExpression = matcher.EqualityMatchExpression,
  8. GTMatchExpression = matcher.GTMatchExpression,
  9. LTMatchExpression = matcher.LTMatchExpression,
  10. MatchDetails = matcher.MatchDetails,
  11. NotMatchExpression = matcher.NotMatchExpression,
  12. RegexMatchExpression = matcher.RegexMatchExpression;
  13. exports.AndMatchExpression = {
  14. "should match nothing with no clauses": function () {
  15. var op = new AndMatchExpression();
  16. assert.ok( op.matches({}));
  17. },
  18. "should match with a three element clause": function() {
  19. var lt = new LTMatchExpression();
  20. var gt = new GTMatchExpression();
  21. var rgx = new RegexMatchExpression();
  22. var op = new AndMatchExpression();
  23. assert.strictEqual(lt.init("a", "z1").code, ErrorCodes.OK);
  24. assert.strictEqual(gt.init("a", "a1").code, ErrorCodes.OK);
  25. assert.strictEqual(rgx.init("a", "1", "").code, ErrorCodes.OK);
  26. op.add(lt);
  27. op.add(gt);
  28. op.add(rgx);
  29. assert(op.matches({a:"r1"}));
  30. assert(!op.matches({a:"z1"}));
  31. assert(!op.matches({a:"a1"}));
  32. assert(!op.matches({a:"r"}));
  33. },
  34. "should match a single clause": function() {
  35. var nop = new NotMatchExpression();
  36. var eq = new EqualityMatchExpression();
  37. var op = new AndMatchExpression();
  38. assert.strictEqual(eq.init("a", 5).code, ErrorCodes.OK);
  39. assert.strictEqual(nop.init(eq).code, ErrorCodes.OK);
  40. op.add(nop);
  41. assert(op.matches({a:4}));
  42. assert(op.matches({a:[4,6]}));
  43. assert(!op.matches({a:5}));
  44. assert(!op.matches({a:[4,5]}));
  45. },
  46. "should match three clauses": function() {
  47. var baseOperand1 = {"$gt":1},
  48. baseOperand2 = {"$lt":10},
  49. baseOperand3 = {"$lt":100},
  50. sub1 = new GTMatchExpression(),
  51. sub2 = new LTMatchExpression(),
  52. sub3 = new LTMatchExpression(),
  53. andOp = new AndMatchExpression();
  54. assert.strictEqual(sub1.init("a", baseOperand1.$gt).code, ErrorCodes.OK);
  55. assert.strictEqual(sub2.init("a", baseOperand2.$lt).code, ErrorCodes.OK);
  56. assert.strictEqual(sub3.init("b", baseOperand3.$lt).code, ErrorCodes.OK);
  57. andOp.add(sub1);
  58. andOp.add(sub2);
  59. andOp.add(sub3);
  60. assert.ok(andOp.matchesJSON({"a":5, "b":6}, null));
  61. assert.ok(!andOp.matchesJSON({"a":5}, null));
  62. assert.ok(!andOp.matchesJSON({"b":6}, null ));
  63. assert.ok(!andOp.matchesJSON({"a":1, "b":6}, null));
  64. assert.ok(!andOp.matchesJSON({"a":10, "b":6}, null));
  65. },
  66. "should have an elemMatchKey": function() {
  67. var baseOperand1 = {"a":1},
  68. baseOperand2 = {"b":2},
  69. sub1 = new EqualityMatchExpression(),
  70. sub2 = new EqualityMatchExpression(),
  71. andOp = new AndMatchExpression(),
  72. details = new MatchDetails();
  73. assert.strictEqual(sub1.init("a", baseOperand1.a).code, ErrorCodes.OK);
  74. assert.strictEqual(sub2.init("b", baseOperand2.b).code, ErrorCodes.OK);
  75. andOp.add(sub1);
  76. andOp.add(sub2);
  77. details.requestElemMatchKey();
  78. assert.ok(!andOp.matchesJSON({"a":[1]}, details));
  79. assert.ok(!details.hasElemMatchKey());
  80. assert.ok(!andOp.matchesJSON({"b":[2]}, details));
  81. assert.ok(!details.hasElemMatchKey());
  82. assert.ok(andOp.matchesJSON({"a":[1], "b":[1, 2]}, details));
  83. assert.ok(details.hasElemMatchKey());
  84. // The elem match key for the second $and clause is recorded.
  85. assert.strictEqual("1", details.elemMatchKey());
  86. },
  87. };