Last active
November 18, 2017 11:54
-
-
Save karfau/edfd56a751fe45fc67391f8af0247e19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Neighbors = {neighbors: number}; | |
type State = Neighbors & {alive: boolean}; | |
function deadCell({neighbors}: Neighbors): State { | |
return {alive: false, neighbors}; | |
} | |
function livingCell({neighbors}: Neighbors): State { | |
return {alive: true, neighbors}; | |
} | |
function next({alive, neighbors}: State): State { | |
switch(true) { | |
case (!alive && neighbors == 3): | |
case (alive && (neighbors == 2 || neighbors == 3)): | |
return livingCell({neighbors}); | |
default: | |
return deadCell({neighbors}); | |
} | |
} | |
let expectLivingCellToDie = function (neighbors: { neighbors: number }) { | |
expect(next(livingCell(neighbors))).toEqual(deadCell(neighbors)); | |
}; | |
describe('cell', () => { | |
it('should die with zero neighbors', () => { | |
let neighbors = {neighbors: 0}; | |
expectLivingCellToDie(neighbors); | |
}); | |
it('should stay dead with two neighbors', () => { | |
let neighbors = {neighbors: 2}; | |
expect(next(deadCell(neighbors))).toEqual(deadCell(neighbors)); | |
}); | |
it('should stay alive with two neighbors', () => { | |
let neighbors = {neighbors: 2}; | |
expect(next(livingCell(neighbors))).toEqual(livingCell(neighbors)); | |
}); | |
it('should stay alive with three neighbors', () => { | |
let neighbors = {neighbors: 3}; | |
expect(next(livingCell(neighbors))).toEqual(livingCell(neighbors)); | |
}); | |
it('should become alive with three neighbors', () => { | |
let neighbors = {neighbors: 3}; | |
expect(next(deadCell(neighbors))).toEqual(livingCell(neighbors)); | |
}); | |
it('should die with four neighbors', () => { | |
let neighbors = {neighbors: 4}; | |
expectLivingCellToDie(neighbors); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment