Skip to content

Commit 432d4cf

Browse files
committed
Renaming some code files
1 parent 990cd9a commit 432d4cf

File tree

7 files changed

+80
-116
lines changed

7 files changed

+80
-116
lines changed

Chapter 10/spy.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
var sinon = require("sinon");
2-
3-
var ToSpyUpon = (function () {
4-
function ToSpyUpon() {
1+
var SpyUpon = (function () {
2+
function SpyUpon() {
53
}
6-
ToSpyUpon.prototype.AddNumbers = function (number1, number2) {
7-
throw new Exception("oops");
8-
return number1 + number2;
4+
SpyUpon.prototype.write = function (toWrite) {
5+
console.log(toWrite);
6+
return 7;
97
};
10-
return ToSpyUpon;
8+
return SpyUpon;
119
})();
1210

13-
var thing = new ToSpyUpon();
14-
var spy = sinon.spy(thing, "AddNumbers");
15-
thing.AddNumbers(1, 2);
16-
console.log(spy.called);
17-
console.log(spy.args[0][1]);
18-
console.dir(spy);
11+
var spyUpon = new SpyUpon();
12+
spyUpon._write = spyUpon.write;
13+
spyUpon.write = function (arg1, arg2, arg3, arg4, arg5) {
14+
this.called = true;
15+
this.args = arguments;
16+
this.result = this._write(arg1, arg2, arg3, arg4, arg5);
17+
return this.result;
18+
};
19+
console.log(spyUpon.write("hello world"));
20+
console.log(spyUpon.called);
21+
console.log(spyUpon.args);
22+
console.log(spyUpon.result);

Chapter 10/spy.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
declare var require: any;
2-
var sinon = require("sinon")
3-
4-
class ToSpyUpon{
5-
public AddNumbers(number1: number, number2: number): number{
6-
throw new Exception("oops");
7-
return number1 + number2;
1+
class SpyUpon{
2+
write(toWrite){
3+
console.log(toWrite);
4+
return 7;
85
}
96
}
107

11-
12-
var thing = new ToSpyUpon();
13-
var spy = sinon.spy(thing, "AddNumbers");
14-
thing.AddNumbers(1,2);
15-
console.log(spy.called);
16-
console.log(spy.args[0][1]);
17-
console.dir(spy);
8+
var spyUpon : any= new SpyUpon();
9+
spyUpon._write = spyUpon.write;
10+
spyUpon.write = function(arg1,arg2,arg3,arg4,arg5){
11+
this.called = true;
12+
this.args = arguments;
13+
this.result = this._write(arg1,arg2,arg3,arg4,arg5);
14+
return this.result;
15+
}
16+
console.log(spyUpon.write("hello world"));
17+
console.log(spyUpon.called);
18+
console.log(spyUpon.args);
19+
console.log(spyUpon.result);

Chapter 10/spy2.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

Chapter 10/spy2.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
File renamed without changes.

Chapter 10/stub.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
var sinon
1+
class Knight{
2+
credentialFactory: CredentialFactory;
3+
constructor(credentialFactory){this.credentialFactory = credentialFactory;}
4+
5+
public presentCredentials(toRoyalty){
6+
console.log("Presenting credentials to " + toRoyalty);
7+
toRoyalty.send(this.credentialFactory.Create());
8+
return true;
9+
}
10+
}
11+
12+
class CredentialFactory{
13+
Create(){
14+
//do complicated credential things
15+
}
16+
}
17+
18+
class StubCredentialFactory{
19+
callCounter = 0;
20+
Create(){
21+
if(this.callCounter == 0)
22+
return new SimpleCredential();
23+
if(this.callCounter == 1)
24+
return new CredentialWithSeal();
25+
if(this.callCounter == 2)
26+
return null;
27+
this.callCounter++;
28+
}
29+
}
30+
31+
function assert(value){
32+
if(!value)
33+
throw "Assertion failure";
34+
}
35+
36+
37+
var knight = new Knight(new CredentialFactory());
38+
knight.presentCredentials("Queen Cersei");
39+
40+
var knight = new Knight(new StubCredentialFactory());
41+
var credentials = knight.presentCredentials("Lord Snow");
42+
assert(credentials.type === "SimpleCredentials");
43+
credentials = knight.presentCredentials("Queen Cersei");
44+
assert(credentials.type === "CredentialWithSeal");
45+
credentials = knight.presentCredentials("Lord Stark");
46+
assert(credentials == null);

Chapter 10/stub2.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)