From 9c8180d8d05abac79a76da480f8ae74ebc30ff2a Mon Sep 17 00:00:00 2001 From: Lee Byrd & Christian Williams Date: Wed, 23 Jun 2010 11:03:05 -0700 Subject: [PATCH] Fixed bug where multiple calls to jasmine.Clock.useMock cause multiple tear downs --- lib/jasmine-0.11.0.js | 35 ++++++++++++++++++++++++++--------- spec/runner.html | 1 + spec/suites/MockClockSpec.js | 4 ++++ src/mock-timeout.js | 23 +++++++++++++++-------- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/lib/jasmine-0.11.0.js b/lib/jasmine-0.11.0.js index 65c5a10..8c7833d 100644 --- a/lib/jasmine-0.11.0.js +++ b/lib/jasmine-0.11.0.js @@ -409,6 +409,11 @@ jasmine.createSpyObj = function(baseName, methodNames) { return obj; }; +/** + * All parameters are pretty-printed and concatenated together, then written to the current spec's output. + * + * Be careful not to leave calls to jasmine.log in production code. + */ jasmine.log = function() { var spec = jasmine.getEnv().currentSpec; spec.log.apply(spec, arguments); @@ -1817,6 +1822,11 @@ jasmine.Spec.prototype.results = function() { return this.results_; }; +/** + * All parameters are pretty-printed and concatenated together, then written to the spec's output. + * + * Be careful not to leave calls to jasmine.log in production code. + */ jasmine.Spec.prototype.log = function() { return this.results_.log(arguments); }; @@ -2180,9 +2190,9 @@ jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMil funcToRun.funcToCall(); if (funcToRun.recurring) { this.scheduleFunction(funcToRun.timeoutKey, - funcToRun.funcToCall, - funcToRun.millis, - true); + funcToRun.funcToCall, + funcToRun.millis, + true); } } catch(e) { } @@ -2226,10 +2236,12 @@ jasmine.Clock = { }, useMock: function() { - var spec = jasmine.getEnv().currentSpec; - spec.after(jasmine.Clock.uninstallMock); + if (!jasmine.Clock.isInstalled()) { + var spec = jasmine.getEnv().currentSpec; + spec.after(jasmine.Clock.uninstallMock); - jasmine.Clock.installMock(); + jasmine.Clock.installMock(); + } }, installMock: function() { @@ -2237,6 +2249,7 @@ jasmine.Clock = { }, uninstallMock: function() { + jasmine.log("uninstall") jasmine.Clock.assertInstalled(); jasmine.Clock.installed = jasmine.Clock.real; }, @@ -2249,11 +2262,15 @@ jasmine.Clock = { }, assertInstalled: function() { - if (jasmine.Clock.installed != jasmine.Clock.defaultFakeTimer) { + if (!jasmine.Clock.isInstalled()) { throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()"); } }, + isInstalled: function() { + return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer; + }, + installed: null }; jasmine.Clock.installed = jasmine.Clock.real; @@ -2287,7 +2304,7 @@ jasmine.getGlobal().clearInterval = function(timeoutKey) { if (jasmine.Clock.installed.clearTimeout.apply) { return jasmine.Clock.installed.clearInterval.apply(this, arguments); } else { - return jasmine.Clock.installed.clearInterval(timeoutKey); + return jasmine.Clock.installed.clearInterval(timeoutKey); } }; @@ -2296,5 +2313,5 @@ jasmine.version_= { "major": 0, "minor": 11, "build": 0, - "revision": 1277253997 + "revision": 1277316068 }; diff --git a/spec/runner.html b/spec/runner.html index ed47b96..6ebd752 100644 --- a/spec/runner.html +++ b/spec/runner.html @@ -39,6 +39,7 @@ + diff --git a/spec/suites/MockClockSpec.js b/spec/suites/MockClockSpec.js index dca3ff2..be68344 100644 --- a/spec/suites/MockClockSpec.js +++ b/spec/suites/MockClockSpec.js @@ -31,4 +31,8 @@ describe("MockClock", function () { expect(interval).toEqual(2); }); }); + + it("shouldn't complain if you call jasmine.Clock.useMock() more than once", function() { + jasmine.Clock.useMock(); + }); }); diff --git a/src/mock-timeout.js b/src/mock-timeout.js index 136d024..c03f9f6 100755 --- a/src/mock-timeout.js +++ b/src/mock-timeout.js @@ -64,9 +64,9 @@ jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMil funcToRun.funcToCall(); if (funcToRun.recurring) { this.scheduleFunction(funcToRun.timeoutKey, - funcToRun.funcToCall, - funcToRun.millis, - true); + funcToRun.funcToCall, + funcToRun.millis, + true); } } catch(e) { } @@ -110,10 +110,12 @@ jasmine.Clock = { }, useMock: function() { - var spec = jasmine.getEnv().currentSpec; - spec.after(jasmine.Clock.uninstallMock); + if (!jasmine.Clock.isInstalled()) { + var spec = jasmine.getEnv().currentSpec; + spec.after(jasmine.Clock.uninstallMock); - jasmine.Clock.installMock(); + jasmine.Clock.installMock(); + } }, installMock: function() { @@ -121,6 +123,7 @@ jasmine.Clock = { }, uninstallMock: function() { + jasmine.log("uninstall") jasmine.Clock.assertInstalled(); jasmine.Clock.installed = jasmine.Clock.real; }, @@ -133,11 +136,15 @@ jasmine.Clock = { }, assertInstalled: function() { - if (jasmine.Clock.installed != jasmine.Clock.defaultFakeTimer) { + if (!jasmine.Clock.isInstalled()) { throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()"); } }, + isInstalled: function() { + return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer; + }, + installed: null }; jasmine.Clock.installed = jasmine.Clock.real; @@ -171,7 +178,7 @@ jasmine.getGlobal().clearInterval = function(timeoutKey) { if (jasmine.Clock.installed.clearTimeout.apply) { return jasmine.Clock.installed.clearInterval.apply(this, arguments); } else { - return jasmine.Clock.installed.clearInterval(timeoutKey); + return jasmine.Clock.installed.clearInterval(timeoutKey); } };