Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added handler for calling mocked method with wrong params
git-svn-id: file:///Users/jyurek/Backups/repositories/jocha/trunk@8 5317845c-832e-0410-b2c0-db6c5bc7c761
- Loading branch information
tfwright
committed
Apr 20, 2007
1 parent
ae8a925
commit 79825a1
Showing
2 changed files
with
83 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
// Thanks http://www.svendtofte.com/code/usefull_prototypes/ | ||
Array.prototype.isEqual = function(arr) { | ||
if (this.length != arr.length) return false; | ||
for (var i = 0; i < arr.length; i++) { | ||
if (this[i].isEqual) { //likely nested array | ||
if (!this[i].isEqual(arr[i])) return false; | ||
else continue; | ||
} | ||
if (this[i] != arr[i]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
Mock = Class.create(); | ||
Mock.prototype = { | ||
initialize : function() { | ||
this.expectations = new Array(); | ||
} | ||
} | ||
Expectation = Class.create(); | ||
Expectation.prototype = { | ||
initialize : function(functionName) { | ||
this.functionName = functionName; | ||
this.params = []; | ||
this.invoked = false; | ||
}, | ||
withParams : function() { | ||
this.params = $A(arguments); | ||
return this; | ||
}, | ||
andReturns : function(return_val) { | ||
this.return_val = return_val; | ||
} | ||
} | ||
|
||
Object.extend(Object.prototype, { | ||
expects : function(functionName) { | ||
this[functionName] = function() { | ||
this.methodMocked(functionName, arguments); | ||
}; | ||
if (!this.mock) | ||
this.mock = new Mock(); | ||
expectation = new Expectation(functionName); | ||
this.mock.expectations.push(expectation); | ||
return expectation; | ||
}, | ||
verify : function() { | ||
if (!this.mock.expectations.all(function(e){return e.invoked})) | ||
return false; | ||
else | ||
return true; | ||
}, | ||
methodMocked : function(functionName, args) { | ||
expectation = this.mock.expectations.find(function(e){return e.functionName == functionName && e.params.isEqual($A(args))}); | ||
expectation.invoked = true; | ||
return expectation.return_val; | ||
// Thanks http://www.svendtofte.com/code/usefull_prototypes/ | ||
Array.prototype.isEqual = function(arr) { | ||
if (this.length != arr.length) return false; | ||
for (var i = 0; i < arr.length; i++) { | ||
if (this[i].isEqual) { //likely nested array | ||
if (!this[i].isEqual(arr[i])) return false; | ||
else continue; | ||
} | ||
}); | ||
if (this[i] != arr[i]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
Mock = Class.create(); | ||
Mock.prototype = { | ||
initialize : function() { | ||
this.expectations = new Array(); | ||
} | ||
} | ||
Expectation = Class.create(); | ||
Expectation.prototype = { | ||
initialize : function(functionName) { | ||
this.functionName = functionName; | ||
this.params = []; | ||
this.invoked = false; | ||
this.return_val = ""; | ||
}, | ||
withParams : function() { | ||
this.params = $A(arguments); | ||
return this; | ||
}, | ||
andReturns : function(returnVal) { | ||
this.returnVal = returnVal; | ||
} | ||
} | ||
|
||
Object.extend(Object.prototype, { | ||
expects : function(functionName) { | ||
this[functionName] = function() { | ||
this.methodMocked(functionName, arguments); | ||
}; | ||
if (!this.mock) | ||
this.mock = new Mock(); | ||
expectation = new Expectation(functionName); | ||
this.mock.expectations.push(expectation); | ||
return expectation; | ||
}, | ||
verify : function() { | ||
if (this.mock.expectations.all(function(e){return e.invoked})) | ||
return true; | ||
else | ||
return false; | ||
}, | ||
methodMocked : function(functionName, args) { | ||
expectation = this.mock.expectations.find(function(e){return e.functionName == functionName && e.params.isEqual($A(args))}); | ||
if (expectation) { | ||
expectation.invoked = true; | ||
return expectation.returnVal; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters