NorMatchExpression.js 3.5 KB

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