Skip to content

Commit

Permalink
Refactors out utility methods for Rule Unit Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Richardson committed Jan 17, 2018
1 parent 6495903 commit 17c6b0a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
7 changes: 4 additions & 3 deletions Sharpasonne.Models/Board.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.Collections.Immutable;
using Optional;
using System.Collections.Generic;

namespace Sharpasonne.Models
{
public class Board
{
private IImmutableDictionary<Point, Placement> Grid { get; }
private ImmutableDictionary<Point, Placement> Grid { get; }
= ImmutableDictionary.Create<Point, Placement>();

public Board() { }

public Board(IImmutableDictionary<Point, Placement> grid)
public Board(IDictionary<Point, Placement> grid)
{
this.Grid = grid;
this.Grid = grid.ToImmutableDictionary();
}

public Option<Placement> Get(Point point)
Expand Down
37 changes: 11 additions & 26 deletions Sharpasonne.Tests/Rules/SpaceIsEmptyRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,27 @@

namespace Sharpasonne.Tests.Rules
{
public class SpaceIsEmptyRuleTests
public class SpaceIsEmptyRuleTests : UnitTest<PlaceTileGameAction>
{
[Fact]
public void When_TileIsEmpty_Then_True()
{
var tile = new TileBuilder()
.CreateTile(Enumerable.Empty<IFeature>())
.ValueOrFailure();
var centerPoint = new Point(0, 0);
var placement = new Placement(tile, Orientation.Top);
Assert.True(new SpaceIsEmptyRule().Verify(new Engine(), new PlaceTileGameAction(centerPoint, placement)));
AssertTrue<SpaceIsEmptyRule>(new Engine(), MakePlaceTile(0, 0));
}

[Fact]
public void When_TileIsNotEmpty_Then_False()
{
var tile = new TileBuilder()
.CreateTile(Enumerable.Empty<IFeature>())
.ValueOrFailure();
var action = MakePlaceTile(0, 0);
var board = new[] {action}.ToDictionary(
a => a.Point,
a => a.Placement
);

var centerPoint = new Point(0, 0);
var placement = new Placement(tile, Orientation.Top);
var board =
new Dictionary<Point, Placement>
{
[centerPoint] = placement
}
.ToImmutableDictionary();

var engine = new Mock<IEngine>();
engine
.Setup(mockEngine => mockEngine.Board)
.Returns(new Board(board));

var placeTileGameAction = new PlaceTileGameAction(centerPoint, placement);
Assert.False(new SpaceIsEmptyRule().Verify(engine.Object, placeTileGameAction));
AssertFalse<SpaceIsEmptyRule>(
MockEngine(new Board(board)),
action
);
}
}
}
4 changes: 2 additions & 2 deletions Sharpasonne.Tests/Rules/UniqueTileInstanceRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace Sharpasonne.Tests.Rules
{
public class UniqueTileInstanceRuleTests : UnitTest
public class UniqueTileInstanceRuleTests : UnitTest<PlaceTileGameAction>
{
[Fact]
public void When_BoardIsEmpty_Then_True()
{
AssertPlaceTileTrue<UniqueTileInstanceRule>(new Engine(), MakePlaceTile(0, 0));
AssertTrue<UniqueTileInstanceRule>(new Engine(), MakePlaceTile(0, 0));
}
}
}
25 changes: 22 additions & 3 deletions Sharpasonne.Tests/Rules/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,36 @@
using Sharpasonne.GameActions;
using Sharpasonne.Models;
using Xunit;
using Moq;

namespace Sharpasonne.Tests.Rules
{
public abstract class UnitTest
public abstract class UnitTest<TGameAction>
where TGameAction : IGameAction
{
protected void AssertPlaceTileTrue<TRule>(Engine engine, PlaceTileGameAction action)
where TRule : IRule<PlaceTileGameAction>, new()
protected void AssertTrue<TRule>(IEngine engine, TGameAction action)
where TRule : IRule<TGameAction>, new()
{
Assert.True(new TRule().Verify(engine, action));
}

protected void AssertFalse<TRule>(IEngine engine, TGameAction action)
where TRule : IRule<TGameAction>, new()
{
Assert.False(new TRule().Verify(engine, action));
}

protected IEngine MockEngine(Board board)
{
var engine = new Mock<IEngine>();

engine
.Setup(mockEngine => mockEngine.Board)
.Returns(board);

return engine.Object;
}

public PlaceTileGameAction MakePlaceTile(
int x,
int y)
Expand Down

0 comments on commit 17c6b0a

Please sign in to comment.