Skip to content
This repository
Browse code

resolving issue that was identified via pivotal/jasmine#199 where Reg…

…Exp objects were not properly compared resulting in non-matching RegExp objects to always return true. a patch to jasmine.Env.equals_ adds an extra step for RexExp objects to be compared.
  • Loading branch information...
commit d65bdc7e5989adfef195ea9439b1086837faa85a 1 parent a1ed567
Joost Elfering authored June 04, 2012
20  lib/jasmine-core/jasmine.js
@@ -874,6 +874,22 @@ jasmine.Env.prototype.xit = function(desc, func) {
874 874
   };
875 875
 };
876 876
 
  877
+jasmine.Env.prototype.compareRegExps_ = function(a, b, mismatchKeys, mismatchValues) {
  878
+  if (a.source != b.source)
  879
+    mismatchValues.push("expected pattern /" + b.source + "/ is not equal to the pattern /" + a.source + "/");
  880
+
  881
+  if (a.ignoreCase != b.ignoreCase)
  882
+    mismatchValues.push("expected modifier i was" + (b.ignoreCase ? " " : " not ") + "set and does not equal the origin modifier");
  883
+
  884
+  if (a.global != b.global)
  885
+    mismatchValues.push("expected modifier g was" + (b.global ? " " : " not ") + "set and does not equal the origin modifier");
  886
+
  887
+  if (a.multiline != b.multiline)
  888
+    mismatchValues.push("expected modifier m was" + (b.multiline ? " " : " not ") + "set and does not equal the origin modifier");
  889
+
  890
+  return (mismatchValues.length === 0);
  891
+};
  892
+
877 893
 jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) {
878 894
   if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) {
879 895
     return true;
@@ -960,6 +976,10 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) {
960 976
     return (a == b);
961 977
   }
962 978
 
  979
+  if (a instanceof RegExp && b instanceof RegExp) {
  980
+    return this.compareRegExps_(a, b, mismatchKeys, mismatchValues);
  981
+  }
  982
+
963 983
   if (typeof a === "object" && typeof b === "object") {
964 984
     return this.compareObjects_(a, b, mismatchKeys, mismatchValues);
965 985
   }
3  spec/core/MatchersSpec.js
@@ -75,6 +75,9 @@ describe("jasmine.Matchers", function() {
75 75
     expect((match(parseInt('5', 10)).toEqual(5))).toPass();
76 76
     expect((match(5).toNotEqual(5))).toFail();
77 77
     expect((match(parseInt('5', 10)).toNotEqual(5))).toFail();
  78
+
  79
+    expect((match(/1/i).toEqual(/1/i))).toPass();
  80
+    expect((match(/1/i).toNotEqual(/1/i))).toFail();
78 81
   });
79 82
 
80 83
   it("toEqual to build an Expectation Result", function() {
20  src/core/Env.js
@@ -168,6 +168,22 @@ jasmine.Env.prototype.xit = function(desc, func) {
168 168
   };
169 169
 };
170 170
 
  171
+jasmine.Env.prototype.compareRegExps_ = function(a, b, mismatchKeys, mismatchValues) {
  172
+  if (a.source != b.source)
  173
+    mismatchValues.push("expected pattern /" + b.source + "/ is not equal to the pattern /" + a.source + "/");
  174
+
  175
+  if (a.ignoreCase != b.ignoreCase)
  176
+    mismatchValues.push("expected modifier i was" + (b.ignoreCase ? " " : " not ") + "set and does not equal the origin modifier");
  177
+
  178
+  if (a.global != b.global)
  179
+    mismatchValues.push("expected modifier g was" + (b.global ? " " : " not ") + "set and does not equal the origin modifier");
  180
+
  181
+  if (a.multiline != b.multiline)
  182
+    mismatchValues.push("expected modifier m was" + (b.multiline ? " " : " not ") + "set and does not equal the origin modifier");
  183
+
  184
+  return (mismatchValues.length === 0);
  185
+};
  186
+
171 187
 jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) {
172 188
   if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) {
173 189
     return true;
@@ -254,6 +270,10 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) {
254 270
     return (a == b);
255 271
   }
256 272
 
  273
+  if (a instanceof RegExp && b instanceof RegExp) {
  274
+    return this.compareRegExps_(a, b, mismatchKeys, mismatchValues);
  275
+  }
  276
+
257 277
   if (typeof a === "object" && typeof b === "object") {
258 278
     return this.compareObjects_(a, b, mismatchKeys, mismatchValues);
259 279
   }

0 notes on commit d65bdc7

Please sign in to comment.
Something went wrong with that request. Please try again.