diff --git a/Tests/TwoDimension/AreaTests.cs b/Tests/TwoDimension/AreaTests.cs index fa861bb..fec1832 100644 --- a/Tests/TwoDimension/AreaTests.cs +++ b/Tests/TwoDimension/AreaTests.cs @@ -170,15 +170,63 @@ public void PositionSet() [Test] public void PositionGet() { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - Assert.Fail(); + Area area = new Area(); + var mockTile = new Mock(); + var tilePosition = new Position2D(0, 0); + var tilePositionNotFound = new Position2D(1, 0); + + mockTile.Object.SetPosition(area, tilePosition); + area.Add(mockTile.Object); + + // Test Nulls + Assert.That(() => area.Get(null), Throws.ArgumentNullException); + + // Test Same Tile + Assert.AreSame(mockTile.Object, area.Get(tilePosition)); + + // Test Tile Not Found + Assert.AreNotSame(mockTile.Object, area.Get(tilePositionNotFound)); } [Test] public void PositionGetNeighbours() { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - Assert.Fail(); + Area area = new Area(); + var mockTile = new Mock(); + var tilePosition = new Position2D(0, 0); + var mockTileNotInArea = new Mock(); + var mockFirstNeighbourTile = new Mock(); + var firstNeighbourTilePosition = new Position2D(1, 0); + var mockSecondNeighbourTile = new Mock(); + var secondNeighbourTilePosition = new Position2D(1, 1); + var mockNotANeighbourTile = new Mock(); + var notANeighbourTilePosition = new Position2D(4, 0); + + mockTile.Object.SetPosition(area, tilePosition); + area.Add(mockTile.Object); + + // Test Nulls + Assert.That(() => area.GetNeighbours(null), Throws.ArgumentNullException); + + // Test Tile Not In Area + Assert.That(() => area.GetNeighbours(mockTileNotInArea.Object), Throws.ArgumentException); + + // Test No Neighbours + Assert.Null(area.GetNeighbours(mockTile.Object)); + + // Test One Or More Neighbours + mockFirstNeighbourTile.Object.SetPosition(area, firstNeighbourTilePosition); + area.Add(mockFirstNeighbourTile.Object); + mockSecondNeighbourTile.Object.SetPosition(area, secondNeighbourTilePosition); + area.Add(mockSecondNeighbourTile.Object); + mockNotANeighbourTile.Object.SetPosition(area, notANeighbourTilePosition); + area.Add(mockNotANeighbourTile.Object); + + var neighbourTiles = area.GetNeighbours(mockTile.Object); + + Assert.IsTrue(neighbourTiles.Contains(mockFirstNeighbourTile.Object)); + Assert.IsTrue(neighbourTiles.Contains(mockSecondNeighbourTile.Object)); + Assert.IsFalse(neighbourTiles.Contains(mockNotANeighbourTile.Object)); } } } diff --git a/TileSystem/Implementation/TwoDimension/Area.cs b/TileSystem/Implementation/TwoDimension/Area.cs index 729705f..75595b4 100644 --- a/TileSystem/Implementation/TwoDimension/Area.cs +++ b/TileSystem/Implementation/TwoDimension/Area.cs @@ -141,8 +141,12 @@ public virtual bool Remove(ITile tile) /// Tile instance or null public ITile Get(IPosition position) { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - throw new NotImplementedException(); + if (position == null) + { + throw new ArgumentNullException("position", "Position can not be null"); + } + + return tiles.Find(tile => tile.Position.CompareTo(position) == 0); } /// @@ -152,8 +156,28 @@ public ITile Get(IPosition position) /// List of neighbours or null public List GetNeighbours(ITile tile) { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - throw new NotImplementedException(); + if (tile == null) + { + throw new ArgumentNullException("tile", "Tile can not be null"); + } + + if (tiles.Contains(tile) == false) + { + throw new ArgumentException("Tile not in Area", "tile"); + } + + List neigbourTiles = tiles.FindAll(t => + Math.Abs(((Position2D)t.Position).X - ((Position2D)tile.Position).X) == 1 || + Math.Abs(((Position2D)t.Position).Y - ((Position2D)tile.Position).Y) == 1); + + if (neigbourTiles.Count <= 0) + { + return null; + } + else + { + return neigbourTiles; + } } ///