From b988b31dd79c0e6da924fd6ebd2cda697db59b57 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 6 Jun 2024 11:38:26 +0200 Subject: [PATCH 1/7] test Constructeur partie --- Sources/UnitTesting/PartieUT.cs | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Sources/UnitTesting/PartieUT.cs diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs new file mode 100644 index 0000000..9666a31 --- /dev/null +++ b/Sources/UnitTesting/PartieUT.cs @@ -0,0 +1,82 @@ +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; + +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 Partie_Constructeur_De_Copie_Cree_Une_Nouvelle_Instance_Avec_Les_Memes_Donnees() + { + + 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); + + + Dictionary joueurs = (Dictionary)joueursField.GetValue(partieOriginale); + joueurs.Add("Joueur1", false); + joueurs.Add("Joueur2", true); + + + List plateaux = (List)plateauxField.GetValue(partieOriginale); + plateaux.Add(new Plateau(4, 10)); + plateaux.Add(new Plateau(4, 10)); + + + tourField.SetValue(partieOriginale, 5); + termineField.SetValue(partieOriginale, false); + + + var partieCopiee = new Partie(partieOriginale); + + + Dictionary joueursCopie = (Dictionary)joueursField.GetValue(partieCopiee); + List plateauxCopie = (List)plateauxField.GetValue(partieCopiee); + int courantCopie = (int)courantField.GetValue(partieCopiee); + int tourCopie = (int)tourField.GetValue(partieCopiee); + bool termineCopie = (bool)termineField.GetValue(partieCopiee); + + + Assert.Equal(joueurs.Count, joueursCopie.Count); + Assert.Equal(plateaux.Count, plateauxCopie.Count); + Assert.Equal(5, tourCopie); + Assert.False(termineCopie); + Assert.Equal(regles, partieCopiee.Regles); + + foreach (var joueur in joueurs.Keys) + { + Assert.True(joueursCopie.ContainsKey(joueur)); + Assert.Equal(joueurs[joueur], joueursCopie[joueur]); + } + + foreach (var plateau in plateaux) + { + Assert.Contains(plateau, plateauxCopie); + } + } + } +} From be7317210dcdf79869c8e63208e5904bee96310b Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 6 Jun 2024 11:52:45 +0200 Subject: [PATCH 2/7] Test Constructeur partie code smell --- Sources/UnitTesting/PartieUT.cs | 86 ++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index 9666a31..7aad0b4 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -30,52 +30,70 @@ namespace UnitTesting 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); + 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); - Dictionary joueurs = (Dictionary)joueursField.GetValue(partieOriginale); - joueurs.Add("Joueur1", false); - joueurs.Add("Joueur2", true); + Dictionary? joueurs = joueursField.GetValue(partieOriginale) as Dictionary; - - List plateaux = (List)plateauxField.GetValue(partieOriginale); - plateaux.Add(new Plateau(4, 10)); - plateaux.Add(new Plateau(4, 10)); + if (joueurs != null) + { + joueurs.Add("Joueur1", false); + joueurs.Add("Joueur2", true); + } - - tourField.SetValue(partieOriginale, 5); - termineField.SetValue(partieOriginale, false); + + 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 = (Dictionary)joueursField.GetValue(partieCopiee); - List plateauxCopie = (List)plateauxField.GetValue(partieCopiee); - int courantCopie = (int)courantField.GetValue(partieCopiee); - int tourCopie = (int)tourField.GetValue(partieCopiee); - bool termineCopie = (bool)termineField.GetValue(partieCopiee); - - - Assert.Equal(joueurs.Count, joueursCopie.Count); - Assert.Equal(plateaux.Count, plateauxCopie.Count); - Assert.Equal(5, tourCopie); - Assert.False(termineCopie); - Assert.Equal(regles, partieCopiee.Regles); - - foreach (var joueur in joueurs.Keys) + + Dictionary? joueursCopie = joueursField.GetValue(partieCopiee) as Dictionary; + List? plateauxCopie = plateauxField.GetValue(partieCopiee) as List; + 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); + } + + foreach (String joueur in joueurs.Keys) { - Assert.True(joueursCopie.ContainsKey(joueur)); - Assert.Equal(joueurs[joueur], joueursCopie[joueur]); + if(joueursCopie != null) + { + Assert.True(joueursCopie.ContainsKey(joueur)); + Assert.Equal(joueurs[joueur], joueursCopie[joueur]); + } + } - foreach (var plateau in plateaux) + foreach (Plateau plateau in plateaux) { - Assert.Contains(plateau, plateauxCopie); + if (plateauxCopie != null) + Assert.Contains(plateau, plateauxCopie); } } } From b1b19c77f5a7c1f3f4c39babe0c532869353401c Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 6 Jun 2024 11:56:22 +0200 Subject: [PATCH 3/7] code smell --- Sources/UnitTesting/PartieUT.cs | 90 +++++++++++++++++---------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index 7aad0b4..cacb7d3 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -25,11 +25,11 @@ namespace UnitTesting [Fact] public void Partie_Constructeur_De_Copie_Cree_Une_Nouvelle_Instance_Avec_Les_Memes_Donnees() { - + 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); @@ -37,63 +37,65 @@ namespace UnitTesting PropertyInfo? termineField = typeof(Partie).GetProperty("Termine", BindingFlags.Public | BindingFlags.Instance); - Dictionary? joueurs = joueursField.GetValue(partieOriginale) as Dictionary; - - if (joueurs != null) + if (joueursField != null && plateauxField != null) { - joueurs.Add("Joueur1", false); - joueurs.Add("Joueur2", true); - } - + 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; + List? plateaux = plateauxField.GetValue(partieOriginale) as List; + if (plateaux != null) + { + plateaux.Add(new Plateau(4, 10)); + plateaux.Add(new Plateau(4, 10)); + } - if (plateaux != null) - { - plateaux.Add(new Plateau(4, 10)); - plateaux.Add(new Plateau(4, 10)); - } + - if(tourField != null) - tourField.SetValue(partieOriginale, 5); + if(tourField != null) + tourField.SetValue(partieOriginale, 5); - if (termineField != null) - termineField.SetValue(partieOriginale, false); + if (termineField != null) + termineField.SetValue(partieOriginale, false); - var partieCopiee = new Partie(partieOriginale); + var partieCopiee = new Partie(partieOriginale); - Dictionary? joueursCopie = joueursField.GetValue(partieCopiee) as Dictionary; - List? plateauxCopie = plateauxField.GetValue(partieCopiee) as List; - int? courantCopie = courantField.GetValue(partieCopiee) as int?; - int? tourCopie = tourField.GetValue(partieCopiee) as int?; - bool? termineCopie = termineField.GetValue(partieCopiee) as bool?; + Dictionary? joueursCopie = joueursField.GetValue(partieCopiee) as Dictionary; + List? plateauxCopie = plateauxField.GetValue(partieCopiee) as List; + 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); - } - - foreach (String joueur in joueurs.Keys) - { - if(joueursCopie != null) + if (joueursCopie != null && plateauxCopie != null && courantCopie != null && tourCopie != null && termineCopie != null && joueurs != null && plateaux != null) { - Assert.True(joueursCopie.ContainsKey(joueur)); - Assert.Equal(joueurs[joueur], joueursCopie[joueur]); + 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); } + + foreach (String joueur in joueurs.Keys) + { + if(joueursCopie != null) + { + Assert.True(joueursCopie.ContainsKey(joueur)); + Assert.Equal(joueurs[joueur], joueursCopie[joueur]); + } - } + } - foreach (Plateau plateau in plateaux) - { - if (plateauxCopie != null) - Assert.Contains(plateau, plateauxCopie); + foreach (Plateau plateau in plateaux) + { + if (plateauxCopie != null) + Assert.Contains(plateau, plateauxCopie); + } } } } From e9402b247c87936ad1bc82509ade22116a92f771 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 6 Jun 2024 12:00:19 +0200 Subject: [PATCH 4/7] code smell --- Sources/UnitTesting/PartieUT.cs | 53 +++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index cacb7d3..8721d43 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -67,35 +67,44 @@ namespace UnitTesting Dictionary? joueursCopie = joueursField.GetValue(partieCopiee) as Dictionary; List? plateauxCopie = plateauxField.GetValue(partieCopiee) as List; - 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) + if (courantField != null && tourField != null && termineField != 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); - } + int? courantCopie = courantField.GetValue(partieCopiee) as int?; + int? tourCopie = tourField.GetValue(partieCopiee) as int?; + bool? termineCopie = termineField.GetValue(partieCopiee) as bool?; - foreach (String joueur in joueurs.Keys) - { - if(joueursCopie != null) + + if (joueursCopie != null && plateauxCopie != null && courantCopie != null && tourCopie != null && termineCopie != null && joueurs != null && plateaux != null) { - Assert.True(joueursCopie.ContainsKey(joueur)); - Assert.Equal(joueurs[joueur], joueursCopie[joueur]); + 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); + } } - - } - foreach (Plateau plateau in plateaux) - { - if (plateauxCopie != null) - Assert.Contains(plateau, plateauxCopie); } + } } } From 5881bfb712bbf28ef3e42b1bab2c1c06435b1efe Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Thu, 6 Jun 2024 15:22:28 +0200 Subject: [PATCH 5/7] Test joueur --- Sources/UnitTesting/JoueurUT.cs | 133 ++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/Sources/UnitTesting/JoueurUT.cs b/Sources/UnitTesting/JoueurUT.cs index 4165f16..78edd94 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() + { + Joueur joueur = new Joueur("Joueur"); + string result = joueur.ToString(); + + Assert.Equal("Joueur1", 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); + } } } From d193fea54d118018bc6a7979077e5459242244ee Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 6 Jun 2024 16:34:31 +0200 Subject: [PATCH 6/7] Test Partie --- Sources/UnitTesting/PartieUT.cs | 200 +++++++++++++++++++++++++++++++- 1 file changed, 199 insertions(+), 1 deletion(-) diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index 8721d43..49fa4ae 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -7,6 +7,8 @@ using CoreLibrary; using CoreLibrary.Core; using CoreLibrary.Joueurs; using CoreLibrary.Regles; +using CoreLibrary.Evenements; + namespace UnitTesting { @@ -23,7 +25,7 @@ namespace UnitTesting } [Fact] - public void Partie_Constructeur_De_Copie_Cree_Une_Nouvelle_Instance_Avec_Les_Memes_Donnees() + public void TestSecondConstructeurValide() { IRegles regles = new ReglesClassiques(); @@ -107,5 +109,201 @@ namespace UnitTesting } } + + [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); + } + } + } + } } } From 6a80ecdb75cf659a332ba696b0073b73d2f5433d Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 6 Jun 2024 18:51:31 +0200 Subject: [PATCH 7/7] Test Partie --- Sources/CoreLibrary/Joueurs/Joueur.cs | 2 +- Sources/UnitTesting/JoueurUT.cs | 6 +- Sources/UnitTesting/PartieUT.cs | 178 +++++++++++++++++++++++++- 3 files changed, 178 insertions(+), 8 deletions(-) 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); + }; + + + } + } + } } }