GTMatchExpression.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. var assert = require("assert"),
  3. MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails'),
  4. GTMatchExpression = require("../../../../lib/pipeline/matcher/GTMatchExpression");
  5. module.exports = {
  6. "GTMatchExpression": {
  7. "should match scalars and strings properly": function (){
  8. var e = new GTMatchExpression();
  9. var s = e.init('x',5);
  10. assert.strictEqual(s.code, 'OK');
  11. assert.ok( ! e.matches({'x':5}) );
  12. assert.ok( ! e.matches({'x':4}) );
  13. assert.ok( e.matches({'x':6}) );
  14. assert.ok( ! e.matches({'x': 'eliot'}) );
  15. },
  16. "should handle invalid End of Object Operand": function testInvalidEooOperand(){
  17. var e = new GTMatchExpression();
  18. var s = e.init('',{});
  19. assert.strictEqual(s.code, 'BAD_VALUE');
  20. },
  21. "should match a pathed number":function() {
  22. var e = new GTMatchExpression();
  23. var s = e.init('a',5);
  24. assert.strictEqual(s.code, 'OK');
  25. assert.ok( e.matches({'a':5.5}) );
  26. assert.ok( ! e.matches({'a':4}) );
  27. },
  28. "should match stuff in an array": function() {
  29. var e = new GTMatchExpression();
  30. var s = e.init('a',5);
  31. assert.strictEqual(s.code, 'OK');
  32. assert.ok( e.matches({'a':[3,5.5]}) );
  33. assert.ok( ! e.matches({'a':[2,4]}) );
  34. },
  35. "should not match full array" : function() {
  36. var e = new GTMatchExpression();
  37. var s = e.init('a',[5]);
  38. assert.strictEqual(s.code, 'OK');
  39. assert.ok( ! e.matches({'a':[6]}) );
  40. },
  41. "should not match null" : function() {
  42. var e = new GTMatchExpression();
  43. var s = e.init('a',null);
  44. assert.strictEqual(s.code, 'OK');
  45. assert.ok( !e.matches({}) );
  46. assert.ok( !e.matches({'a':null}) );
  47. assert.ok( ! e.matches({'a':4}) );
  48. },
  49. "should handle elemMatchKey":function() {
  50. var e = new GTMatchExpression();
  51. var s = e.init('a',5);
  52. var m = new MatchDetails();
  53. m.requestElemMatchKey();
  54. assert.strictEqual( s.code, 'OK' );
  55. assert.ok( ! e.matches({'a':4}, m) );
  56. assert.ok( ! m.hasElemMatchKey() );
  57. assert.ok( e.matches({'a':6}, m) );
  58. assert.ok( ! m.hasElemMatchKey() );
  59. assert.ok( e.matches({'a':[2,6,5]}, m));
  60. assert.ok( m.hasElemMatchKey());
  61. assert.strictEqual('1', m.elemMatchKey());
  62. }
  63. }
  64. };
  65. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);