diff --git a/Sources/CoreLibrary/Joueurs/Joueur.cs b/Sources/CoreLibrary/Joueurs/Joueur.cs index 08aff71..b1414ed 100644 --- a/Sources/CoreLibrary/Joueurs/Joueur.cs +++ b/Sources/CoreLibrary/Joueurs/Joueur.cs @@ -60,7 +60,7 @@ namespace CoreLibrary.Joueurs { if (EstConnecte) throw new JoueurDejaConnecteException(this); - + EstConnecte = true; QuandJoueurSeConnecter(joueur); } diff --git a/Sources/UnitTesting/JoueurUT.cs b/Sources/UnitTesting/JoueurUT.cs index 78edd94..d8a6b15 100644 --- a/Sources/UnitTesting/JoueurUT.cs +++ b/Sources/UnitTesting/JoueurUT.cs @@ -100,10 +100,10 @@ namespace UnitTesting [Fact] public void TestToStringValide() { - Joueur joueur = new Joueur("Joueur"); + string nom = "Joueur"; + Joueur joueur = new Joueur(nom); string result = joueur.ToString(); - - Assert.Equal("Joueur1", result); + Assert.Equal(nom, result); } [Fact] diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index 49fa4ae..34b6ff4 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -273,10 +273,6 @@ namespace UnitTesting MethodInfo? methodInfo = typeof(Partie).GetMethod("DebutPartie", BindingFlags.NonPublic | BindingFlags.Instance); methodInfo?.Invoke(partie, null); - - - - PropertyInfo? tourProperty = typeof(Partie).GetProperty("Tour", BindingFlags.Public | BindingFlags.Instance); Assert.NotNull(tourProperty); if (tourProperty != null) @@ -305,5 +301,179 @@ namespace UnitTesting } } } + + [Fact] + public void TestNouveauTour() + { + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + + + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo? plateauxField = typeof(Partie).GetField("plateaux", BindingFlags.NonPublic | BindingFlags.Instance); + if (joueursField != null && plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if (joueurs != null && plateaux != null) + { + joueurs.Add("Joueur10", false); + joueurs.Add("Joueur50", false); + plateaux.Add(new Plateau(regle.TailleCode, regle.NbTour)); + plateaux.Add(new Plateau(regle.TailleCode, regle.NbTour)); + } + } + + bool demanderJoueurJouerEventTriggered = false; + bool nouveauTourEventTriggered = false; + + + partie.PartieDemanderJoueurJouer += (sender, e) => demanderJoueurJouerEventTriggered = true; + partie.PartieNouveauTour += (sender, e) => nouveauTourEventTriggered = true; + + MethodInfo? methodInfo = typeof(Partie).GetMethod("NouveauTour", BindingFlags.NonPublic | BindingFlags.Instance); + methodInfo?.Invoke(partie, null); + + + + Assert.True(demanderJoueurJouerEventTriggered); + Assert.True(nouveauTourEventTriggered); + } + + [Fact] + public void TestPlateauAjouterCodeIncrementation() + { + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + + + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo? plateauxField = typeof(Partie).GetField("plateaux", BindingFlags.NonPublic | BindingFlags.Instance); + if (joueursField != null && plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if (joueurs != null && plateaux != null) + { + joueurs.Add("Joueur1", false); + joueurs.Add("Joueur2", false); + plateaux.Add(new Plateau(regle.TailleCode, regle.NbTour)); + plateaux.Add(new Plateau(regle.TailleCode, regle.NbTour)); + } + } + + + Plateau plateau = new Plateau(regle.TailleCode, regle.NbTour); + PlateauAjouterCodeEventArgs eventArgs = new PlateauAjouterCodeEventArgs(plateau); + + + MethodInfo? methodInfo = typeof(Partie).GetMethod("PlateauAjouterCode", BindingFlags.NonPublic | BindingFlags.Instance); + methodInfo?.Invoke(partie, new object?[] { null, eventArgs }); + + FieldInfo? courantField = typeof(Partie).GetField("courant", BindingFlags.NonPublic | BindingFlags.Instance); + + if(courantField != null) + { + int? courant = ((int?)courantField.GetValue(partie)).GetValueOrDefault(); + + Assert.Equal(1, courant); + Assert.Equal(0, partie.Tour); + + } + + methodInfo?.Invoke(partie, new object?[] { null, eventArgs }); + + if(courantField != null) + { + int courant2 = ((int?)courantField.GetValue(partie)).GetValueOrDefault(); + Assert.Equal(0, courant2); + Assert.Equal(1, partie.Tour); + } + + } + + [Fact] + public void PlateauAjouterCodeTerminee() + { + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo? plateauxField = typeof(Partie).GetField("plateaux", BindingFlags.NonPublic | BindingFlags.Instance); + Plateau plateau = new Plateau(regle.TailleCode, regle.NbTour); + if (joueursField != null && plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if (joueurs != null && plateaux != null) + { + joueurs.Add("Joueur1", false); + joueurs.Add("Joueur2", false); + plateaux.Add(plateau); + plateaux.Add(plateau); + PropertyInfo? victoireProperty = typeof(Plateau).GetProperty("Victoire"); + if (victoireProperty != null) + { + victoireProperty.SetValue(plateau, true); + } + + FieldInfo? courantField = typeof(Partie).GetField("courant", BindingFlags.NonPublic | BindingFlags.Instance); + if (courantField != null) + { + courantField.SetValue(partie, 1); + } + + PlateauAjouterCodeEventArgs eventArgs = new PlateauAjouterCodeEventArgs(plateau); + MethodInfo? methodInfo = typeof(Partie).GetMethod("PlateauAjouterCode", BindingFlags.NonPublic | BindingFlags.Instance); + methodInfo?.Invoke(partie, new object?[] { null, eventArgs }); + + Assert.True(partie.Termine); + } + } + } + + [Fact] + public void TestPartieTerminee() + { + // Création de la partie avec des règles de jeu + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo? plateauxField = typeof(Partie).GetField("plateaux", BindingFlags.NonPublic | BindingFlags.Instance); + Plateau plateau1 = new Plateau(regle.TailleCode, regle.NbTour); + Plateau plateau2 = new Plateau(regle.TailleCode, regle.NbTour); + if (joueursField != null && plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if (joueurs != null && plateaux != null) + { + joueurs.Add("Joueur1", false); + joueurs.Add("Joueur2", false); + plateaux.Add(plateau1); + plateaux.Add(plateau2); + PropertyInfo? victoireProperty = typeof(Plateau).GetProperty("Victoire"); + if (victoireProperty != null) + { + victoireProperty.SetValue(plateau1, true); + } + + + MethodInfo? methodInfo = typeof(Partie).GetMethod("PartieTerminee", BindingFlags.NonPublic | BindingFlags.Instance); + methodInfo?.Invoke(partie, null); + + Assert.True(partie.Termine); + partie.PartiePartieTerminee += (sender, e) => + { + Assert.True(partie.Termine); + Assert.Contains("Joueur1", e.Gagnants); + Assert.Contains("Joueur2", e.Perdants); + }; + + + } + } + } } }