EqualityMatchExpression.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. var assert = require("assert"),
  3. MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails'),
  4. EqualityMatchExpression = require("../../../../lib/pipeline/matcher/EqualityMatchExpression");
  5. module.exports = {
  6. "EqualityMatchExpression": {
  7. "should initialize equality and match numbers or numbers in arrays": function (){
  8. var e = new EqualityMatchExpression();
  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:[5]}));
  13. assert.ok(e.matches({x:[1,5]}));
  14. assert.ok(e.matches({x:[1,5,2]}));
  15. assert.ok(e.matches({x:[5,2]}));
  16. assert.ok(!e.matches({x:null}));
  17. assert.ok(!e.matches({x:6}));
  18. assert.ok(!e.matches({x:[4,2]}));
  19. assert.ok(!e.matches({x:[[5]]}));
  20. },
  21. //NOTE: from expression_leaf_test.cpp
  22. "should match elements": function testMatchesElement(){
  23. var operand = {a:5},
  24. match = {a:5.0},
  25. notMatch = {a:6};
  26. var eq = new EqualityMatchExpression();
  27. eq.init("a", operand.a);
  28. assert.ok(eq.matches(match));
  29. assert.ok(!eq.matches(notMatch));
  30. assert.ok(eq.equivalent(eq));
  31. },
  32. "should handle invalid End of Object Operand": function testInvalidEooOperand(){
  33. var e = new EqualityMatchExpression();
  34. var s = e.init('',{});
  35. assert.strictEqual(s.code, 'BAD_VALUE');
  36. },
  37. "should match a pathed number":function() {
  38. var e = new EqualityMatchExpression();
  39. var s = e.init('a',5);
  40. assert.strictEqual(s.code, 'OK');
  41. assert.ok( e.matches({'a':5}) );
  42. assert.ok( ! e.matches({'a':4}) );
  43. },
  44. "should match stuff in an array": function() {
  45. var e = new EqualityMatchExpression();
  46. var s = e.init('a',5);
  47. assert.strictEqual(s.code, 'OK');
  48. assert.ok( e.matches({'a':[5,6]}) );
  49. assert.ok( ! e.matches({'a':[6,7]}) );
  50. },
  51. "should match on a longer path": function() {
  52. var e = new EqualityMatchExpression();
  53. var s = e.init('a.b',5);
  54. assert.strictEqual(s.code, 'OK');
  55. assert.ok( e.matches({'a':{'b':5}}) );
  56. assert.ok( e.matches({'a':{'b':[5]}}) );
  57. assert.ok( e.matches({'a':[{'b':5}]}) );
  58. },
  59. "should match in an array": function() {
  60. var e = new EqualityMatchExpression();
  61. var s = e.init('a.0',5);
  62. assert.strictEqual(s.code, 'OK');
  63. assert.ok( e.matches({'a':[5]}) );
  64. assert.ok( ! e.matches({'a':[[5]]}) );
  65. },
  66. "should match null" : function() {
  67. var e = new EqualityMatchExpression();
  68. var s = e.init('a',null);
  69. assert.strictEqual(s.code, 'OK');
  70. assert.ok( e.matches({}) );
  71. assert.ok( e.matches({'a':null}) );
  72. assert.ok( ! e.matches({'a':4}) );
  73. },
  74. "should match full array" : function() {
  75. var e = new EqualityMatchExpression();
  76. var s = e.init('a',[1,2]);
  77. assert.strictEqual(s.code, 'OK');
  78. assert.ok( e.matches({'a':[1,2]}) );
  79. assert.ok( ! e.matches({'a':[1,2,3]}) );
  80. assert.ok( ! e.matches({'a':[1]}) );
  81. assert.ok( ! e.matches({'a':1}) );
  82. },
  83. "should match a nested array": function() {
  84. var e = new EqualityMatchExpression();
  85. var s = e.init('a.b.c.d',3);
  86. assert.strictEqual(s.code, 'OK');
  87. assert.ok( e.matches({a:{b:[{c:[{d:1},{d:2}]},{c:[{d:3}]}]}}) );
  88. },
  89. "should handle elemMatchKey":function() {
  90. var e = new EqualityMatchExpression();
  91. var s = e.init('a', 5);
  92. var m = new MatchDetails();
  93. m.requestElemMatchKey();
  94. assert.strictEqual( s.code, 'OK' );
  95. assert.ok( ! e.matches({'a':4}, m) );
  96. assert.ok( ! m.hasElemMatchKey() );
  97. assert.ok( e.matches({'a':5}, m) );
  98. assert.ok( ! m.hasElemMatchKey() );
  99. assert.ok( e.matches({'a':[1,2,5]}, m));
  100. assert.ok( m.hasElemMatchKey());
  101. assert.strictEqual('2', m.elemMatchKey());
  102. },
  103. "should handle equivalence":function() {
  104. var a = new EqualityMatchExpression();
  105. var b = new EqualityMatchExpression();
  106. var c = new EqualityMatchExpression();
  107. assert.strictEqual( a.init('a',5).code, 'OK' );
  108. assert.strictEqual( b.init('a',5).code, 'OK' );
  109. assert.strictEqual( c.init('c',4).code, 'OK' );
  110. assert.ok( a.equivalent(a) );
  111. assert.ok( a.equivalent(b) );
  112. assert.ok( ! a.equivalent(c) );
  113. }
  114. }
  115. };
  116. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);