From 69821cea422ad56b4668b972d1193b8e183dd677 Mon Sep 17 00:00:00 2001 From: mzjeeawody Date: Tue, 25 Oct 2022 17:46:38 +0200 Subject: [PATCH] change all test based on new DiceGroup Class --- .../Data_UTs/Dice/DiceGroupManagerTest.cs | 352 +++++++++--------- 1 file changed, 175 insertions(+), 177 deletions(-) diff --git a/Sources/Tests/Data_UTs/Dice/DiceGroupManagerTest.cs b/Sources/Tests/Data_UTs/Dice/DiceGroupManagerTest.cs index b9ddd0b..35eb1f5 100644 --- a/Sources/Tests/Data_UTs/Dice/DiceGroupManagerTest.cs +++ b/Sources/Tests/Data_UTs/Dice/DiceGroupManagerTest.cs @@ -17,186 +17,184 @@ using Xunit; namespace Tests.Data_UTs.Dice { public class DiceGroupManagerTest - { - private readonly MasterOfCeremonies stubGameRunner = new Stub().LoadApp(); - [Fact] - public void TestConstructorReturnsEmptyEnumerable() - { - - // Arrange - DiceGroupManager diceGroupManager = new(); - Dictionary> expected; - Dictionary> actual; - - // Act - expected = new Dictionary>(); - actual = (Dictionary>)diceGroupManager.GetAll(); - - // Assert - Xunit.Assert.Equal(expected, actual); - - } - - [Fact] - - public void TestAddWhenDiceGroupThenDoAddAndReturnDiceGroup() - { - // Arrange + { + private static readonly Task stubGameRunner = new Stub().LoadApp(); + [Fact] + public void TestConstructorReturnsEmptyEnumerable() + { + + // Arrange + DiceGroupManager diceGroupManager = new(); + + List expected; + Task> actual; + + // Act + expected = new List(); + actual = diceGroupManager.GetAll(); + + // Assert + Xunit.Assert.Equal(expected, actual.Result); + + } + + [Fact] + + public async Task TestAddWhenDiceGroupThenDoAddAndReturnDiceGroup() + { + // Arrange DiceGroupManager dgm = new(); - KeyValuePair> group1 = stubGameRunner.DiceGroupManager.GetAll().First(); - KeyValuePair> group2 = stubGameRunner.DiceGroupManager.GetAll().Last(); - - // Act - - //...adding keys and values to some dictionary for future comparison - Dictionary> expected = new() + DiceGroup group1 = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + DiceGroup group2 = new("Scrabble", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + + //...storing the results of DiceGroupManager.Add() in variables + Collection expected = new() {group1,group2 }; + Collection actual = new() { - { group1.Key, group1.Value }, - { group2.Key, group2.Value } + await dgm.Add(group1), + await dgm.Add(group2) }; - - //...storing the results of DiceGroupManager.Add() in variables - KeyValuePair> resultFromAdd1 = dgm.Add(group1); - KeyValuePair> resultFromAdd2 = dgm.Add(group2); - - //...using those variables to fill a second dictionary for comparison - Dictionary> actual = new() - { - { resultFromAdd1.Key, resultFromAdd1.Value }, - { resultFromAdd2.Key, resultFromAdd2.Value } - }; - // Assert - Xunit.Assert.Equal(expected, actual); - } - - - [Fact] - public void TestAddIfNullThrowsException() - { - DiceGroupManager dgm = new(); - - KeyValuePair> group1 = stubGameRunner.DiceGroupManager.GetAll().First(); - KeyValuePair> group2 = stubGameRunner.DiceGroupManager.GetAll().Last(); - NumberFace[] d6Faces = new NumberFace[] { new(1), new(2), new(3), new(4), new(5), new(6) }; - - - Dictionary> expected = new() - { - { "", new List{ new NumberDie(d6Faces[0], d6Faces[1..]), new NumberDie(d6Faces[0], d6Faces[3..]) } } - }; - Dictionary> toAdd = new(); -/* void action() => toAdd.Add("", new List{ new NumberDie(d6Faces[0], d6Faces[1..]), - new NumberDie(d6Faces[0], d6Faces[3..])}); */ - // Xunit.Assert.Empty(expected.Keys); - foreach (KeyValuePair> entry in expected) - { - Xunit.Assert.Empty(entry.Key); - // do something with entry.Value or entry.Key - } - } - - - [Fact] - public void TestAddIfAlreadyExistsThrowsException() - { - DiceGroupManager dgm = new(); - - // Act - KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7))}); - dgm.Add(toAdd); - - void action() => dgm.Add(toAdd); - - // Assert - Xunit.Assert.Throws(action); - - } - [Fact] - public void TestGetOneByIdThrowsException() - { - // Arrange - DiceGroupManager dgm = new(); - - // Act - - void action() => dgm.GetOneByID(new("9657b6f0-9431-458e-a2bd-4bb0c7d4a6ed")); - - // Assert - Xunit.Assert.Throws(action); - } - - [Theory] - [InlineData("")] - [InlineData(null)] - [InlineData(" ")] - public void TestGetOneByNameIfInvalidThrowsException(string name) - { - // Arrange - DiceGroupManager dgm = new(); - KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); - dgm.Add(toAdd); - void action() => dgm.GetOneByName(name); - - // Assert - Xunit.Assert.Throws(action); - } - - [Fact] - public void TestRemoveWorksIfExists() - { - // Arrange - DiceGroupManager dgm = new(); - KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); - dgm.Add(toAdd); - dgm.Remove(toAdd); - - Xunit.Assert.DoesNotContain(toAdd, dgm.GetAll()); - } - - [Fact] - public void TestRemoveFailsSilentlyIfGivenNonExistent() - { - DiceGroupManager dgm = new(); - KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); - dgm.Add(toAdd); - - KeyValuePair> toAdd2 = new("Scrabble", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); - - // Act - dgm.Remove(toAdd2); - - // Assert - Xunit.Assert.DoesNotContain(toAdd2, dgm.GetAll()); - } - - [Fact] - public void TestUpdateWorksIfValid() - { - // Arrange - DiceGroupManager dgm = new(); - KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); - dgm.Add(toAdd); - KeyValuePair> toAdd2 = new("Scrabble", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); - dgm.Update(toAdd, toAdd2); - - Xunit.Assert.DoesNotContain(toAdd, dgm.GetAll()); - Xunit.Assert.Contains(toAdd2, dgm.GetAll()); - Xunit.Assert.True(dgm.GetAll().Count() == 1); - } - - - - - - - - - - - - - - + Xunit.Assert.Equal(expected, actual); + Xunit.Assert.Equal(expected, actual); + } + [Fact] + public async Task TestAddIfNullThrowsException() + { + DiceGroupManager dgm = new(); + DiceGroup expected; + + expected = null; + async Task actionAsync() => await dgm.Add(expected);// Add() returns the added element if succesful + + // Assert + Xunit.Assert.Null(expected); + await Xunit.Assert.ThrowsAsync(actionAsync); + Xunit.Assert.DoesNotContain(expected, await dgm.GetAll()); + } + [Fact] + public async Task TestAddIfAlreadyExistsThrowsException() + { + DiceGroupManager dgm = new(); + + await dgm.Add(new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) })); + DiceGroup group1 = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + + async Task actionAsync() => await dgm.Add(group1); + + // Assert + await Xunit.Assert.ThrowsAsync(actionAsync); + + } + + /* + + [Fact] + public void TestAddIfAlreadyExistsThrowsException() + { + DiceGroupManager dgm = new(); + + // Act + KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + DiceGroup diceGroup = new DiceGroup("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + + dgm.Add(diceGroup); + + void action() => dgm.Add(diceGroup); + + // Assert + Xunit.Assert.Throws(action); + + } + [Fact] + public void TestGetOneByIdThrowsException() + { + // Arrange + DiceGroupManager dgm = new(); + + // Act + + void action() => dgm.GetOneByID(new("9657b6f0-9431-458e-a2bd-4bb0c7d4a6ed")); + + // Assert + Xunit.Assert.Throws(action); + } + + [Theory] + [InlineData("")] + [InlineData(null)] + [InlineData(" ")] + public void TestGetOneByNameIfInvalidThrowsException(string name) + { + // Arrange + DiceGroupManager dgm = new(); + KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + DiceGroup diceGroup = new DiceGroup("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + + dgm.Add(diceGroup); + void action() => dgm.GetOneByName(name); + + // Assert + Xunit.Assert.Throws(action); + } + + [Fact] + public void TestRemoveWorksIfExists() + { + // Arrange + DiceGroupManager dgm = new(); + // KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + DiceGroup diceGroup = new DiceGroup("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + + dgm.Add(diceGroup); + dgm.Remove(diceGroup); + + Xunit.Assert.DoesNotContain(diceGroup, (IEnumerable)dgm.GetAll()); + } + + [Fact] + public void TestRemoveFailsSilentlyIfGivenNonExistent() + { + DiceGroupManager dgm = new(); + KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + dgm.Add(toAdd); + + KeyValuePair> toAdd2 = new("Scrabble", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + + // Act + dgm.Remove(toAdd2); + + // Assert + Xunit.Assert.DoesNotContain(toAdd2, dgm.GetAll()); + } + + [Fact] + public void TestUpdateWorksIfValid() + { + // Arrange + DiceGroupManager dgm = new(); + KeyValuePair> toAdd = new("Monopoly", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + dgm.Add(toAdd); + KeyValuePair> toAdd2 = new("Scrabble", new List { new NumberDie(new NumberFace(5), new NumberFace(7)), new NumberDie(new NumberFace(5), new NumberFace(7)) }); + dgm.Update(toAdd, toAdd2); + + Xunit.Assert.DoesNotContain(toAdd, dgm.GetAll()); + Xunit.Assert.Contains(toAdd2, dgm.GetAll()); + Xunit.Assert.True(dgm.GetAll().Count() == 1); + } + +*/ + + + + + + + + + + + + } }