using CoreLibrary.Exceptions; using System.Reflection; using CoreLibrary.Core; using Xunit; namespace UnitTesting { public class PlateauUT { [Fact] public void TestConstructeurValide() { Plateau plateau = new Plateau(4,12); Assert.NotNull(plateau); Assert.False(plateau.Victoire); } [Fact] public void TestConstructeurInvalide() { Assert.Throws(() => new Plateau(-1, 10)); Assert.Throws(() => new Plateau(3, -1)); } [Fact] public void TestEstCompletTrue() { Plateau plateau = new Plateau(4, 3); Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)]; Code code = new Code(4); code.AjouterJeton(jetons[0]); code.AjouterJeton(jetons[1]); code.AjouterJeton(jetons[2]); code.AjouterJeton(jetons[3]); plateau.AjouterCode(code); plateau.AjouterCode(code); plateau.AjouterCode(code); bool estComplet = plateau.Complet; Assert.True(estComplet); } [Fact] public void TestEstCompletFalse() { Plateau plateau = new Plateau(4, 3); Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)]; Code code = new Code(4); code.AjouterJeton(jetons[0]); code.AjouterJeton(jetons[1]); code.AjouterJeton(jetons[2]); code.AjouterJeton(jetons[3]); plateau.AjouterCode(code); plateau.AjouterCode(code); bool estComplet = plateau.Complet; Assert.False(estComplet); } [Fact] public void TestAjouterCodeTailleIncorrecte() { Plateau plateau = new Plateau(4, 10); Jeton[] jetons = new Jeton[] { new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune) }; Code code = new Code(4); code.AjouterJeton(jetons[0]); code.AjouterJeton(jetons[1]); code.AjouterJeton(jetons[2]); code.AjouterJeton(jetons[3]); Assert.Throws(() => code.AjouterJeton(new Jeton(Couleur.Bleu))); } [Fact] public void TestAjouterCodeIncomplet() { Plateau plateau = new Plateau(4, 10); Code code = new Code(4); Assert.Throws(() => plateau.AjouterCode(code)); } [Fact] public void TestAjouterCodeBonCode() { Plateau plateau = new Plateau(4, 10); Type type = typeof(Plateau); FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(fieldInfo); Code? codeSecret = (Code?)fieldInfo.GetValue(plateau); Assert.NotNull(codeSecret); plateau.AjouterCode(codeSecret); Assert.True(plateau.Victoire); } [Fact] public void TestGrilleAjouterCode() { Code code = new Code(4); Couleur[] couleurs = Enum.GetValues(); Plateau plateau = new Plateau(4, 12); Jeton[] jetons = new Jeton[] { new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune) }; code.AjouterJeton(jetons[0]); code.AjouterJeton(jetons[1]); code.AjouterJeton(jetons[2]); code.AjouterJeton(jetons[3]); plateau.AjouterCode(code); (IEnumerable>, IEnumerable>) grille = plateau.Grille; var (jetonsGrille, indicateurs) = grille; Assert.Single(jetonsGrille); Assert.Equal(4, jetonsGrille.First().Count()); Assert.Single(indicateurs); } [Fact] public void TestGrilleEstVide() { Plateau plateau = new Plateau(4, 12); (IEnumerable>, IEnumerable>) grille = plateau.Grille; var (jetons, indicateurs) = grille; Assert.Empty(jetons); Assert.Empty(indicateurs); } [Fact] public void AjouterCode_GrilleComplete_ThrowsGrilleCompleteException() { Plateau plateau = new Plateau(4, 2); Code codeComplet1 = new Code(4); Code codeComplet2 = new Code(4); Code codeComplet3 = new Code(4); Jeton[] jetons = new Jeton[] { new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune) }; foreach (Jeton jeton in jetons) { codeComplet1.AjouterJeton(jeton); codeComplet2.AjouterJeton(jeton); codeComplet3.AjouterJeton(jeton); } plateau.AjouterCode(codeComplet1); plateau.AjouterCode(codeComplet2); Assert.Throws(() => plateau.AjouterCode(codeComplet3)); } } }