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; using CoreLibrary.Exceptions; 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 TestJoueurConnecteDejaPresent() { IRegles regle = new ReglesClassiques(); Partie partie = new Partie(regle); bool appelee = false; partie.PartieDemanderJoueur += (sender, e) => { appelee = true; if (e.Indice == 1) e.JoueurDemande.SeConnecter(new Joueur("Céleste")); else Assert.Throws(() => e.JoueurDemande.SeConnecter(new Joueur("Céleste"))); }; partie.Jouer(); Assert.True(appelee); } [Fact] public void TestJoueurConnecteNomInterdit() { IRegles regle = new ReglesClassiques(); Partie partie = new Partie(regle); bool appelee = false; partie.PartieDemanderJoueur += (sender, e) => { appelee = true; Assert.Throws(() => e.JoueurDemande.SeConnecter(new Joueur("Robot"))); }; partie.Jouer(); Assert.True(appelee); } [Fact] public void TestRobot() { IRegles regle = new ReglesClassiques(); Partie partie = new Partie(regle); Assert.NotNull(partie.Robots); Assert.Empty(partie.Robots); partie.PartieDemanderJoueur += (sender, e) => { e.JoueurDemande.SeConnecter(new Robot()); }; partie.Jouer(); Assert.NotEmpty(partie.Robots); } [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); }; } } } } }