Re-add Mock Clock behavior as global 'clock'
- Use clock.install, clock.tick... - Add unit coverage. - Fixes old bug in function scheduler
This commit is contained in:
93
src/core/Clock.js
Normal file
93
src/core/Clock.js
Normal file
@@ -0,0 +1,93 @@
|
||||
jasmine.Clock = function(global, delayedFunctionScheduler) {
|
||||
var self = this,
|
||||
realTimingFunctions = {
|
||||
setTimeout: global.setTimeout,
|
||||
clearTimeout: global.clearTimeout,
|
||||
setInterval: global.setInterval,
|
||||
clearInterval: global.clearInterval
|
||||
},
|
||||
fakeTimingFunctions = {
|
||||
setTimeout: setTimeout,
|
||||
clearTimeout: clearTimeout,
|
||||
setInterval: setInterval,
|
||||
clearInterval: clearInterval
|
||||
},
|
||||
timer = realTimingFunctions,
|
||||
installed = false;
|
||||
|
||||
self.install = function() {
|
||||
installed = true;
|
||||
timer = fakeTimingFunctions;
|
||||
};
|
||||
|
||||
self.uninstall = function() {
|
||||
delayedFunctionScheduler.reset();
|
||||
installed = false;
|
||||
timer = realTimingFunctions;
|
||||
};
|
||||
|
||||
self.setTimeout = function(fn, delay, params) {
|
||||
if (legacyIE()) {
|
||||
if (arguments.length > 2) {
|
||||
throw new Error("IE < 9 cannot support extra params to setTimeout without a polyfill");
|
||||
}
|
||||
return timer.setTimeout(fn, delay);
|
||||
}
|
||||
return timer.setTimeout.apply(null, arguments);
|
||||
};
|
||||
|
||||
self.setInterval = function(fn, delay, params) {
|
||||
if (legacyIE()) {
|
||||
if (arguments.length > 2) {
|
||||
throw new Error("IE < 9 cannot support extra params to setInterval without a polyfill");
|
||||
}
|
||||
return timer.setInterval(fn, delay);
|
||||
}
|
||||
return timer.setInterval.apply(null, arguments);
|
||||
};
|
||||
|
||||
self.clearTimeout = function(id) {
|
||||
return timer.clearTimeout(id);
|
||||
};
|
||||
|
||||
self.clearInterval = function(id) {
|
||||
return timer.clearInterval(id);
|
||||
};
|
||||
|
||||
self.tick = function(millis) {
|
||||
if (installed) {
|
||||
delayedFunctionScheduler.tick(millis)
|
||||
} else {
|
||||
throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()");
|
||||
}
|
||||
};
|
||||
|
||||
return self;
|
||||
|
||||
function legacyIE() {
|
||||
//if these methods are polyfilled, apply will be present
|
||||
//TODO: it may be difficult to load the polyfill before jasmine loads
|
||||
//(env should be new-ed inside of onload)
|
||||
return !(global.setTimeout || global.setInterval).apply;
|
||||
}
|
||||
|
||||
function setTimeout(fn, delay) {
|
||||
return delayedFunctionScheduler.scheduleFunction(fn, delay, argSlice(arguments,2));
|
||||
}
|
||||
|
||||
function clearTimeout(id) {
|
||||
return delayedFunctionScheduler.removeFunctionWithId(id);
|
||||
}
|
||||
|
||||
function setInterval(fn, interval) {
|
||||
return delayedFunctionScheduler.scheduleFunction(fn, interval, argSlice(arguments, 2), true);
|
||||
}
|
||||
|
||||
function clearInterval(id) {
|
||||
return delayedFunctionScheduler.removeFunctionWithId(id);
|
||||
}
|
||||
|
||||
function argSlice(argsObj, n) {
|
||||
return Array.prototype.slice.call(argsObj, 2);
|
||||
}
|
||||
};
|
||||
102
src/core/DelayedFunctionScheduler.js
Normal file
102
src/core/DelayedFunctionScheduler.js
Normal file
@@ -0,0 +1,102 @@
|
||||
jasmine.DelayedFunctionScheduler = function() {
|
||||
var self = this;
|
||||
var scheduledFunctions = {};
|
||||
var currentTime = 0;
|
||||
var delayedFnCount = 0;
|
||||
|
||||
self.tick = function(millis) {
|
||||
runFunctionsWithinRange(currentTime, currentTime + millis);
|
||||
currentTime = currentTime + millis;
|
||||
};
|
||||
|
||||
self.scheduleFunction = function(funcToCall, millis, params, recurring, timeoutKey, runAtMillis) {
|
||||
timeoutKey = timeoutKey || ++delayedFnCount;
|
||||
runAtMillis = runAtMillis || (currentTime + millis);
|
||||
scheduledFunctions[timeoutKey] = {
|
||||
runAtMillis: runAtMillis,
|
||||
funcToCall: funcToCall,
|
||||
recurring: recurring,
|
||||
params: params,
|
||||
timeoutKey: timeoutKey,
|
||||
millis: millis
|
||||
};
|
||||
return timeoutKey;
|
||||
};
|
||||
|
||||
self.removeFunctionWithId = function(timeoutKey) {
|
||||
delete scheduledFunctions[timeoutKey];
|
||||
};
|
||||
|
||||
self.reset = function() {
|
||||
currentTime = 0;
|
||||
scheduledFunctions = {};
|
||||
delayedFnCount = 0;
|
||||
};
|
||||
|
||||
return self;
|
||||
|
||||
|
||||
//finds/dupes functions within range and removes them.
|
||||
function functionsWithinRange(startMillis, endMillis) {
|
||||
var fnsToRun = [];
|
||||
for (var timeoutKey in scheduledFunctions) {
|
||||
var scheduledFunc = scheduledFunctions[timeoutKey];
|
||||
if (scheduledFunc &&
|
||||
scheduledFunc.runAtMillis >= startMillis &&
|
||||
scheduledFunc.runAtMillis <= endMillis) {
|
||||
//remove fn -- we'll reschedule later if it is recurring.
|
||||
self.removeFunctionWithId(timeoutKey);
|
||||
if (!scheduledFunc.recurring) {
|
||||
fnsToRun.push(scheduledFunc); // schedules each function only once
|
||||
} else {
|
||||
fnsToRun.push(buildNthInstanceOf(scheduledFunc, 0));
|
||||
var additionalTimesFnRunsInRange =
|
||||
Math.floor((endMillis - scheduledFunc.runAtMillis) / scheduledFunc.millis);
|
||||
for (var i = 0; i < additionalTimesFnRunsInRange; i++) {
|
||||
fnsToRun.push(buildNthInstanceOf(scheduledFunc, i + 1));
|
||||
}
|
||||
reschedule(buildNthInstanceOf(scheduledFunc, additionalTimesFnRunsInRange));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fnsToRun;
|
||||
}
|
||||
|
||||
function buildNthInstanceOf(scheduledFunc, n) {
|
||||
return {
|
||||
runAtMillis: scheduledFunc.runAtMillis + (scheduledFunc.millis * n),
|
||||
funcToCall: scheduledFunc.funcToCall,
|
||||
params: scheduledFunc.params,
|
||||
millis: scheduledFunc.millis,
|
||||
recurring: scheduledFunc.recurring,
|
||||
timeoutKey: scheduledFunc.timeoutKey
|
||||
};
|
||||
}
|
||||
|
||||
function reschedule(scheduledFn) {
|
||||
self.scheduleFunction(scheduledFn.funcToCall,
|
||||
scheduledFn.millis,
|
||||
scheduledFn.params,
|
||||
true,
|
||||
scheduledFn.timeoutKey,
|
||||
scheduledFn.runAtMillis + scheduledFn.millis);
|
||||
}
|
||||
|
||||
|
||||
function runFunctionsWithinRange(startMillis, endMillis) {
|
||||
var funcsToRun = functionsWithinRange(startMillis, endMillis);
|
||||
if (funcsToRun.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
funcsToRun.sort(function(a, b) {
|
||||
return a.runAtMillis - b.runAtMillis;
|
||||
});
|
||||
|
||||
for (var i = 0; i < funcsToRun.length; ++i) {
|
||||
var funcToRun = funcsToRun[i];
|
||||
funcToRun.funcToCall.apply(null, funcToRun.params);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -4,12 +4,17 @@
|
||||
* @constructor
|
||||
*/
|
||||
(function() {
|
||||
jasmine.Env = function() {
|
||||
jasmine.Env = function(options) {
|
||||
options = options || {};
|
||||
var self = this;
|
||||
var global = options.global || jasmine.getGlobal();
|
||||
|
||||
this.clock = new jasmine.Clock(global, new jasmine.DelayedFunctionScheduler());
|
||||
|
||||
var suiteConstructor = jasmine.Suite;
|
||||
var isSuite = function(thing) {
|
||||
return thing instanceof suiteConstructor;
|
||||
}
|
||||
};
|
||||
this.jasmine = jasmine;
|
||||
this.currentRunner_ = new jasmine.Runner(this, isSuite);
|
||||
this.spies_ = [];
|
||||
@@ -95,8 +100,7 @@
|
||||
afterFns: afterFns(suite),
|
||||
expectationFactory: expectationFactory,
|
||||
exceptionFormatter: exceptionFormatter,
|
||||
//TODO: move spec creation to more appropriate level and remove this shim
|
||||
resultCallback: function(result) { self.currentSpec = null; suite.specComplete(result); },
|
||||
resultCallback: specResultCallback,
|
||||
fullNameFactory: function(spec) { return fullNameFactory(spec, suite) },
|
||||
startCallback: startCallback,
|
||||
description: description,
|
||||
@@ -110,8 +114,13 @@
|
||||
}
|
||||
|
||||
return spec;
|
||||
};
|
||||
|
||||
function specResultCallback(result) {
|
||||
self.clock.uninstall();
|
||||
self.currentSpec = null;
|
||||
suite.specComplete(result);
|
||||
}
|
||||
};
|
||||
|
||||
var queueConstructor = jasmine.Queue;
|
||||
var queueFactory = function() {
|
||||
@@ -123,12 +132,6 @@
|
||||
|
||||
};
|
||||
|
||||
|
||||
jasmine.Env.prototype.setTimeout = jasmine.setTimeout;
|
||||
jasmine.Env.prototype.clearTimeout = jasmine.clearTimeout;
|
||||
jasmine.Env.prototype.setInterval = jasmine.setInterval;
|
||||
jasmine.Env.prototype.clearInterval = jasmine.clearInterval;
|
||||
|
||||
//TODO: shim Spec addMatchers behavior into Env. Should be rewritten to remove globals, etc.
|
||||
jasmine.Env.prototype.addMatchers = function(matchersPrototype) {
|
||||
var parent = this.matchersClass;
|
||||
|
||||
@@ -108,8 +108,8 @@ jasmine.MessageResult.prototype.toString = function() {
|
||||
/**
|
||||
* Getter for the Jasmine environment. Ensures one gets created
|
||||
*/
|
||||
jasmine.getEnv = function() {
|
||||
var env = jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env();
|
||||
jasmine.getEnv = function(options) {
|
||||
var env = jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env(options);
|
||||
//jasmine. singletons in here (setTimeout blah blah).
|
||||
return env;
|
||||
};
|
||||
|
||||
@@ -1,184 +0,0 @@
|
||||
// Mock setTimeout, clearTimeout
|
||||
// Contributed by Pivotal Computer Systems, www.pivotalsf.com
|
||||
|
||||
jasmine.FakeTimer = function() {
|
||||
this.reset();
|
||||
|
||||
var self = this;
|
||||
self.setTimeout = function(funcToCall, millis) {
|
||||
self.timeoutsMade++;
|
||||
self.scheduleFunction(self.timeoutsMade, funcToCall, millis, false);
|
||||
return self.timeoutsMade;
|
||||
};
|
||||
|
||||
self.setInterval = function(funcToCall, millis) {
|
||||
self.timeoutsMade++;
|
||||
self.scheduleFunction(self.timeoutsMade, funcToCall, millis, true);
|
||||
return self.timeoutsMade;
|
||||
};
|
||||
|
||||
self.clearTimeout = function(timeoutKey) {
|
||||
self.scheduledFunctions[timeoutKey] = jasmine.undefined;
|
||||
};
|
||||
|
||||
self.clearInterval = function(timeoutKey) {
|
||||
self.scheduledFunctions[timeoutKey] = jasmine.undefined;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
jasmine.FakeTimer.prototype.reset = function() {
|
||||
this.timeoutsMade = 0;
|
||||
this.scheduledFunctions = {};
|
||||
this.nowMillis = 0;
|
||||
};
|
||||
|
||||
jasmine.FakeTimer.prototype.tick = function(millis) {
|
||||
var oldMillis = this.nowMillis;
|
||||
var newMillis = oldMillis + millis;
|
||||
this.runFunctionsWithinRange(oldMillis, newMillis);
|
||||
this.nowMillis = newMillis;
|
||||
};
|
||||
|
||||
jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMillis) {
|
||||
var scheduledFunc;
|
||||
var funcsToRun = [];
|
||||
for (var timeoutKey in this.scheduledFunctions) {
|
||||
scheduledFunc = this.scheduledFunctions[timeoutKey];
|
||||
if (scheduledFunc != jasmine.undefined &&
|
||||
scheduledFunc.runAtMillis >= oldMillis &&
|
||||
scheduledFunc.runAtMillis <= nowMillis) {
|
||||
funcsToRun.push(scheduledFunc);
|
||||
this.scheduledFunctions[timeoutKey] = jasmine.undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (funcsToRun.length > 0) {
|
||||
funcsToRun.sort(function(a, b) {
|
||||
return a.runAtMillis - b.runAtMillis;
|
||||
});
|
||||
for (var i = 0; i < funcsToRun.length; ++i) {
|
||||
try {
|
||||
var funcToRun = funcsToRun[i];
|
||||
this.nowMillis = funcToRun.runAtMillis;
|
||||
funcToRun.funcToCall();
|
||||
if (funcToRun.recurring) {
|
||||
this.scheduleFunction(funcToRun.timeoutKey,
|
||||
funcToRun.funcToCall,
|
||||
funcToRun.millis,
|
||||
true);
|
||||
}
|
||||
} catch(e) {
|
||||
}
|
||||
}
|
||||
this.runFunctionsWithinRange(oldMillis, nowMillis);
|
||||
}
|
||||
};
|
||||
|
||||
jasmine.FakeTimer.prototype.scheduleFunction = function(timeoutKey, funcToCall, millis, recurring) {
|
||||
this.scheduledFunctions[timeoutKey] = {
|
||||
runAtMillis: this.nowMillis + millis,
|
||||
funcToCall: funcToCall,
|
||||
recurring: recurring,
|
||||
timeoutKey: timeoutKey,
|
||||
millis: millis
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* @namespace
|
||||
*/
|
||||
jasmine.Clock = {
|
||||
defaultFakeTimer: new jasmine.FakeTimer(),
|
||||
|
||||
reset: function() {
|
||||
jasmine.Clock.assertInstalled();
|
||||
jasmine.Clock.defaultFakeTimer.reset();
|
||||
},
|
||||
|
||||
tick: function(millis) {
|
||||
jasmine.Clock.assertInstalled();
|
||||
jasmine.Clock.defaultFakeTimer.tick(millis);
|
||||
},
|
||||
|
||||
runFunctionsWithinRange: function(oldMillis, nowMillis) {
|
||||
jasmine.Clock.defaultFakeTimer.runFunctionsWithinRange(oldMillis, nowMillis);
|
||||
},
|
||||
|
||||
scheduleFunction: function(timeoutKey, funcToCall, millis, recurring) {
|
||||
jasmine.Clock.defaultFakeTimer.scheduleFunction(timeoutKey, funcToCall, millis, recurring);
|
||||
},
|
||||
|
||||
useMock: function() {
|
||||
if (!jasmine.Clock.isInstalled()) {
|
||||
//TODO: this is using an interface that doesn't exist.
|
||||
var spec = jasmine.getEnv().currentSpec;
|
||||
spec.after(jasmine.Clock.uninstallMock);
|
||||
|
||||
jasmine.Clock.installMock();
|
||||
}
|
||||
},
|
||||
|
||||
installMock: function() {
|
||||
jasmine.Clock.installed = jasmine.Clock.defaultFakeTimer;
|
||||
},
|
||||
|
||||
uninstallMock: function() {
|
||||
jasmine.Clock.assertInstalled();
|
||||
jasmine.Clock.installed = jasmine.Clock.real;
|
||||
},
|
||||
|
||||
real: {
|
||||
setTimeout: jasmine.getGlobal().setTimeout,
|
||||
clearTimeout: jasmine.getGlobal().clearTimeout,
|
||||
setInterval: jasmine.getGlobal().setInterval,
|
||||
clearInterval: jasmine.getGlobal().clearInterval
|
||||
},
|
||||
|
||||
assertInstalled: function() {
|
||||
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;
|
||||
|
||||
//else for IE support
|
||||
jasmine.getGlobal().setTimeout = function(funcToCall, millis) {
|
||||
if (jasmine.Clock.installed.setTimeout.apply) {
|
||||
return jasmine.Clock.installed.setTimeout.apply(this, arguments);
|
||||
} else {
|
||||
return jasmine.Clock.installed.setTimeout(funcToCall, millis);
|
||||
}
|
||||
};
|
||||
|
||||
jasmine.getGlobal().setInterval = function(funcToCall, millis) {
|
||||
if (jasmine.Clock.installed.setInterval.apply) {
|
||||
return jasmine.Clock.installed.setInterval.apply(this, arguments);
|
||||
} else {
|
||||
return jasmine.Clock.installed.setInterval(funcToCall, millis);
|
||||
}
|
||||
};
|
||||
|
||||
jasmine.getGlobal().clearTimeout = function(timeoutKey) {
|
||||
if (jasmine.Clock.installed.clearTimeout.apply) {
|
||||
return jasmine.Clock.installed.clearTimeout.apply(this, arguments);
|
||||
} else {
|
||||
return jasmine.Clock.installed.clearTimeout(timeoutKey);
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user