-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.js
More file actions
208 lines (188 loc) · 6.9 KB
/
tests.js
File metadata and controls
208 lines (188 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var expect = require('chai').expect;
var Game = require('./game');
describe('Game', function() {
var game;
beforeEach(function() {
game = new Game();
});
it('should create a Game object', function() {
expect(game).to.be.object;
});
it('should be able to log in', function() {
expect(game).to.respondTo('loginPlayer');
var playername = 'foo';
var player = game.loginPlayer(playername);
expect(player).to.be.a('object');
expect(player).to.have.property('name');
expect(player).to.have.property('secret');
expect(player.secret).to.be.a('string').with.length.above(2);
expect(game).to.have.property('players');
expect(game.players).to.have.property('left');
expect(game.players.left).to.have.property('name');
expect(game.players.left.name).to.equal(playername);
});
it('should only allow 2 players to log in', function() {
expect(game.loginPlayer('one')).to.be.ok;
expect(game.loginPlayer('two')).to.be.ok;
expect(function() {
game.loginPlayer('three');
}).to.throw(Error);
});
it('should start the game, if two players log in', function(done) {
game.config.WAIT_BEFORE_START = 10;
expect(game).to.have.property('status');
expect(game.status).to.equal('login');
game.loginPlayer('left');
game.loginPlayer('right');
expect(game.status).to.equal('ready');
setTimeout(function() {
expect(game.status).to.equal('started');
done();
}, 10);
});
it('should add one point to left player, if ball goes out on right side', function() {
game.ballDelta = [1, 0];
game.paddleRight = 0;
var x = game.config.FIELD_WIDTH - game.config.BALL_RADIUS -
game.config.PADDLE_WIDTH;
game.ball = [x-1, 100];
game.scoreLeft = 0;
game.step();
expect(game.scoreLeft).to.be.equal(0);
game.step();
expect(game.scoreLeft).to.be.equal(1);
var center = [game.config.FIELD_WIDTH / 2, game.config.FIELD_HEIGHT /2];
expect(game.ball).to.eql(center);
});
it('should add one point to right player, if ball goes out on left side', function() {
game.ballDelta = [-1, 0];
game.paddleLeft = 0;
var x = game.config.BALL_RADIUS + game.config.PADDLE_WIDTH;
game.ball = [x + 1, 100];
game.scoreLeft = 0;
game.step();
expect(game.scoreRight).to.be.equal(0);
game.step();
expect(game.scoreRight).to.be.equal(1);
var center = [game.config.FIELD_WIDTH / 2, game.config.FIELD_HEIGHT /2];
expect(game.ball).to.eql(center);
});
it('should be won by player with x points', function() {
game.config.SCORE_TO_WIN = 1;
game.ballDelta = [-1, 0];
game.paddleLeft = 0;
var x = game.config.BALL_RADIUS + game.config.PADDLE_WIDTH;
game.ball = [x, 100];
game.scoreLeft = 0;
game.step();
expect(game.scoreRight).to.be.equal(1);
expect(game.status).to.equal('finished');
});
describe('Paddle', function() {
it('should move, but only by player with secret', function () {
var playerLeft = game.loginPlayer('left');
var playerRight = game.loginPlayer('right');
var posLeft = game.paddleLeft;
var posRight = game.paddleLeft;
game.moveDown(playerLeft.name, playerLeft.secret);
expect(game.paddleLeft).to.be.above(posLeft);
game.moveUp(playerRight.name, playerRight.secret);
expect(game.paddleRight).to.be.below(posRight);
expect(function movePaddleWithWrongSecrect() {
game.moveUp(player.name, 'x');
}).to.throw(Error);
});
it('should can be moved only defined times in configured number of steps', function() {
game.config.NUMBER_OF_PADDLE_MOVES = 2;
game.config.NUMBER_OF_STEPS = 1;
var playerLeft = game.loginPlayer('left');
game.moveDown(playerLeft.name, playerLeft.secret);
game.moveDown(playerLeft.name, playerLeft.secret);
expect(function movePaddleToMuch() {
game.moveDown(playerLeft.name, playerLeft.secret);
}).to.throw(Error);
game.step();
expect(function() {
game.moveDown(playerLeft.name, playerLeft.secret);
}).to.not.throw(Error);
});
it('counter should not become value below 0', function() {
game.config.NUMBER_OF_STEPS = 1;
var playerLeft = game.loginPlayer('left');
game.moveDown(playerLeft.name, playerLeft.secret);
game.step();
expect(game.leftMoveCounter).to.not.be.below(0);
game.step();
expect(game.leftMoveCounter).to.not.be.below(0);
});
});
describe('Ball', function() {
it('should move when game runs', function(done) {
game.config.WAIT_BEFORE_START = 0;
game.config.TIME_QUANTUM = 1;
var pos = game.ball;
game.loginPlayer('left');
game.loginPlayer('right');
setTimeout(function() {
expect(game.ball[0]).to.not.equal(pos[0]);
done();
}, 10);
});
it('should be reflected on top of the field', function() {
game.ballDelta = [0, -1];
var y = game.config.BALL_RADIUS;
game.ball = [100, y];
game.step();
expect(game.ballDelta).to.be.eql([0, 1]);
expect(game.ball[1]).to.be.above(y);
});
it('should be reflected on bottom of the field', function() {
game.ballDelta = [0, 1];
var y = game.config.FIELD_HEIGHT - game.config.BALL_RADIUS;
game.ball = [100, y];
game.step();
expect(game.ballDelta).to.be.eql([0, -1]);
expect(game.ball[1]).to.be.below(y);
});
it('should be reflected on left paddle', function() {
game.ballDelta = [-1, 0];
game.paddleLeft = 100;
var x = game.config.BALL_RADIUS + game.config.PADDLE_WIDTH;
game.ball = [x, 100];
game.step();
expect(game.ballDelta).to.be.eql([1, 0]);
expect(game.ball[0]).to.be.above(x);
});
it('should not be reflected if left paddle is not in position', function() {
game.ballDelta = [-1, 0];
game.paddleLeft = 0;
var x = game.config.BALL_RADIUS + game.config.PADDLE_WIDTH;
game.ball = [x, 100];
game.step();
// ball reseted to center
expect(game.ballDelta).to.not.be.eql([-1, 0]);
expect(game.ball[0]).to.be.equal(game.config.FIELD_WIDTH/2);
});
it('should be reflected on right paddle', function() {
game.ballDelta = [1, 0];
game.paddleRight = 100;
var x = game.config.FIELD_WIDTH - game.config.BALL_RADIUS -
game.config.PADDLE_WIDTH;
game.ball = [x, 100];
game.step();
expect(game.ballDelta).to.be.eql([-1, 0]);
expect(game.ball[0]).to.be.below(x);
});
it('should not be reflected if right paddle is not in position', function() {
game.ballDelta = [1, 0];
game.paddleRight = 0;
var x = game.config.FIELD_WIDTH - game.config.BALL_RADIUS -
game.config.PADDLE_WIDTH;
game.ball = [x, 100];
game.step();
expect(game.ballDelta).to.not.be.eql([1, 0]);
// ball reseted to center
expect(game.ball[0]).to.be.equal(game.config.FIELD_WIDTH/2);
});
});
});