Skip to content

Instantly share code, notes, and snippets.

@jbrains
Forked from patrickwilsonwelsh/RulesForNextGenerationTest
Created January 20, 2011 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrains/788684 to your computer and use it in GitHub Desktop.
Save jbrains/788684 to your computer and use it in GitHub Desktop.
One way to remove duplication from PWW's tests
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);
}
@Override
public String toString() {
return "I'm " + state + " and I have " + numberOfLiveNeighbors
+ " live neighbors.";
}
}
package rules;
public enum CellState {
DEAD, ALIVE
}
package rules
import spock.lang.Specification
import spock.lang.Unroll
class EvolveCellOneGeneration extends Specification {
@Unroll("a dead cell with #liveNeighbors live neighbors dies")
def "rules for cells to live or die"() {
given:
def inNextGeneration = new RulesForNextGeneration()
def cell = initiallyAlive ? new Cell().thatIsAlive().withLiveNeighbors(liveNeighbors) : new Cell().withLiveNeighbors(liveNeighbors)
expect:
inNextGeneration.itTurnsOutThat(cell).comesAlive() == endsUpAlive
where:
liveNeighbors | initiallyAlive | endsUpAlive
0 | false | false
1 | false | false
2 | false | false
3 | false | true
4 | false | false
0 | true | false
1 | true | false
2 | true | true
3 | true | true
4 | true | false
}
}
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;
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