|
1 | 1 | import { describe, expect, test } from '@jest/globals'; |
2 | 2 | import { birdsInWeek, fixBirdCountLog, totalBirdCount } from './bird-watcher'; |
3 | 3 |
|
| 4 | +const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom'); |
| 5 | +const customLogSymbol = Symbol.for('exercism.javascript.util.log'); |
| 6 | + |
| 7 | +// Follow the instructions in case you are stuck on "list.method is not a function" |
| 8 | +class CountingReport { |
| 9 | + constructor(counts) { |
| 10 | + // Enables array[index] |
| 11 | + counts.forEach((count, index) => { |
| 12 | + this[index] = count; |
| 13 | + }); |
| 14 | + |
| 15 | + // Enables .length |
| 16 | + this.length = counts.length; |
| 17 | + } |
| 18 | + |
| 19 | + // Log value in non-upgraded environments |
| 20 | + toString() { |
| 21 | + return arrayOf(this).toString(); |
| 22 | + } |
| 23 | + |
| 24 | + // Overrides logging in node (ie. students working locally) |
| 25 | + [customInspectSymbol]() { |
| 26 | + return `Seen birds per day: ${arrayOf(this)}`; |
| 27 | + } |
| 28 | + |
| 29 | + // Overrides log overrides in web environment (ie. students working in editor) |
| 30 | + [customLogSymbol]() { |
| 31 | + return `Seen birds per day: ${arrayOf(this)}`; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function report(...values) { |
| 36 | + return new CountingReport(values); |
| 37 | +} |
| 38 | + |
| 39 | +function arrayOf(countingReport) { |
| 40 | + return Array.from( |
| 41 | + { length: countingReport.length }, |
| 42 | + (_, i) => countingReport[i], |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +function randomArray(length) { |
| 47 | + return Array.from({ length }, () => Math.floor(Math.random() * 8)); |
| 48 | +} |
| 49 | + |
4 | 50 | describe('totalBirdCount', () => { |
5 | 51 | test('calculates the correct total number of birds', () => { |
6 | | - const birdsPerDay = [9, 0, 8, 4, 5, 1, 3]; |
| 52 | + const birdsPerDay = report(9, 0, 8, 4, 5, 1, 3); |
7 | 53 | expect(totalBirdCount(birdsPerDay)).toBe(30); |
8 | 54 | }); |
9 | 55 |
|
10 | 56 | test('works for a short bird count list', () => { |
11 | | - const birdsPerDay = [2]; |
| 57 | + const birdsPerDay = report(2); |
12 | 58 | expect(totalBirdCount(birdsPerDay)).toBe(2); |
13 | 59 | }); |
14 | 60 |
|
15 | 61 | test('works for a long bird count list', () => { |
16 | 62 | // prettier-ignore |
17 | | - const birdsPerDay = [2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6]; |
| 63 | + const birdsPerDay = report( |
| 64 | + 2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6 |
| 65 | + ); |
| 66 | + |
18 | 67 | expect(totalBirdCount(birdsPerDay)).toBe(57); |
19 | 68 | }); |
20 | 69 | }); |
21 | 70 |
|
22 | 71 | describe('birdsInWeek', () => { |
23 | 72 | test('calculates the number of birds in the first week', () => { |
24 | | - const birdsPerDay = [3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0, 8, 0]; |
| 73 | + const birdsPerDay = report(3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0, 8, 0); |
25 | 74 | expect(birdsInWeek(birdsPerDay, 1)).toBe(14); |
26 | 75 | }); |
27 | 76 |
|
28 | 77 | test('calculates the number of birds for a week in the middle of the log', () => { |
29 | 78 | // prettier-ignore |
30 | | - const birdsPerDay = [4, 7, 3, 2, 1, 1, 2, 0, 2, 3, 2, 7, 1, 3, 0, 6, 5, 3, 7, 2, 3]; |
| 79 | + const birdsPerDay = report(4, 7, 3, 2, 1, 1, 2, 0, 2, 3, 2, 7, 1, 3, 0, 6, 5, 3, 7, 2, 3); |
31 | 80 | expect(birdsInWeek(birdsPerDay, 2)).toBe(18); |
32 | 81 | }); |
33 | 82 |
|
34 | 83 | test('works when there is only one week', () => { |
35 | | - const birdsPerDay = [3, 0, 3, 3, 2, 1, 0]; |
| 84 | + const birdsPerDay = report(3, 0, 3, 3, 2, 1, 0); |
36 | 85 | expect(birdsInWeek(birdsPerDay, 1)).toBe(12); |
37 | 86 | }); |
38 | 87 |
|
39 | 88 | test('works for a long bird count list', () => { |
40 | | - const week21 = [2, 0, 1, 4, 1, 3, 0]; |
41 | | - const birdsPerDay = randomArray(20 * 7) |
42 | | - .concat(week21) |
43 | | - .concat(randomArray(10 * 7)); |
| 89 | + const week21 = report(2, 0, 1, 4, 1, 3, 0); |
| 90 | + const birdsPerDay = report( |
| 91 | + ...randomArray(20 * 7) |
| 92 | + .concat(arrayOf(week21)) |
| 93 | + .concat(randomArray(10 * 7)), |
| 94 | + ); |
44 | 95 |
|
45 | 96 | expect(birdsInWeek(birdsPerDay, 21)).toBe(11); |
46 | 97 | }); |
47 | 98 | }); |
48 | 99 |
|
49 | 100 | describe('fixBirdCountLog', () => { |
50 | 101 | test('returns a bird count list with the corrected values', () => { |
51 | | - const birdsPerDay = [3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0]; |
| 102 | + const birdsPerDay = report(3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0); |
52 | 103 | const expected = [4, 0, 6, 1, 1, 4, 2, 0, 4, 4, 4, 0]; |
53 | | - expect(fixBirdCountLog(birdsPerDay)).toEqual(expected); |
54 | | - }); |
55 | | - |
56 | | - test('does not create a new array', () => { |
57 | | - const birdsPerDay = [2, 0, 1, 4, 1, 3, 0]; |
| 104 | + fixBirdCountLog(birdsPerDay); |
58 | 105 |
|
59 | | - // This checks that the same object that was passed in is returned. |
60 | | - // https://jestjs.io/docs/expect#tobevalue |
61 | | - expect(Object.is(fixBirdCountLog(birdsPerDay), birdsPerDay)).toBe(true); |
| 106 | + expect(arrayOf(birdsPerDay)).toEqual(expected); |
62 | 107 | }); |
63 | 108 |
|
64 | 109 | test('works for a short bird count list', () => { |
65 | | - expect(fixBirdCountLog([4, 2])).toEqual([5, 2]); |
| 110 | + const birdsPerDay = report(4, 2); |
| 111 | + fixBirdCountLog(birdsPerDay); |
| 112 | + |
| 113 | + expect(arrayOf(birdsPerDay)).toEqual([5, 2]); |
66 | 114 | }); |
67 | 115 |
|
68 | 116 | test('works for a long bird count list', () => { |
69 | 117 | // prettier-ignore |
70 | | - const birdsPerDay = [2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6]; |
| 118 | + const birdsPerDay = report( |
| 119 | + 2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6 |
| 120 | + ); |
| 121 | + |
71 | 122 | // prettier-ignore |
72 | | - const expected = [3, 8, 5, 1, 4, 5, 1, 4, 2, 6, 1, 3, 1, 1, 6, 4, 2, 1, 3, 6]; |
73 | | - expect(fixBirdCountLog(birdsPerDay)).toEqual(expected); |
| 123 | + const expected = [ |
| 124 | + 3, 8, 5, 1, 4, 5, 1, 4, 2, 6, 1, 3, 1, 1, 6, 4, 2, 1, 3, 6 |
| 125 | + ] |
| 126 | + |
| 127 | + fixBirdCountLog(birdsPerDay); |
| 128 | + expect(arrayOf(birdsPerDay)).toEqual(expected); |
74 | 129 | }); |
75 | 130 | }); |
76 | | - |
77 | | -function randomArray(length) { |
78 | | - return Array.from({ length: length }, () => Math.floor(Math.random() * 8)); |
79 | | -} |
|
0 commit comments