Skip to content

Instantly share code, notes, and snippets.

@patrickwilsonwelsh
Created January 20, 2011 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save patrickwilsonwelsh/788676 to your computer and use it in GitHub Desktop.
Save patrickwilsonwelsh/788676 to your computer and use it in GitHub Desktop.
Exercise: Being Skooled By J.B. In DRY
package rules;
public class RulesForNextGeneration {
private Cell cell;
public RulesForNextGeneration itTurnsOutThat(Cell cell) {
this.cell = cell;
return this;
}
public boolean comesAlive() {
if (cell.isAlive() && cell.numberOfLiveNeighbors == 2) return true;
return (cell.numberOfLiveNeighbors == 3);
}
}
package rules;
public class Cell {
protected int numberOfLiveNeighbors;
private CellState state;
public Cell() {
this.state = CellState.DEAD;
}
public Cell withLiveNeighbors(int numberOfLiveNeighbors) {
this.numberOfLiveNeighbors = numberOfLiveNeighbors;
return this;
}
public Cell thatIsAlive() {
this.state = CellState.ALIVE;
return this;
}
public boolean isAlive() {
return (state == CellState.ALIVE);
}
}
package rules;
public enum CellState {
DEAD, ALIVE
}
package rules;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class RulesForNextGenerationTest {
private RulesForNextGeneration inNextGeneration;
@Before
public void setup() {
inNextGeneration = new RulesForNextGeneration();
}
@Test
public void deadCellWith_0_LiveNeighbors_StaysDead() throws Exception {
assertIsDeadNextTime(new Cell().withLiveNeighbors(0));
}
@Test
public void deadCellWith_1_LiveNeighbors_StaysDead() throws Exception {
assertIsDeadNextTime(new Cell().withLiveNeighbors(1));
}
@Test
public void deadCellWith_2_LiveNeighbors_StaysDead() throws Exception {
assertIsDeadNextTime(new Cell().withLiveNeighbors(2));
}
@Test
public void deadCellWith_3_LiveNeighbors_ComesAlive() throws Exception {
assertIsAliveNextTime(new Cell().withLiveNeighbors(3));
}
@Test
public void deadCellWith_4_LiveNeighbors_staysdead() throws Exception {
assertIsDeadNextTime(new Cell().withLiveNeighbors(4));
}
@Test
public void liveCellWith_0_LiveNeighbors_Dies() throws Exception {
assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(0));
}
@Test
public void liveCellWith_1_LiveNeighbors_Dies() throws Exception {
assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(1));
}
@Test
public void liveCellWith_2_LiveNeighbors_StaysAlive() throws Exception {
assertIsAliveNextTime(new Cell().thatIsAlive().withLiveNeighbors(2));
}
@Test
public void liveCellWith_3_LiveNeighbors_StaysAlive() throws Exception {
assertIsAliveNextTime(new Cell().thatIsAlive().withLiveNeighbors(3));
}
@Test
public void liveCellWith_4_LiveNeighbors_Dies() throws Exception {
assertIsDeadNextTime(new Cell().thatIsAlive().withLiveNeighbors(4));
}
private void assertIsAliveNextTime(Cell cell) {
assertTrue(inNextGeneration.itTurnsOutThat(cell).comesAlive());
}
private void assertIsDeadNextTime(Cell cell) {
assertFalse(inNextGeneration.itTurnsOutThat(cell).comesAlive());
}
// Test list
// TODO a dead cell with 0 live neighbors stays dead.
// TODO a dead cell with 1 live neighbors stays dead.
// TODO a dead cell with 2 live neighbors stays dead.
// TODO a dead cell with 3 live neighbors comes alive.
// TODO a dead cell with 4 live neighbors stays dead.
// TODO a live cell with 0 live neighbors dies.
// TODO a live cell with 1 live neighbor dies.
// TODO a live cell with 2 live neighbors stays alive.
// TODO a live cell with 3 live neighbors stays alive.
// TODO a live cell with 4 live neighbors dies.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment