From 79825a11afcaf4a9d79c5c00e450baa8c4a0d9be Mon Sep 17 00:00:00 2001 From: tfwright Date: Fri, 20 Apr 2007 15:22:30 +0000 Subject: [PATCH] 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 --- jocha.js | 117 ++++++++++++++++++++++--------------------- test/jocha_test.html | 25 ++++++++- 2 files changed, 83 insertions(+), 59 deletions(-) diff --git a/jocha.js b/jocha.js index eca26e1..b97af95 100755 --- a/jocha.js +++ b/jocha.js @@ -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; } - }); \ No newline at end of file + 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; + } + } +}); \ No newline at end of file diff --git a/test/jocha_test.html b/test/jocha_test.html index 5e10cc1..d6407c5 100755 --- a/test/jocha_test.html +++ b/test/jocha_test.html @@ -5,17 +5,38 @@ jocha.js - - + +