-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from CodebreakerApp/156-fix_game6x4mini
156 fix game6x4mini
- Loading branch information
Showing
6 changed files
with
270 additions
and
29 deletions.
There are no files selected for viewing
247 changes: 247 additions & 0 deletions
247
...es/gameapi/Codebreaker.GameAPIs.Analyzers.Tests/Analyzers/SimpleGameGuessAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
using System.Collections; | ||
|
||
using static Codebreaker.GameAPIs.Models.Colors; | ||
|
||
namespace Codebreaker.GameAPIs.Analyzer.Tests; | ||
|
||
public class SimpleGameGuessAnalyzerTests | ||
{ | ||
[Fact] | ||
public void SetMoveShouldReturnThreeCorrectColor() | ||
{ | ||
SimpleColorResult expectedKeyPegs = new( | ||
[ | ||
ResultValue.CorrectColor, | ||
ResultValue.CorrectColor, | ||
ResultValue.CorrectColor, | ||
ResultValue.Incorrect | ||
]); | ||
SimpleColorResult resultKeyPegs = TestSkeleton( | ||
[Green, Yellow, Green, Black], | ||
[Yellow, Green, Black, Blue] | ||
); | ||
|
||
Assert.Equal(expectedKeyPegs, resultKeyPegs); | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(TestData6x4Mini))] | ||
public void SetMoveUsingVariousDataUsingDataClass(string[] code, string[] guess, SimpleColorResult expectedKeyPegs) | ||
{ | ||
SimpleColorResult actualKeyPegs = TestSkeleton(code, guess); | ||
Assert.Equal(expectedKeyPegs, actualKeyPegs); | ||
} | ||
|
||
[Fact] | ||
public void SetMove_ShouldThrowOnInvalidGuessCount() | ||
{ | ||
Assert.Throws<ArgumentException>(() => | ||
TestSkeleton( | ||
["Black", "Black", "Black", "Black"], | ||
["Black"] | ||
)); | ||
} | ||
|
||
[Fact] | ||
public void SetMove_ShouldThrowOnInvalidGuessValues() | ||
{ | ||
Assert.Throws<ArgumentException>(() => | ||
TestSkeleton( | ||
["Black", "Black", "Black", "Black"], | ||
["Black", "Der", "Blue", "Yellow"] // "Der" is the wrong value | ||
)); | ||
} | ||
|
||
[Fact] | ||
public void SetMove_ShouldThrowOnInvalidMoveNumber() | ||
{ | ||
Assert.Throws<ArgumentException>(() => | ||
TestSkeleton( | ||
[Green, Yellow, Green, Black], | ||
[Yellow, Green, Black, Blue], moveNumber: 2)); | ||
} | ||
|
||
[Fact] | ||
public void GetResult_ShouldNotIncrementMoveNumberOnInvalidMove() | ||
{ | ||
IGame game = TestSkeletonWithGame( | ||
[Green, Yellow, Green, Black], | ||
[Yellow, Green, Black, Blue], moveNumber: 2); | ||
|
||
Assert.Equal(0, game?.LastMoveNumber); | ||
} | ||
|
||
[Fact] | ||
public void GetResult_WithGameWon_ShouldSetCorrectGameEndInformation() | ||
{ | ||
// Arrange | ||
var game = new MockColorGame | ||
{ | ||
GameType = GameTypes.Game6x4Mini, | ||
NumberCodes = 4, | ||
MaxMoves = 12, | ||
IsVictory = false, | ||
FieldValues = new Dictionary<string, IEnumerable<string>>() | ||
{ | ||
[FieldCategories.Colors] = [.. TestData6x4.Colors6] | ||
}, | ||
Codes = ["Red", "Blue", "Green", "Yellow"] | ||
}; | ||
|
||
var guesses = new string[] { "Red", "Blue", "Green", "Yellow" }.ToPegs<ColorField>().ToArray(); | ||
var analyzer = new SimpleGameGuessAnalyzer(game, guesses, 1); | ||
|
||
// Act | ||
var result = analyzer.GetResult(); | ||
|
||
// Assert | ||
Assert.NotNull(game.EndTime); | ||
Assert.NotNull(game.Duration); | ||
Assert.True(game.IsVictory); | ||
} | ||
|
||
[Fact] | ||
public void GetResult_WithGameNotComplete_ShouldSetCorrectGameEndInformation() | ||
{ | ||
// Arrange | ||
var game = new MockColorGame | ||
{ | ||
GameType = GameTypes.Game6x4Mini, | ||
NumberCodes = 4, | ||
MaxMoves = 12, | ||
IsVictory = false, | ||
FieldValues = new Dictionary<string, IEnumerable<string>>() | ||
{ | ||
[FieldCategories.Colors] = [.. TestData6x4.Colors6] | ||
}, | ||
Codes = ["Red", "Blue", "Green", "Yellow"] | ||
}; | ||
|
||
var guesses = new string[] { "Red", "Yellow", "Green", "Yellow" }.ToPegs<ColorField>().ToArray(); | ||
var analyzer = new SimpleGameGuessAnalyzer(game, guesses, 1); | ||
|
||
// Act | ||
var result = analyzer.GetResult(); | ||
|
||
// Assert | ||
Assert.Null(game.EndTime); | ||
Assert.False(game.IsVictory); | ||
} | ||
|
||
private static SimpleColorResult TestSkeleton(string[] codes, string[] guesses, int moveNumber = 1) | ||
{ | ||
MockColorGame game = new() | ||
{ | ||
GameType = GameTypes.Game6x4Mini, | ||
NumberCodes = 4, | ||
MaxMoves = 12, | ||
IsVictory = false, | ||
FieldValues = new Dictionary<string, IEnumerable<string>>() | ||
{ | ||
[FieldCategories.Colors] = [.. TestData6x4.Colors6] | ||
}, | ||
Codes = codes | ||
}; | ||
|
||
SimpleGameGuessAnalyzer analyzer = new(game,guesses.ToPegs<ColorField>().ToArray(), moveNumber); | ||
return analyzer.GetResult(); | ||
} | ||
|
||
private static IGame TestSkeletonWithGame(string[] codes, string[] guesses, int moveNumber = 1) | ||
{ | ||
MockColorGame game = new() | ||
{ | ||
GameType = GameTypes.Game6x4, | ||
NumberCodes = 4, | ||
MaxMoves = 12, | ||
IsVictory = false, | ||
FieldValues = new Dictionary<string, IEnumerable<string>>() | ||
{ | ||
[FieldCategories.Colors] = [.. TestData6x4.Colors6] | ||
}, | ||
Codes = codes | ||
}; | ||
|
||
SimpleGameGuessAnalyzer analyzer = new(game, guesses.ToPegs<ColorField>().ToArray(), moveNumber); | ||
try | ||
{ | ||
analyzer.GetResult(); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
|
||
} | ||
return game; | ||
} | ||
} | ||
|
||
public class TestData6x4Mini : IEnumerable<object[]> | ||
{ | ||
public static readonly string[] Colors6 = [Red, Green, Blue, Yellow, Black, White]; | ||
|
||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
yield return new object[] | ||
{ | ||
new string[] { Green, Blue, Green, Yellow }, // code | ||
new string[] { Green, Green, Black, White }, // inputdata | ||
new SimpleColorResult( | ||
[ | ||
ResultValue.CorrectPositionAndColor, | ||
ResultValue.CorrectColor, | ||
ResultValue.Incorrect, | ||
ResultValue.Incorrect | ||
]) // expected | ||
}; | ||
yield return new object[] | ||
{ | ||
new string[] { Red, Blue, Black, White }, | ||
new string[] { Black, Black, Red, Yellow }, | ||
new SimpleColorResult( | ||
[ | ||
ResultValue.CorrectColor, | ||
ResultValue.Incorrect, | ||
ResultValue.CorrectColor, | ||
ResultValue.Incorrect | ||
]) | ||
}; | ||
yield return new object[] | ||
{ | ||
new string[] { Yellow, Black, Yellow, Green }, | ||
new string[] { Black, Black, Black, Black }, | ||
new SimpleColorResult( | ||
[ | ||
ResultValue.Incorrect, | ||
ResultValue.CorrectPositionAndColor, | ||
ResultValue.Incorrect, | ||
ResultValue.Incorrect | ||
]) | ||
}; | ||
yield return new object[] | ||
{ | ||
new string[] { Yellow, Yellow, White, Red }, | ||
new string[] { Green, Yellow, White, Red }, | ||
new SimpleColorResult( | ||
[ | ||
ResultValue.Incorrect, | ||
ResultValue.CorrectPositionAndColor, | ||
ResultValue.CorrectPositionAndColor, | ||
ResultValue.CorrectPositionAndColor | ||
]) | ||
}; | ||
yield return new object[] | ||
{ | ||
new string[] { White, Black, Yellow, Black }, | ||
new string[] { Black, Blue, Black, White }, | ||
new SimpleColorResult( | ||
[ | ||
ResultValue.CorrectColor, | ||
ResultValue.Incorrect, | ||
ResultValue.CorrectColor, | ||
ResultValue.CorrectColor | ||
]) | ||
}; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.