From d65bdc7e5989adfef195ea9439b1086837faa85a Mon Sep 17 00:00:00 2001 From: Joost Elfering Date: Mon, 4 Jun 2012 00:12:58 +0200 Subject: [PATCH 1/3] resolving issue that was identified via pivotal/jasmine#199 where RegExp 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. --- lib/jasmine-core/jasmine.js | 20 ++++++++++++++++++++ spec/core/MatchersSpec.js | 3 +++ src/core/Env.js | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index c65f7e13..d1c365c4 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -874,6 +874,22 @@ jasmine.Env.prototype.xit = function(desc, func) { }; }; +jasmine.Env.prototype.compareRegExps_ = function(a, b, mismatchKeys, mismatchValues) { + if (a.source != b.source) + mismatchValues.push("expected pattern /" + b.source + "/ is not equal to the pattern /" + a.source + "/"); + + if (a.ignoreCase != b.ignoreCase) + mismatchValues.push("expected modifier i was" + (b.ignoreCase ? " " : " not ") + "set and does not equal the origin modifier"); + + if (a.global != b.global) + mismatchValues.push("expected modifier g was" + (b.global ? " " : " not ") + "set and does not equal the origin modifier"); + + if (a.multiline != b.multiline) + mismatchValues.push("expected modifier m was" + (b.multiline ? " " : " not ") + "set and does not equal the origin modifier"); + + return (mismatchValues.length === 0); +}; + jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) { if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) { return true; @@ -960,6 +976,10 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { return (a == b); } + if (a instanceof RegExp && b instanceof RegExp) { + return this.compareRegExps_(a, b, mismatchKeys, mismatchValues); + } + if (typeof a === "object" && typeof b === "object") { return this.compareObjects_(a, b, mismatchKeys, mismatchValues); } diff --git a/spec/core/MatchersSpec.js b/spec/core/MatchersSpec.js index addd96ee..64f72fc5 100644 --- a/spec/core/MatchersSpec.js +++ b/spec/core/MatchersSpec.js @@ -75,6 +75,9 @@ describe("jasmine.Matchers", function() { expect((match(parseInt('5', 10)).toEqual(5))).toPass(); expect((match(5).toNotEqual(5))).toFail(); expect((match(parseInt('5', 10)).toNotEqual(5))).toFail(); + + expect((match(/1/i).toEqual(/1/i))).toPass(); + expect((match(/1/i).toNotEqual(/1/i))).toFail(); }); it("toEqual to build an Expectation Result", function() { diff --git a/src/core/Env.js b/src/core/Env.js index 4ebc0963..a3ae8ab2 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -168,6 +168,22 @@ jasmine.Env.prototype.xit = function(desc, func) { }; }; +jasmine.Env.prototype.compareRegExps_ = function(a, b, mismatchKeys, mismatchValues) { + if (a.source != b.source) + mismatchValues.push("expected pattern /" + b.source + "/ is not equal to the pattern /" + a.source + "/"); + + if (a.ignoreCase != b.ignoreCase) + mismatchValues.push("expected modifier i was" + (b.ignoreCase ? " " : " not ") + "set and does not equal the origin modifier"); + + if (a.global != b.global) + mismatchValues.push("expected modifier g was" + (b.global ? " " : " not ") + "set and does not equal the origin modifier"); + + if (a.multiline != b.multiline) + mismatchValues.push("expected modifier m was" + (b.multiline ? " " : " not ") + "set and does not equal the origin modifier"); + + return (mismatchValues.length === 0); +}; + jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) { if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) { return true; @@ -254,6 +270,10 @@ jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { return (a == b); } + if (a instanceof RegExp && b instanceof RegExp) { + return this.compareRegExps_(a, b, mismatchKeys, mismatchValues); + } + if (typeof a === "object" && typeof b === "object") { return this.compareObjects_(a, b, mismatchKeys, mismatchValues); } From 639f757f6fb3573c90919288860295c73b9c5709 Mon Sep 17 00:00:00 2001 From: Joost Elfering Date: Tue, 5 Jun 2012 20:58:42 +0200 Subject: [PATCH 2/3] added some specs to strengthen the coverage pivotal/jasmine#234 --- spec/core/MatchersSpec.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/core/MatchersSpec.js b/spec/core/MatchersSpec.js index 64f72fc5..c095cc24 100644 --- a/spec/core/MatchersSpec.js +++ b/spec/core/MatchersSpec.js @@ -78,6 +78,8 @@ describe("jasmine.Matchers", function() { expect((match(/1/i).toEqual(/1/i))).toPass(); expect((match(/1/i).toNotEqual(/1/i))).toFail(); + expect((match(/[abc]/gm).toEqual(/1/i))).toFail(); + expect((match(/[abc]/gm).toNotEqual(/1/i))).toPass(); }); it("toEqual to build an Expectation Result", function() { From c8436d1d44baec389266de8e7acd76a61b65711b Mon Sep 17 00:00:00 2001 From: Joost Elfering Date: Thu, 19 Jul 2012 01:52:27 +0200 Subject: [PATCH 3/3] adding a check for the sticky regExp option supported by Firefox and accepted by the ES6. Note that the tests for this case are checking for the support of the sticky parameter. the logic is still tested by the other expect statements in browsers that do not support sticky but will never enter that block as creating a regExp with that flag is not allowed. Coverage is still good. See pivotal/jasmine#234 --- lib/jasmine-core/jasmine.js | 3 +++ spec/core/MatchersSpec.js | 7 +++++++ src/core/Env.js | 3 +++ 3 files changed, 13 insertions(+) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index d1c365c4..d71facfb 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -887,6 +887,9 @@ jasmine.Env.prototype.compareRegExps_ = function(a, b, mismatchKeys, mismatchVal if (a.multiline != b.multiline) mismatchValues.push("expected modifier m was" + (b.multiline ? " " : " not ") + "set and does not equal the origin modifier"); + if (a.sticky != b.sticky) + mismatchValues.push("expected modifier y was" + (b.sticky ? " " : " not ") + "set and does not equal the origin modifier"); + return (mismatchValues.length === 0); }; diff --git a/spec/core/MatchersSpec.js b/spec/core/MatchersSpec.js index c095cc24..73cfe1f7 100644 --- a/spec/core/MatchersSpec.js +++ b/spec/core/MatchersSpec.js @@ -80,6 +80,13 @@ describe("jasmine.Matchers", function() { expect((match(/1/i).toNotEqual(/1/i))).toFail(); expect((match(/[abc]/gm).toEqual(/1/i))).toFail(); expect((match(/[abc]/gm).toNotEqual(/1/i))).toPass(); + + // only test if the browser supports the sticky option on a regExp see pull #234 + if (RegExp.prototype.sticky !== undefined) { + var sticky_regexp = new RegExp("[abc]", "y"); + expect((match(sticky_regexp).toEqual(/1/i))).toFail(); + expect((match(sticky_regexp).toNotEqual(/1/i))).toPass(); + } }); it("toEqual to build an Expectation Result", function() { diff --git a/src/core/Env.js b/src/core/Env.js index a3ae8ab2..aa461ab4 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -181,6 +181,9 @@ jasmine.Env.prototype.compareRegExps_ = function(a, b, mismatchKeys, mismatchVal if (a.multiline != b.multiline) mismatchValues.push("expected modifier m was" + (b.multiline ? " " : " not ") + "set and does not equal the origin modifier"); + if (a.sticky != b.sticky) + mismatchValues.push("expected modifier y was" + (b.sticky ? " " : " not ") + "set and does not equal the origin modifier"); + return (mismatchValues.length === 0); };