diff --git a/Sources/CoreLibrary/Joueurs/Joueur.cs b/Sources/CoreLibrary/Joueurs/Joueur.cs index 1f9c21a..d465c04 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 4165f16..d8a6b15 100644 --- a/Sources/UnitTesting/JoueurUT.cs +++ b/Sources/UnitTesting/JoueurUT.cs @@ -1,6 +1,9 @@ using CoreLibrary.Core; using CoreLibrary.Evenements; +using CoreLibrary.Exceptions; using CoreLibrary.Joueurs; +using CoreLibrary.Regles; +using CoreLibrary.Statistiques; using System.Reflection; using Xunit; @@ -33,5 +36,135 @@ namespace UnitTesting Assert.False(joueur.EstConnecte); } + [Fact] + public void TestQuandJoueurSeConnecter() + { + Joueur joueur1 = new Joueur("Joueur1"); + Joueur joueur2 = new Joueur("Joueur2"); + bool eventTriggered = false; + JoueurSeConnecterEventArgs? eventArgs = null; + + joueur1.JoueurSeConnecter += (sender, e) => + { + eventTriggered = true; + eventArgs = e; + }; + + MethodInfo? methodInfo = typeof(Joueur).GetMethod("QuandJoueurSeConnecter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + methodInfo?.Invoke(joueur1, new object[] { joueur2 }); + + Assert.True(eventTriggered); + Assert.NotNull(eventArgs); + Assert.Equal(joueur2, eventArgs?.Joueur); + } + + [Fact] + public void TestSeConnecterDeclencheEvenement() + { + Joueur joueur1 = new Joueur("Joueur1"); + Joueur joueur2 = new Joueur("Joueur2"); + bool eventTriggered = false; + JoueurSeConnecterEventArgs? eventArgs = null; + + joueur1.JoueurSeConnecter += (sender, e) => + { + eventTriggered = true; + eventArgs = e; + }; + + joueur1.SeConnecter(joueur2); + + Assert.True(eventTriggered); + Assert.NotNull(eventArgs); + Assert.Equal(joueur2, eventArgs?.Joueur); + } + + [Fact] + public void TestSeConnecterThrowException() + { + Joueur joueur = new Joueur("Joueur"); + joueur.SeConnecter(joueur); + + Assert.Throws(() => joueur.SeConnecter(joueur)); + } + + [Fact] + public void SeConnecter_MetAJourEstConnecte() + { + Joueur joueur = new Joueur("Joueur"); + joueur.SeConnecter(joueur); + + Assert.True(joueur.EstConnecte); + } + + [Fact] + public void TestToStringValide() + { + string nom = "Joueur"; + Joueur joueur = new Joueur(nom); + string result = joueur.ToString(); + Assert.Equal(nom, result); + } + + [Fact] + public void TestToStringVide() + { + Joueur joueur = new Joueur(); + string result = joueur.ToString(); + + Assert.Equal("", result); + } + + [Fact] + public void TestStatistiqueNonDefinie() + { + Joueur joueur = new Joueur("Joueur"); + ReglesClassiques regles = new ReglesClassiques(); + Statistique statistique = new Statistique(); + + int result = joueur.Statistique(regles, statistique); + + Assert.Equal(0, result); + } + + [Fact] + public void TestStatistiqueDefinie() + { + Joueur joueur = new Joueur("Joueur"); + ReglesClassiques regles = new ReglesClassiques(); + Statistique statistique = new Statistique(); + joueur.IncrementerStatistique(regles, statistique); + + int result = joueur.Statistique(regles, statistique); + + Assert.Equal(1, result); + } + + [Fact] + public void TestIncrementerStatistiqueUn() + { + Joueur joueur = new Joueur("Joueur"); + ReglesClassiques regles = new ReglesClassiques(); + Statistique statistique = new Statistique(); + + joueur.IncrementerStatistique(regles, statistique); + int result = joueur.Statistique(regles, statistique); + + Assert.Equal(1, result); + } + + [Fact] + public void TestIncrementerStatistiqueDeux() + { + Joueur joueur = new Joueur("Joueur"); + ReglesClassiques regles = new ReglesClassiques(); + Statistique statistique = new Statistique(); + + joueur.IncrementerStatistique(regles, statistique); + joueur.IncrementerStatistique(regles, statistique); + int result = joueur.Statistique(regles, statistique); + + Assert.Equal(2, result); + } } } diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs new file mode 100644 index 0000000..34b6ff4 --- /dev/null +++ b/Sources/UnitTesting/PartieUT.cs @@ -0,0 +1,479 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Xunit; +using CoreLibrary; +using CoreLibrary.Core; +using CoreLibrary.Joueurs; +using CoreLibrary.Regles; +using CoreLibrary.Evenements; + + +namespace UnitTesting +{ + public class PartieUT + { + [Fact] + + public void TestPremierConstructeurValide() + { + IRegles regle = new ReglesClassiques(); + + Partie partie = new Partie(regle); + Assert.Equal(regle, partie.Regles); + } + + [Fact] + public void TestSecondConstructeurValide() + { + + IRegles regles = new ReglesClassiques(); + Partie partieOriginale = new Partie(regles); + + + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo? plateauxField = typeof(Partie).GetField("plateaux", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo? courantField = typeof(Partie).GetField("courant", BindingFlags.NonPublic | BindingFlags.Instance); + PropertyInfo? tourField = typeof(Partie).GetProperty("Tour", BindingFlags.Public | BindingFlags.Instance); + PropertyInfo? termineField = typeof(Partie).GetProperty("Termine", BindingFlags.Public | BindingFlags.Instance); + + + if (joueursField != null && plateauxField != null) + { + Dictionary? joueurs = joueursField.GetValue(partieOriginale) as Dictionary; + if (joueurs != null) + { + joueurs.Add("Joueur1", false); + joueurs.Add("Joueur2", true); + } + + List? plateaux = plateauxField.GetValue(partieOriginale) as List; + if (plateaux != null) + { + plateaux.Add(new Plateau(4, 10)); + plateaux.Add(new Plateau(4, 10)); + } + + + + if(tourField != null) + tourField.SetValue(partieOriginale, 5); + + if (termineField != null) + termineField.SetValue(partieOriginale, false); + + + var partieCopiee = new Partie(partieOriginale); + + + Dictionary? joueursCopie = joueursField.GetValue(partieCopiee) as Dictionary; + List? plateauxCopie = plateauxField.GetValue(partieCopiee) as List; + + if (courantField != null && tourField != null && termineField != null) + { + int? courantCopie = courantField.GetValue(partieCopiee) as int?; + int? tourCopie = tourField.GetValue(partieCopiee) as int?; + bool? termineCopie = termineField.GetValue(partieCopiee) as bool?; + + + if (joueursCopie != null && plateauxCopie != null && courantCopie != null && tourCopie != null && termineCopie != null && joueurs != null && plateaux != null) + { + Assert.Equal(joueurs.Count, joueursCopie.Count); + Assert.Equal(plateaux.Count, plateauxCopie.Count); + Assert.Equal(5, tourCopie.Value); + Assert.False(termineCopie.Value); + Assert.Equal(regles, partieCopiee.Regles); + } + + + + if (joueurs != null && joueursCopie != null) + { + foreach (string joueur in joueurs.Keys) + { + Assert.True(joueursCopie.ContainsKey(joueur)); + Assert.Equal(joueurs[joueur], joueursCopie[joueur]); + } + } + + if (plateaux != null && plateauxCopie != null) + { + foreach (Plateau plateau in plateaux) + { + Assert.Contains(plateau, plateauxCopie); + } + } + + } + + } + } + + [Fact] + public void TestJouerDemanderJouer() + { + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + + if(joueursField != null) + { + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if (joueurs != null) + { + joueurs.Add("Joueur1", false); + } + + bool demanderJoueurCalled = false; + partie.PartieDemanderJoueur += (sender, e) => demanderJoueurCalled = true; + partie.Jouer(); + Assert.True(demanderJoueurCalled); + } + } + + [Fact] + public void TestJouerDebutPartie() + { + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + + if (joueursField != null) + { + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if (joueurs != null) + { + joueurs.Add("Joueur1", false); + joueurs.Add("Joueur2", false); + } + + bool debutPartieCalled = false; + partie.PartieDebutPartie += (sender, e) => debutPartieCalled = true; + partie.Jouer(); + Assert.True(debutPartieCalled); + } + } + + [Fact] + public void TestDemanderJoueur() + { + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + + Joueur? joueurDemande = null; + + partie.PartieDemanderJoueur += (sender, e) => + { + joueurDemande = e.JoueurDemande; + }; + + MethodInfo? methodInfo = typeof(Partie).GetMethod("DemanderJoueur", BindingFlags.NonPublic | BindingFlags.Instance); + methodInfo?.Invoke(partie, null); + + Assert.NotNull(joueurDemande); + bool joueurConnecteCalled = false; + if (joueurDemande != null) + { + joueurDemande.JoueurSeConnecter += (sender, e) => joueurConnecteCalled = true; + joueurDemande.SeConnecter(joueurDemande); + Assert.True(joueurConnecteCalled); + } + } + + [Fact] + public void TestJoueurConnecte() + { + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + + bool demanderJoueurAppelée = false; + partie.PartieDemanderJoueur += (sender, e) => demanderJoueurAppelée = true; + + Joueur? joueur1 = new Joueur("Joueur1"); + JoueurSeConnecterEventArgs joueur1EventArgs = new JoueurSeConnecterEventArgs(joueur1); + + + MethodInfo? methodInfo = typeof(Partie).GetMethod("JoueurConnecte", BindingFlags.NonPublic | BindingFlags.Instance); + methodInfo?.Invoke(partie, new object?[] { null, joueur1EventArgs }); + + + Assert.True(demanderJoueurAppelée); + + FieldInfo? joueursField = typeof(Partie).GetField("joueurs", BindingFlags.NonPublic | BindingFlags.Instance); + + if (joueursField != null) + { + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if(joueurs != null) + Assert.True(joueurs.ContainsKey(joueur1.Nom)); + } + + FieldInfo? plateauxField = typeof(Partie).GetField("plateaux", BindingFlags.NonPublic | BindingFlags.Instance); + if (plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + if(plateaux != null) + Assert.Single(plateaux); + } + + Joueur joueur2 = new Joueur("Joueur2"); + JoueurSeConnecterEventArgs joueur2EventArgs = new JoueurSeConnecterEventArgs(joueur2); + + demanderJoueurAppelée = false; + methodInfo?.Invoke(partie, new object?[] { null, joueur2EventArgs }); + + if(joueursField != null) + { + Dictionary? joueurs = joueursField.GetValue(partie) as Dictionary; + if(joueurs != null) + { + Assert.Equal(2, joueurs.Count); + Assert.True(joueurs.ContainsKey(joueur2.Nom)); + } + + } + + if (plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + if (plateaux != null) + Assert.Equal(2, plateaux.Count); + } + + PropertyInfo? tourProperty = typeof(Partie).GetProperty("Tour", BindingFlags.Public | BindingFlags.Instance); + if (tourProperty != null) + { + object? tourValue = tourProperty.GetValue(partie); + int? tour = tourValue as int?; + if (tour != null) + { + Assert.Equal(1, tour); + } + } + } + [Fact] + public void TestDebutPartie() + { + + IRegles regle = new ReglesClassiques(); + Partie partie = new Partie(regle); + + PropertyInfo? plateauxField = typeof(Partie).GetProperty("plateaux", BindingFlags.Public | BindingFlags.Instance); + if (plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + if (plateaux != null) + { + plateaux.Add(new Plateau(4, 10)); + plateaux.Add(new Plateau(4, 10)); + } + } + + 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) + { + object? tourValue = tourProperty.GetValue(partie); + int? tour = tourValue as int?; + if (tour != null) + { + Assert.Equal(1, tour); + } + } + + bool plateauAjouterCodeCalled = false; + + if (plateauxField != null) + { + List? plateaux = plateauxField.GetValue(partie) as List; + if (plateaux != null) + { + foreach (Plateau plateau in plateaux) + { + plateau.PlateauAjouterCode += (sender, e) => plateauAjouterCodeCalled = true; + plateau.AjouterCode(new Code(regle.TailleCode)); + Assert.True(plateauAjouterCodeCalled); + } + } + } + } + + [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); + }; + + + } + } + } + } +}