From 7b1c640c3e909ad0cd605d71c6235de45bb8effb Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:40:22 +0100 Subject: [PATCH 1/9] Upgraded test project to .NET 8 --- .../Codebreaker.ViewModels.Tests.csproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj b/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj index abfcec5..b633d40 100644 --- a/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj +++ b/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj @@ -1,10 +1,9 @@ - net7.0 + net8.0 enable enable - false From c56cce7f2b72ab5e6c1177d94c536814b4468c12 Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:40:34 +0100 Subject: [PATCH 2/9] Upgraded nuget packages of test project --- .../Codebreaker.ViewModels.Tests.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj b/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj index b633d40..7b01af9 100644 --- a/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj +++ b/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj @@ -10,12 +10,12 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all From 4d32f9743e78674ab68eff15ece08ccd00fbd240 Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:47:11 +0100 Subject: [PATCH 3/9] Removed options and dialog service mock --- .../GamePageViewModelTests.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index c65d98a..d39fde1 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -17,16 +17,11 @@ public GamePageViewModelTests() }); Mock gameClient = new(); - gameClient.Setup( - client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(returnValue); + gameClient.Setup(client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(returnValue); - Mock> options = new(); - options.Setup(o => o.Value).Returns(new GamePageViewModelOptions()); - - Mock dialogService = new(); Mock infoBarService = new(); - _viewModel = new GamePageViewModel(gameClient.Object, options.Object, dialogService.Object, infoBarService.Object); + _viewModel = new GamePageViewModel(gameClient.Object, infoBarService.Object); } [Fact] From 8ab643feec214c6dc349b25194ececfbe05c299e Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:49:27 +0100 Subject: [PATCH 4/9] Made service mocks class members. Removed viewmodel class member and moved it to the test cases. --- .../GamePageViewModelTests.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index d39fde1..fbbafd2 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -6,7 +6,8 @@ namespace CodeBreaker.ViewModels.Tests; public class GamePageViewModelTests { - private readonly GamePageViewModel _viewModel; + private readonly Mock _gamesClientMock; + private readonly Mock _infoBarServiceMock; public GamePageViewModelTests() { @@ -16,19 +17,18 @@ public GamePageViewModelTests() { "colors", new string[] { "Black", "White", "Red", "Green", "Blue", "Yellow" } } }); - Mock gameClient = new(); - gameClient.Setup(client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(returnValue); + _gamesClientMock = new(); + _gamesClientMock.Setup(client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(returnValue); - Mock infoBarService = new(); - - _viewModel = new GamePageViewModel(gameClient.Object, infoBarService.Object); + _infoBarServiceMock = new(); } [Fact] public async Task TestGameModeStartedAfterStart() { - _viewModel.Name = "Test"; - await _viewModel.StartGameCommand.ExecuteAsync(null); + var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); + viewModel.Name = "Test"; + await viewModel.StartGameCommand.ExecuteAsync(null); Assert.Equal(GameMode.Started, _viewModel.GameStatus); } @@ -36,11 +36,12 @@ public async Task TestGameModeStartedAfterStart() [Fact] public async Task TestInProgressNotificationAfterStart() { + var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); List expected = new() { true, false }; - _viewModel.Name = "Test"; + viewModel.Name = "Test"; List inProgressValues = new(); - _viewModel.PropertyChanged += (sender, e) => + viewModel.PropertyChanged += (sender, e) => { if (e.PropertyName is "InProgress") { @@ -48,7 +49,7 @@ public async Task TestInProgressNotificationAfterStart() } }; - await _viewModel.StartGameCommand.ExecuteAsync(null); + await viewModel.StartGameCommand.ExecuteAsync(null); Assert.Equal(expected, inProgressValues); } From 6933322b116b70a18cda1789ea47eada8f6b0c37 Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:50:22 +0100 Subject: [PATCH 5/9] Used collection expression --- src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index fbbafd2..75549e7 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -14,7 +14,7 @@ public GamePageViewModelTests() (Guid GameType, int NumberCodes, int MaxMoves, IDictionary FieldValues) returnValue = (Guid.NewGuid(), 4, 12, new Dictionary() { - { "colors", new string[] { "Black", "White", "Red", "Green", "Blue", "Yellow" } } + { "colors", ["Black", "White", "Red", "Green", "Blue", "Yellow"] } }); _gamesClientMock = new(); From 236b2ee41c869d07a3ec26c7de1157320de816bd Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:55:26 +0100 Subject: [PATCH 6/9] Renamed test case _TestGameModeStartedAfterStart_ to _TestGameStart_. Adapted test case for new vm. Enhanced assertions for that test case. --- .../GamePageViewModelTests.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index 75549e7..f0bda8f 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -24,13 +24,15 @@ public GamePageViewModelTests() } [Fact] - public async Task TestGameModeStartedAfterStart() + public async Task TestGameStart() { var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); - viewModel.Name = "Test"; + viewModel.Username = "Test"; await viewModel.StartGameCommand.ExecuteAsync(null); - Assert.Equal(GameMode.Started, _viewModel.GameStatus); + Assert.Equal(4, viewModel.SelectedFields.Length); + Assert.All(viewModel.SelectedFields, field => Assert.NotNull(field)); + Assert.NotNull(viewModel.Game); } [Fact] From 49ec3c2e003d591e151c66e300aedff7402dd5d2 Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:55:56 +0100 Subject: [PATCH 7/9] Renamed test case _TestInProgressNotificationAfterStart_ to _TestIsLoadingNotificationAfterStart_ and adpated this test case for new vm. --- .../GamePageViewModelTests.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index f0bda8f..4eed9ff 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -36,23 +36,21 @@ public async Task TestGameStart() } [Fact] - public async Task TestInProgressNotificationAfterStart() + public async Task TestIsLoadingNotificationAfterStart() { var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); - List expected = new() { true, false }; - viewModel.Name = "Test"; - List inProgressValues = new(); + List expectedIsLoadingValues = [true, false]; + List actualInProgressValues = []; + viewModel.Username = "Test"; viewModel.PropertyChanged += (sender, e) => { - if (e.PropertyName is "InProgress") - { - inProgressValues.Add(_viewModel.InProgress); - } + if (e.PropertyName is nameof(GamePageViewModel.IsLoading)) + actualInProgressValues.Add(viewModel.IsLoading); }; await viewModel.StartGameCommand.ExecuteAsync(null); - Assert.Equal(expected, inProgressValues); + Assert.Equal(expectedIsLoadingValues, actualInProgressValues); } } From 759ef62a10365a6834268230fa7b975a7721cbe1 Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:58:07 +0100 Subject: [PATCH 8/9] Defined data for a simulated game and adpated the GamesClient mock the simulated game. --- .../GamePageViewModelTests.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index 4eed9ff..02fd2c9 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -6,19 +6,39 @@ namespace CodeBreaker.ViewModels.Tests; public class GamePageViewModelTests { + private static readonly string[] s_code = ["Green", "Blue", "Yellow", "Orange"]; + private static readonly (string[] GuessPegs, string[] KeyPegs, bool HasEnded, bool IsVictory)[] s_moves = [ + (["Red", "Red", "Red", "Red"], [], false, false), + (["Green", "Green", "Green", "Green"], ["Black"], false, false), + (["Blue", "Green", "Green", "Green"], ["White", "White"], false, false), + (["Green", "Blue", "Blue", "Blue"], ["Black", "Black"], false, false), + (["Green", "Blue", "Yellow", "Yellow"], ["Black", "Black", "Black"], false, false), + (["Green", "Blue", "Yellow", "Purple"], ["Black", "Black", "Black"], false, false), + (["Green", "Blue", "Yellow", "Orange"], ["Black", "Black", "Black", "Black"], true, true), + ]; + private readonly Mock _gamesClientMock; private readonly Mock _infoBarServiceMock; public GamePageViewModelTests() { - (Guid GameType, int NumberCodes, int MaxMoves, IDictionary FieldValues) returnValue = + (Guid Id, int NumberCodes, int MaxMoves, IDictionary FieldValues) gameReturnValue = (Guid.NewGuid(), 4, 12, new Dictionary() { { "colors", ["Black", "White", "Red", "Green", "Blue", "Yellow"] } }); _gamesClientMock = new(); - _gamesClientMock.Setup(client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(returnValue); + // Setup the StartGameAsync method of the GamesClient. + _gamesClientMock.Setup(client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(gameReturnValue); + + // Setup the SetMoveAsync method of the GamesClient. + for (int i = 0; i < s_moves.Length; i++) + { + var move = s_moves[i]; + _gamesClientMock.Setup(client => client.SetMoveAsync(gameReturnValue.Id, "Test", GameType.Game6x4, i + 1, move.GuessPegs, CancellationToken.None)) + .ReturnsAsync((move.KeyPegs, move.HasEnded, move.IsVictory)); + } _infoBarServiceMock = new(); } From 34d486f98067e5c83990f3070790ef7abd876858 Mon Sep 17 00:00:00 2001 From: Sebastian Szvetecz Date: Wed, 20 Mar 2024 12:58:45 +0100 Subject: [PATCH 9/9] Added test case for playing a game. --- .../GamePageViewModelTests.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index 02fd2c9..b86373e 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -73,4 +73,27 @@ public async Task TestIsLoadingNotificationAfterStart() Assert.Equal(expectedIsLoadingValues, actualInProgressValues); } + + [Fact] + public async Task TestMoves() + { + // Start game + var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); + viewModel.Username = "Test"; + + await viewModel.StartGameCommand.ExecuteAsync(null); + + // Play game + foreach ((string[] guessPegs, _, _, _) in s_moves) + { + for (int i = 0; i < guessPegs.Length; i++) + viewModel.SelectedFields[i].Color = guessPegs[i]; + + await viewModel.MakeMoveCommand.ExecuteAsync(null); + } + + Assert.Equal(s_moves.Length, viewModel.Game?.Moves.Count); + Assert.NotNull(viewModel.Game?.EndTime); + Assert.True(viewModel.Game?.IsVictory); + } }