LTMatchExpression.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. var assert = require("assert"),
  3. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails"),
  4. LTMatchExpression = require("../../../../lib/pipeline/matcher/LTMatchExpression");
  5. module.exports = {
  6. "LTMatchExpression": {
  7. "should match scalars and strings properly": function (){
  8. var e = new LTMatchExpression();
  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 LTMatchExpression();
  18. var s = e.init("",{});
  19. assert.strictEqual(s.code, "BAD_VALUE");
  20. },
  21. "should match a pathed number":function() {
  22. var e = new LTMatchExpression();
  23. var s = e.init("a",5);
  24. assert.strictEqual(s.code, "OK");
  25. assert.ok( e.matches({"a":4.5}) );
  26. assert.ok( ! e.matches({"a":6}) );
  27. },
  28. "should match an empty pathed number":function() {
  29. var e = new LTMatchExpression();
  30. var s = e.init("",5);
  31. assert.strictEqual(s.code, "OK");
  32. assert.ok( e.matches({"":4.5}) );
  33. assert.ok( ! e.matches({"":6}) );
  34. },
  35. "should match stuff in an array": function() {
  36. var e = new LTMatchExpression();
  37. var s = e.init("a",5);
  38. assert.strictEqual(s.code, "OK");
  39. assert.ok( e.matches({"a":[6,4.5]}) );
  40. assert.ok( ! e.matches({"a":[6,7]}) );
  41. },
  42. "should not match full array" : function() {
  43. var e = new LTMatchExpression();
  44. var s = e.init("a",[5]);
  45. assert.strictEqual(s.code, "OK");
  46. assert.ok( e.matches({"a":[4]}) );
  47. },
  48. "should not match null" : function() {
  49. var e = new LTMatchExpression();
  50. var s = e.init("a",null);
  51. assert.strictEqual(s.code, "OK");
  52. assert.ok( ! e.matches({}) );
  53. assert.ok( ! e.matches({"a":null}) );
  54. assert.ok( ! e.matches({"a":4}) );
  55. },
  56. "should handle elemMatchKey":function() {
  57. var e = new LTMatchExpression();
  58. var s = e.init("a",5);
  59. var m = new MatchDetails();
  60. m.requestElemMatchKey();
  61. assert.strictEqual( s.code, "OK" );
  62. assert.ok( ! e.matches({"a":6}, m) );
  63. assert.ok( ! m.hasElemMatchKey() );
  64. assert.ok( e.matches({"a":4}, m) );
  65. assert.ok( ! m.hasElemMatchKey() );
  66. assert.ok( e.matches({"a":[6,2,5]}, m));
  67. assert.ok( m.hasElemMatchKey());
  68. assert.strictEqual("1", m.elemMatchKey());
  69. }
  70. }
  71. };
  72. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);