diff --git a/Chapter 10/spy.js b/Chapter 10/spy.js index 8212ee6..1bbecb0 100644 --- a/Chapter 10/spy.js +++ b/Chapter 10/spy.js @@ -1,18 +1,22 @@ -var sinon = require("sinon"); - -var ToSpyUpon = (function () { - function ToSpyUpon() { +var SpyUpon = (function () { + function SpyUpon() { } - ToSpyUpon.prototype.AddNumbers = function (number1, number2) { - throw new Exception("oops"); - return number1 + number2; + SpyUpon.prototype.write = function (toWrite) { + console.log(toWrite); + return 7; }; - return ToSpyUpon; + return SpyUpon; })(); -var thing = new ToSpyUpon(); -var spy = sinon.spy(thing, "AddNumbers"); -thing.AddNumbers(1, 2); -console.log(spy.called); -console.log(spy.args[0][1]); -console.dir(spy); +var spyUpon = new SpyUpon(); +spyUpon._write = spyUpon.write; +spyUpon.write = function (arg1, arg2, arg3, arg4, arg5) { + this.called = true; + this.args = arguments; + this.result = this._write(arg1, arg2, arg3, arg4, arg5); + return this.result; +}; +console.log(spyUpon.write("hello world")); +console.log(spyUpon.called); +console.log(spyUpon.args); +console.log(spyUpon.result); diff --git a/Chapter 10/spy.ts b/Chapter 10/spy.ts index 966ffb9..0e9c307 100644 --- a/Chapter 10/spy.ts +++ b/Chapter 10/spy.ts @@ -1,17 +1,19 @@ -declare var require: any; -var sinon = require("sinon") - - class ToSpyUpon{ - public AddNumbers(number1: number, number2: number): number{ - throw new Exception("oops"); - return number1 + number2; +class SpyUpon{ + write(toWrite){ + console.log(toWrite); + return 7; } } - -var thing = new ToSpyUpon(); -var spy = sinon.spy(thing, "AddNumbers"); -thing.AddNumbers(1,2); -console.log(spy.called); -console.log(spy.args[0][1]); -console.dir(spy); +var spyUpon : any= new SpyUpon(); +spyUpon._write = spyUpon.write; +spyUpon.write = function(arg1,arg2,arg3,arg4,arg5){ + this.called = true; + this.args = arguments; + this.result = this._write(arg1,arg2,arg3,arg4,arg5); + return this.result; +} +console.log(spyUpon.write("hello world")); +console.log(spyUpon.called); +console.log(spyUpon.args); +console.log(spyUpon.result); diff --git a/Chapter 10/spy2.js b/Chapter 10/spy2.js deleted file mode 100644 index 1bbecb0..0000000 --- a/Chapter 10/spy2.js +++ /dev/null @@ -1,22 +0,0 @@ -var SpyUpon = (function () { - function SpyUpon() { - } - SpyUpon.prototype.write = function (toWrite) { - console.log(toWrite); - return 7; - }; - return SpyUpon; -})(); - -var spyUpon = new SpyUpon(); -spyUpon._write = spyUpon.write; -spyUpon.write = function (arg1, arg2, arg3, arg4, arg5) { - this.called = true; - this.args = arguments; - this.result = this._write(arg1, arg2, arg3, arg4, arg5); - return this.result; -}; -console.log(spyUpon.write("hello world")); -console.log(spyUpon.called); -console.log(spyUpon.args); -console.log(spyUpon.result); diff --git a/Chapter 10/spy2.ts b/Chapter 10/spy2.ts deleted file mode 100644 index 0e9c307..0000000 --- a/Chapter 10/spy2.ts +++ /dev/null @@ -1,19 +0,0 @@ -class SpyUpon{ - write(toWrite){ - console.log(toWrite); - return 7; - } -} - -var spyUpon : any= new SpyUpon(); -spyUpon._write = spyUpon.write; -spyUpon.write = function(arg1,arg2,arg3,arg4,arg5){ - this.called = true; - this.args = arguments; - this.result = this._write(arg1,arg2,arg3,arg4,arg5); - return this.result; -} -console.log(spyUpon.write("hello world")); -console.log(spyUpon.called); -console.log(spyUpon.args); -console.log(spyUpon.result); diff --git a/Chapter 10/stub2.js b/Chapter 10/stub.js similarity index 100% rename from Chapter 10/stub2.js rename to Chapter 10/stub.js diff --git a/Chapter 10/stub.ts b/Chapter 10/stub.ts index b9d6cd0..dfb86f3 100644 --- a/Chapter 10/stub.ts +++ b/Chapter 10/stub.ts @@ -1 +1,46 @@ -var sinon +class Knight{ + credentialFactory: CredentialFactory; + constructor(credentialFactory){this.credentialFactory = credentialFactory;} + + public presentCredentials(toRoyalty){ + console.log("Presenting credentials to " + toRoyalty); + toRoyalty.send(this.credentialFactory.Create()); + return true; + } +} + +class CredentialFactory{ + Create(){ + //do complicated credential things + } +} + +class StubCredentialFactory{ + callCounter = 0; + Create(){ + if(this.callCounter == 0) + return new SimpleCredential(); + if(this.callCounter == 1) + return new CredentialWithSeal(); + if(this.callCounter == 2) + return null; + this.callCounter++; + } +} + +function assert(value){ + if(!value) + throw "Assertion failure"; +} + + +var knight = new Knight(new CredentialFactory()); +knight.presentCredentials("Queen Cersei"); + +var knight = new Knight(new StubCredentialFactory()); +var credentials = knight.presentCredentials("Lord Snow"); +assert(credentials.type === "SimpleCredentials"); +credentials = knight.presentCredentials("Queen Cersei"); +assert(credentials.type === "CredentialWithSeal"); +credentials = knight.presentCredentials("Lord Stark"); +assert(credentials == null); diff --git a/Chapter 10/stub2.ts b/Chapter 10/stub2.ts deleted file mode 100644 index dfb86f3..0000000 --- a/Chapter 10/stub2.ts +++ /dev/null @@ -1,46 +0,0 @@ -class Knight{ - credentialFactory: CredentialFactory; - constructor(credentialFactory){this.credentialFactory = credentialFactory;} - - public presentCredentials(toRoyalty){ - console.log("Presenting credentials to " + toRoyalty); - toRoyalty.send(this.credentialFactory.Create()); - return true; - } -} - -class CredentialFactory{ - Create(){ - //do complicated credential things - } -} - -class StubCredentialFactory{ - callCounter = 0; - Create(){ - if(this.callCounter == 0) - return new SimpleCredential(); - if(this.callCounter == 1) - return new CredentialWithSeal(); - if(this.callCounter == 2) - return null; - this.callCounter++; - } -} - -function assert(value){ - if(!value) - throw "Assertion failure"; -} - - -var knight = new Knight(new CredentialFactory()); -knight.presentCredentials("Queen Cersei"); - -var knight = new Knight(new StubCredentialFactory()); -var credentials = knight.presentCredentials("Lord Snow"); -assert(credentials.type === "SimpleCredentials"); -credentials = knight.presentCredentials("Queen Cersei"); -assert(credentials.type === "CredentialWithSeal"); -credentials = knight.presentCredentials("Lord Stark"); -assert(credentials == null); diff --git a/Chapter 4/Facade.js b/Chapter 4/Facade.js index d7559ec..6a7a564 100644 --- a/Chapter 4/Facade.js +++ b/Chapter 4/Facade.js @@ -11,7 +11,7 @@ var Westeros; Fleet.prototype.resupply = function () { }; - Fleet.prototype.attach = function (destination) { + Fleet.prototype.attack = function (destination) { //attack a city }; return Fleet; diff --git a/Chapter 4/Facade.ts b/Chapter 4/Facade.ts index 91ccf7c..b4ff8ac 100644 --- a/Chapter 4/Facade.ts +++ b/Chapter 4/Facade.ts @@ -37,8 +37,9 @@ module Westeros.Transportation{ } module Azure.Settings{ - public interface SettingSaver{ + export interface SettingSaver{ Save(settings: Settings); Retrieve():Settings; } + export interface Settings{} } diff --git a/Chapter 5/ChainOfResponsibility.js b/Chapter 5/ChainOfResponsibility.js index ed84cec..31eba7c 100644 --- a/Chapter 5/ChainOfResponsibility.js +++ b/Chapter 5/ChainOfResponsibility.js @@ -14,7 +14,7 @@ var Westeros; var ClerkOfTheCourt = (function () { function ClerkOfTheCourt() { } - ClerkOfTheCourt.prototype.IsInterestedInComplaint = function (complaint) { + ClerkOfTheCourt.prototype.IsAbleToResolveComplaint = function (complaint) { //decide if this is a complaint which can be solved by the clerk return false; }; @@ -31,7 +31,7 @@ var Westeros; var King = (function () { function King() { } - King.prototype.IsInterestedInComplaint = function (complaint) { + King.prototype.IsAbleToResolveComplaint = function (complaint) { return true; }; @@ -52,7 +52,7 @@ var Westeros; } ComplaintResolver.prototype.ResolveComplaint = function (complaint) { for (var i = 0; i < this.complaintListeners.length; i++) { - if (this.complaintListeners[i].IsInterestedInComplaint(complaint)) { + if (this.complaintListeners[i].IsAbleToResolveComplaint(complaint)) { return this.complaintListeners[i].ListenToComplaint(complaint); } } diff --git a/Chapter 5/ChainOfResponsibility.ts b/Chapter 5/ChainOfResponsibility.ts index 1e9980c..0c20b93 100644 --- a/Chapter 5/ChainOfResponsibility.ts +++ b/Chapter 5/ChainOfResponsibility.ts @@ -7,12 +7,12 @@ module Westeros.JudicialSystem{ } export interface ComplaintListener{ - IsInterestedInComplaint(complaint: Complaint): boolean; + IsAbleToResolveComplaint(complaint: Complaint): boolean; ListenToComplaint(complaint: Complaint): string; } export class ClerkOfTheCourt implements ComplaintListener{ - IsInterestedInComplaint(complaint: Complaint): boolean{ + IsAbleToResolveComplaint(complaint: Complaint): boolean{ //decide if this is a complaint which can be solved by the clerk return false; } @@ -26,7 +26,7 @@ module Westeros.JudicialSystem{ } export class King implements ComplaintListener{ - IsInterestedInComplaint(complaint: Complaint): boolean{ + IsAbleToResolveComplaint(complaint: Complaint): boolean{ return true; } @@ -49,7 +49,7 @@ module Westeros.JudicialSystem{ public ResolveComplaint(complaint: Complaint): string{ for(var i = 0; i