ExistsMatchExpression_test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. ExistsMatchExpression = require("../../../../lib/pipeline/matcher/ExistsMatchExpression"),
  6. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails");
  7. module.exports = {
  8. "ExistsMatchExpression": {
  9. "should match an element": function (){
  10. var e = new ExistsMatchExpression();
  11. var s = e.init("a");
  12. assert.strictEqual(s.code, ErrorCodes.OK);
  13. assert.ok( e.matches({"a":5}) );
  14. assert.ok( e.matches({"a":null}) );
  15. assert.ok( ! e.matches({"a":{}}) );
  16. },
  17. "should match a boolean":function() {
  18. var e = new ExistsMatchExpression();
  19. var s = e.init("a");
  20. assert.strictEqual( s.code, ErrorCodes.OK);
  21. assert.ok( e.matches({"a":5}) );
  22. assert.ok( ! e.matches({}) );
  23. },
  24. "should match a number":function() {
  25. var e = new ExistsMatchExpression();
  26. var s = e.init("a");
  27. assert.strictEqual( s.code, ErrorCodes.OK);
  28. assert.ok( e.matches({"a":1}) );
  29. assert.ok( e.matches({"a":null}) );
  30. assert.ok( ! e.matches({"b":1}) );
  31. },
  32. "should match an array":function() {
  33. var e = new ExistsMatchExpression();
  34. var s = e.init("a");
  35. assert.strictEqual( s.code, ErrorCodes.OK);
  36. assert.ok( e.matches({"a":[4,5.5]}) );
  37. },
  38. "should yield an elemMatchKey":function() {
  39. var e = new ExistsMatchExpression();
  40. var s = e.init("a.b");
  41. var m = new MatchDetails();
  42. m.requestElemMatchKey();
  43. assert.strictEqual( s.code, ErrorCodes.OK);
  44. assert.ok( ! e.matches({"a":1}, m) );
  45. assert.ok( ! m.hasElemMatchKey() );
  46. assert.ok( e.matches({"a":{"b":6}}));
  47. assert.ok( ! m.hasElemMatchKey() );
  48. assert.ok( e.matches({"a":[2, {"b":7}]}, m) );
  49. assert.ok( m.hasElemMatchKey() );
  50. assert.strictEqual("1", m.elemMatchKey() );
  51. },
  52. "should handle equivalence":function() {
  53. var e = new ExistsMatchExpression();
  54. var s = e.init("a");
  55. var b = new ExistsMatchExpression();
  56. assert.strictEqual( s.code, ErrorCodes.OK);
  57. s = b.init("b");
  58. assert.strictEqual(s.code, ErrorCodes.OK);
  59. assert.ok( e.equivalent(e) );
  60. assert.ok( ! e.equivalent(b) );
  61. }
  62. }
  63. };