Skip to content

Commit

Permalink
Added EngineTest. Adds Engine constructor with some validation. Chang…
Browse files Browse the repository at this point in the history
…es IRule definition.
  • Loading branch information
Lee Richardson committed Dec 20, 2017
1 parent febeb2c commit 7ab8e92
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 10 deletions.
5 changes: 2 additions & 3 deletions Sharpasonne.Models.Tests/FeatureListValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using Optional.Unsafe;
using Sharpasonne.Models.Features;
using Xunit;

Expand Down Expand Up @@ -31,10 +30,10 @@ void Given_OverlappingFeaturesTile_When_CheckingForOverlaps_Then_IsNotValid()
Assert.False(isValid);
}

[Fact]
/*[Fact]
void Given_EmptyTile_When_CheckingForFields_Then_AllFields()
{
throw new NotImplementedException();
}
}*/
}
}
64 changes: 64 additions & 0 deletions Sharpasonne.Tests/EngineTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Xunit;

namespace Sharpasonne.Tests
{
public class EngineTests
{
[Fact]
public void When_CreatingAnEngine_Then_BoardIsNotNull()
{
var engine = new Engine(
ImmutableQueue<IGameAction>.Empty,
ImmutableDictionary<Type, IImmutableList<IRule>>.Empty);

Assert.NotNull(engine.Board);
}

[Fact]
public void Given_ARuleSetWithANonGameActionKey_When_CreatingAnEngine_Then_Throw()
{
var ruleSet = new Dictionary<Type, IImmutableList<IRule>>
{
[typeof(string)] = ImmutableList.Create<IRule>(new DummyRule())
}.ToImmutableDictionary();


var exception = Record.Exception(() => new Engine(
ImmutableQueue<IGameAction>.Empty,
ruleSet));

Assert.IsType<ArgumentOutOfRangeException>(exception);
}

[Fact]
public void Given_ARuleSetWithAGameActionKey_When_CreatingAnEngine_Then_DontThrow()
{
var ruleSet = new Dictionary<Type, IImmutableList<IRule>>
{
[typeof(DummyGameAction)] = ImmutableList.Create<IRule>(new DummyRule())
}.ToImmutableDictionary();


var exception = Record.Exception(() => new Engine(
ImmutableQueue<IGameAction>.Empty,
ruleSet));

Assert.Null(exception);
}

class DummyRule : IRule
{
public bool Verify<T>(Engine engine, T gameAction) where T : IGameAction
{
throw new System.NotImplementedException();
}
}

class DummyGameAction : IGameAction
{
}
}
}
6 changes: 3 additions & 3 deletions Sharpasonne.Tests/Sharpasonne.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Moq" Version="4.7.145" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
36 changes: 34 additions & 2 deletions Sharpasonne/Engine.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
using System;
using System.Collections;
using System.Collections.Immutable;
using System.Linq;
using JetBrains.Annotations;
using Sharpasonne.Models;

namespace Sharpasonne
{
internal class Engine
// TODO: Consider rename.
public class Engine
{
public Board Board { get; } = new Board();

public Engine(
[NotNull] IImmutableQueue<IGameAction> gameActions,
[NotNull] IImmutableDictionary<Type, IImmutableList<IRule>> rules)
{
var nonGameActions = rules.Keys
.Where(k => !k.GetInterfaces().Any(i => i == typeof(IGameAction)))
.ToList();
if (nonGameActions.Any())
{
var typeNames = string.Join(", ", nonGameActions.Select(t => t.FullName));
var message = $"'{typeNames}' don't implement {nameof(IGameAction)}";
throw new ArgumentOutOfRangeException(message);
}
}

/*public Optional.Option<Engine, IEnumerable<string>> PlaceTile(
Point point,
[NotNull] Placement placement)
{
}*/
}

public interface IGameAction
{
Board Board { get; }
}
}
5 changes: 3 additions & 2 deletions Sharpasonne/IRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Sharpasonne
{
internal interface IRule
public interface IRule
{
bool Match(Board board, Placement placement, Tile tile);
bool Verify<T>(Engine engine, T gameAction)
where T : IGameAction;
}
}

0 comments on commit 7ab8e92

Please sign in to comment.