InMatchExpression.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. var assert = require("assert"),
  3. MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails'),
  4. InMatchExpression = require("../../../../lib/pipeline/matcher/InMatchExpression");
  5. module.exports = {
  6. "InMatchExpression": {
  7. "should match a single element": function (){
  8. var e = new InMatchExpression();
  9. var s = e.init('a');
  10. assert.strictEqual( s.code,'OK' );
  11. e._arrayEntries._equalities = [1];
  12. assert.ok( e.matchesSingleElement(1) );
  13. assert.ok( ! e.matchesSingleElement(2) );
  14. },
  15. "should not match with an empty array": function() {
  16. var e = new InMatchExpression();
  17. var s = e.init('a');
  18. assert.strictEqual( s.code,'OK' );
  19. e._arrayEntries._equalities = [];
  20. assert.ok( ! e.matchesSingleElement(2) );
  21. assert.ok( ! e.matches({'a':null}) );
  22. assert.ok( ! e.matches({'a':1}) );
  23. },
  24. "should match with multiple elements": function() {
  25. var e = new InMatchExpression();
  26. var s = e.init('a');
  27. assert.strictEqual( s.code,'OK' );
  28. e._arrayEntries._equalities = [1,'r',true,1];
  29. assert.ok( e.matchesSingleElement( 1 ) );
  30. assert.ok( e.matchesSingleElement( 'r' ) );
  31. assert.ok( e.matchesSingleElement( true ) );
  32. assert.ok( !e.matchesSingleElement( false ) );
  33. },
  34. "should match a scalar":function() {
  35. var e = new InMatchExpression();
  36. var s = e.init('a');
  37. assert.strictEqual( s.code,'OK' );
  38. e._arrayEntries._equalities = [5];
  39. assert.ok( e.matches({'a':5}) );
  40. assert.ok( ! e.matches({'a':4}) );
  41. },
  42. "should match an array":function() {
  43. var e = new InMatchExpression();
  44. var s = e.init('a');
  45. assert.strictEqual( s.code,'OK' );
  46. e._arrayEntries._equalities = [5];
  47. assert.ok( e.matches({'a':[5,6]}) );
  48. assert.ok( ! e.matches({'a':[6,7]}) );
  49. assert.ok( ! e.matches({'a':[[5]]}) );
  50. },
  51. "should match null": function() {
  52. var e = new InMatchExpression();
  53. var s = e.init('a');
  54. assert.strictEqual( s.code,'OK' );
  55. e._arrayEntries._equalities = [null];
  56. assert.ok( e.matches({}) );
  57. assert.ok( e.matches({'a':null}) );
  58. assert.ok( ! e.matches({'a':4}) );
  59. },
  60. /*"should match MinKey": function() {
  61. var e = new InMatchExpression();
  62. var s = e.init('a');
  63. var fakeCon = {'name':'MinKey'}, minkey = {}, maxkey = {};
  64. minkey.contructor = fakeCon;
  65. minkey.constructor.name='MinKey';
  66. maxkey.constructor = fakeCon;
  67. maxkey.constructor.name = 'MaxKey';
  68. assert.strictEqual( s.code,'OK' );
  69. e._arrayEntries._equalities = [minkey];
  70. assert.ok( e.matches({'a':minkey}) );
  71. assert.ok( ! e.matches({'a':maxkey}) );
  72. assert.ok( ! e.matches({'a':4}) );
  73. },
  74. "should match MaxKey": function() {
  75. var e = new InMatchExpression();
  76. var s = e.init('a');
  77. var minkey = {}, maxkey = {};
  78. minkey.contructor = {};
  79. minkey.constructor.name='MinKey';
  80. maxkey.constructor = {};
  81. maxkey.constructor.name = 'MaxKey';
  82. assert.strictEqual( s.code,'OK' );
  83. e._arrayEntries._equalities = [minkey];
  84. assert.ok( ! e.matches({'a':minkey}) );
  85. assert.ok( e.matches({'a':maxkey}) );
  86. assert.ok( ! e.matches({'a':4}) );
  87. },*/
  88. "should match a full array":function() {
  89. var e = new InMatchExpression();
  90. var s = e.init('a');
  91. assert.strictEqual( s.code,'OK' );
  92. e._arrayEntries._equalities = [[1,2],4,5];
  93. assert.ok( e.matches({'a':[1,2]}) );
  94. assert.ok( ! e.matches({'a':[1,2,3]}) );
  95. assert.ok( ! e.matches({'a':[1]}) );
  96. assert.ok( ! e.matches({'a':1}) );
  97. },
  98. "should match elemmatchKey": function() {
  99. var e = new InMatchExpression();
  100. var s = e.init('a');
  101. var m = new MatchDetails();
  102. assert.strictEqual( s.code,'OK' );
  103. e._arrayEntries._equalities = [5,2];
  104. m.requestElemMatchKey();
  105. assert.ok( !e.matches({'a':4}, m) );
  106. assert.ok( !m.hasElemMatchKey() );
  107. assert.ok( e.matches({'a':5}, m) );
  108. assert.ok( !m.hasElemMatchKey() );
  109. assert.ok( e.matches({'a':[1,2,5]}, m ));
  110. assert.ok( m.hasElemMatchKey() );
  111. assert.strictEqual( m.elemMatchKey(), '1' );
  112. }
  113. }
  114. };
  115. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);