RegexMatchExpression_test.js 7.7 KB

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