ExistsMatchExpression_test.js 2.1 KB

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