AndMatchExpression.js 3.6 KB

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