200 lines
6.5 KiB
C#
200 lines
6.5 KiB
C#
using GameHandler.DeathGame;
|
|
using GameModel;
|
|
using GameModel.DeathGame;
|
|
using NuGet.Frameworks;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GameHandler.UnitTests.DeathGame
|
|
{
|
|
[TestFixture]
|
|
public class DeathGameHandlerTests
|
|
{
|
|
private DeathGameSettings _settings;
|
|
private DeathGameHandler _gh;
|
|
private int[] _players;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_settings = new DeathGameSettings(6);
|
|
_gh = new DeathGameHandler();
|
|
_players = new[] { 1, 2, 3, 4, 5, 6 };
|
|
}
|
|
|
|
[Test]
|
|
public void CreateGameModelWithDuplicatePlayers_ThrowsException()
|
|
{
|
|
var players = new[] { 1, 2, 3, 1 };
|
|
Assert.That(() => _gh.InitGameModel(players,_settings), Throws.TypeOf<InvalidDataException>());
|
|
}
|
|
|
|
[Test]
|
|
public void CreateGameModelWithTooManyPlayers_ThrowsException()
|
|
{
|
|
var players = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
|
|
Assert.That(() => _gh.InitGameModel(players, _settings), Throws.TypeOf<InvalidDataException>());
|
|
}
|
|
|
|
[Test]
|
|
public void CreateGameModelWithTooLessPlayers_ThrowsException()
|
|
{
|
|
var players = new[] { 1, 2 };
|
|
Assert.That(() => _gh.InitGameModel(players, _settings), Throws.TypeOf<InvalidDataException>());
|
|
}
|
|
|
|
[Test]
|
|
[TestCase(2)]
|
|
[TestCase(13)]
|
|
public void InvalidMaxCoffinSize_ThrowsException(int maxCoffinSize)
|
|
{
|
|
var players = new[] { 1, 2, 3, 4 };
|
|
var invalidSettings = new DeathGameSettings(maxCoffinSize);
|
|
Assert.That(() => _gh.InitGameModel(players, invalidSettings), Throws.TypeOf<InvalidDataException>());
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void NewGameModel_IsWithoutXandLineCount()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
|
|
Assert.That(gm.Coffins.Count, Is.EqualTo(_players.Count()));
|
|
Assert.That(gm.Coffins.Any(_ => _.XCount > 0), Is.False);
|
|
Assert.That(gm.Coffins.Any(_ => _.LineCount > 0), Is.False);
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void InvalidFirstThrow_RaisesExpense()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
ExpenseTrigger tr = ExpenseTrigger.Circle;
|
|
_gh.ExpenseTriggered += (trigger) => tr = trigger;
|
|
var newGm = _gh.CalcNextModel(gm, 2, false, true);
|
|
|
|
Assert.That(tr, Is.EqualTo(ExpenseTrigger.FirstThrowFail));
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void AfterFirstThrow_SomeFirstPlayerHasAnX()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
|
|
var newGm = _gh.CalcNextModel(gm, 2, false, true);
|
|
Assert.That(newGm.Coffins.Last().XCount, Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public void AfterThrowIntoAllPins_SomeFirstPlayerHasAnX()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
|
|
var newGm = _gh.CalcNextModel(gm, 2, true, true);
|
|
Assert.That(newGm.Coffins.Last().XCount, Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public void AfterThrowWithoutPins_SomeFirstPlayerHasAnLine()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
|
|
var newGm = _gh.CalcNextModel(gm, 0, false, false);
|
|
Assert.That(newGm.Coffins.Last().XCount, Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public void ThirdX_ChangesToAddedLineAndZeroX()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
var coffins = gm.Coffins.ToList();
|
|
var first = gm.Coffins.First() with { XCount = 2 };
|
|
coffins.RemoveAt(0);
|
|
coffins.Insert(0, first);
|
|
var gm2 = gm with { Coffins = coffins.ToArray(), Id = 2 };
|
|
|
|
var newGm = _gh.CalcNextModel(gm2, 2, true, true);
|
|
|
|
Assert.That(newGm.Coffins.Last().XCount, Is.EqualTo(0));
|
|
Assert.That(newGm.Coffins.Last().LineCount, Is.EqualTo(first.LineCount + 1));
|
|
}
|
|
|
|
[Test]
|
|
public void FirstPlayerIsLast_AfterModelUpdate()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
|
|
var first = gm.Coffins.First();
|
|
DeathGameModel? newGm = null;
|
|
|
|
Assert.That(() => { newGm = _gh.CalcNextModel(gm, 2, false, false); }, Throws.Nothing);
|
|
|
|
Assert.That(first.PlayerId,Is.EqualTo(newGm.Coffins.Last().PlayerId));
|
|
Assert.That(gm.Coffins.Count, Is.EqualTo(newGm.Coffins.Count()));
|
|
}
|
|
|
|
[Test]
|
|
public void AllPins_CausesLineAtPreviousPlayer()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
|
|
var prev = gm.Coffins.Last();
|
|
var newGm = _gh.CalcNextModel(gm, 2, false, true);
|
|
var prevNew = newGm.Coffins.First(_ => _.PlayerId == prev.PlayerId);
|
|
|
|
Assert.That(prevNew.LineCount, Is.EqualTo(prev.LineCount + 1));
|
|
}
|
|
|
|
[Test]
|
|
public void LineLimit_CausesDeath()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
Coffin? deadCoffin = null;
|
|
_gh.CoffinCompleted += (c) => deadCoffin = c;
|
|
|
|
var coffins = gm.Coffins.ToList();
|
|
var first = gm.Coffins.First() with { LineCount = _settings.MaxCoffinSize };
|
|
coffins.RemoveAt(0);
|
|
coffins.Insert(0, first);
|
|
var gm2 = gm with { Coffins = coffins.ToArray(), Id = 2 };
|
|
|
|
var newGm = _gh.CalcNextModel(gm2, 0, false, false);
|
|
|
|
|
|
|
|
Assert.That(deadCoffin.PlayerId, Is.EqualTo(first.PlayerId));
|
|
}
|
|
|
|
[Test]
|
|
public void PreviousPlayerIsDead_WhenCurrentClearsBoard()
|
|
{
|
|
var gm = _gh.InitGameModel(_players, _settings);
|
|
Coffin? deadCoffin = null;
|
|
_gh.CoffinCompleted += (c) => deadCoffin = c;
|
|
|
|
var coffins = gm.Coffins.ToList();
|
|
var last = gm.Coffins.Last() with { LineCount = _settings.MaxCoffinSize };
|
|
coffins.RemoveAt(gm.Coffins.Length-1);
|
|
coffins.Insert(gm.Coffins.Length - 1, last);
|
|
var gm2 = gm with { Coffins = coffins.ToArray(), Id = 2 };
|
|
|
|
var newGm = _gh.CalcNextModel(gm2, 2, false, true);
|
|
|
|
|
|
|
|
Assert.That(deadCoffin.PlayerId, Is.EqualTo(last.PlayerId));
|
|
}
|
|
|
|
[Test]
|
|
public void NexxtPlayerIsDead_WhenCurrentCausesThirdX()
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|