Comparing version 0.2.2beta to 0.2.3beta
@@ -31,12 +31,22 @@ /** | ||
var currentMockFunction = null; | ||
var args = []; | ||
var returnValue = []; | ||
var callbackIndex = []; | ||
var callbackArgs = []; | ||
var shouldFail = []; | ||
var entries = {}; | ||
var mockFunction = function(method) { | ||
function getValidEntry(args) { | ||
for(var index in entries[method]) { | ||
entry = entries[method][index]; | ||
if(deepObjectCheck(entry.args, args)) { | ||
return entry; | ||
} | ||
} | ||
return false; | ||
} | ||
return function() { | ||
//check for argument length | ||
var entry; | ||
@@ -49,8 +59,3 @@ if(shouldFail[method]) { | ||
} else if((args[method].length != arguments.length)) { | ||
throw new Error("Number of Arguments passed(" + arguments.length + | ||
") is not tally with expected(" + args[method].length + | ||
") in '" + method + "()'"); | ||
} else if(!deepObjectCheck(args[method], arguments)) { | ||
} else if(!(entry = getValidEntry(arguments))) { | ||
//check for argument content | ||
@@ -61,6 +66,6 @@ throw new Error("Arguments content passed is not tally with expected"); | ||
//calling the callback | ||
if(callbackIndex[method]) { | ||
var func = arguments[callbackIndex[method]]; | ||
if(entry.callbackIndex) { | ||
var func = arguments[entry.callbackIndex]; | ||
if(typeof(func) == "function") { | ||
func.apply(this, callbackArgs[method] || []); | ||
func.apply(this, entry.callbackArgs || []); | ||
} else { | ||
@@ -71,3 +76,3 @@ throw new Error("Expected callback is not defined as callback"); | ||
return returnValue[method]; | ||
return entry.returns; | ||
}; | ||
@@ -77,3 +82,4 @@ }; | ||
this.takes = function() { | ||
args[currentMockFunction] = arguments; | ||
var entry = entries[currentMockFunction][entries[currentMockFunction].length - 1]; | ||
entry.args = arguments; | ||
return this; | ||
@@ -83,3 +89,4 @@ }; | ||
this.returns = function(value) { | ||
returnValue[currentMockFunction] = value; | ||
var entry = entries[currentMockFunction][entries[currentMockFunction].length - 1]; | ||
entry.returns = value; | ||
return this; | ||
@@ -91,12 +98,13 @@ }; | ||
if(typeof(arguments[0]) == "number") { | ||
callbackIndex[currentMockFunction] = arguments[0]; | ||
var entry = entries[currentMockFunction][entries[currentMockFunction].length - 1]; | ||
entry.callbackIndex = arguments[0]; | ||
if(arguments[1] && (arguments[1] instanceof Array)) { | ||
entry.callbackArgs = arguments[1]; | ||
} | ||
return this; | ||
} else { | ||
throw new Error("First arg of the calls() should be the index of the callback"); | ||
} | ||
if(arguments[1] && (arguments[1] instanceof Array)) { | ||
callbackArgs[currentMockFunction] = arguments[1]; | ||
} | ||
return this; | ||
}; | ||
@@ -111,8 +119,4 @@ | ||
if(!this[method]) { | ||
//ground work | ||
args[method] = []; | ||
returnValue[method] = undefined; | ||
callbackIndex[method] = null; | ||
callbackArgs[method] = null; | ||
entries[currentMockFunction] = []; | ||
//assign the mock method | ||
@@ -122,2 +126,9 @@ this[method] = mockFunction(method); | ||
entries[currentMockFunction].push({ | ||
args: [], | ||
callbackIndex: null, | ||
callbackParams: null, | ||
returns: undefined | ||
}); | ||
return this; | ||
@@ -135,3 +146,3 @@ }; | ||
var deepObjectCheck = function(expected, actual) { | ||
function deepObjectCheck(expected, actual) { | ||
@@ -158,2 +169,4 @@ if(expected.length != actual.length) return false; | ||
//initialize the mocking | ||
@@ -160,0 +173,0 @@ this.mock(methodName); |
{ | ||
"name": "nodemock", | ||
"version": "0.2.2beta", | ||
"version": "0.2.3beta", | ||
"directories": { | ||
@@ -5,0 +5,0 @@ "lib": "./lib" |
@@ -208,25 +208,2 @@ /** | ||
exports.testMockEdit = function(test) { | ||
var mock = nm.mock("foo").takes(10).returns(30); | ||
test.throws(function() { | ||
test.equal(mock.foo(10, 20), 30); | ||
}); | ||
test.doesNotThrow(function() { | ||
mock.mock("foo").takes(10, 20); | ||
test.equal(mock.foo(10, 20), 30); | ||
}); | ||
test.doesNotThrow(function() { | ||
mock.mock("foo").takes(function(){}).calls(0, [10]); | ||
mock.foo(function(val) { | ||
test.value(10, val); | ||
}); | ||
}); | ||
test.done(); | ||
}; | ||
exports.testFailNoAnyMockMethod = function(test) { | ||
@@ -253,2 +230,20 @@ | ||
test.done(); | ||
}; | ||
}; | ||
exports.testMultipleEntriesForOneMethod = function(test) { | ||
var mock = nm.mock("foo").takes(10, 20).returns(30); | ||
mock.mock("foo").takes(10, 30).returns(40); | ||
test.doesNotThrow(function() { | ||
test.ok(mock.foo(10, 20) == 30); | ||
test.ok(mock.foo(10, 30) == 40); | ||
}); | ||
test.throws(function() { | ||
mock.foo(10); | ||
mock.foo(10, 20); | ||
mock.bar(10, 30); | ||
}); | ||
test.done(); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15549
7
311