Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Stubbed out a spyOn implementation for whiskey.

  • Loading branch information...
commit 7577ec280f946fa1a4fc6704ef9fa2a86cf8cebb 1 parent 2386ac9
Bjorn Tipling authored
Showing with 71 additions and 0 deletions.
  1. +71 −0 lib/common.js
View
71 lib/common.js
@@ -199,6 +199,7 @@ Test.prototype._getTestObject = function(finishFunc) {
testObj.finish = finishFunc;
testObj.skip = skipFunc;
+ testObj.spyOn = new SpyOn();
return testObj;
};
@@ -544,3 +545,73 @@ exports.TestFile = TestFile;
exports.getTestFilePathAndPattern = getTestFilePathAndPattern;
exports.registerCustomAssertionFunctions = registerCustomAssertionFunctions;
+
+/**
+ * @constructor
+ */
+function SpyOn (){
+ /**
+ * This tracks the number of times a function has been fired.
+ * @param {Object.<number>}
+ * @private
+ */
+ this.callCount_ = {};
+ /**
+ * This tracks the function to call
+ * @param {Object.<Function>}
+ * @private
+ */
+ this.funcMap_ = {};
+};
+
+/**
+ * @param {string} funcName The key to use for tracking call counts.
+ * @param {Object} context The context in which the function should execute.
+ * @param {Function} func The function to spy on.
+ */
+SpyOn.prototype.spyOn = function (funcName, context, func) {
+ if (this.callCount_.hasOwnProperty(funcName)) {
+ throw "Function already being tracked.";
+ }
+ this.reset(funcName);
+ var wrapper = (function () {
+ this.callCount_[funcName] += 1;
+ return func.apply(context, arguments);
+ }).bind(this);
+ context[funcName] = wrapper;
+ this.funcMap_[funcName] = func;
+ return this;
+};
+
+/**
+ * @param {string} funcName The key to clear.
+ * @param {Object} context The context in which the function should execute.
+ * @param {Function} opt_func The optional function to reapply to the context.
+ */
+SpyOn.prototype.clear = function (funcName, context, opt_func) {
+ if (opt_func) {
+ context[funcName] = func;
+ } else {
+ context[funcName] = this.funcMap_[funcName];
+ }
+ delete this.callCount_[funcName];
+ delete this.funcMap_[funcName];
+ return this;
+};
+
+/**
+ * Reset a call count.
+ * @param {string} funcName The name of the function.
+ */
+SpyOn.prototype.reset = function (funcName) {
+ this.callCount_[funcName] = 0;
+};
+
+/**
+ * Get the call count for a spied on function.
+ * @param {string} funcName
+ */
+SpyOn.prototype.called = function (funcName) {
+ return this.callCount_[funcName];
+};
+
Please sign in to comment.
Something went wrong with that request. Please try again.