From 9e31201f1a8e350c5a2ea9b904ed1597746123e8 Mon Sep 17 00:00:00 2001 From: "Davis W. Frank" Date: Mon, 3 Jun 2013 08:22:45 -0700 Subject: [PATCH] Breaking out Spies into their own source file --- lib/jasmine-core/jasmine.js | 172 +++++++++++++++++++----------------- src/core/Spy.js | 86 ++++++++++++++++++ src/core/base.js | 84 +----------------- src/core/requireCore.js | 1 + 4 files changed, 177 insertions(+), 166 deletions(-) create mode 100644 src/core/Spy.js diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 7ac6623d..f690fa8c 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -48,6 +48,7 @@ getJasmineRequireObj().core = function(jRequire) { j$.QueueRunner = jRequire.QueueRunner(); j$.ReportDispatcher = jRequire.ReportDispatcher(); j$.Spec = jRequire.Spec(); + j$.Spy = jRequire.Spy(j$); j$.Suite = jRequire.Suite(); j$.version = jRequire.version(); @@ -141,89 +142,7 @@ getJasmineRequireObj().base = function(j$) { j$.objectContaining = function(sample) { return new j$.ObjectContaining(sample); }; - - j$.Spy = function(name) { - this.identity = name || 'unknown'; - this.isSpy = true; - this.plan = function() { - }; - this.mostRecentCall = {}; - - this.argsForCall = []; - this.calls = []; - }; - - j$.Spy.prototype.andCallThrough = function() { - this.plan = this.originalValue; - return this; - }; - - j$.Spy.prototype.andReturn = function(value) { - this.plan = function() { - return value; - }; - return this; - }; - - j$.Spy.prototype.andThrow = function(exceptionMsg) { - this.plan = function() { - throw exceptionMsg; - }; - return this; - }; - - j$.Spy.prototype.andCallFake = function(fakeFunc) { - this.plan = fakeFunc; - return this; - }; - - j$.Spy.prototype.reset = function() { - this.wasCalled = false; - this.callCount = 0; - this.argsForCall = []; - this.calls = []; - this.mostRecentCall = {}; - }; - - j$.createSpy = function(name) { - - var spyObj = function() { - spyObj.wasCalled = true; - spyObj.callCount++; - var args = j$.util.argsToArray(arguments); - spyObj.mostRecentCall.object = this; - spyObj.mostRecentCall.args = args; - spyObj.argsForCall.push(args); - spyObj.calls.push({object: this, args: args}); - return spyObj.plan.apply(this, arguments); - }; - - var spy = new j$.Spy(name); - - for (var prop in spy) { - spyObj[prop] = spy[prop]; - } - - spyObj.reset(); - - return spyObj; - }; - - j$.isSpy = function(putativeSpy) { - return putativeSpy && putativeSpy.isSpy; - }; - - j$.createSpyObj = function(baseName, methodNames) { - if (!j$.isArray_(methodNames) || methodNames.length === 0) { - throw "createSpyObj requires a non-empty array of method names to create spies for"; - } - var obj = {}; - for (var i = 0; i < methodNames.length; i++) { - obj[methodNames[i]] = j$.createSpy(baseName + '.' + methodNames[i]); - } - return obj; - }; -}; + }; getJasmineRequireObj().util = function() { @@ -1458,6 +1377,93 @@ getJasmineRequireObj().ReportDispatcher = function() { }; +getJasmineRequireObj().Spy = function(j$) { + + function Spy(name) { + this.identity = name || 'unknown'; + this.isSpy = true; + this.plan = function() { + }; + this.mostRecentCall = {}; + + this.argsForCall = []; + this.calls = []; + } + + Spy.prototype.andCallThrough = function() { + this.plan = this.originalValue; + return this; + }; + + Spy.prototype.andReturn = function(value) { + this.plan = function() { + return value; + }; + return this; + }; + + Spy.prototype.andThrow = function(exceptionMsg) { + this.plan = function() { + throw exceptionMsg; + }; + return this; + }; + + Spy.prototype.andCallFake = function(fakeFunc) { + this.plan = fakeFunc; + return this; + }; + + Spy.prototype.reset = function() { + this.wasCalled = false; + this.callCount = 0; + this.argsForCall = []; + this.calls = []; + this.mostRecentCall = {}; + }; + + j$.createSpy = function(name) { + + var spyObj = function() { + spyObj.wasCalled = true; + spyObj.callCount++; + var args = j$.util.argsToArray(arguments); + spyObj.mostRecentCall.object = this; + spyObj.mostRecentCall.args = args; + spyObj.argsForCall.push(args); + spyObj.calls.push({object: this, args: args}); + return spyObj.plan.apply(this, arguments); + }; + + var spy = new Spy(name); + + for (var prop in spy) { + spyObj[prop] = spy[prop]; + } + + spyObj.reset(); + + return spyObj; + }; + + j$.isSpy = function(putativeSpy) { + return putativeSpy && putativeSpy.isSpy; + }; + + j$.createSpyObj = function(baseName, methodNames) { + if (!j$.isArray_(methodNames) || methodNames.length === 0) { + throw "createSpyObj requires a non-empty array of method names to create spies for"; + } + var obj = {}; + for (var i = 0; i < methodNames.length; i++) { + obj[methodNames[i]] = j$.createSpy(baseName + '.' + methodNames[i]); + } + return obj; + }; + + return Spy; +}; + getJasmineRequireObj().Suite = function() { function Suite(attrs) { this.env = attrs.env; diff --git a/src/core/Spy.js b/src/core/Spy.js new file mode 100644 index 00000000..faf7751d --- /dev/null +++ b/src/core/Spy.js @@ -0,0 +1,86 @@ +getJasmineRequireObj().Spy = function(j$) { + + function Spy(name) { + this.identity = name || 'unknown'; + this.isSpy = true; + this.plan = function() { + }; + this.mostRecentCall = {}; + + this.argsForCall = []; + this.calls = []; + } + + Spy.prototype.andCallThrough = function() { + this.plan = this.originalValue; + return this; + }; + + Spy.prototype.andReturn = function(value) { + this.plan = function() { + return value; + }; + return this; + }; + + Spy.prototype.andThrow = function(exceptionMsg) { + this.plan = function() { + throw exceptionMsg; + }; + return this; + }; + + Spy.prototype.andCallFake = function(fakeFunc) { + this.plan = fakeFunc; + return this; + }; + + Spy.prototype.reset = function() { + this.wasCalled = false; + this.callCount = 0; + this.argsForCall = []; + this.calls = []; + this.mostRecentCall = {}; + }; + + j$.createSpy = function(name) { + + var spyObj = function() { + spyObj.wasCalled = true; + spyObj.callCount++; + var args = j$.util.argsToArray(arguments); + spyObj.mostRecentCall.object = this; + spyObj.mostRecentCall.args = args; + spyObj.argsForCall.push(args); + spyObj.calls.push({object: this, args: args}); + return spyObj.plan.apply(this, arguments); + }; + + var spy = new Spy(name); + + for (var prop in spy) { + spyObj[prop] = spy[prop]; + } + + spyObj.reset(); + + return spyObj; + }; + + j$.isSpy = function(putativeSpy) { + return putativeSpy && putativeSpy.isSpy; + }; + + j$.createSpyObj = function(baseName, methodNames) { + if (!j$.isArray_(methodNames) || methodNames.length === 0) { + throw "createSpyObj requires a non-empty array of method names to create spies for"; + } + var obj = {}; + for (var i = 0; i < methodNames.length; i++) { + obj[methodNames[i]] = j$.createSpy(baseName + '.' + methodNames[i]); + } + return obj; + }; + + return Spy; +}; diff --git a/src/core/base.js b/src/core/base.js index 370ff358..c0f2c680 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -54,86 +54,4 @@ getJasmineRequireObj().base = function(j$) { j$.objectContaining = function(sample) { return new j$.ObjectContaining(sample); }; - - j$.Spy = function(name) { - this.identity = name || 'unknown'; - this.isSpy = true; - this.plan = function() { - }; - this.mostRecentCall = {}; - - this.argsForCall = []; - this.calls = []; - }; - - j$.Spy.prototype.andCallThrough = function() { - this.plan = this.originalValue; - return this; - }; - - j$.Spy.prototype.andReturn = function(value) { - this.plan = function() { - return value; - }; - return this; - }; - - j$.Spy.prototype.andThrow = function(exceptionMsg) { - this.plan = function() { - throw exceptionMsg; - }; - return this; - }; - - j$.Spy.prototype.andCallFake = function(fakeFunc) { - this.plan = fakeFunc; - return this; - }; - - j$.Spy.prototype.reset = function() { - this.wasCalled = false; - this.callCount = 0; - this.argsForCall = []; - this.calls = []; - this.mostRecentCall = {}; - }; - - j$.createSpy = function(name) { - - var spyObj = function() { - spyObj.wasCalled = true; - spyObj.callCount++; - var args = j$.util.argsToArray(arguments); - spyObj.mostRecentCall.object = this; - spyObj.mostRecentCall.args = args; - spyObj.argsForCall.push(args); - spyObj.calls.push({object: this, args: args}); - return spyObj.plan.apply(this, arguments); - }; - - var spy = new j$.Spy(name); - - for (var prop in spy) { - spyObj[prop] = spy[prop]; - } - - spyObj.reset(); - - return spyObj; - }; - - j$.isSpy = function(putativeSpy) { - return putativeSpy && putativeSpy.isSpy; - }; - - j$.createSpyObj = function(baseName, methodNames) { - if (!j$.isArray_(methodNames) || methodNames.length === 0) { - throw "createSpyObj requires a non-empty array of method names to create spies for"; - } - var obj = {}; - for (var i = 0; i < methodNames.length; i++) { - obj[methodNames[i]] = j$.createSpy(baseName + '.' + methodNames[i]); - } - return obj; - }; -}; + }; diff --git a/src/core/requireCore.js b/src/core/requireCore.js index acd8848c..96da2d76 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -26,6 +26,7 @@ getJasmineRequireObj().core = function(jRequire) { j$.QueueRunner = jRequire.QueueRunner(); j$.ReportDispatcher = jRequire.ReportDispatcher(); j$.Spec = jRequire.Spec(); + j$.Spy = jRequire.Spy(j$); j$.Suite = jRequire.Suite(); j$.version = jRequire.version();