Skip to content

Commit 2844618

Browse files
committed
Add tests up to Player.eat
The closure was removed so that the tests could actually see the code. The order of the comments in `zombies.js` was changed so to make writing the code more succinct. Instead of calling `runGame` directly, it's called after the tests are run on line 20 of index.html.
1 parent 4361bb4 commit 2844618

File tree

3 files changed

+730
-419
lines changed

3 files changed

+730
-419
lines changed

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<script src="lib/js/chai.js" type="text/javascript"></script>
1515
<script src="lib/js/sinon-chai.js" type="text/javascript"></script>
1616
<script src="lib/js/sinon.js" type="text/javascript"></script>
17-
<script src="classes.js" type="text/javascript"></script>
17+
<script src="zombies.js" type="text/javascript"></script>
1818
<script>mocha.setup('bdd');</script>
1919
<script src="test/test-script.js"type="text/javascript"></script>
20-
<script>mocha.run();</script>
20+
<script>mocha.run(runGame);</script>
2121
</body>
2222
</html>

test/test-script.js

+321-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,331 @@
1-
21
var expect = chai.expect;
32
var should = chai.should();
43
var sandbox;
54

65
beforeEach(function() {
7-
// create a sandbox
86
sandbox = sinon.sandbox.create();
9-
sinon.spy(window.console, "log");
107
});
118

129
afterEach(function() {
1310
sandbox.restore();
14-
console.log.restore();
15-
});
11+
});
12+
13+
describe('Item', function() {
14+
it('should be a function', function() {
15+
expect(Item).to.be.a('function');
16+
});
17+
18+
it('should have a name', function() {
19+
var torch = new Item("Torch");
20+
torch.name.should.equal('Torch');
21+
});
22+
});
23+
24+
describe('Weapon', function() {
25+
it('should be a function', function() {
26+
expect(Weapon).to.be.a('function');
27+
});
28+
29+
it('should be an Item', function() {
30+
var dagger = new Weapon("Dagger", 10);
31+
expect(dagger instanceof Item).to.be.true;
32+
});
33+
34+
it('should call Item\'s constructor', function() {
35+
sandbox.stub(window.Item, "call");
36+
37+
var dagger = new Weapon("Dagger", 10);
38+
sinon.assert.calledWithExactly(Item.call, dagger, 'Dagger');
39+
});
40+
41+
it('should have a name', function() {
42+
var dagger = new Weapon("Dagger", 10);
43+
dagger.name.should.equal('Dagger');
44+
});
45+
46+
it('should have a damage property', function() {
47+
var dagger = new Weapon("Dagger", 10);
48+
dagger.damage.should.equal(10);
49+
});
50+
});
51+
52+
describe('Food', function() {
53+
it('should be a function', function() {
54+
expect(Food).to.be.a('function');
55+
});
56+
57+
it('should call Item\'s constructor', function() {
58+
sandbox.stub(window.Item, "call");
59+
60+
var apple = new Food("Apple", 25);
61+
sinon.assert.calledWithExactly(Item.call, apple, 'Apple');
62+
});
63+
64+
it('should have restore energy', function() {
65+
var apple = new Food("Apple", 25);
66+
apple.energy.should.equal(25);
67+
});
68+
});
69+
70+
describe('Player', function() {
71+
it('should be a function', function() {
72+
expect(Player).to.be.a('function');
73+
});
74+
75+
it('should have a name', function() {
76+
var player = new Player("Lee", 100, 15, 7);
77+
player.name.should.equal("Lee");
78+
});
79+
80+
it('should have health', function() {
81+
var player = new Player("Lee", 100, 15, 7);
82+
player.health.should.equal(100);
83+
});
84+
85+
it('should have a strength attribute', function() {
86+
var player = new Player("Lee", 100, 15, 7);
87+
player.strength.should.equal(15);
88+
});
89+
90+
it('should have a speed attribute', function() {
91+
var player = new Player("Lee", 100, 15, 7);
92+
player.speed.should.equal(7);
93+
});
94+
95+
it('should have a private pack variable', function() {
96+
var player = new Player("Lee", 100, 15, 7);
97+
expect(player.pack).to.be.undefined;
98+
});
99+
100+
it('should have a private pack variable', function() {
101+
var player = new Player("Lee", 100, 15, 7);
102+
expect(player.maxHealth).to.be.undefined;
103+
});
104+
105+
it('should be alive', function() {
106+
var player = new Player("Lee", 100, 15, 7);
107+
player.isAlive.should.equal(true);
108+
});
109+
110+
it('should not be equipped', function() {
111+
var player = new Player("Lee", 100, 15, 7);
112+
player.equipped.should.equal(false);
113+
});
114+
115+
describe('.getPack', function() {
116+
it('should be a function', function() {
117+
var player = new Player("Lee", 100, 15, 7);
118+
expect(player.getPack).to.be.a('function');
119+
});
120+
it('should return the player\'s pack', function() {
121+
var player = new Player("Lee", 100, 15, 7);
122+
expect(player.getPack()).to.be.an('Array');
123+
});
124+
});
125+
126+
describe('.getMaxHealth', function() {
127+
it('should be a function', function() {
128+
var player = new Player("Lee", 100, 15, 7);
129+
expect(player.getMaxHealth).to.be.a('function');
130+
});
131+
132+
it('should return the player\'s max health', function() {
133+
var player = new Player("Lee", 100, 15, 7);
134+
expect(player.getMaxHealth()).to.be.a('number');
135+
});
136+
137+
it('should be at least the player\'s current health', function() {
138+
var player = new Player("Lee", 100, 15, 7);
139+
expect(player.getMaxHealth()).to.be.at.least(player.health);
140+
});
141+
});
142+
143+
describe('.takeItem', function() {
144+
it('should be a function', function() {
145+
var player = new Player("Lee", 100, 15, 7);
146+
expect(player.takeItem).to.be.a('function');
147+
});
148+
149+
it('should place an item into the player\'s pack', function() {
150+
var player = new Player("Lee", 100, 15, 7);
151+
var torch = new Item("Torch");
152+
var dagger = new Weapon("Dagger", 10);
153+
var apple = new Food("Apple", 25);
154+
155+
156+
player.takeItem(torch);
157+
player.takeItem(dagger);
158+
player.takeItem(apple);
159+
160+
player.getPack().should.contain(torch);
161+
player.getPack().should.contain(dagger);
162+
player.getPack().should.contain(apple);
163+
});
164+
165+
it('should not place an item into the player\'s pack if it is full', function() {
166+
var player = new Player("Lee", 100, 15, 7);
167+
var torch = new Item("Torch");
168+
var dagger = new Weapon("Dagger", 10);
169+
var apple = new Food("Apple", 25);
170+
var banana = new Food("Banana", 35);
171+
172+
sandbox.stub(console, "log");
173+
player.takeItem(torch);
174+
player.takeItem(dagger);
175+
player.takeItem(apple);
176+
player.takeItem(banana);
177+
sinon.assert.called(console.log);
178+
179+
player.getPack().should.contain(torch);
180+
player.getPack().should.contain(dagger);
181+
player.getPack().should.contain(apple);
182+
player.getPack().should.not.contain(banana);
183+
});
184+
});
185+
186+
describe('.discardItem', function() {
187+
it('should be a function', function() {
188+
var player = new Player("Lee", 100, 15, 7);
189+
expect(player.discardItem).to.be.a('function');
190+
});
191+
192+
it('should discard an item from the player\'s pack', function() {
193+
var player = new Player("Lee", 100, 15, 7);
194+
var torch = new Item("Torch");
195+
var dagger = new Weapon("Dagger", 10);
196+
var apple = new Food("Apple", 25);
197+
198+
player.takeItem(torch);
199+
player.takeItem(dagger);
200+
player.takeItem(apple);
201+
202+
sandbox.stub(console, "log");
203+
player.discardItem(dagger).should.equal(true);
204+
sinon.assert.called(console.log);
205+
206+
player.getPack().should.contain(torch);
207+
player.getPack().should.not.contain(dagger);
208+
player.getPack().should.contain(apple);
209+
210+
});
211+
});
212+
213+
describe('.checkPack', function() {
214+
it('should be a function', function() {
215+
var player = new Player("Lee", 100, 15, 7);
216+
expect(player.checkPack).to.be.a('function');
217+
});
218+
219+
it('should print the contents of the player\'s pack', function() {
220+
sandbox.stub(console, "log");
221+
var player = new Player("Lee", 100, 15, 7);
222+
223+
player.checkPack();
224+
sinon.assert.called(console.log);
225+
});
226+
});
227+
228+
describe('.equip', function() {
229+
it('should be a function', function() {
230+
var player = new Player("Lee", 100, 15, 7);
231+
expect(player.equip).to.be.a('function');
232+
});
233+
234+
it('should only be able to equip Weapons', function() {
235+
var player = new Player("Lee", 100, 15, 7);
236+
var battery = new Item("Battery");
237+
var banana = new Food("Banana", 35);
238+
239+
player.equip(battery);
240+
player.equip(banana);
241+
242+
player.equipped.should.be.false;
243+
});
244+
245+
it('should not equip weapons that are not in the player\'s pack', function() {
246+
var player = new Player("Lee", 100, 15, 7);
247+
var dagger = new Weapon("Dagger", 10);
248+
249+
player.equip(dagger);
250+
251+
player.equipped.should.be.false;
252+
});
253+
254+
it('should equip weapons that are in the player\'s pack', function() {
255+
var player = new Player("Lee", 100, 15, 7);
256+
var dagger = new Weapon("Dagger", 10);
257+
258+
player.takeItem(dagger);
259+
player.equip(dagger);
260+
261+
player.equipped.should.equal(dagger);
262+
player.getPack().should.not.contain(dagger);
263+
});
264+
265+
it('should swap weapons if a one is already equipped', function() {
266+
var player = new Player("Lee", 100, 15, 7);
267+
var dagger = new Weapon("Dagger", 10);
268+
var crowbar = new Weapon("Crowbar", 45);
269+
270+
player.takeItem(dagger);
271+
player.takeItem(crowbar);
272+
273+
player.equip(dagger);
274+
player.equip(crowbar);
275+
276+
player.equipped.should.equal(crowbar);
277+
player.getPack().should.not.contain(crowbar);
278+
player.getPack().should.contain(dagger);
279+
280+
});
281+
});
282+
283+
describe('.eat', function() {
284+
it('should be a function', function() {
285+
var player = new Player("Lee", 100, 15, 7);
286+
expect(player.eat).be.a('function');
287+
});
288+
289+
it('should only eat Food items', function() {
290+
var player = new Player("Lee", 100, 15, 7);
291+
var battery = new Item("Battery");
292+
var crowbar = new Weapon("Crowbar", 45);
293+
294+
player.takeItem(battery);
295+
player.takeItem(crowbar);
296+
player.eat(battery);
297+
player.eat(crowbar);
298+
299+
player.getPack().should.contain(battery);
300+
player.getPack().should.contain(crowbar);
301+
});
302+
303+
it('should not eat Food items if they are not in player\'s pack', function() {
304+
var player = new Player("Lee", 100, 15, 7);
305+
var apple = new Food("Apple", 25);
306+
307+
player.eat(apple);
308+
player.health.should.equal(100);
309+
});
310+
311+
it('should eat Food items that are in player\'s pack', function() {
312+
var player = new Player("Lee", 100, 15, 7);
313+
var apple = new Food("Apple", 25);
314+
315+
player.takeItem(apple);
316+
player.eat(apple);
317+
player.health.should.equal(125);
318+
});
319+
320+
it('should eat Food items that are in player\'s pack', function() {
321+
var player = new Player("Lee", 100, 15, 7);
322+
var maxHealth = player.getMaxHealth();
323+
var maxTomato = new Food("Maximum tomato", maxHealth);
324+
325+
player.takeItem(maxTomato);
326+
player.eat(maxTomato);
327+
player.health.should.equal(maxHealth);
328+
});
329+
});
330+
331+
});

0 commit comments

Comments
 (0)