| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- "use strict";
- if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
- var assert = require("assert"),
- ErrorCodes = require("../../../lib/errors").ErrorCodes,
- matcher = require("../../../lib/matcher/"),
- AndMatchExpression = matcher.AndMatchExpression,
- EqualityMatchExpression = matcher.EqualityMatchExpression,
- GTMatchExpression = matcher.GTMatchExpression,
- LTMatchExpression = matcher.LTMatchExpression,
- MatchDetails = matcher.MatchDetails,
- NotMatchExpression = matcher.NotMatchExpression,
- RegexMatchExpression = matcher.RegexMatchExpression;
- exports.AndMatchExpression = {
- "should match nothing with no clauses": function () {
- var op = new AndMatchExpression();
- assert.ok( op.matches({}));
- },
- "should match with a three element clause": function() {
- var lt = new LTMatchExpression();
- var gt = new GTMatchExpression();
- var rgx = new RegexMatchExpression();
- var op = new AndMatchExpression();
- assert.strictEqual(lt.init("a", "z1").code, ErrorCodes.OK);
- assert.strictEqual(gt.init("a", "a1").code, ErrorCodes.OK);
- assert.strictEqual(rgx.init("a", "1", "").code, ErrorCodes.OK);
- op.add(lt);
- op.add(gt);
- op.add(rgx);
- assert(op.matches({a:"r1"}));
- assert(!op.matches({a:"z1"}));
- assert(!op.matches({a:"a1"}));
- assert(!op.matches({a:"r"}));
- },
- "should match a single clause": function() {
- var nop = new NotMatchExpression();
- var eq = new EqualityMatchExpression();
- var op = new AndMatchExpression();
- assert.strictEqual(eq.init("a", 5).code, ErrorCodes.OK);
- assert.strictEqual(nop.init(eq).code, ErrorCodes.OK);
- op.add(nop);
- assert(op.matches({a:4}));
- assert(op.matches({a:[4,6]}));
- assert(!op.matches({a:5}));
- assert(!op.matches({a:[4,5]}));
- },
- "should match three clauses": function() {
- var baseOperand1 = {"$gt":1},
- baseOperand2 = {"$lt":10},
- baseOperand3 = {"$lt":100},
- sub1 = new GTMatchExpression(),
- sub2 = new LTMatchExpression(),
- sub3 = new LTMatchExpression(),
- andOp = new AndMatchExpression();
- assert.strictEqual(sub1.init("a", baseOperand1.$gt).code, ErrorCodes.OK);
- assert.strictEqual(sub2.init("a", baseOperand2.$lt).code, ErrorCodes.OK);
- assert.strictEqual(sub3.init("b", baseOperand3.$lt).code, ErrorCodes.OK);
- andOp.add(sub1);
- andOp.add(sub2);
- andOp.add(sub3);
- assert.ok(andOp.matchesJSON({"a":5, "b":6}, null));
- assert.ok(!andOp.matchesJSON({"a":5}, null));
- assert.ok(!andOp.matchesJSON({"b":6}, null ));
- assert.ok(!andOp.matchesJSON({"a":1, "b":6}, null));
- assert.ok(!andOp.matchesJSON({"a":10, "b":6}, null));
- },
- "should have an elemMatchKey": function() {
- var baseOperand1 = {"a":1},
- baseOperand2 = {"b":2},
- sub1 = new EqualityMatchExpression(),
- sub2 = new EqualityMatchExpression(),
- andOp = new AndMatchExpression(),
- details = new MatchDetails();
- assert.strictEqual(sub1.init("a", baseOperand1.a).code, ErrorCodes.OK);
- assert.strictEqual(sub2.init("b", baseOperand2.b).code, ErrorCodes.OK);
- andOp.add(sub1);
- andOp.add(sub2);
- details.requestElemMatchKey();
- assert.ok(!andOp.matchesJSON({"a":[1]}, details));
- assert.ok(!details.hasElemMatchKey());
- assert.ok(!andOp.matchesJSON({"b":[2]}, details));
- assert.ok(!details.hasElemMatchKey());
- assert.ok(andOp.matchesJSON({"a":[1], "b":[1, 2]}, details));
- assert.ok(details.hasElemMatchKey());
- // The elem match key for the second $and clause is recorded.
- assert.strictEqual("1", details.elemMatchKey());
- },
- };
|