using CoreLibrary.Exceptions; using System.Reflection; using CoreLibrary.Core; using Xunit; namespace UnitTesting { /// /// Classe test pour la classe Plateau. /// public class PlateauUT { /// /// Test que le constructeur de Plateau soit valide. /// [Fact] public void TestConstructeurValide() { Plateau plateau = new Plateau(4,12); Assert.NotNull(plateau); Assert.False(plateau.Victoire); } /// /// Test que le constructeur de Plateau soit invalide. /// [Fact] public void TestConstructeurInvalide() { Assert.Throws(() => new Plateau(-1, 10)); Assert.Throws(() => new Plateau(3, -1)); } /// /// Test la methode EstComplet de Plateau et verifie qu'il renvoie True. /// [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); } /// /// Test la methode EstComplet de Plateau et verifie qu'il renvoie False. /// [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); } /// /// Test l'exception CodeCompletException de Plateau avec une taille de code superieure a ce qui est defini. /// [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))); } /// /// Test l'exception CodeIncompletException de Plateau avec un code incomplet. /// [Fact] public void TestAjouterCodeIncomplet() { Plateau plateau = new Plateau(4, 10); Code code = new Code(4); Assert.Throws(() => plateau.AjouterCode(code)); } /// /// Test la methode AjouterCode de Plateau avec un bon 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); } /// /// Test la methode AjouterCode de Plateau et verifie la grille et ses composants. /// [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); } /// /// Test que la grille du plateau est vide. /// [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); } /// /// Test l'exception GrilleCompleteException de Plateau. /// [Fact] public void TestAjouterCode_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)); } /// /// Test l'evenement QuandPlateauAjouterCode de Plateau. /// [Fact] public void TestPlateauEcoute() { Plateau plateau = new Plateau(4, 2); MethodInfo? QuandPlateauAjouterCodeInfo = typeof(Plateau).GetMethod("QuandPlateauAjouterCode", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(QuandPlateauAjouterCodeInfo); QuandPlateauAjouterCodeInfo?.Invoke(plateau, []); bool appel = false; plateau.PlateauAjouterCode += (sender, e) => appel = true; QuandPlateauAjouterCodeInfo?.Invoke(plateau, []); Assert.True(appel); } /// /// Test la methode AjouterCode de Plateau et verifie tous les cas d'arrets. /// [Fact] public void TestAjouterCodeVictoire() { // Cas 1 : Victoire : false, code correct : false Code code = new Code(1); code.AjouterJeton(new Jeton(Couleur.Rouge)); Plateau plateau; Code? codeSecret; do { plateau = new Plateau(1, 2); FieldInfo? codeSecretInfo = typeof(Plateau).GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(codeSecretInfo); codeSecret = codeSecretInfo.GetValue(plateau) as Code; Assert.NotNull(codeSecret); } while (codeSecret.Jetons.ElementAt(0).Equals(code.Jetons.ElementAt(0))); Assert.NotNull(codeSecret); plateau.AjouterCode(code); // Cas 2 : Victoire : false, code correct : true Plateau plateau2 = new Plateau(1, 2); FieldInfo? codeSecretInfo2 = typeof(Plateau).GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(codeSecretInfo2); Code? code2 = codeSecretInfo2.GetValue(plateau2) as Code; Assert.NotNull(code2); plateau2.AjouterCode(code2); // Cas 3 : Victoire : true, code correct : false Code code3 = new Code(1); code3.AjouterJeton(new Jeton(Couleur.Rouge)); Plateau plateau3; Code? codeSecret3; do { plateau3 = new Plateau(1, 2); FieldInfo? codeSecretInfo3 = typeof(Plateau).GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(codeSecretInfo3); codeSecret3 = codeSecretInfo3.GetValue(plateau) as Code; Assert.NotNull(codeSecret3); } while (codeSecret3.Jetons.ElementAt(0).Equals(code3.Jetons.ElementAt(0))); Assert.NotNull(codeSecret3); PropertyInfo? VictoireInfo3 = typeof(Plateau).GetProperty("Victoire", BindingFlags.Public | BindingFlags.Instance); Assert.NotNull(VictoireInfo3); VictoireInfo3.SetValue(plateau3, true); plateau3.AjouterCode(code3); // Cas 4 : Victoire : true, code correct : true Plateau plateau4 = new Plateau(1, 2); FieldInfo? codeSecretInfo4 = typeof(Plateau).GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); Assert.NotNull(codeSecretInfo4); Code? code4 = codeSecretInfo4.GetValue(plateau4) as Code; Assert.NotNull(code4); PropertyInfo? VictoireInfo4 = typeof(Plateau).GetProperty("Victoire", BindingFlags.Public | BindingFlags.Instance); Assert.NotNull(VictoireInfo4); VictoireInfo4.SetValue(plateau4, true); plateau4.AjouterCode(code4); } } }