NotMatchExpression_test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. NotMatchExpression = matcher.NotMatchExpression,
  7. LTMatchExpression = matcher.LTMatchExpression,
  8. MatchDetails = matcher.MatchDetails;
  9. exports.NotMatchExpression = {
  10. "should match a scalar": function() {
  11. var lt = new LTMatchExpression();
  12. assert.strictEqual(lt.init("a", 5).code, ErrorCodes.OK);
  13. var op = new NotMatchExpression();
  14. assert.strictEqual( op.init(lt).code, ErrorCodes.OK);
  15. assert.ok( op.matches({"a":6}), "{$not: {$lt: 5}}, {a:6}" );
  16. assert.ok( !op.matches({"a":4}), "{$not: {$lt: 5}}, {a:4}" );
  17. },
  18. "should match an Array": function() {
  19. var lt = new LTMatchExpression();
  20. assert.strictEqual(lt.init("a",5).code, ErrorCodes.OK);
  21. var op = new NotMatchExpression();
  22. assert.strictEqual(op.init(lt).code, ErrorCodes.OK);
  23. assert.ok( op.matches({"a": [6]}) , "{$not: {$lt: 5}}, {a: [6]}");
  24. assert.ok( !op.matches({"a": [4]}) , "{$not: {$lt: 5}}, {a: [4]}");
  25. assert.ok( !op.matches({"a": [4,5,6]}) , "{$not: {$lt: 5}}, {a: [4,5,6]}");
  26. },
  27. "should not have an ElemMatchKey": function() {
  28. var lt = new LTMatchExpression();
  29. assert.strictEqual(lt.init("a",5).code, ErrorCodes.OK);
  30. var op = new NotMatchExpression();
  31. assert.strictEqual( op.init( lt ).code, ErrorCodes.OK);
  32. var details = new MatchDetails();
  33. details.requestElemMatchKey();
  34. assert.ok( ! op.matches({"a":[1]}, details), "{$not: {a: {$lt : 5}}}, {a: [1]}" );
  35. assert.ok( ! details.hasElemMatchKey() , "ElemMatchKey Check");
  36. assert.ok( op.matches({"a": 6 }, details), "{$not: {a: {$lt : 5}}},{a: 6}");
  37. assert.ok( ! details.hasElemMatchKey(), "ElemMatchKey Check");
  38. assert.ok( op.matches({"a":[6]}, details), "{$not: {a: {$lt : 5}}}, {a:[6]}");
  39. assert.ok( ! details.hasElemMatchKey() );
  40. },
  41. };