-
Notifications
You must be signed in to change notification settings - Fork 3
/
corespec.js
347 lines (333 loc) · 14.9 KB
/
corespec.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
describe("Turtle", function () {
var turtle;
beforeEach(function () {
turtle = new Turtle();
});
describe("new", function () {
it("should set default direction and position", function () {
expect(turtle.position()).toEqual({direction:0, x:0, y:0});
});
it("should use given direction and position as defaults", function () {
turtle = new Turtle({direction:Turtle.radians(60), x:50, y:40});
expect(turtle.position()).toEqual({direction:Turtle.radians(60), x:50, y:40});
});
});
describe("position({direction:degrees})", function () {
it("should change turtle.position().direction", function () {
turtle.position({direction:Turtle.radians(30)});
expect(Turtle.degrees(turtle.position().direction)).toEqual(30);
});
it("should convert directions greater than 360 degrees", function () {
turtle.position({direction:Turtle.radians(400)});
expect(Turtle.degrees(turtle.position().direction)).toEqual(40);
});
it("should convert negative degrees", function () {
turtle.position({direction:Turtle.radians(-30)});
expect(Turtle.degrees(turtle.position().direction)).toEqual(330);
turtle.position({direction:Turtle.radians(-400)});
expect(Turtle.degrees(turtle.position().direction)).toEqual(320);
});
});
describe("position({x:pixels, y:pixels})", function () {
it("should change turtle.position()", function () {
turtle.position({x:5, y:20});
expect([turtle.position().x,turtle.position().y]).toEqual([5, 20]);
});
});
describe("home", function () {
function random(i, j) {return Math.floor(Math.random()*(j-i+1))+i}
it("should reset direction and position to defaults", function () {
turtle.position({
direction:random(0, 2*Math.PI),
x:random(-100, 100),
y:random(-100, 100)
});
turtle.home();
expect(turtle.position()).toEqual({direction:0, x:0, y:0});
});
it("should reset direction and position to user specified defaults", function () {
turtle = new Turtle({direction:Turtle.radians(90), x:80, y:70});
turtle.position({
direction:random(0, 2*Math.PI),
x:random(-100, 100),
y:random(-100, 100)
});
turtle.home();
expect(turtle.position()).toEqual({direction:Turtle.radians(90), x:80, y:70});
});
it("should also change the turtle's home when given arguments", function () {
turtle.home({direction:Turtle.radians(15), x:25, y:35});
expect(turtle.position()).toEqual({direction:Turtle.radians(15), x:25, y:35});
turtle.position({
direction:random(0, 2*Math.PI),
x:random(-100, 100),
y:random(-100, 100)
});
turtle.home();
expect(turtle.position()).toEqual({direction:Turtle.radians(15), x:25, y:35});
});
});
describe("turn(degrees)", function () {
it("should change turtle.position().direction relative to its current direction", function () {
turtle.turn(Turtle.radians(30));
expect(Turtle.degrees(turtle.position().direction)).toEqual(30);
turtle.turn(Turtle.radians(-45));
expect(Turtle.degrees(turtle.position().direction)).toEqual(345);
turtle.turn(Turtle.radians(90));
expect(Turtle.degrees(turtle.position().direction)).toEqual(75);
});
});
describe("move(pixels)", function () {
it("should move the turtle location <pixels> in the turtle's direction", function () {
this.addMatchers({
toRoundTo: function (expected) {
return Math.round(this.actual) === expected;
},
});
var smallest_angle_of_3_4_5_right_triangle = Turtle.radians(37);
turtle.position({direction:smallest_angle_of_3_4_5_right_triangle});
turtle.move(50);
expect(turtle.position().x).toRoundTo(40);
expect(turtle.position().y).toRoundTo(30);
});
});
});
describe("TurtlePenDecorator", function () {
var turtle, context;
beforeEach(function () {
context = jasmine.createSpyObj(
'context',
'moveTo lineTo beginPath stroke clearRect'.split(' '));
turtle = new Turtle.Pen(new Turtle(), context);
});
it("pendown() should call beginPath and change the pen to down", function () {
expect(turtle.pendown).toBeDefined();
expect(turtle.pendown().pen).toEqual("down");
});
it("penup() should change the pen to up", function () {
expect(turtle.penup).toBeDefined();
expect(turtle.penup().pen).toEqual("up");
});
it("move() calls context.moveTo when up and context.lineTo when down", function () {
turtle.move(20).pendown().move(30);
expect(context.beginPath).toHaveBeenCalled();
expect(context.moveTo).toHaveBeenCalledWith(20, 0);
expect(context.lineTo).toHaveBeenCalledWith(50, 0);
expect(context.stroke).toHaveBeenCalled();
});
it("clear() calls context.clearRect() and turtle.home()", function () {
context.canvas = {width: 200, height: 100};
turtle.turn(Turtle.radians(30)).move(40).clear();
expect(context.clearRect).toHaveBeenCalledWith(0,0,200,100);
expect(turtle.position()).toEqual({direction:0, x:0, y:0});
});
it("pencolor() sets context.strokeStyle", function () {
turtle.pencolor("DarkOrchid");
expect(turtle._pencolor).toEqual("DarkOrchid");
expect(context.strokeStyle).toEqual("DarkOrchid");
});
it("pensize() sets context.lineWidth", function () {
turtle.pensize(3);
expect(context.lineWidth).toEqual(3);
});
});
describe("TurtleShapeDecorator", function () {
var turtle, context;
beforeEach(function () {
context = jasmine.createSpyObj('context', [
'moveTo',
'drawImage',
'stroke',
'fill',
'clearRect',
'translate',
'rotate',
'save',
'restore',
'getImageData',
'putImageData'
]);
context.getImageData.andReturn('stubbedImageData');
context.canvas = {width: 100, height: 80};
pen = new Turtle.Pen(new Turtle(), context);
turtle = new Turtle.Shape(pen);
});
it("should draw the triangle after home()", function () {
turtle.home({direction: Turtle.radians(60), x:50, y:40});
expect(context.save).toHaveBeenCalled();
expect(context.translate).toHaveBeenCalledWith(50, 40);
expect(context.rotate).toHaveBeenCalledWith(Math.PI/3);
expect(context.getImageData).toHaveBeenCalled();
expect(context.drawImage).toHaveBeenCalled();
expect(context.restore).toHaveBeenCalled();
});
it("should draw the triangle after changing the position()", function () {
turtle.position({direction: Turtle.radians(90), x:80, y:70});
expect(context.save).toHaveBeenCalled();
expect(context.translate).toHaveBeenCalledWith(80, 70);
expect(context.rotate).toHaveBeenCalledWith(Math.PI/2);
expect(context.getImageData).toHaveBeenCalled();
expect(context.drawImage).toHaveBeenCalled();
expect(context.restore).toHaveBeenCalled();
});
it("should draw the triangle after turn()", function () {
turtle.turn(Turtle.radians(30));
expect(context.save).toHaveBeenCalled();
expect(context.translate).toHaveBeenCalledWith(0, 0);
expect(context.rotate).toHaveBeenCalledWith(Math.PI/6);
expect(context.getImageData).toHaveBeenCalled();
expect(context.drawImage).toHaveBeenCalled();
expect(context.restore).toHaveBeenCalled();
});
it("should draw the triangle after move()", function () {
turtle.move(90);
expect(context.save).toHaveBeenCalled();
expect(context.translate).toHaveBeenCalledWith(90, 0);
expect(context.rotate).toHaveBeenCalledWith(0);
expect(context.getImageData).toHaveBeenCalled();
expect(context.drawImage).toHaveBeenCalled();
expect(context.restore).toHaveBeenCalled();
});
it("should draw the triangle after clear()", function () {
turtle.clear();
expect(context.clearRect).toHaveBeenCalledWith(0, 0, 100, 80);
expect(context.save).toHaveBeenCalled();
expect(context.translate).toHaveBeenCalledWith(0, 0);
expect(context.rotate).toHaveBeenCalledWith(0);
expect(context.getImageData).toHaveBeenCalled();
expect(context.drawImage).toHaveBeenCalled();
expect(context.restore).toHaveBeenCalled();
});
it("should save and restore the background", function () {
turtle.move(90).turn(Turtle.radians(90));
expect(context.getImageData).toHaveBeenCalled();
expect(context.putImageData).toHaveBeenCalled();
});
it("should change the triangle fillStyle for pendown", function () {
turtle.pendown();
expect(turtle.shape.getContext('2d').fillStyle).toEqual("#000000");
expect(context.drawImage).toHaveBeenCalled();
turtle.penup();
expect(context.drawImage).toHaveBeenCalled();
});
it("should set the triangle's fillStyle to match the pen's color", function () {
turtle.pencolor("MediumSlateBlue").pendown();
var ctx = turtle.shape.getContext('2d');
expect(ctx.fillStyle).toEqual("#7b68ee");
turtle.pendown().pencolor("Navy");
expect(ctx.fillStyle).toEqual("#000080");
});
});
describe("TurtleCommandRecorder", function () {
var turtle, recorder;
beforeEach(function () {
turtle = jasmine.createSpyObj("turtle", [
"home", "position", "turn", "move", "pendown", "penup", "clear"
]);
recorder = new Turtle.Recorder();
});
it("should record the core turtle commands: home, position, turn, and move", function () {
recorder.home().position({x: 10, y:20, direction:Turtle.radians(30)}).turn(Turtle.radians(40)).move(50);
expect(recorder.queue.length).toEqual(4);
recorder.queue[0].apply(undefined, [turtle]);
expect(turtle.home).toHaveBeenCalled();
recorder.queue[1].apply(undefined, [turtle]);
expect(turtle.position).toHaveBeenCalledWith({x: 10, y: 20, direction: Turtle.radians(30)});
recorder.queue[2].apply(undefined, [turtle]);
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(40));
recorder.queue[3].apply(undefined, [turtle]);
expect(turtle.move).toHaveBeenCalledWith(50);
});
it("should record the TurtlePenDecorator commands: pendown and penup", function () {
recorder.pendown().penup();
expect(recorder.queue.length).toEqual(2);
recorder.queue[0].apply(undefined, [turtle]);
expect(turtle.pendown).toHaveBeenCalled();
recorder.queue[1].apply(undefined, [turtle]);
expect(turtle.penup).toHaveBeenCalled();
});
it("should be able to play() back all recorded commands", function () {
recorder.home().position({x: 10, y:20, direction:Turtle.radians(30)}).turn(Turtle.radians(40)).move(50).pendown().penup();
recorder.play(turtle);
expect(turtle.home).toHaveBeenCalled();
expect(turtle.position).toHaveBeenCalledWith({x: 10, y: 20, direction: Turtle.radians(30)});
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(40));
expect(turtle.move).toHaveBeenCalledWith(50);
expect(turtle.pendown).toHaveBeenCalled();
expect(turtle.penup).toHaveBeenCalled();
});
it("should be able to play() back all recorded commands animated", function () {
recorder.home().position({x: 10, y:20, direction:Turtle.radians(30)}).turn(Turtle.radians(40)).move(50).pendown().penup();
runs(function () {
recorder.play(turtle, 50);
expect(turtle.home).toHaveBeenCalled();
expect(turtle.position).not.toHaveBeenCalled();
});
waits(60);
runs(function () {
expect(turtle.position).toHaveBeenCalled();
expect(turtle.turn).not.toHaveBeenCalled();
});
waits(60);
runs(function () {
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(40));
expect(turtle.move).not.toHaveBeenCalled();
});
waits(60);
runs(function () {
expect(turtle.move).toHaveBeenCalledWith(50);
expect(turtle.pendown).not.toHaveBeenCalled();
});
waits(60);
runs(function () {
expect(turtle.pendown).toHaveBeenCalled();
expect(turtle.penup).not.toHaveBeenCalled();
});
waits(60);
runs(function () {
expect(turtle.penup).toHaveBeenCalled();
});
});
it("should be able to animate recorded commands in chunks", function () {
runs(function () {
recorder.turn(0).move(5);
recorder.turn(Turtle.radians(10)).move(15);
recorder.turn(Turtle.radians(20)).move(25);
recorder.turn(Turtle.radians(30)).move(35);
recorder.turn(Turtle.radians(40)).move(45);
recorder.turn(Turtle.radians(50)).move(55);
recorder.turn(Turtle.radians(60)).move(65);
recorder.play(turtle, 50, 4);
});
waits(51);
runs(function () {
expect(turtle.turn).toHaveBeenCalledWith(0);
expect(turtle.move).toHaveBeenCalledWith(5);
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(10));
expect(turtle.move).toHaveBeenCalledWith(15);
expect(turtle.turn).not.toHaveBeenCalledWith(Turtle.radians(30));
});
waits(51);
runs(function () {
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(20));
expect(turtle.move).toHaveBeenCalledWith(25);
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(30));
expect(turtle.move).toHaveBeenCalledWith(35);
expect(turtle.turn).not.toHaveBeenCalledWith(Turtle.radians(50));
});
waits(51);
runs(function () {
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(40));
expect(turtle.move).toHaveBeenCalledWith(45);
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(50));
expect(turtle.move).toHaveBeenCalledWith(55);
expect(turtle.turn).not.toHaveBeenCalledWith(Turtle.radians(70));
});
waits(120);
runs(function () {
expect(turtle.turn).toHaveBeenCalledWith(Turtle.radians(60));
expect(turtle.move).toHaveBeenCalledWith(65);
expect(turtle.turn).not.toHaveBeenCalledWith(Turtle.radians(70));
});
});
});