ExistsMatchExpression.js 2.1 KB

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