-
Notifications
You must be signed in to change notification settings - Fork 732
/
Copy pathmodule1.test.js
550 lines (503 loc) · 16.6 KB
/
module1.test.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
const esprima = require("esprima");
const gameoflife = require("../js/gameoflife.js");
describe("Conway's Game of Life", () => {
const trimOfWhitespace = (s) => typeof s == "undefined" ? "" : s.replace(/\s/g, '');
function containsAll(targets = [], state) {
return targets.every(t => gameoflife.contains.call(state, t));
}
describe("Seed function", () => {
it("Should add a `seed` function. @seed-function", () => {
assert(
gameoflife.seed,
"Have you created and exported a `seed` function?"
);
assert(
gameoflife.seed &&
Array.isArray(gameoflife.seed([1, 2], [5, 6])) &&
gameoflife.seed([1, 2], [5, 6]).length === 2,
"Have you converted `arguments` to a real array?"
);
});
});
describe("Same function", () => {
it("Should have a `same` function. @same-function", () => {
assert(
gameoflife.same,
"Have you created and exported a `same` function?"
);
var sameNode;
esprima.parseModule(source, {}, function(node) {
if (node.id && node.id.name === "same") {
sameNode = node;
}
});
assert.equal(
sameNode.params.length,
2,
"Have you created a `same` function with two arguments?"
);
assert(
gameoflife.same([1, 2], [1, 2]),
"Have you created a `same` function that returns true if the two point parameters are the same?"
);
assert(
!gameoflife.same([1, 2], [5, 2]),
"Have you created a `same` function that returns false if the two point parameters are not the same?"
);
});
});
describe("Contains function", () => {
it("Should have a `contains` function. @contains-function", () => {
assert(
gameoflife.contains,
"Have you created and exported a `contains` function?"
);
const boundContains = (gameoflife.contains || (() => {})).bind([
[1, 2],
[3, 4],
[4, 4]
]);
assert(
boundContains([1, 2]) &&
boundContains([3, 4]) &&
boundContains([4, 4]),
"Have you implemented a check that the passed cell is in the passed game state?"
);
assert(
!(
boundContains([5, 6]) ||
boundContains([2, 1]) ||
boundContains([3, 3])
),
"Have you implemented a check that the passed cell is in the passed game state?"
);
});
});
describe("Printing a cell", () => {
it("Should have a printCell function. @printCell-function", () => {
assert(
gameoflife.printCell,
"Have you created and exported a `printCell` function?"
);
assert.equal(
trimOfWhitespace(gameoflife.printCell(
[1, 1],
[
[1, 1],
[2, 2]
]
)
),
"\u25A3",
"Have you returned '\u25A3' for living cells?"
);
assert.equal(
trimOfWhitespace(gameoflife.printCell(
[1, 2],
[
[1, 1],
[2, 2]
]
)
),
"\u25A2",
"Have you returned '\u25A2' for non-living cells?"
);
});
});
describe("Finding the corners", () => {
it("Should have a corners function. @corners-function", () => {
const corners = gameoflife.corners([
[2, 3],
[2, 1],
[4, 3],
[1, 1],
[2, 1],
[3, 1]
]);
assert(
gameoflife.corners,
"Have you created and exported a `corners` function?"
);
const zeroCorners = ((gameoflife.corners || (() => ({topRight: [], bottomLeft: []})))()) || {topRight: [], bottomLeft: []};
assert(
gameoflife.same &&
gameoflife.corners &&
gameoflife.same(zeroCorners.topRight, [0, 0]),
"Have you ensured that topRight is [0,0] if there are no living cells?"
);
assert(
gameoflife.same &&
gameoflife.corners &&
gameoflife.same(zeroCorners.bottomLeft, [0, 0]),
"Have you ensured that botomLeft is [0,0] if there are no living cells?"
);
assert(gameoflife.corners && corners.topRight, "");
assert(gameoflife.corners && Array.isArray(corners.topRight), "");
assert(gameoflife.corners && corners.topRight.length === 2, "");
assert(
gameoflife.same &&
gameoflife.corners &&
gameoflife.same(corners.topRight, [4, 3]),
"Have you implemented a corners function that returns the correct top right coordinate?"
);
assert(gameoflife.corners && corners.bottomLeft, "");
assert(gameoflife.corners && Array.isArray(corners.bottomLeft), "");
assert(gameoflife.corners && corners.bottomLeft.length === 2, "");
assert(
gameoflife.same &&
gameoflife.corners &&
gameoflife.same(corners.bottomLeft, [1, 1]),
"Have you implemented a corners function that returns the correct bottom left coordinate?"
);
var cornersNode;
esprima.parseModule(source, {}, function(node) {
if (
(node.type === "VariableDeclarator" ||
node.type === "FunctionDeclaration") &&
node.id &&
node.id.name === "corners"
) {
cornersNode = node;
}
});
assert(
typeof cornersNode != "undefined" &&
(cornersNode.params || cornersNode.init.params)[0].type ==
"AssignmentPattern",
"Have you provided a default value for the 'corners' function parameter?"
);
});
});
describe("Printing the game state", () => {
it("Should have a printCells function. @printCells-function", () => {
assert(
gameoflife.printCells,
"Have you created and exported a 'printCells' function?"
);
assert.equal(
typeof gameoflife.printCells([[3, 2]]), "string",
"Have you created a 'printCells' function that returns a string representation of the game state?"
);
assert.equal(trimOfWhitespace(gameoflife.printCells([[3, 2]])), "▣",
"Have you created a 'printCells' function that prints '▣' for each living cell, '▢' for each non-living cell and a newline character at the end of each row?"
);
assert.equal(trimOfWhitespace(gameoflife.printCells([
[3, 2],
[5, 2]
])), trimOfWhitespace("▣ ▢ \n ▣\n"),
"Have you created a 'printCells' function that prints '▣' for each living cell, '▢' for each non-living cell, a space in between each cell and a newline character at the end of each row?"
);
assert(
trimOfWhitespace(gameoflife.printCells([
[3, 2],
[2, 3],
[3, 3],
[3, 4],
[4, 4]
])),
"▢ ▣ ▣\n▣ ▣ ▢\n▢ ▣ ▢\n",
"Have you created a 'printCells' function that prints '▣' for each living cell, '▢' for each non-living cell, a space in between each cell and a newline character at the end of each row?"
);
});
});
describe("Finding neighbors", () => {
it("Should have a getNeighborsOf function. @getNeighborsOf-function", () => {
assert(
gameoflife.getNeighborsOf,
"Have you created and exported a 'getNeighborsOf' function?"
);
const neighborsOf3Comma4 = gameoflife.getNeighborsOf([3, 4]) || [];
assert(
neighborsOf3Comma4.length === 8,
"Have you created a function 'getNeighborsOf' that returns the eight neighbors of the given cell?"
);
assert(
gameoflife.contains.call(neighborsOf3Comma4, [2, 4]) &&
gameoflife.contains.call(neighborsOf3Comma4, [4, 4]) &&
gameoflife.contains.call(neighborsOf3Comma4, [2, 3]) &&
gameoflife.contains.call(neighborsOf3Comma4, [3, 3]) &&
gameoflife.contains.call(neighborsOf3Comma4, [4, 3]) &&
gameoflife.contains.call(neighborsOf3Comma4, [2, 5]) &&
gameoflife.contains.call(neighborsOf3Comma4, [3, 5]) &&
gameoflife.contains.call(neighborsOf3Comma4, [4, 5]),
"Have you created a function 'getNeighborsOf' that returns the eight neighbors of the given cell?"
);
const neighborsOfNeg1Neg1 = gameoflife.getNeighborsOf([-1, -1]);
assert(
gameoflife.contains.call(neighborsOfNeg1Neg1, [-2, -1]) &&
gameoflife.contains.call(neighborsOfNeg1Neg1, [0, -1]) &&
gameoflife.contains.call(neighborsOfNeg1Neg1, [-2, -2]) &&
gameoflife.contains.call(neighborsOfNeg1Neg1, [-1, -2]) &&
gameoflife.contains.call(neighborsOfNeg1Neg1, [0, -2]) &&
gameoflife.contains.call(neighborsOfNeg1Neg1, [-2, 0]) &&
gameoflife.contains.call(neighborsOfNeg1Neg1, [-1, 0]) &&
gameoflife.contains.call(neighborsOfNeg1Neg1, [0, 0]),
"Have you created a function 'getNeighborsOf' that returns the eight neighbors of the given cell?"
);
var getNeighborsNode;
esprima.parseModule(source, {}, function(node) {
if (node.type === "VariableDeclarator" && node.id.name === "getNeighborsOf") {
getNeighborsNode = node;
}
});
assert(getNeighborsNode, "Have you implemented an arrow function named `getNeighborsOf`?");
assert(
getNeighborsNode && getNeighborsNode.init.type === "ArrowFunctionExpression",
"Have you implemented an arrow function named `getNeighborsOf`?"
);
assert(
getNeighborsNode && getNeighborsNode.init.body.type === "ArrayExpression",
"Have you implemented an arrow function named `getNeighborsOf`?"
);
});
});
describe("Finding living neighbors", () => {
it("Should have a getLivingNeighbors function. @getLivingNeighbors-function", () => {
assert(
gameoflife.getLivingNeighbors,
"Have you created and exported a 'getLivingNeighbors' function?"
);
const rPentomino = [
[3, 2],
[2, 3],
[3, 3],
[3, 4],
[4, 4]
];
const rPentominoNeighborsOfFourThree = gameoflife.getLivingNeighbors(
[4, 3],
rPentomino
) || [];
assert.equal(rPentominoNeighborsOfFourThree.length, 4,
"Have you created a function 'getLivingNeighbors' that returns the living neighbors of the given cell?"
);
assert(
gameoflife.contains.call(rPentominoNeighborsOfFourThree, [4, 4]) &&
gameoflife.contains.call(rPentominoNeighborsOfFourThree, [3, 4]) &&
gameoflife.contains.call(rPentominoNeighborsOfFourThree, [3, 3]) &&
gameoflife.contains.call(rPentominoNeighborsOfFourThree, [3, 2]),
"Have you created a function 'getLivingNeighbors' that returns the living neighbors of the given cell?"
);
assert.equal(
gameoflife.getLivingNeighbors([6, 6], rPentomino).length, 0,
"Have you created a function 'getLivingNeighbors' that returns the living neighbors of the given cell?"
);
});
});
describe("Calculating a cell's future state", () => {
it("Should have a willBeAlive function. @willBeAlive-function", () => {
assert(
gameoflife.willBeAlive,
"Have you created and exported a 'willBeAlive' function?"
);
const rPentomino = [
[3, 2],
[2, 3],
[3, 3],
[3, 4],
[4, 4]
];
const cells = [
[2, 4],
[3, 4],
[4, 4],
[2, 3],
[3, 3],
[4, 3],
[2, 2],
[3, 2],
[4, 2]
];
const nextGen = cells.filter(c => gameoflife.willBeAlive(c, rPentomino));
assert.equal(
nextGen.length, 6,
"Have you created a 'willBeAlive' function that calculates if a cell will be alive in the next game state?"
);
assert(
containsAll(
[
[2, 2],
[3, 2],
[2, 3],
[2, 4],
[3, 4],
[4, 4]
],
nextGen
),
"Have you created a 'willBeAlive' function that calculates if a cell will be alive in the next game state?"
);
});
});
describe("Calculating the next state", () => {
var start, next;
before(() => {
start = gameoflife.seed([3, 2], [2, 3], [3, 3], [3, 4], [4, 4]);
next = gameoflife.calculateNext(start);
});
it("Should have a calculateNext function. @calculateNext-function", () => {
assert(
gameoflife.calculateNext,
"Have you created and exported a 'calculateNext' function?"
);
assert(
containsAll(
[
[2, 2],
[3, 2],
[2, 3],
[2, 4],
[3, 4],
[4, 4]
],
next
),
"Have you created a 'calculateNext' function that calculates the next game state?"
);
const rPentominoPlusTwo = gameoflife.calculateNext(next);
assert(
containsAll(
[
[3, 5],
[2, 4],
[3, 4],
[1, 3],
[4, 3],
[2, 2],
[3, 2]
],
rPentominoPlusTwo
),
"Have you created a 'calculateNext' function that calculates the next game state?"
);
});
});
describe("Calculating multiple game states", () => {
it("Should have an iterate function. @iterate-function", () => {
assert(
gameoflife.iterate,
"Have you created and exported an 'iterate' function?"
);
const states = gameoflife.iterate(gameoflife.startPatterns.square, 2) || [];
assert.equal(
states.length, 3,
"Have you created an 'iterate' that calculates subsequent game states?"
);
assert(
containsAll(gameoflife.startPatterns.square, states[0]),
"Have you created an 'iterate' that calculates subsequent game states?"
);
assert(
containsAll(gameoflife.startPatterns.square, states[1]),
"Have you created an 'iterate' that calculates subsequent game states?"
);
assert(
containsAll(gameoflife.startPatterns.square, states[2]),
"Have you created an 'iterate' that calculates subsequent game states?"
);
const two = gameoflife.iterate(
[
[1, 1],
[2, 1],
[1, 2]
],
2
);
assert.equal(
two.length, 3,
"Have you created an 'iterate' that calculates subsequent game states?"
);
assert(
containsAll(
[
[1, 1],
[2, 1],
[1, 2],
[2, 2]
],
two[1]
),
"Have you created an 'iterate' that calculates subsequent game states?"
);
assert(
containsAll(
[
[1, 1],
[2, 1],
[1, 2],
[2, 2]
],
two[2]
),
"Have you created an 'iterate' that calculates subsequent game states?"
);
const rPentominoNext = gameoflife.iterate(
gameoflife.startPatterns.rpentomino,
2
);
assert.equal(
rPentominoNext.length, 3,
"Have you created an 'iterate' that calculates subsequent game states?"
);
assert(
containsAll(
[
[2, 2],
[3, 2],
[2, 3],
[2, 4],
[3, 4],
[4, 4]
],
rPentominoNext[1]
),
"Have you created an 'iterate' that calculates subsequent game states?"
);
assert(
containsAll(
[
[3, 5],
[2, 4],
[3, 4],
[1, 3],
[4, 3],
[2, 2],
[3, 2]
],
rPentominoNext[2]
),
"Have you created an 'iterate' that calculates subsequent game states?"
);
});
});
const withMonkeyPatchedLog = (action, logger) => {
const realLogger = console.log;
console.log = logger;
try {
action();
} finally {
console.log = realLogger;
}
};
describe("Main function", () => {
it("Should have a main function. @main-function", () => {
assert(
gameoflife.main,
"Have you created and exported a 'main' function?"
);
var results = "";
const logger = (i) => {
results += i;
};
// monkey patch console.log
withMonkeyPatchedLog(() => {
gameoflife.main("rpentomino", 2);
}, logger);
assert.equal(trimOfWhitespace(results),
trimOfWhitespace("▢ ▣ ▣\n▣ ▣ ▢\n▢ ▣ ▢\n▣ ▣ ▣\n▣ ▢ ▢\n▣ ▣ ▢\n▢ ▢ ▣ ▢\n▢ ▣ ▣ ▢\n▣ ▢ ▢ ▣\n▢ ▣ ▣ ▢\n"),
"Have you created a 'main' function ?"
);
});
});
});