diff --git a/Sources/Model/Dice/DieManager.cs b/Sources/Model/Dice/DieManager.cs index ad3d8b9..200ea6b 100644 --- a/Sources/Model/Dice/DieManager.cs +++ b/Sources/Model/Dice/DieManager.cs @@ -4,6 +4,6 @@ namespace Model.Dice { public class DieManager { - private IEnumerable dice = new List(); + private readonly IEnumerable dice = new List(); } } diff --git a/Sources/Model/Dice/Faces/ColorDieFace.cs b/Sources/Model/Dice/Faces/ColorDieFace.cs index d787f3a..2c8fe12 100644 --- a/Sources/Model/Dice/Faces/ColorDieFace.cs +++ b/Sources/Model/Dice/Faces/ColorDieFace.cs @@ -38,7 +38,12 @@ namespace Model.Dice.Faces { // https://stackoverflow.com/questions/1139957/convert-integer-to-hexadecimal-and-back-again // maybe prepend it with a "#"... - return Value.ToString("X"); + return Value.ToString("X6").Insert(0, "#"); + } + + public override string ToString() + { + return GetPracticalValue().ToString(); } } } diff --git a/Sources/Model/Dice/Faces/ImageDieFace.cs b/Sources/Model/Dice/Faces/ImageDieFace.cs index 966832c..e33aa95 100644 --- a/Sources/Model/Dice/Faces/ImageDieFace.cs +++ b/Sources/Model/Dice/Faces/ImageDieFace.cs @@ -31,5 +31,10 @@ namespace Model.Dice.Faces { return string.Format("Assets/images/{0}", Value); } + + public override string ToString() + { + return GetPracticalValue().ToString(); + } } } diff --git a/Sources/Model/Dice/Faces/NumberDieFace.cs b/Sources/Model/Dice/Faces/NumberDieFace.cs index 0e236a9..82cd950 100644 --- a/Sources/Model/Dice/Faces/NumberDieFace.cs +++ b/Sources/Model/Dice/Faces/NumberDieFace.cs @@ -18,5 +18,10 @@ namespace Model.Dice.Faces { return Value; } + + public override string ToString() + { + return GetPracticalValue().ToString(); + } } } diff --git a/Sources/Model/Dice/FavGroupManager.cs b/Sources/Model/Dice/FavGroupManager.cs index 9a5bc78..8e589d7 100644 --- a/Sources/Model/Dice/FavGroupManager.cs +++ b/Sources/Model/Dice/FavGroupManager.cs @@ -6,7 +6,7 @@ namespace Model.Dice { private IEnumerable favGroups; - private DieManager dieManager; + private readonly DieManager dieManager; public FavGroupManager(DieManager dieManager) { diff --git a/Sources/Model/Games/Game.cs b/Sources/Model/Games/Game.cs index b0b4864..cb339df 100644 --- a/Sources/Model/Games/Game.cs +++ b/Sources/Model/Games/Game.cs @@ -10,7 +10,7 @@ namespace Model.Games { public string Name { get; private set; } - private IEnumerable turns = new List(); + private readonly IEnumerable turns = new List(); public Game(string name) { diff --git a/Sources/Model/Games/GameRunner.cs b/Sources/Model/Games/GameRunner.cs index 6cbcdf9..b7a30d8 100644 --- a/Sources/Model/Games/GameRunner.cs +++ b/Sources/Model/Games/GameRunner.cs @@ -10,9 +10,9 @@ namespace Model.Games { public class GameRunner { - private PlayerManager globalPlayerManager; - private FavGroupManager favGroupManager; - private IEnumerable games; + private readonly PlayerManager globalPlayerManager; + private readonly FavGroupManager favGroupManager; + private readonly IEnumerable games; public GameRunner(PlayerManager globalPlayerManager, FavGroupManager favGroupManager, IEnumerable games) { diff --git a/Sources/Model/Games/Turn.cs b/Sources/Model/Games/Turn.cs index d4a0c77..8ae0a37 100644 --- a/Sources/Model/Games/Turn.cs +++ b/Sources/Model/Games/Turn.cs @@ -1,4 +1,10 @@ using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using Model.Dice.Faces; using Model.Players; namespace Model.Games @@ -23,22 +29,22 @@ namespace Model.Games /// public readonly Player player; - // ... faces - + /// + /// the collection of Face that were rolled + /// + private readonly IEnumerable faces; /// /// this private constructor is to be used only by factories /// /// date and time of the turn /// player who played the turn - - // ... faces - private Turn(DateTime when, Player player/*, IEnumerable faces*/) + /// faces that were rolled + private Turn(DateTime when, Player player, IEnumerable faces) { this.when = when; this.player = player; - // ... faces - + this.faces = faces; } /// @@ -49,14 +55,16 @@ namespace Model.Games /// /// date and time of the turn /// player who played the turn + /// faces that were rolled /// a new Turn object - - // ... faces - public static Turn CreateWithSpecifiedTime(DateTime when, Player player/*, IEnumerable faces*/) + public static Turn CreateWithSpecifiedTime(DateTime when, Player player, IEnumerable faces) { - // ... faces - if (player == null) + if (faces is null || !faces.Any()) + { + throw new ArgumentException("param should not be null or empty", nameof(faces)); + } + if (player is null) { throw new ArgumentNullException(nameof(player), "param should not be null"); } @@ -65,38 +73,43 @@ namespace Model.Games when = when.ToUniversalTime(); } - return new Turn(when, player/*, faces*/); + return new Turn(when, player, faces); } /// /// creates a Turn with a default time, which is "now" in UTC. /// /// player who played the turn + /// faces that were rolled /// a new Turn object - - // ... faces - public static Turn CreateWithDefaultTime(Player player/*, IEnumerable faces*/) + public static Turn CreateWithDefaultTime(Player player, IEnumerable faces) { - return CreateWithSpecifiedTime(DateTime.UtcNow, player/*, faces*/); + return CreateWithSpecifiedTime(DateTime.UtcNow, player, faces); } - // ... faces - /// /// represents a turn in string format /// /// a turn in string format public override string ToString() { - return string.Format("{0} -- {1} rolled {2}", - ToStringIsoWithZ(), - player.ToString(), - ", , ..."); - } + string[] datetime = when.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T"); + string date = datetime[0]; + string time = datetime[1]; - private string ToStringIsoWithZ() - { - return when.ToString("s", System.Globalization.CultureInfo.InvariantCulture) + "Z"; + StringBuilder sb = new(); + + sb.AppendFormat("{0} {1} -- {2} rolled:", + date, + time, + player.ToString()); + + foreach (AbstractDieFace face in faces) + { + sb.Append(" " + face.ToString()); + } + + return sb.ToString(); } } } diff --git a/Sources/Tests/Model_UTs/TurnTest.cs b/Sources/Tests/Model_UTs/TurnTest.cs index 9551fa0..a801f3d 100644 --- a/Sources/Tests/Model_UTs/TurnTest.cs +++ b/Sources/Tests/Model_UTs/TurnTest.cs @@ -1,12 +1,35 @@ -using Model.Games; +using Model.Dice.Faces; +using Model.Games; using Model.Players; +using Newtonsoft.Json.Linq; using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; using Xunit; namespace Tests.Model_UTs { public class TurnTest + { + private readonly List FACES; + private readonly int FACE_ONE = 1; + private readonly int FACE_TWO = 12; + private readonly int FACE_THREE = 54; + private readonly int FACE_FOUR = 16548; + + public TurnTest() + { + FACES = new List + { + new NumberDieFace(FACE_ONE), + new NumberDieFace(FACE_TWO), + new ImageDieFace(FACE_THREE), + new ColorDieFace(FACE_FOUR) + }; + } + [Fact] public void TestCreateWithSpecifiedTimeNotUTCThenValid() { @@ -16,7 +39,7 @@ namespace Tests.Model_UTs Assert.NotEqual(DateTimeKind.Utc, dateTime.Kind); // Act - Turn turn = Turn.CreateWithSpecifiedTime(dateTime, player); + Turn turn = Turn.CreateWithSpecifiedTime(dateTime, player, FACES); // Assert Assert.Equal(DateTimeKind.Utc, turn.when.Kind); @@ -32,7 +55,7 @@ namespace Tests.Model_UTs Assert.Equal(DateTimeKind.Utc, dateTime.Kind); // Act - Turn turn = Turn.CreateWithSpecifiedTime(dateTime, player); + Turn turn = Turn.CreateWithSpecifiedTime(dateTime, player, FACES); // Assert Assert.Equal(DateTimeKind.Utc, turn.when.Kind); @@ -46,38 +69,54 @@ namespace Tests.Model_UTs DateTime dateTime = new(year: 2018, month: 06, day: 15, hour: 16, minute: 30, second: 0, kind: DateTimeKind.Utc); // Act - void action() => Turn.CreateWithSpecifiedTime(dateTime, null); + void action() => Turn.CreateWithSpecifiedTime(dateTime, null, FACES); // Assert Assert.Throws(action); } [Fact] - public void TestCreateWithDefaultTimeThenValid() + public void TestCreateWithSpecifiedTimeNullFacesThenException() { // Arrange - Player player = new("Chloe"); + DateTime dateTime = new(year: 2018, month: 06, day: 15, hour: 16, minute: 30, second: 0, kind: DateTimeKind.Utc); + Player player = new("Chucky"); // Act - Turn turn = Turn.CreateWithDefaultTime(player); + void action() => Turn.CreateWithSpecifiedTime(dateTime, player, null); // Assert - Assert.Equal(DateTimeKind.Utc, turn.when.Kind); - Assert.Equal(DateTime.Now.ToUniversalTime().Date, turn.when.Date); - /*** N.B.: might fail between 11:59:59PM and 00:00:00AM ***/ + Assert.Throws(action); } [Fact] - public void TestCreateWithDefaultTimeNullPlayerThenException() + public void TestCreateWithSpecifiedTimeEmptyFacesThenException() { // Arrange - Player player = new("Devon"); + DateTime dateTime = new(year: 2018, month: 06, day: 15, hour: 16, minute: 30, second: 0, kind: DateTimeKind.Utc); + Player player = new("Chucky"); + FACES.Clear(); // Act - static void action() => Turn.CreateWithDefaultTime(null); + void action() => Turn.CreateWithSpecifiedTime(dateTime, player, FACES); // Assert - Assert.Throws(action); + Assert.Throws(action); + } + + [Fact] + public void TestCreateWithDefaultTimeThenValid() + { + // Arrange + Player player = new("Chloe"); + + // Act + Turn turn = Turn.CreateWithDefaultTime(player, FACES); + + // Assert + Assert.Equal(DateTimeKind.Utc, turn.when.Kind); + Assert.Equal(DateTime.Now.ToUniversalTime().Date, turn.when.Date); + /*** N.B.: might fail between 11:59:59PM and 00:00:00AM ***/ } [Fact] @@ -87,11 +126,17 @@ namespace Tests.Model_UTs 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 , , ..."; - Turn turn = Turn.CreateWithSpecifiedTime(dateTime, player); + string expected = $"2018-06-15 16:30:00 -- {name} rolled: " + + FACE_ONE + " " + + FACE_TWO + + " Assets/images/" + FACE_THREE + " " + + FACE_FOUR.ToString("X6").Insert(0, "#"); + + Turn turn = Turn.CreateWithSpecifiedTime(dateTime, player, FACES); // Act string actual = turn.ToString(); + Debug.WriteLine(actual); // Assert Assert.Equal(expected, actual);