|
1 | 1 | import { Subject } from 'rxjs'; |
2 | 2 | import { Cell } from './cell.js'; |
3 | | -import { State } from './util.js'; |
| 3 | + |
| 4 | +import { State } from './state.js'; |
4 | 5 |
|
5 | 6 | describe('Cells', () => { |
6 | 7 | /** @type {Subject<void>} */ |
7 | 8 | let ticker; |
8 | 9 |
|
9 | | - /** @type {Subject<number, number, State>} */ |
| 10 | + /** @type {Subject<[number, number, State]>} */ |
10 | 11 | let changeEmitter; |
11 | 12 |
|
12 | 13 | /** @type {Subject<void>} */ |
@@ -41,62 +42,82 @@ describe('Cells', () => { |
41 | 42 | ticker = new Subject(); |
42 | 43 | changeEmitter = new Subject(); |
43 | 44 | stopSignal = new Subject(); |
44 | | - cell = new Cell(ticker.asObservable(), 1, 1, changeEmitter); |
45 | 45 | }); |
46 | 46 |
|
47 | | - it('Any live cell with fewer than two live neighbours dies, as if by underpopulation', (done) => { |
48 | | - cell.state = State.ALIVE; |
49 | | - setupNeighbors(cell, [State.ALIVE, State.DEAD]); |
| 47 | + describe('Game of Life', () => { |
| 48 | + beforeEach(() => { |
| 49 | + cell = new Cell(ticker.asObservable(), 1, 1, changeEmitter); |
| 50 | + }); |
| 51 | + |
| 52 | + it('Any live cell with fewer than two live neighbours dies, as if by underpopulation', (done) => { |
| 53 | + cell.state = State.ALIVE; |
| 54 | + setupNeighbors(cell, [State.ALIVE, State.DEAD]); |
50 | 55 |
|
51 | | - cell.listenUntil(stopSignal).subscribe({ |
52 | | - next: expectNext(State.DEAD, done), |
| 56 | + cell.listenUntil(stopSignal).subscribe({ |
| 57 | + next: expectNext(State.DEAD, done), |
| 58 | + }); |
| 59 | + ticker.next(void 0); |
| 60 | + stopSignal.next(void 0); |
53 | 61 | }); |
54 | | - ticker.next(void 0); |
55 | | - stopSignal.next(void 0); |
56 | | - }); |
57 | 62 |
|
58 | | - it('Any live cell with two live neighbours lives on to the next generation', (done) => { |
59 | | - cell.state = State.ALIVE; |
60 | | - setupNeighbors(cell, [State.ALIVE, State.ALIVE]); |
| 63 | + it('Any live cell with two live neighbours lives on to the next generation', (done) => { |
| 64 | + cell.state = State.ALIVE; |
| 65 | + setupNeighbors(cell, [State.ALIVE, State.ALIVE]); |
61 | 66 |
|
62 | | - cell.listenUntil(stopSignal).subscribe({ |
63 | | - next: expectNext(State.ALIVE, done), |
| 67 | + cell.listenUntil(stopSignal).subscribe({ |
| 68 | + next: expectNext(State.ALIVE, done), |
| 69 | + }); |
| 70 | + ticker.next(void 0); |
| 71 | + stopSignal.next(void 0); |
64 | 72 | }); |
65 | | - ticker.next(void 0); |
66 | | - stopSignal.next(void 0); |
67 | | - }); |
68 | 73 |
|
69 | | - it('Any live cell with three live neighbours lives on to the next generation', (done) => { |
70 | | - cell.state = State.ALIVE; |
71 | | - setupNeighbors(cell, [State.ALIVE, State.ALIVE, State.ALIVE]); |
| 74 | + it('Any live cell with three live neighbours lives on to the next generation', (done) => { |
| 75 | + cell.state = State.ALIVE; |
| 76 | + setupNeighbors(cell, [State.ALIVE, State.ALIVE, State.ALIVE]); |
72 | 77 |
|
73 | | - cell.listenUntil(stopSignal).subscribe({ |
74 | | - next: expectNext(State.ALIVE, done), |
| 78 | + cell.listenUntil(stopSignal).subscribe({ |
| 79 | + next: expectNext(State.ALIVE, done), |
| 80 | + }); |
| 81 | + ticker.next(void 0); |
| 82 | + stopSignal.next(void 0); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Any live cell with more than three live neighbours dies, as if by overpopulation', (done) => { |
| 86 | + cell.state = State.ALIVE; |
| 87 | + setupNeighbors(cell, [ |
| 88 | + State.ALIVE, |
| 89 | + State.ALIVE, |
| 90 | + State.ALIVE, |
| 91 | + State.ALIVE, |
| 92 | + ]); |
| 93 | + |
| 94 | + cell.listenUntil(stopSignal).subscribe({ |
| 95 | + next: expectNext(State.DEAD, done), |
| 96 | + }); |
| 97 | + ticker.next(void 0); |
| 98 | + stopSignal.next(void 0); |
75 | 99 | }); |
76 | | - ticker.next(void 0); |
77 | | - stopSignal.next(void 0); |
78 | | - }); |
79 | 100 |
|
80 | | - it('Any live cell with more than three live neighbours dies, as if by overpopulation', (done) => { |
81 | | - cell.state = State.ALIVE; |
82 | | - setupNeighbors(cell, [State.ALIVE, State.ALIVE, State.ALIVE, State.ALIVE]); |
| 101 | + it('Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction', (done) => { |
| 102 | + cell.state = State.DEAD; |
| 103 | + setupNeighbors(cell, [State.ALIVE, State.ALIVE, State.ALIVE]); |
83 | 104 |
|
84 | | - cell.listenUntil(stopSignal).subscribe({ |
85 | | - next: expectNext(State.DEAD, done), |
| 105 | + cell.listenUntil(stopSignal).subscribe({ |
| 106 | + next: expectNext(State.ALIVE, done), |
| 107 | + }); |
| 108 | + ticker.next(void 0); |
| 109 | + stopSignal.next(void 0); |
86 | 110 | }); |
87 | | - ticker.next(void 0); |
88 | | - stopSignal.next(void 0); |
89 | 111 | }); |
90 | 112 |
|
91 | | - it('Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction', (done) => { |
92 | | - cell.state = State.DEAD; |
93 | | - setupNeighbors(cell, [State.ALIVE, State.ALIVE, State.ALIVE]); |
| 113 | + describe('Overrides and defaults', () => { |
| 114 | + beforeEach(() => { |
| 115 | + cell = new Cell(ticker.asObservable(), 1, 1, changeEmitter); |
| 116 | + }); |
94 | 117 |
|
95 | | - cell.listenUntil(stopSignal).subscribe({ |
96 | | - next: expectNext(State.ALIVE, done), |
| 118 | + test('Cell.toString', () => { |
| 119 | + expect(cell.toString()).toEqual('Cell(1,1): dead'); |
97 | 120 | }); |
98 | | - ticker.next(void 0); |
99 | | - stopSignal.next(void 0); |
100 | 121 | }); |
101 | 122 | }); |
102 | 123 |
|
|
0 commit comments