forked from overengineering/sharpasonne
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added EngineTest. Adds Engine constructor with some validation. Chang…
…es IRule definition.
- Loading branch information
Lee Richardson
committed
Dec 20, 2017
1 parent
febeb2c
commit 7ab8e92
Showing
5 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters