Package pair.gameoflife.a20140102

Source Code of pair.gameoflife.a20140102.NeighbourCountTest$LinkedCoordsBuilder

package pair.gameoflife.a20140102;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import pair.gameoflife.a20140102.CoordTest.Coord;
import pair.gameoflife.a20140102.LinkedCoordsTest.LinkedCoords;
import pair.gameoflife.a20140102.LinkedCoordsTest.OccupiedCoords;

import org.junit.Test;

public class NeighbourCountTest {

    private static final Coord CENTER = new Coord(1, 1);

    public static class LinkedCoordsBuilder {

        private final LinkedCoords currentLinkedCoords;

        public LinkedCoordsBuilder() {
            this(LinkedCoords.EMPTY);
        }

        private LinkedCoordsBuilder(LinkedCoords currentLinkedCoords) {
            this.currentLinkedCoords = currentLinkedCoords;
        }

        public LinkedCoordsBuilder with(int x, int y) {
            Coord coord = new Coord(x, y);
            OccupiedCoords linkedCoords = new OccupiedCoords(coord, currentLinkedCoords);
            return new LinkedCoordsBuilder(linkedCoords);
        }

        public int countNeighbours(Coord center) {
            return currentLinkedCoords.countNeighbours(center);
        }
       
    }
   
    private LinkedCoordsBuilder coords() {
        return new LinkedCoordsBuilder();
    }

    @Test
    public void shouldCountNoNeighboursAsZero() {
        assertThat(coords().countNeighbours(CENTER), is(0));
    }

    @Test
    public void shouldCountNeighbouringCoordinateAsOneNeighbour() {
        assertThat(coords().with(1, 0).countNeighbours(CENTER), is(1));
    }

    @Test
    public void shouldCountLeftAndRightNeighbouringCoordinatesAsTwoNeighbours() {
        assertThat(coords()
                .with(0, 1)
                .with(2, 1)
           .countNeighbours(CENTER), is(2));
    }

    @Test
    public void shouldNotCountTooFarNeighbourRightOrDownAsAnyNeighbours() {
        assertThat(coords()
                .with(3, 1)
                .with(4, 1)
                .with(1, 3)
           .countNeighbours(CENTER), is(0));
    }
   
    @Test
    public void shouldNotCountTooFarNeighbourLeftOrUpAsAnyNeighbours() {
        assertThat(coords()
                .with(0, 2)
                .with(2, 0)
           .countNeighbours(new Coord(2, 2)), is(0));
    }

    @Test
    public void shouldCountSelfAsNeighbours() {
        assertThat(coords().with(2, 2).countNeighbours(new Coord(2, 2)), is(1));
    }

}
TOP

Related Classes of pair.gameoflife.a20140102.NeighbourCountTest$LinkedCoordsBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.