👔 Add Turn class and first UTs

pull/53/head
Alexis Drai 2 years ago
parent 9d898c0337
commit d734fd9215

@ -0,0 +1,100 @@
using System;
namespace Model
{
/// <summary>
/// a Turn consists of a Player, a DateTime, and a IEnumerable of AbstractDieFace
/// Like a turn in some game.
/// <br/>
/// Two turns are equal if they are litterally the same instance in RAM
/// (default behaviors Equals() and GetHashCode())
/// </summary>
public class Turn
{
/// <summary>
/// the date and time, adjusted to UTC
/// </summary>
public readonly DateTime when;
/// <summary>
/// the Player who rolled the dice
/// </summary>
public readonly Player player;
/*
public IEnumerable<AbstractDieFace> Faces { get; private set; }
*/
/// <summary>
/// this private constructor is to be used only by factories
/// </summary>
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
// TODO add faces
private Turn(DateTime when, Player player/*, IEnumerable<AbstractDieFace> faces*/)
{
this.when = when;
this.player = player;
/*Faces = faces;*/
}
/// <summary>
/// creates a Turn with a specified time, passed as a parameter.
/// <br/>
/// whatever the DateTimeKind of <paramref name="when"/> might be,
/// it will become UTC during construction
/// </summary>
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
/// <returns>a new Turn object</returns>
// TODO add faces
public static Turn CreateWithSpecifiedTime(DateTime when, Player player/*, IEnumerable<AbstractDieFace> faces*/)
{
// TODO add validation for faces too
if (player == null)
{
throw new ArgumentNullException(nameof(player), "param should not be null");
}
if (when.Kind != DateTimeKind.Utc)
{
when = when.ToUniversalTime();
}
return new Turn(when, player/*, faces*/);
}
/// <summary>
/// creates a Turn with a default time, which is "now" in UTC.
/// </summary>
/// <param name="player">player who played the turn</param>
/// <returns>a new Turn object</returns>
// TODO add faces
public static Turn CreateWithDefaultTime(Player player/*, IEnumerable<AbstractDieFace> faces*/)
{
return CreateWithSpecifiedTime(DateTime.UtcNow, player/*, faces*/);
}
//TODO add faces
/// <summary>
/// represents a turn in string format
/// </summary>
/// <returns>a turn in string format</returns>
public override string ToString()
{
//string[] datetime = this.when.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T");
//string date = datetime[0];
//string time = datetime[1];
return String.Format("{0} -- {1} rolled {2}",
ToStringIsoWithZ(),
this.player.ToString(),
"<face>, <face>, <face>...");
}
private string ToStringIsoWithZ()
{
return this.when.ToString("s", System.Globalization.CultureInfo.InvariantCulture) + "Z";
}
}
}

@ -0,0 +1,58 @@
using Model;
using System;
using Xunit;
namespace Tests.Model_UTs
{
public class TurnTest
{
[Fact]
public void TestCreateWithSpecifiedTimeNotUTCThenValid()
{
}
[Fact]
public void TestCreateWithSpecifiedTimeUTCThenValid()
{
}
[Fact]
public void TestCreateWithSpecifiedTimeNullPlayerThenException()
{
}
[Fact]
public void TestCreateWithDefaultTimeThenValid()
{
// check that the date (not the time part) is the same as today
// ... would fail tests at 11:59:59pm -- acceptable (?)
}
[Fact]
public void TestCreateWithDefaultTimeNullPlayerThenException()
{
}
[Fact]
public void TestToStringValidIfAllNormal()
{
// Arrange
DateTime dateTime = new(year: 2018, month: 06, day: 15, hour: 16, minute: 30, second: 0, kind: DateTimeKind.Utc);
string name = "Bobby";
Player player = new(name);
string expected = $"2018-06-15T16:30:00Z -- {name} rolled <face>, <face>, <face>...";
Turn turn = Turn.CreateWithSpecifiedTime(dateTime, player);
// Act
string actual = turn.ToString();
// Assert
Assert.Equal(expected, actual);
}
}
}
Loading…
Cancel
Save