RegexMatchExpression.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. "use strict";
  2. var assert = require("assert"),
  3. XRegExp = require("xregexp").XRegExp,
  4. RegexMatchExpression = require("../../../../lib/pipeline/matcher/RegexMatchExpression"),
  5. MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails");
  6. module.exports = {
  7. "RegexMatchExpression": {
  8. "should match an exact element": function(){
  9. var match = {"a":"b"},
  10. notMatch = {"a":"c"};
  11. var regex = new RegexMatchExpression();
  12. assert.strictEqual(regex.init("", "b", "").code, 'OK');
  13. assert(regex.matchesSingleElement(match.a));
  14. assert(!regex.matchesSingleElement(notMatch.a));
  15. },
  16. "should error if the pattern is too large": function(){
  17. var tooLargePattern = "";
  18. for (var i = 0; i<32765; i++){tooLargePattern += '3';}
  19. var regex = new RegexMatchExpression();
  20. assert(regex.init("a", tooLargePattern, "").code !== 'OK');
  21. },
  22. "should match an element with a simple prefix": function(){
  23. var match = {"x":"abc"},
  24. notMatch = {"x":"adz"};
  25. var regex = new RegexMatchExpression();
  26. assert.strictEqual(regex.init("", "^ab", "").code, 'OK');
  27. assert(regex.matchesSingleElement(match.x));
  28. assert(!regex.matchesSingleElement(notMatch.x));
  29. },
  30. "should match an element case sensitive": function(){
  31. var match = {"x":"abc"},
  32. notMatch = {"x":"ABC"};
  33. var regex = new RegexMatchExpression();
  34. assert.strictEqual(regex.init("", "abc", "").code, 'OK');
  35. assert(regex.matchesSingleElement(match.x));
  36. assert(!regex.matchesSingleElement(notMatch.x));
  37. },
  38. "should match an element case insensitive": function(){
  39. var match = {"x":"abc"},
  40. matchUppercase = {"x":"ABC"},
  41. notMatch = {"x":"adz"};
  42. var regex = new RegexMatchExpression();
  43. assert.strictEqual(regex.init("", "abc", "i").code, 'OK');
  44. assert(regex.matchesSingleElement(match.x));
  45. assert(regex.matchesSingleElement(matchUppercase.x));
  46. assert(!regex.matchesSingleElement(notMatch.x));
  47. },
  48. "should match an element multiline off": function(){
  49. var match = {"x":"az"},
  50. notMatch = {"x":"\naz"};
  51. var regex = new RegexMatchExpression();
  52. assert.strictEqual(regex.init("", "^a", "").code, 'OK');
  53. assert(regex.matchesSingleElement(match.x));
  54. assert(!regex.matchesSingleElement(notMatch.x));
  55. },
  56. "should match an element multiline on": function(){
  57. var match = {"x":"az"},
  58. matchMultiline = {"x":"\naz"},
  59. notMatch = {"x":"\n\n"};
  60. var regex = new RegexMatchExpression();
  61. assert.strictEqual(regex.init("", "^a", "m").code, 'OK');
  62. assert(regex.matchesSingleElement(match.x));
  63. assert(regex.matchesSingleElement(matchMultiline.x));
  64. assert(!regex.matchesSingleElement(notMatch.x));
  65. },
  66. "should match an element with extended off": function(){
  67. var match = {"x":"a b"},
  68. notMatch = {"x":"ab"};
  69. var regex = new RegexMatchExpression();
  70. assert.strictEqual(regex.init("", "a b", "").code, 'OK');
  71. assert(regex.matchesSingleElement(match.x));
  72. assert(!regex.matchesSingleElement(notMatch.x));
  73. },
  74. "should match an element with extended on": function(){
  75. var match = {"x":"ab"},
  76. notMatch = {"x":"a b"};
  77. var regex = new RegexMatchExpression();
  78. assert.strictEqual(regex.init("", "a b", "x").code, 'OK');
  79. assert(regex.matchesSingleElement(match.x));
  80. assert(!regex.matchesSingleElement(notMatch.x));
  81. },
  82. "should match an element with dot matches all off": function(){
  83. var match = {"x":"a b"},
  84. notMatch = {"x":"a\nb"};
  85. var regex = new RegexMatchExpression();
  86. assert.strictEqual(regex.init("", "a.b", "").code, 'OK');
  87. assert(regex.matchesSingleElement(match.x));
  88. assert(!regex.matchesSingleElement(notMatch.x));
  89. },
  90. "should match an element with dot matches all on": function(){
  91. var match = {"x":"a b"},
  92. matchDotAll = {"x":"a\nb"},
  93. notMatch = {"x":"ab"};
  94. var regex = new RegexMatchExpression();
  95. assert.strictEqual(regex.init("", "a.b", "s").code, 'OK');
  96. assert(regex.matchesSingleElement(match.x));
  97. assert(regex.matchesSingleElement(matchDotAll.x));
  98. assert(!regex.matchesSingleElement(notMatch.x));
  99. },
  100. "should match an element with multiple flags": function(){
  101. var match = {"x":"\na\nb"};
  102. var regex = new RegexMatchExpression();
  103. assert.strictEqual(regex.init("", "^a.b", "ms").code, 'OK');
  104. assert(regex.matchesSingleElement(match.x));
  105. },
  106. "should match an element with type regex": function(){
  107. var match = {"x":new XRegExp('yz', 'i')},
  108. notMatchPattern = {"x":new XRegExp('r', 'i')},
  109. notMatchFlags = {"x":new XRegExp('yz', 's')};
  110. var regex = new RegexMatchExpression();
  111. assert.strictEqual(regex.init("", "yz", "i").code, 'OK');
  112. assert(regex.matchesSingleElement(match.x));
  113. assert(!regex.matchesSingleElement(notMatchPattern.x));
  114. assert(!regex.matchesSingleElement(notMatchFlags.x));
  115. },
  116. //skipped as we don't support symbols yet
  117. /*
  118. TEST( RegexMatchExpression, MatchesElementSymbolType ) {
  119. BSONObj match = BSONObjBuilder().appendSymbol( "x", "yz" ).obj();
  120. BSONObj notMatch = BSONObjBuilder().appendSymbol( "x", "gg" ).obj();
  121. RegexMatchExpression regex;
  122. ASSERT( regex.init( "", "yz", "" ).isOK() );
  123. ASSERT( regex.matchesSingleElement( match.firstElement() ) );
  124. ASSERT( !regex.matchesSingleElement( notMatch.firstElement() ) );
  125. }
  126. */
  127. "should not match an element with the wrong type": function(){
  128. var notMatchInt = {"x":1},
  129. notMatchBool = {"x":true};
  130. var regex = new RegexMatchExpression();
  131. assert.strictEqual(regex.init("", "1", "").code, 'OK');
  132. assert(!regex.matchesSingleElement(notMatchInt.x));
  133. assert(!regex.matchesSingleElement(notMatchBool.x));
  134. },
  135. "should match an element that is Utf8": function(){
  136. var matches = {"x":"\u03A9"};
  137. var regex = new RegexMatchExpression();
  138. assert.strictEqual(regex.init("", "^.*", "").code, 'OK');
  139. assert(regex.matchesSingleElement(matches.x));
  140. },
  141. "should match an element that is scalar": function(){
  142. var regex = new RegexMatchExpression();
  143. assert.strictEqual(regex.init("a", "b", "").code, 'OK');
  144. assert(regex.matches({"a":"b"}));
  145. assert(!regex.matches({"a":"c"}));
  146. },
  147. "should match an array value": function(){
  148. var regex = new RegexMatchExpression();
  149. assert.strictEqual(regex.init("a", "b", "").code, 'OK');
  150. assert(regex.matches({"a":["c","b"]}));
  151. assert(!regex.matches({"a":["d","c"]}));
  152. },
  153. "should match null": function(){
  154. var regex = new RegexMatchExpression();
  155. assert.strictEqual(regex.init("a", "b", "").code, 'OK');
  156. assert(!regex.matches({}));
  157. assert(!regex.matches({"a":null}));
  158. },
  159. "should match element keys": function(){
  160. var regex = new RegexMatchExpression();
  161. assert.strictEqual(regex.init("a", "b", "").code, 'OK');
  162. var details = new MatchDetails();
  163. details.requestElemMatchKey();
  164. assert(!regex.matches({"a":"c"}, details));
  165. assert(!details.hasElemMatchKey());
  166. assert(regex.matches({"a":"b"}, details));
  167. assert(!details.hasElemMatchKey());
  168. assert(regex.matches({"a":["c", "b"]}, details));
  169. assert(details.hasElemMatchKey());
  170. assert.strictEqual(details.elemMatchKey(), "1");
  171. },
  172. "should determine equivalency": function(){
  173. var r1 = new RegexMatchExpression(),
  174. r2 = new RegexMatchExpression(),
  175. r3 = new RegexMatchExpression(),
  176. r4 = new RegexMatchExpression();
  177. assert.strictEqual(r1.init("a", "b", "").code, 'OK');
  178. assert.strictEqual(r2.init("a", "b", "x").code, 'OK');
  179. assert.strictEqual(r3.init("a", "c", "").code, 'OK');
  180. assert.strictEqual(r4.init("b", "b", "").code, 'OK');
  181. assert(r1.equivalent(r1));
  182. assert(!r1.equivalent(r2));
  183. assert(!r1.equivalent(r3));
  184. assert(!r1.equivalent(r4));
  185. },
  186. }
  187. };
  188. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);