Skip to content

Commit

Permalink
fix for issue sinonjs#59
Browse files Browse the repository at this point in the history
  • Loading branch information
Rogier Schouten committed May 26, 2016
1 parent 3281d41 commit ed4faf3
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 23 deletions.
144 changes: 122 additions & 22 deletions lolex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.lolex=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.lolex = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (global){
/*global global, window*/
/**
Expand All @@ -24,7 +24,7 @@

// setImmediate is not a standard function
// avoid adding the prop to the window object if not present
if('setImmediate' in global) {
if (global.setImmediate !== undefined) {
global.setImmediate = glbl.setImmediate;
global.clearImmediate = glbl.clearImmediate;
}
Expand All @@ -36,6 +36,7 @@
var NOOP = function () { return undefined; };
var timeoutResult = setTimeout(NOOP, 0);
var addTimerReturnsObject = typeof timeoutResult === "object";
var hrtimePresent = (global.process && typeof global.process.hrtime === "function");
clearTimeout(timeoutResult);

var NativeDate = Date;
Expand Down Expand Up @@ -72,6 +73,20 @@
return ms * 1000;
}

/**
* Floor function that also works for negative numbers
*/
function fixedFloor(n) {
return (n >= 0 ? Math.floor(n) : Math.ceil(n));
}

/**
* % operator that also works for negative numbers
*/
function fixedModulo(n, m) {
return ((n % m) + m) % m;
}

/**
* Used to grok the `now` parameter to createClock.
*/
Expand Down Expand Up @@ -250,6 +265,22 @@
return timer;
}

function lastTimer(clock) {
var timers = clock.timers,
timer = null,
id;

for (id in timers) {
if (timers.hasOwnProperty(id)) {
if (!timer || compareTimers(timer, timers[id]) === -1) {
timer = timers[id];
}
}
}

return timer;
}

function callTimer(clock, timer) {
var exception;

Expand Down Expand Up @@ -284,11 +315,11 @@
function timerType(timer) {
if (timer.immediate) {
return "Immediate";
} else if (typeof timer.interval !== "undefined") {
}
if (timer.interval !== undefined) {
return "Interval";
} else {
return "Timeout";
}
return "Timeout";
}

function clearTimer(clock, timerId, ttype) {
Expand All @@ -314,25 +345,29 @@
if (timerType(timer) === ttype) {
delete clock.timers[timerId];
} else {
throw new Error("Cannot clear timer: timer created with set" + ttype + "() but cleared with clear" + timerType(timer) + "()");
}
throw new Error("Cannot clear timer: timer created with set" + ttype + "() but cleared with clear" + timerType(timer) + "()");
}
}
}

function uninstall(clock, target) {
var method,
i,
l;
var installedHrTime = "_hrtime"; // make jslint happy

for (i = 0, l = clock.methods.length; i < l; i++) {
method = clock.methods[i];

if (target[method].hadOwnProperty) {
target[method] = clock["_" + method];
if (method === "hrtime" && target.process) {
target.process.hrtime = clock[installedHrTime];
} else {
try {
delete target[method];
} catch (ignore) {}
if (target[method].hadOwnProperty) {
target[method] = clock["_" + method];
} else {
try {
delete target[method];
} catch (ignore) {}
}
}
}

Expand Down Expand Up @@ -371,9 +406,13 @@
clearImmediate: global.clearImmediate,
setInterval: setInterval,
clearInterval: clearInterval,
Date: Date
Date: Date,
};

if (hrtimePresent) {
timers.hrtime = global.process.hrtime;
}

var keys = Object.keys || function (obj) {
var ks = [],
key;
Expand All @@ -389,11 +428,15 @@

exports.timers = timers;

function createClock(now) {
function createClock(now, loopLimit) {
loopLimit = loopLimit || 1000;

var clock = {
now: getEpoch(now),
hrNow: 0,
timeouts: {},
Date: createDate()
Date: createDate(),
loopLimit: loopLimit
};

clock.Date.clock = clock;
Expand Down Expand Up @@ -443,10 +486,16 @@

clock.duringTick = true;

function updateHrTime(newNow) {
clock.hrNow += (newNow - clock.now);
}

var firstException;
while (timer && tickFrom <= tickTo) {
if (clock.timers[timer.id]) {
tickFrom = clock.now = timer.callAt;
updateHrTime(timer.callAt);
tickFrom = timer.callAt;
clock.now = timer.callAt;
try {
oldNow = clock.now;
callTimer(clock, timer);
Expand All @@ -466,6 +515,7 @@
}

clock.duringTick = false;
updateHrTime(tickTo);
clock.now = tickTo;

if (firstException) {
Expand All @@ -491,6 +541,29 @@
}
};

clock.runAll = function runAll() {
var numTimers, i;
for (i = 0; i < clock.loopLimit; i++) {
numTimers = Object.keys(clock.timers).length;
if (numTimers === 0) {
return clock.now;
}

clock.next();
}

throw new Error('Aborting after running ' + clock.loopLimit + 'timers, assuming an infinite loop!');
};

clock.runToLast = function runToLast() {
var timer = lastTimer(clock);
if (!timer) {
return clock.now;
}

return clock.tick(timer.callAt);
};

clock.reset = function reset() {
clock.timers = {};
};
Expand All @@ -499,25 +572,46 @@
// determine time difference
var newNow = getEpoch(now);
var difference = newNow - clock.now;
var id, timer;

// update 'system clock'
clock.now = newNow;

// update timers and intervals to keep them stable
for (var id in clock.timers) {
for (id in clock.timers) {
if (clock.timers.hasOwnProperty(id)) {
var timer = clock.timers[id];
timer = clock.timers[id];
timer.createdAt += difference;
timer.callAt += difference;
}
}
};

if (hrtimePresent) {
clock.hrtime = function (prev) {
if (Array.isArray(prev)) {
var oldSecs = (prev[0] + prev[1] / 1e9);
var newSecs = (clock.hrNow / 1000);
var difference = (newSecs - oldSecs);
var secs = fixedFloor(difference);
var nanosecs = fixedModulo(difference * 1e9, 1e9);
return [
secs,
nanosecs
];
}
return [
fixedFloor(clock.hrNow / 1000),
fixedModulo(clock.hrNow * 1e6, 1e9)
];
};
}

return clock;
}
exports.createClock = createClock;

exports.install = function install(target, now, toFake) {
exports.install = function install(target, now, toFake, loopLimit) {
var i,
l;

Expand All @@ -531,7 +625,7 @@
target = global;
}

var clock = createClock(now);
var clock = createClock(now, loopLimit);

clock.uninstall = function () {
uninstall(clock, target);
Expand All @@ -544,7 +638,13 @@
}

for (i = 0, l = clock.methods.length; i < l; i++) {
hijackMethod(target, clock.methods[i], clock);
if (clock.methods[i] === "hrtime") {
if (target.process && typeof target.process.hrtime === "function") {
hijackMethod(target.process, clock.methods[i], clock);
}
} else {
hijackMethod(target, clock.methods[i], clock);
}
}

return clock;
Expand Down
2 changes: 1 addition & 1 deletion src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@

for (i = 0, l = clock.methods.length; i < l; i++) {
method = clock.methods[i];
if (method === "hrtime") {
if (method === "hrtime" && target.process) {
target.process.hrtime = clock[installedHrTime];
} else {
if (target[method].hadOwnProperty) {
Expand Down

0 comments on commit ed4faf3

Please sign in to comment.