From 3b72d209de483a14527acf279d2f6760dfe05a37 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Mon, 13 May 2024 11:48:19 +0200 Subject: [PATCH 01/21] Tests Code et Plateau --- Sources/UnitTesting/CodeUT.cs | 31 +++++++++++++++++ Sources/UnitTesting/PlateauUT.cs | 60 ++++++++++++++++++++++++++++---- 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/Sources/UnitTesting/CodeUT.cs b/Sources/UnitTesting/CodeUT.cs index fe39ebb..36f236b 100644 --- a/Sources/UnitTesting/CodeUT.cs +++ b/Sources/UnitTesting/CodeUT.cs @@ -88,6 +88,13 @@ namespace UnitTesting Assert.Throws(() => code.RecupererJeton(4)); } + [Fact] + public void TestRecupererJetonNull() + { + Code code = new Code(4); + Assert.Throws(() => code.RecupererJeton(1)); + } + [Fact] public void TestJetonsValid() { @@ -170,6 +177,19 @@ namespace UnitTesting Assert.DoesNotContain(Indicateur.BONNEPLACE, indicateurs); } + [Fact] + public void TestComparerBonnePlace() + { + Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); + Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.NOIR)]); + + IEnumerable indicateurs = code.Comparer(autreCode); + + Assert.Equal(2, indicateurs.Count()); + Assert.DoesNotContain(Indicateur.BONNECOULEUR, indicateurs); + Assert.Contains(Indicateur.BONNEPLACE, indicateurs); + } + [Fact] public void TestComparerGoodPlaceAndColor() { @@ -192,6 +212,17 @@ namespace UnitTesting Assert.Empty(indicateurs); } + + [Fact] + public void TestComparerCodeIncomplet() + { + Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); + Code autreCode = new Code(3); + IEnumerable indicateurs = code.Comparer(autreCode); + + Assert.Empty(indicateurs); + } + } } diff --git a/Sources/UnitTesting/PlateauUT.cs b/Sources/UnitTesting/PlateauUT.cs index 1b99bb7..b0c8f9d 100644 --- a/Sources/UnitTesting/PlateauUT.cs +++ b/Sources/UnitTesting/PlateauUT.cs @@ -1,6 +1,6 @@ using CoreLibrary; using CoreLibrary.Exceptions; -using System.Linq; +using System.Reflection; using Xunit; namespace UnitTesting @@ -20,6 +20,7 @@ namespace UnitTesting public void TestConstructorInvalid() { Assert.Throws(() => new Plateau(0, 10)); + Assert.Throws(() => new Plateau(3, 0)); } [Fact] @@ -50,7 +51,7 @@ namespace UnitTesting } [Fact] - public void TestAjouterCodeValid() + public void TestAjouterCodeValide() { Plateau plateau = new Plateau(4, 10); Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); @@ -61,7 +62,7 @@ namespace UnitTesting } [Fact] - public void TestAjouterCodeIncorrectSize() + public void TestAjouterCodeTailleIncorrecte() { Plateau plateau = new Plateau(4, 10); Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); @@ -70,7 +71,7 @@ namespace UnitTesting } [Fact] - public void TestAjouterCodeIncomplete() + public void TestAjouterCodeIncomplet() { Plateau plateau = new Plateau(4, 10); Code code = new Code(4); @@ -79,14 +80,61 @@ namespace UnitTesting } [Fact] - public void TestEstBonCodeTrue() + 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); + } + + [Fact] + public void TestEstBonCodeTailleException() + { + Plateau plateau = new Plateau(3, 5); + Code code = new Code(4); + + Assert.Throws(() => plateau.EstBonCode(code)); + } + + [Fact] + public void TestEstBonCodeIncomplet() + { + Plateau plateau = new Plateau(3, 5); + Code code = new Code(3); + + Assert.Throws(() => plateau.EstBonCode(code)); + } + + [Fact] + public void TestEstBonCodeTrue() + { + 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); + bool resultat = plateau.EstBonCode(codeSecret); + Assert.True(resultat); } [Fact] public void TestEstBonCodeFalse() { + // Modifier pour obtenir code différent de codeSecret Plateau plateau = new Plateau(4, 10); Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); plateau.AjouterCode(code); From 22564d1bdcf21baca3e130ffed981d87bac0c704 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Mon, 13 May 2024 17:52:43 +0200 Subject: [PATCH 02/21] Correction test Code --- Sources/UnitTesting/CodeUT.cs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/Sources/UnitTesting/CodeUT.cs b/Sources/UnitTesting/CodeUT.cs index 36f236b..4f2a6a1 100644 --- a/Sources/UnitTesting/CodeUT.cs +++ b/Sources/UnitTesting/CodeUT.cs @@ -139,7 +139,7 @@ namespace UnitTesting } [Fact] - public void TestComparerValid() + public void TestComparerValide() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); @@ -152,7 +152,7 @@ namespace UnitTesting } [Fact] - public void TestComparerGoodPlace() + public void TestComparerBonnePlace() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.NOIR)]); @@ -165,7 +165,7 @@ namespace UnitTesting } [Fact] - public void TestComparerGoodColor() + public void TestComparerBonneCouleur() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Code autreCode = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.ROUGE), new Jeton(Couleur.NOIR)]); @@ -178,43 +178,40 @@ namespace UnitTesting } [Fact] - public void TestComparerBonnePlace() + public void TestComparerBonnePlaceEtCouleur() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); - Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.NOIR)]); + Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLANC), new Jeton(Couleur.BLEU)]); IEnumerable indicateurs = code.Comparer(autreCode); - Assert.Equal(2, indicateurs.Count()); - Assert.DoesNotContain(Indicateur.BONNECOULEUR, indicateurs); + Assert.Equal(3, indicateurs.Count()); Assert.Contains(Indicateur.BONNEPLACE, indicateurs); + Assert.Contains(Indicateur.BONNECOULEUR, indicateurs); } [Fact] - public void TestComparerGoodPlaceAndColor() + public void TestComparerDifferent() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); - Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLANC), new Jeton(Couleur.BLEU)]); - + Code autreCode = new Code([new Jeton(Couleur.VERT), new Jeton(Couleur.JAUNE), new Jeton(Couleur.NOIR)]); IEnumerable indicateurs = code.Comparer(autreCode); - Assert.Equal(3, indicateurs.Count()); - Assert.Contains(Indicateur.BONNEPLACE, indicateurs); - Assert.Contains(Indicateur.BONNECOULEUR, indicateurs); + Assert.Empty(indicateurs); } [Fact] - public void TestComparerNoMatch() + public void TestComparerMonCodeIncomplet() { - Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); - Code autreCode = new Code([new Jeton(Couleur.VERT), new Jeton(Couleur.JAUNE), new Jeton(Couleur.NOIR)]); + Code code = new Code(3); + Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); IEnumerable indicateurs = code.Comparer(autreCode); Assert.Empty(indicateurs); } [Fact] - public void TestComparerCodeIncomplet() + public void TestComparerSonCodeIncomplet() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Code autreCode = new Code(3); From 54e859fe2a01705631666be2d023ee88e27fa090 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Mon, 13 May 2024 19:06:22 +0200 Subject: [PATCH 03/21] Test de Plateau --- Sources/UnitTesting/PlateauUT.cs | 87 ++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/Sources/UnitTesting/PlateauUT.cs b/Sources/UnitTesting/PlateauUT.cs index b0c8f9d..1012b35 100644 --- a/Sources/UnitTesting/PlateauUT.cs +++ b/Sources/UnitTesting/PlateauUT.cs @@ -127,21 +127,71 @@ namespace UnitTesting Code? codeSecret = (Code?)fieldInfo.GetValue(plateau); Assert.NotNull(codeSecret); - bool resultat = plateau.EstBonCode(codeSecret); - Assert.True(resultat); + Assert.True(plateau.EstBonCode(codeSecret)); } [Fact] public void TestEstBonCodeFalse() { - // Modifier pour obtenir code différent de codeSecret + List couleurs = new List((Couleur[])Enum.GetValues(typeof(Couleur))); + Plateau plateau = new Plateau(4, 10); - Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); - plateau.AjouterCode(code); + + 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); + + Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray(); + + Couleur couleurJeton = jetons[0].Couleur; + int indice = couleurs.IndexOf(couleurJeton) + 1; + if (indice >= couleurs.Count) + indice = 0; + + jetons[0] = new Jeton(couleurs[indice]); + + Code code = new Code(jetons); Assert.False(plateau.EstBonCode(code)); } + [Fact] + public void TestEstBonCodeAucunIndicateur() + { + List couleurs = new List((Couleur[])Enum.GetValues(typeof(Couleur))); + 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); + + Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray(); + + for (int i=0; i Date: Mon, 13 May 2024 19:22:04 +0200 Subject: [PATCH 04/21] Test regles classiques --- Sources/UnitTesting/ReglesClassiquesUT.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/UnitTesting/ReglesClassiquesUT.cs b/Sources/UnitTesting/ReglesClassiquesUT.cs index 6e05912..8da2a13 100644 --- a/Sources/UnitTesting/ReglesClassiquesUT.cs +++ b/Sources/UnitTesting/ReglesClassiquesUT.cs @@ -6,6 +6,12 @@ namespace UnitTesting { public class ReglesClassiquesUT { + [Fact] + public void TestNom() + { + ReglesClassiques regle = new ReglesClassiques(); + Assert.Equal("Règles classiques", regle.Nom); + } [Fact] public void TestConstructor() { From ec59f2fcdb2d1e09eeb0f148dfe22531acd410d0 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Mon, 13 May 2024 22:14:25 +0200 Subject: [PATCH 05/21] test unitaire classe ajoutercodeeventargs --- Sources/UnitTesting/AjouterCodeEventArgsUT.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Sources/UnitTesting/AjouterCodeEventArgsUT.cs diff --git a/Sources/UnitTesting/AjouterCodeEventArgsUT.cs b/Sources/UnitTesting/AjouterCodeEventArgsUT.cs new file mode 100644 index 0000000..4069c36 --- /dev/null +++ b/Sources/UnitTesting/AjouterCodeEventArgsUT.cs @@ -0,0 +1,20 @@ +using CoreLibrary; +using CoreLibrary.Events; +using Xunit; + +namespace UnitTesting +{ + public class AjouterCodeEventArgsUT + { + [Fact] + public void TestConstructeurValide() + { + Code monCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU)]); + + AjouterCodeEventArgs evenement = new AjouterCodeEventArgs(monCode); + + Assert.Equal(monCode, evenement.Code); + } + + } +} From e999c3f547d37ec27be2236288eb95af355508d8 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Mon, 13 May 2024 22:27:13 +0200 Subject: [PATCH 06/21] Tests unitaires des event args --- .../UnitTesting/AjouterJetonEventArgsUT.cs | 20 +++++++++++++++ Sources/UnitTesting/AjouterJoueurEventArgs.cs | 20 +++++++++++++++ .../UnitTesting/DemanderJoueurEventArgsUT.cs | 18 +++++++++++++ Sources/UnitTesting/NouveauTourEventArgsUT.cs | 25 +++++++++++++++++++ .../UnitTesting/PartieTermineeEventArgsUT.cs | 23 +++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 Sources/UnitTesting/AjouterJetonEventArgsUT.cs create mode 100644 Sources/UnitTesting/AjouterJoueurEventArgs.cs create mode 100644 Sources/UnitTesting/DemanderJoueurEventArgsUT.cs create mode 100644 Sources/UnitTesting/NouveauTourEventArgsUT.cs create mode 100644 Sources/UnitTesting/PartieTermineeEventArgsUT.cs diff --git a/Sources/UnitTesting/AjouterJetonEventArgsUT.cs b/Sources/UnitTesting/AjouterJetonEventArgsUT.cs new file mode 100644 index 0000000..efab09b --- /dev/null +++ b/Sources/UnitTesting/AjouterJetonEventArgsUT.cs @@ -0,0 +1,20 @@ +using CoreLibrary; +using CoreLibrary.Events; +using Xunit; + +namespace UnitTesting +{ + public class AjouterJetonEventArgsUT + { + [Fact] + public void TestConstructeurValide() + { + Jeton monJeton = new Jeton(Couleur.JAUNE); + + AjouterJetonEventArgs evenement = new AjouterJetonEventArgs(monJeton); + + Assert.Equal(monJeton, evenement.Jeton); + } + + } +} diff --git a/Sources/UnitTesting/AjouterJoueurEventArgs.cs b/Sources/UnitTesting/AjouterJoueurEventArgs.cs new file mode 100644 index 0000000..7785e96 --- /dev/null +++ b/Sources/UnitTesting/AjouterJoueurEventArgs.cs @@ -0,0 +1,20 @@ +using CoreLibrary; +using CoreLibrary.Events; +using Xunit; + +namespace UnitTesting +{ + public class AjouterJoueurEventArgsUT + { + [Fact] + public void TestConstructeurValide() + { + Joueur monJoueur = new Joueur("Céleste", new Plateau(4, 12)); + + AjouterJoueursEventArgs evenement = new AjouterJoueursEventArgs(monJoueur); + + Assert.Equal(monJoueur, evenement.Joueur); + } + + } +} diff --git a/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs b/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs new file mode 100644 index 0000000..0a8d594 --- /dev/null +++ b/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs @@ -0,0 +1,18 @@ +using CoreLibrary; +using CoreLibrary.Events; +using Xunit; + +namespace UnitTesting +{ + public class DemanderJoueurEventArgsUT + { + [Fact] + public void TestConstructeurValide() + { + DemanderJoueurEventArgs evenement = new DemanderJoueurEventArgs(3); + + Assert.Equal(3, evenement.Numero); + } + + } +} diff --git a/Sources/UnitTesting/NouveauTourEventArgsUT.cs b/Sources/UnitTesting/NouveauTourEventArgsUT.cs new file mode 100644 index 0000000..67e85e6 --- /dev/null +++ b/Sources/UnitTesting/NouveauTourEventArgsUT.cs @@ -0,0 +1,25 @@ +using CoreLibrary; +using CoreLibrary.Events; +using Xunit; + +namespace UnitTesting +{ + public class NouveauTourEventArgsUT + { + [Fact] + public void TestConstructeurValide() + { + Plateau monPlateau = new Plateau(4, 12); + Joueur monJoueur = new Joueur("Céleste", monPlateau); + + NouveauTourEventArgs evenement = + new NouveauTourEventArgs(monJoueur, 5, monPlateau.Grille(), monPlateau.Indicateurs()); + + Assert.Equal(monJoueur, evenement.Joueur); + Assert.Equal(5, evenement.Tour); + Assert.Equal(monPlateau.Grille(), evenement.Grille); + Assert.Equal(monPlateau.Indicateurs(), evenement.Indicateurs); + } + + } +} diff --git a/Sources/UnitTesting/PartieTermineeEventArgsUT.cs b/Sources/UnitTesting/PartieTermineeEventArgsUT.cs new file mode 100644 index 0000000..23a53f8 --- /dev/null +++ b/Sources/UnitTesting/PartieTermineeEventArgsUT.cs @@ -0,0 +1,23 @@ +using CoreLibrary; +using CoreLibrary.Events; +using Xunit; + +namespace UnitTesting +{ + public class PartieTermineeEventArgsUT + { + [Fact] + public void TestConstructeurValide() + { + Plateau plateau = new Plateau(4, 12); + Joueur[] gagnants = [new Joueur("Pauline", plateau), new Joueur("Camille", plateau)]; + Joueur[] perdants = [new Joueur("Céleste", plateau)]; + + PartieTermineeEventArgs evenement = new PartieTermineeEventArgs(gagnants, perdants); + + Assert.Equal(gagnants, evenement.Gagnants); + Assert.Equal(perdants, evenement.Perdants); + } + + } +} From 6fe01f66988ac924598489e66ad4e81c388bec40 Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Mon, 13 May 2024 22:49:54 +0200 Subject: [PATCH 07/21] tentative tests unitaires sur une exception --- .../UnitTesting/DemanderJoueurEventArgsUT.cs | 3 +- .../UnitTesting/GrilleCompleteExceptionUT.cs | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 Sources/UnitTesting/GrilleCompleteExceptionUT.cs diff --git a/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs b/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs index 0a8d594..7662d5d 100644 --- a/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs +++ b/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs @@ -1,5 +1,4 @@ -using CoreLibrary; -using CoreLibrary.Events; +using CoreLibrary.Events; using Xunit; namespace UnitTesting diff --git a/Sources/UnitTesting/GrilleCompleteExceptionUT.cs b/Sources/UnitTesting/GrilleCompleteExceptionUT.cs new file mode 100644 index 0000000..592fd65 --- /dev/null +++ b/Sources/UnitTesting/GrilleCompleteExceptionUT.cs @@ -0,0 +1,54 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class GrilleCompleteExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new GrilleCompleteException()); + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new GrilleCompleteException(message)); + + try + { + throw new GrilleCompleteException(message); + } + catch(GrilleCompleteException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new GrilleCompleteException(message, parent)); + + try + { + throw new GrilleCompleteException(message, parent); + } + catch (GrilleCompleteException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} From 5dbb30d4859e4c2837f698bd64bffd7f559128fb Mon Sep 17 00:00:00 2001 From: "nicolas.barbosa" Date: Mon, 13 May 2024 23:12:03 +0200 Subject: [PATCH 08/21] tests unitaires sur les exceptions --- Sources/UnitTesting/CodeCompletExceptionUT.cs | 54 ++++++++++++++ .../UnitTesting/CodeIncompletExceptionUT.cs | 54 ++++++++++++++ .../UnitTesting/CodeInvalideExceptionUT.cs | 71 +++++++++++++++++++ Sources/UnitTesting/CodeVideExceptionUT.cs | 54 ++++++++++++++ Sources/UnitTesting/IndiceCodeExceptionUT.cs | 70 ++++++++++++++++++ .../PartieNonCommenceeExceptionUT.cs | 54 ++++++++++++++ Sources/UnitTesting/TailleCodeExceptionUT.cs | 69 ++++++++++++++++++ .../UnitTesting/TailleGrilleExceptionUT.cs | 69 ++++++++++++++++++ 8 files changed, 495 insertions(+) create mode 100644 Sources/UnitTesting/CodeCompletExceptionUT.cs create mode 100644 Sources/UnitTesting/CodeIncompletExceptionUT.cs create mode 100644 Sources/UnitTesting/CodeInvalideExceptionUT.cs create mode 100644 Sources/UnitTesting/CodeVideExceptionUT.cs create mode 100644 Sources/UnitTesting/IndiceCodeExceptionUT.cs create mode 100644 Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs create mode 100644 Sources/UnitTesting/TailleCodeExceptionUT.cs create mode 100644 Sources/UnitTesting/TailleGrilleExceptionUT.cs diff --git a/Sources/UnitTesting/CodeCompletExceptionUT.cs b/Sources/UnitTesting/CodeCompletExceptionUT.cs new file mode 100644 index 0000000..06ebf97 --- /dev/null +++ b/Sources/UnitTesting/CodeCompletExceptionUT.cs @@ -0,0 +1,54 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class CodeCompletExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new CodeCompletException()); + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new CodeCompletException(message)); + + try + { + throw new CodeCompletException(message); + } + catch(CodeCompletException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new CodeCompletException(message, parent)); + + try + { + throw new CodeCompletException(message, parent); + } + catch (CodeCompletException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} diff --git a/Sources/UnitTesting/CodeIncompletExceptionUT.cs b/Sources/UnitTesting/CodeIncompletExceptionUT.cs new file mode 100644 index 0000000..b6bed0f --- /dev/null +++ b/Sources/UnitTesting/CodeIncompletExceptionUT.cs @@ -0,0 +1,54 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class CodeIncompletExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new CodeIncompletException()); + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new CodeIncompletException(message)); + + try + { + throw new CodeIncompletException(message); + } + catch(CodeIncompletException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new CodeIncompletException(message, parent)); + + try + { + throw new CodeIncompletException(message, parent); + } + catch (CodeIncompletException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} diff --git a/Sources/UnitTesting/CodeInvalideExceptionUT.cs b/Sources/UnitTesting/CodeInvalideExceptionUT.cs new file mode 100644 index 0000000..57fcf60 --- /dev/null +++ b/Sources/UnitTesting/CodeInvalideExceptionUT.cs @@ -0,0 +1,71 @@ +using CoreLibrary.Exceptions; +using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities; +using Xunit; + +namespace UnitTesting +{ + public class CodeInvalideExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new CodeInvalideException()); + } + + [Fact] + public void ExceptionAttributs() + { + Assert.ThrowsAsync(() => throw new CodeInvalideException(3, 4)); + + try + { + throw new CodeInvalideException(3, 4); + } + catch (CodeInvalideException e) + { + Assert.Contains("3", e.Message); + Assert.Contains("4", e.Message); + } + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new CodeInvalideException(message)); + + try + { + throw new CodeInvalideException(message); + } + catch(CodeInvalideException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new CodeInvalideException(message, parent)); + + try + { + throw new CodeInvalideException(message, parent); + } + catch (CodeInvalideException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} diff --git a/Sources/UnitTesting/CodeVideExceptionUT.cs b/Sources/UnitTesting/CodeVideExceptionUT.cs new file mode 100644 index 0000000..650dc3a --- /dev/null +++ b/Sources/UnitTesting/CodeVideExceptionUT.cs @@ -0,0 +1,54 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class CodeVideExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new CodeVideException()); + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new CodeVideException(message)); + + try + { + throw new CodeVideException(message); + } + catch(CodeVideException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new CodeVideException(message, parent)); + + try + { + throw new CodeVideException(message, parent); + } + catch (CodeVideException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} diff --git a/Sources/UnitTesting/IndiceCodeExceptionUT.cs b/Sources/UnitTesting/IndiceCodeExceptionUT.cs new file mode 100644 index 0000000..50e40c4 --- /dev/null +++ b/Sources/UnitTesting/IndiceCodeExceptionUT.cs @@ -0,0 +1,70 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class IndiceCodeExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new IndiceCodeException()); + } + + [Fact] + public void ExceptionAttributs() + { + Assert.ThrowsAsync(() => throw new IndiceCodeException(5, 3)); + + try + { + throw new IndiceCodeException(5, 3); + } + catch (IndiceCodeException e) + { + Assert.Contains("5", e.Message); + Assert.Contains("3", e.Message); + } + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new IndiceCodeException(message)); + + try + { + throw new IndiceCodeException(message); + } + catch(IndiceCodeException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new IndiceCodeException(message, parent)); + + try + { + throw new IndiceCodeException(message, parent); + } + catch (IndiceCodeException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} diff --git a/Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs b/Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs new file mode 100644 index 0000000..c0733cf --- /dev/null +++ b/Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs @@ -0,0 +1,54 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class PartieNonCommenceeExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new PartieNonCommenceeException()); + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new PartieNonCommenceeException(message)); + + try + { + throw new PartieNonCommenceeException(message); + } + catch(PartieNonCommenceeException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new PartieNonCommenceeException(message, parent)); + + try + { + throw new PartieNonCommenceeException(message, parent); + } + catch (PartieNonCommenceeException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} diff --git a/Sources/UnitTesting/TailleCodeExceptionUT.cs b/Sources/UnitTesting/TailleCodeExceptionUT.cs new file mode 100644 index 0000000..eda2056 --- /dev/null +++ b/Sources/UnitTesting/TailleCodeExceptionUT.cs @@ -0,0 +1,69 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class TailleCodeExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new TailleCodeException()); + } + + [Fact] + public void ExceptionAttributs() + { + Assert.ThrowsAsync(() => throw new TailleCodeException(-3)); + + try + { + throw new TailleCodeException(-3); + } + catch (TailleCodeException e) + { + Assert.Contains("-3", e.Message); + } + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new TailleCodeException(message)); + + try + { + throw new TailleCodeException(message); + } + catch(TailleCodeException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new TailleCodeException(message, parent)); + + try + { + throw new TailleCodeException(message, parent); + } + catch (TailleCodeException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} diff --git a/Sources/UnitTesting/TailleGrilleExceptionUT.cs b/Sources/UnitTesting/TailleGrilleExceptionUT.cs new file mode 100644 index 0000000..8063a17 --- /dev/null +++ b/Sources/UnitTesting/TailleGrilleExceptionUT.cs @@ -0,0 +1,69 @@ +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class TailleGrilleExceptionUT + { + [Fact] + public void ExceptionDefaut() + { + Assert.ThrowsAsync(() => throw new TailleGrilleException()); + } + + [Fact] + public void ExceptionAttributs() + { + Assert.ThrowsAsync(() => throw new TailleGrilleException(0)); + + try + { + throw new TailleCodeException(0); + } + catch (TailleCodeException e) + { + Assert.Contains("0", e.Message); + } + } + + [Fact] + public void ExceptionMessage() + { + string message = "Mon super gros problème."; + + Assert.ThrowsAsync(() => throw new TailleGrilleException(message)); + + try + { + throw new TailleGrilleException(message); + } + catch(TailleGrilleException e) + { + Assert.Equal(message, e.Message); + } + } + + [Fact] + public void ExceptionMessageEtException() + { + string message = "Mon super gros problème."; + string message2 = "Pas de chance..."; + InvalidOperationException parent = new InvalidOperationException(message2); + + Assert.ThrowsAsync(() => throw new TailleGrilleException(message, parent)); + + try + { + throw new TailleGrilleException(message, parent); + } + catch (TailleGrilleException e) + { + Assert.Equal(message, e.Message); + Assert.NotNull(e.InnerException); + Assert.IsType(e.InnerException); + Assert.Equal(message2, e.InnerException.Message); + } + } + + } +} From 65ecbf1e0d1c8bdb56df989046291fa67c3c8f59 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Tue, 14 May 2024 23:37:48 +0200 Subject: [PATCH 09/21] Test ReglesClassiques --- Sources/UnitTesting/ReglesClassiquesUT.cs | 47 +++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/Sources/UnitTesting/ReglesClassiquesUT.cs b/Sources/UnitTesting/ReglesClassiquesUT.cs index 8da2a13..89e3597 100644 --- a/Sources/UnitTesting/ReglesClassiquesUT.cs +++ b/Sources/UnitTesting/ReglesClassiquesUT.cs @@ -1,5 +1,6 @@ using CoreLibrary; using CoreLibrary.Exceptions; +using System.Reflection; using Xunit; namespace UnitTesting @@ -69,6 +70,28 @@ namespace UnitTesting Assert.Equal("joueur2", joueurCourantSuivant.Nom); } + [Fact] + public void TestPasserLaMainCompteurReinitialise() + { + ReglesClassiques regles = new ReglesClassiques(); + Type type = typeof(ReglesClassiques); + + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + regles.CommencerLaPartie(); + + FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(fieldInfo); + int? joueurCourant = (int?)fieldInfo.GetValue(regles); + + regles.PasserLaMain(); + regles.PasserLaMain(); + regles.PasserLaMain(); + + Assert.NotNull(joueurCourant); + Assert.Equal(0, joueurCourant); + } + [Fact] public void TestPasserLaMainInvalid() { @@ -101,9 +124,16 @@ namespace UnitTesting } [Fact] - public void TestEstTermineeTrue() + public void TestEstTermineeFalseUnJoueur() { - // + ReglesClassiques regles = new ReglesClassiques(); + regles.AjouterJoueur("joueur1"); + regles.CommencerLaPartie(); + regles.PasserLaMain(); + + bool result = regles.EstTerminee(); + + Assert.False(result); } [Fact] @@ -119,7 +149,18 @@ namespace UnitTesting Assert.False(estTerminee); } - //Test Gagnants et Perdants + TestEstTermineeTrue + TestConstructor PB + [Fact] + public void TestGagnantsAucunGagnants() + { + ReglesClassiques regles = new ReglesClassiques(); + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + regles.CommencerLaPartie(); + + IEnumerable gagnants = regles.Gagnants(); + + Assert.Empty(gagnants); + } } } From 3a60868f0ea122cbdc189238f9b0f178174469a7 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Wed, 15 May 2024 00:37:17 +0200 Subject: [PATCH 10/21] Modification noms des fonctions tests --- Sources/UnitTesting/CodeUT.cs | 28 +++++++++---------- Sources/UnitTesting/JetonUT.cs | 2 +- Sources/UnitTesting/JoueurUT.cs | 2 +- Sources/UnitTesting/PartieUT.cs | 33 +++++++++++++++++++++++ Sources/UnitTesting/PlateauUT.cs | 8 +++--- Sources/UnitTesting/ReglesClassiquesUT.cs | 10 +++---- Sources/UnitTesting/UnitTesting.csproj | 1 + 7 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 Sources/UnitTesting/PartieUT.cs diff --git a/Sources/UnitTesting/CodeUT.cs b/Sources/UnitTesting/CodeUT.cs index 4f2a6a1..0911ac4 100644 --- a/Sources/UnitTesting/CodeUT.cs +++ b/Sources/UnitTesting/CodeUT.cs @@ -7,7 +7,7 @@ namespace UnitTesting public class CodeUT { [Fact] - public void TestFirstConstructorValidArguments() + public void TestPremierConstructeurValide() { Code code = new Code(4); Assert.NotNull(code); @@ -16,14 +16,14 @@ namespace UnitTesting } [Fact] - public void TestConstructorInvalidArguments() + public void TestPremierConstructeurInvalide() { Assert.Throws(() => new Code(0)); Assert.Throws(() => new Code(-1)); } [Fact] - public void TestSecondConstructorValidArguments() + public void TestDeuxiemeConstructeurValide() { Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; @@ -34,13 +34,13 @@ namespace UnitTesting } [Fact] - public void TestSecondConstructorInvalidArguments() + public void TestDeuxiemeConstructeurInvalide() { Assert.Throws(() => new Code([])); } [Fact] - public void TestAjouterJetonValid() + public void TestAjouterJetonValide() { Jeton jeton = new Jeton(Couleur.JAUNE); Code code = new Code(3); @@ -50,14 +50,14 @@ namespace UnitTesting } [Fact] - public void TestAjouterJetonInvalid() + public void TestAjouterJetonInvalide() { Code code = new Code([new Jeton(Couleur.NOIR)]); Assert.Throws(() => code.AjouterJeton(new Jeton(Couleur.ROUGE))); } [Fact] - public void TestSupprimerDernierJetonValid() + public void TestSupprimerDernierJetonValide() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); code.SupprimerDernierJeton(); @@ -65,14 +65,14 @@ namespace UnitTesting } [Fact] - public void TestSupprimerDernierJetonInvalid() + public void TestSupprimerDernierJetonInvalide() { Code code = new Code(4); Assert.Throws(() => code.SupprimerDernierJeton()); } [Fact] - public void TestRecupererJetonValid() + public void TestRecupererJetonValide() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Jeton jetonAttendu = new Jeton(Couleur.BLEU); @@ -81,7 +81,7 @@ namespace UnitTesting } [Fact] - public void TestRecupererJetonInvalid() + public void TestRecupererJetonInvalide() { Code code = new Code(4); Assert.Throws(() => code.RecupererJeton(-1)); @@ -96,7 +96,7 @@ namespace UnitTesting } [Fact] - public void TestJetonsValid() + public void TestJetonsValide() { Jeton[] jetonsAttendus = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; Code code = new Code(jetonsAttendus); @@ -113,7 +113,7 @@ namespace UnitTesting } [Fact] - public void TestEstCompletValid() + public void TestEstCompletValide() { Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); bool estComplet = code.EstComplet(); @@ -121,7 +121,7 @@ namespace UnitTesting } [Fact] - public void TestEstCompletInvalid() + public void TestEstCompletInvalide() { Code code = new Code(3); bool estComplet = code.EstComplet(); @@ -129,7 +129,7 @@ namespace UnitTesting } [Fact] - public void TestTailleMaximaleValid() + public void TestTailleMaximaleValide() { Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; Code code = new Code(jetons); diff --git a/Sources/UnitTesting/JetonUT.cs b/Sources/UnitTesting/JetonUT.cs index e315b41..0af1ec5 100644 --- a/Sources/UnitTesting/JetonUT.cs +++ b/Sources/UnitTesting/JetonUT.cs @@ -6,7 +6,7 @@ namespace UnitTesting public class JetonUT { [Fact] - public void TestConstructorValid() + public void TestConstructeurValide() { Couleur[] listeCouleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); diff --git a/Sources/UnitTesting/JoueurUT.cs b/Sources/UnitTesting/JoueurUT.cs index d105743..aaf6f81 100644 --- a/Sources/UnitTesting/JoueurUT.cs +++ b/Sources/UnitTesting/JoueurUT.cs @@ -6,7 +6,7 @@ namespace UnitTesting public class JoueurUT { [Fact] - public void TestConstructorWithValidArguments() + public void TestConstructeurValide() { Plateau plateau = new Plateau(4, 10); Joueur joueur = new Joueur("MonJoueur", plateau); diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs new file mode 100644 index 0000000..8ce6f65 --- /dev/null +++ b/Sources/UnitTesting/PartieUT.cs @@ -0,0 +1,33 @@ +using CoreLibrary; +using CoreLibrary.Exceptions; +using System.Reflection; +using Xunit; + +namespace UnitTesting +{ + public class PartieUT + { + [Fact] + public void TestJouer() + { + //ReglesClassiques regles = new ReglesClassiques(); + //Partie partie = new Partie(regles); + + //partie.Jouer(); + + //Assert.True(regles.EstTerminee()); + + //IEnumerable gagnants = regles.Gagnants(); + //IEnumerable perdants = regles.Perdants(); + + //Assert.NotEmpty(gagnants); + //Assert.All(gagnants, joueur => Assert.DoesNotContain(joueur, perdants)); + + //foreach (var joueur in regles.joueurs()) + //{ + //Assert.NotNull(joueur.Plateau); + //Assert.True(joueur.Plateau.EstComplet()); + //} + } + } +} diff --git a/Sources/UnitTesting/PlateauUT.cs b/Sources/UnitTesting/PlateauUT.cs index 1012b35..dc88673 100644 --- a/Sources/UnitTesting/PlateauUT.cs +++ b/Sources/UnitTesting/PlateauUT.cs @@ -8,7 +8,7 @@ namespace UnitTesting public class PlateauUT { [Fact] - public void TestConstructorValid() + public void TestConstructeurValide() { Plateau plateau = new Plateau(4,12); Assert.NotNull(plateau); @@ -17,7 +17,7 @@ namespace UnitTesting } [Fact] - public void TestConstructorInvalid() + public void TestConstructeurInvalide() { Assert.Throws(() => new Plateau(0, 10)); Assert.Throws(() => new Plateau(3, 0)); @@ -193,7 +193,7 @@ namespace UnitTesting } [Fact] - public void TestGrilleValid() + public void TestGrilleValide() { Plateau plateau = new Plateau(4, 3); Code code1 = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); @@ -212,7 +212,7 @@ namespace UnitTesting } [Fact] - public void TestGrilleEmpty() + public void TestGrilleVide() { Plateau plateau = new Plateau(4, 3); diff --git a/Sources/UnitTesting/ReglesClassiquesUT.cs b/Sources/UnitTesting/ReglesClassiquesUT.cs index 89e3597..d0aa497 100644 --- a/Sources/UnitTesting/ReglesClassiquesUT.cs +++ b/Sources/UnitTesting/ReglesClassiquesUT.cs @@ -14,7 +14,7 @@ namespace UnitTesting Assert.Equal("Règles classiques", regle.Nom); } [Fact] - public void TestConstructor() + public void TestConstructeur() { ReglesClassiques regles = new ReglesClassiques(); @@ -34,7 +34,7 @@ namespace UnitTesting } [Fact] - public void TestJoueurCourantWithPlayer() + public void TestJoueurCourantAvecJoueur() { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("joueur1"); @@ -48,14 +48,14 @@ namespace UnitTesting } [Fact] - public void TestJoueurCourantNoPlayer() + public void TestJoueurCourantSansJoueur() { ReglesClassiques regles = new ReglesClassiques(); Assert.Throws(() => regles.JoueurCourant()); } [Fact] - public void TestPasserLaMainValid() + public void TestPasserLaMainValide() { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("joueur1"); @@ -93,7 +93,7 @@ namespace UnitTesting } [Fact] - public void TestPasserLaMainInvalid() + public void TestPasserLaMainInvalide() { ReglesClassiques regles = new ReglesClassiques(); Assert.Throws(() => regles.PasserLaMain()); diff --git a/Sources/UnitTesting/UnitTesting.csproj b/Sources/UnitTesting/UnitTesting.csproj index 3586adb..66853b1 100644 --- a/Sources/UnitTesting/UnitTesting.csproj +++ b/Sources/UnitTesting/UnitTesting.csproj @@ -23,6 +23,7 @@ + From 7cd837c045ab68c56ebfcff18cb3ee35e86b0922 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Wed, 15 May 2024 17:41:42 +0200 Subject: [PATCH 11/21] Test Utils --- Sources/UnitTesting/UtilsUT.cs | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Sources/UnitTesting/UtilsUT.cs diff --git a/Sources/UnitTesting/UtilsUT.cs b/Sources/UnitTesting/UtilsUT.cs new file mode 100644 index 0000000..b2f310f --- /dev/null +++ b/Sources/UnitTesting/UtilsUT.cs @@ -0,0 +1,60 @@ +using ConsoleApp; +using CoreLibrary; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using System.Threading.Tasks; +using Xunit; +using System.Reflection; + +namespace UnitTesting +{ + public class UtilsUT + { + [Fact] + public void TestCouleursTerminal() + { + Dictionary < Couleur, ConsoleColor> expectedCouleursTerminal = new Dictionary() + { + {Couleur.NOIR, ConsoleColor.Black }, + {Couleur.BLANC, ConsoleColor.White }, + {Couleur.ROUGE, ConsoleColor.Red }, + {Couleur.VERT, ConsoleColor.Green }, + {Couleur.BLEU, ConsoleColor.Blue }, + {Couleur.JAUNE, ConsoleColor.DarkYellow } + }; + + var actualCouleursTerminal = typeof(Utils).GetField("couleursTerminal", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); + + Assert.Equal(expectedCouleursTerminal, actualCouleursTerminal); + } + + [Fact] + public void TestIndicateursTerminal() + { + Dictionary expectedIndicateursTerminal = new Dictionary() + { + {Indicateur.BONNEPLACE, ConsoleColor.Black }, + {Indicateur.BONNECOULEUR, ConsoleColor.White } + }; + + var actualIndicateursTerminal = typeof(Utils).GetField("indicateursTerminal", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); + + Assert.Equal(expectedIndicateursTerminal, actualIndicateursTerminal); + } + + [Fact] + public void TestDessinerPionValide() + { + using (StringWriter sw = new StringWriter()) + { + Console.SetOut(sw); + Utils.DessinerPion(Couleur.NOIR); + string expected = " ⬤ "; + Assert.Equal(expected, sw.ToString()); + } + } + } +} From f53dc3d86a385de264d4ca611cd82f9c82d5e20b Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Wed, 15 May 2024 17:56:47 +0200 Subject: [PATCH 12/21] Correction code smells --- Sources/UnitTesting/PartieUT.cs | 22 ---------------------- Sources/UnitTesting/UtilsUT.cs | 6 +++--- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index 8ce6f65..87ab8d1 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -7,27 +7,5 @@ namespace UnitTesting { public class PartieUT { - [Fact] - public void TestJouer() - { - //ReglesClassiques regles = new ReglesClassiques(); - //Partie partie = new Partie(regles); - - //partie.Jouer(); - - //Assert.True(regles.EstTerminee()); - - //IEnumerable gagnants = regles.Gagnants(); - //IEnumerable perdants = regles.Perdants(); - - //Assert.NotEmpty(gagnants); - //Assert.All(gagnants, joueur => Assert.DoesNotContain(joueur, perdants)); - - //foreach (var joueur in regles.joueurs()) - //{ - //Assert.NotNull(joueur.Plateau); - //Assert.True(joueur.Plateau.EstComplet()); - //} - } } } diff --git a/Sources/UnitTesting/UtilsUT.cs b/Sources/UnitTesting/UtilsUT.cs index b2f310f..1e4446f 100644 --- a/Sources/UnitTesting/UtilsUT.cs +++ b/Sources/UnitTesting/UtilsUT.cs @@ -26,7 +26,7 @@ namespace UnitTesting {Couleur.JAUNE, ConsoleColor.DarkYellow } }; - var actualCouleursTerminal = typeof(Utils).GetField("couleursTerminal", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); + var actualCouleursTerminal = typeof(Utils).GetField("couleursTerminal", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)!.GetValue(null); Assert.Equal(expectedCouleursTerminal, actualCouleursTerminal); } @@ -40,9 +40,9 @@ namespace UnitTesting {Indicateur.BONNECOULEUR, ConsoleColor.White } }; - var actualIndicateursTerminal = typeof(Utils).GetField("indicateursTerminal", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); + var actualIndicateursTerminal = typeof(Utils).GetField("indicateursTerminal", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)!.GetValue(null); - Assert.Equal(expectedIndicateursTerminal, actualIndicateursTerminal); + Assert.Equal(expectedIndicateursTerminal, actualIndicateursTerminal); } [Fact] From 7fe5ed8774410fcf049b8b54908f8306396d2918 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Wed, 15 May 2024 20:34:58 +0200 Subject: [PATCH 13/21] =?UTF-8?q?Les=20=C3=A9v=C3=A9nements=20sont=20bien?= =?UTF-8?q?=20appel=C3=A9s=20dans=20la=20class=20Partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/UnitTesting/PartieUT.cs | 157 ++++++++ Sources/UnitTesting/PlateauUT.cs | 442 +++++++++++----------- Sources/UnitTesting/ReglesClassiquesUT.cs | 268 ++++++------- 3 files changed, 512 insertions(+), 355 deletions(-) diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index 87ab8d1..8a01a01 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -7,5 +7,162 @@ namespace UnitTesting { public class PartieUT { + [Fact] + public void TestPartieInitialiseCorrectement() + { + + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + + Assert.NotNull(partie); + } + + [Fact] + public void TestJouerAppelleDemanderJoueurEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + + partie.DemanderJoueur += (sender, e) => + { + eventAppelle = true; + return $"Joueur {e.Numero}"; + }; + + partie.Jouer(); + Assert.True(eventAppelle); + } + + + [Fact] + public void TestJouerAppelleDemanderJetonEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.DemanderJeton += (sender, e) => + { + eventAppelle = true; + return new Jeton(); + }; + + partie.Jouer(); + Assert.True(eventAppelle); + } + + [Fact] + public void TestJouerAppelleAjouterJoueurEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.AjouterJoueur += (sender, e) => + { + eventAppelle = true; + + }; + partie.Jouer(); + Assert.True(eventAppelle); + + } + + [Fact] + public void TestJouerAppelleDebutPartieEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.DebutPartie += (sender, e) => + { + eventAppelle = true; + }; + partie.Jouer(); + Assert.True(eventAppelle); + } + + [Fact] + public void TestJouerAppelleNouveauTourEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.NouveauTour += (sender, e) => + { + eventAppelle = true; + }; + + partie.Jouer(); + Assert.True(eventAppelle); + } + + [Fact] + public void TestJouerAppelleNouveauJetonEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.AjouterJeton += (sender, e) => + { + eventAppelle = true; + }; + + partie.Jouer(); + Assert.True(eventAppelle); + } + + [Fact] + public void TestJouerAppelleNouveauCodeEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.AjouterCode += (sender, e) => + { + eventAppelle = true; + }; + + partie.Jouer(); + Assert.True(eventAppelle); + } + + [Fact] + public void TestJouerAppellePasserMainEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.PasserMain += (sender, e) => + { + eventAppelle = true; + }; + + partie.Jouer(); + Assert.True(eventAppelle); + } + + [Fact] + public void TestJouerAppellePartieTermineeEvent() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool eventAppelle = false; + + partie.PartieTerminee += (sender, e) => + { + eventAppelle = true; + }; + + partie.Jouer(); + Assert.True(eventAppelle); + } } } diff --git a/Sources/UnitTesting/PlateauUT.cs b/Sources/UnitTesting/PlateauUT.cs index dc88673..b02c98a 100644 --- a/Sources/UnitTesting/PlateauUT.cs +++ b/Sources/UnitTesting/PlateauUT.cs @@ -1,231 +1,231 @@ -using CoreLibrary; -using CoreLibrary.Exceptions; -using System.Reflection; -using Xunit; - -namespace UnitTesting -{ - public class PlateauUT - { - [Fact] - public void TestConstructeurValide() - { - Plateau plateau = new Plateau(4,12); - Assert.NotNull(plateau); - Assert.Equal(1, plateau.Tour); - Assert.False(plateau.Victoire); - } - - [Fact] - public void TestConstructeurInvalide() - { - Assert.Throws(() => new Plateau(0, 10)); - Assert.Throws(() => new Plateau(3, 0)); - } - - [Fact] - public void TestEstCompletTrue() - { - Plateau plateau = new Plateau(4, 3); - Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); - plateau.AjouterCode(code); - plateau.AjouterCode(code); - plateau.AjouterCode(code); - - bool estComplet = plateau.EstComplet(); - - Assert.True(estComplet); - } - - [Fact] - public void TestEstCompletFalse() - { - Plateau plateau = new Plateau(4, 3); - Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); - plateau.AjouterCode(code); - plateau.AjouterCode(code); - - bool estComplet = plateau.EstComplet(); - - Assert.False(estComplet); - } - - [Fact] - public void TestAjouterCodeValide() - { - Plateau plateau = new Plateau(4, 10); - Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); - - plateau.AjouterCode(code); - - Assert.Equal(2, plateau.Tour); - } - - [Fact] - public void TestAjouterCodeTailleIncorrecte() - { - Plateau plateau = new Plateau(4, 10); - Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); - - Assert.Throws(() => plateau.AjouterCode(code)); - } - - [Fact] - public void TestAjouterCodeIncomplet() - { - Plateau plateau = new Plateau(4, 10); - Code code = new Code(4); - - Assert.Throws(() => plateau.AjouterCode(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); - } - - [Fact] - public void TestEstBonCodeTailleException() - { +using CoreLibrary; +using CoreLibrary.Exceptions; +using System.Reflection; +using Xunit; + +namespace UnitTesting +{ + public class PlateauUT + { + [Fact] + public void TestConstructeurValide() + { + Plateau plateau = new Plateau(4,12); + Assert.NotNull(plateau); + Assert.Equal(1, plateau.Tour); + Assert.False(plateau.Victoire); + } + + [Fact] + public void TestConstructeurInvalide() + { + Assert.Throws(() => new Plateau(0, 10)); + Assert.Throws(() => new Plateau(3, 0)); + } + + [Fact] + public void TestEstCompletTrue() + { + Plateau plateau = new Plateau(4, 3); + Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); + plateau.AjouterCode(code); + plateau.AjouterCode(code); + plateau.AjouterCode(code); + + bool estComplet = plateau.EstComplet(); + + Assert.True(estComplet); + } + + [Fact] + public void TestEstCompletFalse() + { + Plateau plateau = new Plateau(4, 3); + Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); + plateau.AjouterCode(code); + plateau.AjouterCode(code); + + bool estComplet = plateau.EstComplet(); + + Assert.False(estComplet); + } + + [Fact] + public void TestAjouterCodeValide() + { + Plateau plateau = new Plateau(4, 10); + Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); + + plateau.AjouterCode(code); + + Assert.Equal(2, plateau.Tour); + } + + [Fact] + public void TestAjouterCodeTailleIncorrecte() + { + Plateau plateau = new Plateau(4, 10); + Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); + + Assert.Throws(() => plateau.AjouterCode(code)); + } + + [Fact] + public void TestAjouterCodeIncomplet() + { + Plateau plateau = new Plateau(4, 10); + Code code = new Code(4); + + Assert.Throws(() => plateau.AjouterCode(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); + } + + [Fact] + public void TestEstBonCodeTailleException() + { Plateau plateau = new Plateau(3, 5); Code code = new Code(4); Assert.Throws(() => plateau.EstBonCode(code)); - } - - [Fact] - public void TestEstBonCodeIncomplet() - { + } + + [Fact] + public void TestEstBonCodeIncomplet() + { Plateau plateau = new Plateau(3, 5); Code code = new Code(3); Assert.Throws(() => plateau.EstBonCode(code)); - } - - [Fact] - public void TestEstBonCodeTrue() - { - 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); + } + + [Fact] + public void TestEstBonCodeTrue() + { + 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); Assert.True(plateau.EstBonCode(codeSecret)); - } - - [Fact] - public void TestEstBonCodeFalse() - { - List couleurs = new List((Couleur[])Enum.GetValues(typeof(Couleur))); - - 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); - - Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray(); - - Couleur couleurJeton = jetons[0].Couleur; - int indice = couleurs.IndexOf(couleurJeton) + 1; - if (indice >= couleurs.Count) - indice = 0; - - jetons[0] = new Jeton(couleurs[indice]); - - Code code = new Code(jetons); - - Assert.False(plateau.EstBonCode(code)); - } - - [Fact] - public void TestEstBonCodeAucunIndicateur() - { - List couleurs = new List((Couleur[])Enum.GetValues(typeof(Couleur))); - 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); - - Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray(); - + } + + [Fact] + public void TestEstBonCodeFalse() + { + List couleurs = new List((Couleur[])Enum.GetValues(typeof(Couleur))); + + 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); + + Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray(); + + Couleur couleurJeton = jetons[0].Couleur; + int indice = couleurs.IndexOf(couleurJeton) + 1; + if (indice >= couleurs.Count) + indice = 0; + + jetons[0] = new Jeton(couleurs[indice]); + + Code code = new Code(jetons); + + Assert.False(plateau.EstBonCode(code)); + } + + [Fact] + public void TestEstBonCodeAucunIndicateur() + { + List couleurs = new List((Couleur[])Enum.GetValues(typeof(Couleur))); + 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); + + Jeton[] jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray(); + for (int i=0; i> grille = plateau.Grille(); - - Assert.Equal(4, grille.First().Count()); - Assert.Equal(4, grille.Last().Count()); - Assert.Equal(code1.Jetons(), grille.ElementAt(0)); - Assert.Equal(code2.Jetons(), grille.ElementAt(1)); - - } - - [Fact] - public void TestGrilleVide() - { - Plateau plateau = new Plateau(4, 3); - - IEnumerable> grille = plateau.Grille(); - - foreach (IEnumerable tour in grille) - { - Assert.All(tour, jeton => Assert.Null(jeton)); - } - } - - [Fact] - public void TestIndicateursVide() + } + + [Fact] + public void TestGrilleValide() + { + Plateau plateau = new Plateau(4, 3); + Code code1 = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); + Code code2 = new Code([new Jeton(Couleur.VERT), new Jeton(Couleur.JAUNE), new Jeton(Couleur.ROUGE), new Jeton(Couleur.NOIR)]); + + plateau.AjouterCode(code1); + plateau.AjouterCode(code2); + + IEnumerable> grille = plateau.Grille(); + + Assert.Equal(4, grille.First().Count()); + Assert.Equal(4, grille.Last().Count()); + Assert.Equal(code1.Jetons(), grille.ElementAt(0)); + Assert.Equal(code2.Jetons(), grille.ElementAt(1)); + + } + + [Fact] + public void TestGrilleVide() + { + Plateau plateau = new Plateau(4, 3); + + IEnumerable> grille = plateau.Grille(); + + foreach (IEnumerable tour in grille) + { + Assert.All(tour, jeton => Assert.Null(jeton)); + } + } + + [Fact] + public void TestIndicateursVide() { Plateau plateau = new Plateau(4, 5); @@ -233,13 +233,13 @@ namespace UnitTesting { Assert.Null(indicateur); } - } - - [Fact] - public void TestIndicateursAjouterDeuxCodes() - { - Plateau plateau = new Plateau(4, 5); - Code code1 = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); + } + + [Fact] + public void TestIndicateursAjouterDeuxCodes() + { + Plateau plateau = new Plateau(4, 5); + Code code1 = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]); Code code2 = new Code([new Jeton(Couleur.VERT), new Jeton(Couleur.JAUNE), new Jeton(Couleur.ROUGE), new Jeton(Couleur.NOIR)]); plateau.AjouterCode(code1); @@ -250,6 +250,6 @@ namespace UnitTesting Assert.Null(plateau.Indicateurs().ElementAt(2)); Assert.Null(plateau.Indicateurs().ElementAt(3)); Assert.Null(plateau.Indicateurs().ElementAt(4)); - } - } -} + } + } +} diff --git a/Sources/UnitTesting/ReglesClassiquesUT.cs b/Sources/UnitTesting/ReglesClassiquesUT.cs index d0aa497..ef4b834 100644 --- a/Sources/UnitTesting/ReglesClassiquesUT.cs +++ b/Sources/UnitTesting/ReglesClassiquesUT.cs @@ -1,131 +1,131 @@ -using CoreLibrary; -using CoreLibrary.Exceptions; -using System.Reflection; -using Xunit; - -namespace UnitTesting -{ - public class ReglesClassiquesUT - { - [Fact] +using CoreLibrary; +using CoreLibrary.Exceptions; +using System.Reflection; +using Xunit; + +namespace UnitTesting +{ + public class ReglesClassiquesUT + { + [Fact] public void TestNom() { ReglesClassiques regle = new ReglesClassiques(); Assert.Equal("Règles classiques", regle.Nom); - } - [Fact] - public void TestConstructeur() - { - ReglesClassiques regles = new ReglesClassiques(); - - Assert.NotNull(regles); - Assert.Equal(0, regles.NbJoueurs); - Assert.Equal(2, regles.NbJoueursMaximum); - } - - [Fact] - public void TestAjouterJoueur() - { - ReglesClassiques regles = new ReglesClassiques(); - - regles.AjouterJoueur("MonJoueur"); - - Assert.Equal(1, regles.NbJoueurs); - } - - [Fact] - public void TestJoueurCourantAvecJoueur() - { - ReglesClassiques regles = new ReglesClassiques(); - regles.AjouterJoueur("joueur1"); - regles.AjouterJoueur("joueur2"); - regles.CommencerLaPartie(); - - Joueur joueurCourant = regles.JoueurCourant(); - - Assert.NotNull(joueurCourant); - Assert.Equal("joueur1", joueurCourant.Nom); - } - - [Fact] - public void TestJoueurCourantSansJoueur() - { - ReglesClassiques regles = new ReglesClassiques(); - Assert.Throws(() => regles.JoueurCourant()); - } - - [Fact] - public void TestPasserLaMainValide() - { - ReglesClassiques regles = new ReglesClassiques(); - regles.AjouterJoueur("joueur1"); - regles.AjouterJoueur("joueur2"); - - regles.CommencerLaPartie(); - Joueur joueurCourantInitial = regles.JoueurCourant(); - regles.PasserLaMain(); - Joueur joueurCourantSuivant = regles.JoueurCourant(); - - Assert.NotEqual(joueurCourantInitial, joueurCourantSuivant); - Assert.Equal("joueur2", joueurCourantSuivant.Nom); - } - - [Fact] + } + [Fact] + public void TestConstructeur() + { + ReglesClassiques regles = new ReglesClassiques(); + + Assert.NotNull(regles); + Assert.Equal(0, regles.NbJoueurs); + Assert.Equal(2, regles.NbJoueursMaximum); + } + + [Fact] + public void TestAjouterJoueur() + { + ReglesClassiques regles = new ReglesClassiques(); + + regles.AjouterJoueur("MonJoueur"); + + Assert.Equal(1, regles.NbJoueurs); + } + + [Fact] + public void TestJoueurCourantAvecJoueur() + { + ReglesClassiques regles = new ReglesClassiques(); + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + regles.CommencerLaPartie(); + + Joueur joueurCourant = regles.JoueurCourant(); + + Assert.NotNull(joueurCourant); + Assert.Equal("joueur1", joueurCourant.Nom); + } + + [Fact] + public void TestJoueurCourantSansJoueur() + { + ReglesClassiques regles = new ReglesClassiques(); + Assert.Throws(() => regles.JoueurCourant()); + } + + [Fact] + public void TestPasserLaMainValide() + { + ReglesClassiques regles = new ReglesClassiques(); + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + + regles.CommencerLaPartie(); + Joueur joueurCourantInitial = regles.JoueurCourant(); + regles.PasserLaMain(); + Joueur joueurCourantSuivant = regles.JoueurCourant(); + + Assert.NotEqual(joueurCourantInitial, joueurCourantSuivant); + Assert.Equal("joueur2", joueurCourantSuivant.Nom); + } + + [Fact] public void TestPasserLaMainCompteurReinitialise() { - ReglesClassiques regles = new ReglesClassiques(); + ReglesClassiques regles = new ReglesClassiques(); Type type = typeof(ReglesClassiques); regles.AjouterJoueur("joueur1"); - regles.AjouterJoueur("joueur2"); - regles.CommencerLaPartie(); - - FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); - Assert.NotNull(fieldInfo); + regles.AjouterJoueur("joueur2"); + regles.CommencerLaPartie(); + + FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(fieldInfo); int? joueurCourant = (int?)fieldInfo.GetValue(regles); regles.PasserLaMain(); regles.PasserLaMain(); - regles.PasserLaMain(); - + regles.PasserLaMain(); + Assert.NotNull(joueurCourant); Assert.Equal(0, joueurCourant); - } - - [Fact] - public void TestPasserLaMainInvalide() - { - ReglesClassiques regles = new ReglesClassiques(); - Assert.Throws(() => regles.PasserLaMain()); - } - - [Fact] - public void TestGenererCode() - { - ReglesClassiques regles = new ReglesClassiques(); - Code code = regles.GenererCode(); - - Assert.NotNull(code); - Assert.Equal(regles.TailleCodeMaximum, code.TailleMaximale()); - } - - [Fact] - public void TestCommencerLaPartie() - { - ReglesClassiques regles = new ReglesClassiques(); - regles.AjouterJoueur("joueur1"); - regles.AjouterJoueur("joueur2"); - - regles.CommencerLaPartie(); - Joueur joueurCourant = regles.JoueurCourant(); - - Assert.NotNull(joueurCourant); - Assert.Equal("joueur1", joueurCourant.Nom); - } - - [Fact] - public void TestEstTermineeFalseUnJoueur() - { + } + + [Fact] + public void TestPasserLaMainInvalide() + { + ReglesClassiques regles = new ReglesClassiques(); + Assert.Throws(() => regles.PasserLaMain()); + } + + [Fact] + public void TestGenererCode() + { + ReglesClassiques regles = new ReglesClassiques(); + Code code = regles.GenererCode(); + + Assert.NotNull(code); + Assert.Equal(regles.TailleCodeMaximum, code.TailleMaximale()); + } + + [Fact] + public void TestCommencerLaPartie() + { + ReglesClassiques regles = new ReglesClassiques(); + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + + regles.CommencerLaPartie(); + Joueur joueurCourant = regles.JoueurCourant(); + + Assert.NotNull(joueurCourant); + Assert.Equal("joueur1", joueurCourant.Nom); + } + + [Fact] + public void TestEstTermineeFalseUnJoueur() + { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("joueur1"); regles.CommencerLaPartie(); @@ -133,23 +133,23 @@ namespace UnitTesting bool result = regles.EstTerminee(); - Assert.False(result); - } - - [Fact] - public void TestEstTermineeFalse() - { - ReglesClassiques regles = new ReglesClassiques(); - regles.AjouterJoueur("joueur1"); - regles.AjouterJoueur("joueur2"); - regles.CommencerLaPartie(); - - bool estTerminee = regles.EstTerminee(); - - Assert.False(estTerminee); - } - - [Fact] + Assert.False(result); + } + + [Fact] + public void TestEstTermineeFalse() + { + ReglesClassiques regles = new ReglesClassiques(); + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + regles.CommencerLaPartie(); + + bool estTerminee = regles.EstTerminee(); + + Assert.False(estTerminee); + } + + [Fact] public void TestGagnantsAucunGagnants() { ReglesClassiques regles = new ReglesClassiques(); @@ -160,7 +160,7 @@ namespace UnitTesting IEnumerable gagnants = regles.Gagnants(); Assert.Empty(gagnants); - } - - } -} + } + + } +} From 177d882aecf3efd1b9ba162226d44ff6826ea1c1 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Wed, 15 May 2024 22:32:50 +0200 Subject: [PATCH 14/21] Test Evenement.cs --- Sources/UnitTesting/EvenementsUT.cs | 158 ++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 Sources/UnitTesting/EvenementsUT.cs diff --git a/Sources/UnitTesting/EvenementsUT.cs b/Sources/UnitTesting/EvenementsUT.cs new file mode 100644 index 0000000..736c11c --- /dev/null +++ b/Sources/UnitTesting/EvenementsUT.cs @@ -0,0 +1,158 @@ +using ConsoleApp; +using CoreLibrary; +using CoreLibrary.Events; +using Microsoft.VisualStudio.TestPlatform.Utilities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using Xunit; + + +namespace UnitTesting +{ + public class EvenementsUT + { + [Fact] + public void TestDemanderJoueur() + { + DemanderJoueurEventArgs eventArgs = new DemanderJoueurEventArgs(1); + string expectedNom = "TestNom"; + + string simulatedInput = "TestNom"; + + using (StringReader sr = new StringReader(simulatedInput)) + { + Console.SetIn(sr); + + // Sauvegarder le flux de sortie standard + TextWriter originalConsoleOut = Console.Out; + + // Exécuter le test + string? nom = Evenements.DemanderJoueur(null, eventArgs); + + // Restaurer le flux de sortie standard + Console.SetOut(originalConsoleOut); + + Assert.Equal(expectedNom, nom); + } + } + + [Fact] + public void TestCommencerLaPartie() + { + + + using (var sw = new StringWriter()) + { + Console.SetOut(sw); + + Evenements.CommencerLaPartie(null, null); + + string consoleOutput = sw.ToString().Trim(); + + string separateurAttendu = "───────────────────────────────────────────────────────"; + string separateurDessine = consoleOutput.Split('\n')[0]; + + Assert.Equal(separateurAttendu.Trim(), separateurDessine.Trim(), ignoreCase: true); + + string phraseAttendue = "La partie commence, bonne chance à tous !"; + string resteSortieAttendue = consoleOutput.Substring(separateurAttendu.Length).Trim(); + + Assert.Equal(phraseAttendue, resteSortieAttendue); + } + } + + [Fact] + public void TestAjouterJeton() + { + + using (StringWriter sw = new StringWriter()) + { + Console.SetOut(sw); + + + Couleur couleurJeton = Couleur.BLEU; + Jeton jeton = new Jeton(couleurJeton); + AjouterJetonEventArgs eventArgs = new AjouterJetonEventArgs(jeton); + + + Evenements.AjouterJeton(null, eventArgs); + + + string consoleOutput = sw.ToString().Trim(); + + + string expectedOutput = $"⬤"; + Assert.Contains(expectedOutput, consoleOutput); + } + } + + [Fact] + public void TestSupprimerDernierJeton() + { + + using (StringWriter sw = new StringWriter()) + { + Console.SetOut(sw); + + + Evenements.SupprimerDernierJeton(null, null); + + + string consoleOutput = sw.ToString().Trim(); + + + Assert.Equal("\b\b\b \b\b\b\b\b\b", consoleOutput); + } + } + + [Fact] + public void TestAjouterCode() + { + + using (StringWriter sw = new StringWriter()) + { + Console.SetOut(sw); + + + Evenements.AjouterCode(null, null); + + + string consoleOutput = sw.ToString().Trim(); + + + Assert.Equal("───────────────────────────────────────────────────────", consoleOutput); + } + } + + [Fact] + public void TestPartieTerminee() + { + + using (StringWriter sw = new StringWriter()) + { + Console.SetOut(sw); + + + Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { }, new Joueur[] { })); + + + Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Camille", new Plateau(4, 12)) }, new Joueur[] { })); + + + Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Pauline", new Plateau(4, 12)), new Joueur("Celeste", new Plateau(4, 12)) }, new Joueur[] { })); + + + string consoleOutput = sw.ToString().Trim(); + + + Assert.Contains("C'est une défaite des deux joueurs...", consoleOutput); + Assert.Contains("C'est une victoire de Camille.", consoleOutput); + Assert.Contains("C'est une égalité !", consoleOutput); + } + } + } +} From 0b267f58b026b27c5ff0e6888c871445bd96eb67 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Wed, 15 May 2024 22:37:33 +0200 Subject: [PATCH 15/21] Correction test demander joueur --- Sources/UnitTesting/EvenementsUT.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Sources/UnitTesting/EvenementsUT.cs b/Sources/UnitTesting/EvenementsUT.cs index 736c11c..0138df2 100644 --- a/Sources/UnitTesting/EvenementsUT.cs +++ b/Sources/UnitTesting/EvenementsUT.cs @@ -21,20 +21,17 @@ namespace UnitTesting DemanderJoueurEventArgs eventArgs = new DemanderJoueurEventArgs(1); string expectedNom = "TestNom"; - string simulatedInput = "TestNom"; - using (StringReader sr = new StringReader(simulatedInput)) + using (StringReader sr = new StringReader("TestNom")) { Console.SetIn(sr); - // Sauvegarder le flux de sortie standard - TextWriter originalConsoleOut = Console.Out; + // Exécuter le test string? nom = Evenements.DemanderJoueur(null, eventArgs); - // Restaurer le flux de sortie standard - Console.SetOut(originalConsoleOut); + Assert.Equal(expectedNom, nom); } From ad71f477fd06adb893ddb9200300e858f77e2904 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Wed, 15 May 2024 22:41:33 +0200 Subject: [PATCH 16/21] suppression de test demander joueur --- Sources/UnitTesting/EvenementsUT.cs | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/Sources/UnitTesting/EvenementsUT.cs b/Sources/UnitTesting/EvenementsUT.cs index 0138df2..214774d 100644 --- a/Sources/UnitTesting/EvenementsUT.cs +++ b/Sources/UnitTesting/EvenementsUT.cs @@ -15,28 +15,6 @@ namespace UnitTesting { public class EvenementsUT { - [Fact] - public void TestDemanderJoueur() - { - DemanderJoueurEventArgs eventArgs = new DemanderJoueurEventArgs(1); - string expectedNom = "TestNom"; - - - using (StringReader sr = new StringReader("TestNom")) - { - Console.SetIn(sr); - - - - // Exécuter le test - string? nom = Evenements.DemanderJoueur(null, eventArgs); - - - - Assert.Equal(expectedNom, nom); - } - } - [Fact] public void TestCommencerLaPartie() { From a727b43824c3898f0d51928f8a8e994312c27472 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Wed, 15 May 2024 23:01:19 +0200 Subject: [PATCH 17/21] Correction code smell de EvenementUT --- Sources/UnitTesting/EvenementsUT.cs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Sources/UnitTesting/EvenementsUT.cs b/Sources/UnitTesting/EvenementsUT.cs index 214774d..f1ceebf 100644 --- a/Sources/UnitTesting/EvenementsUT.cs +++ b/Sources/UnitTesting/EvenementsUT.cs @@ -4,6 +4,7 @@ using CoreLibrary.Events; using Microsoft.VisualStudio.TestPlatform.Utilities; using System; using System.Collections.Generic; +using System.Diagnostics.Tracing; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -24,7 +25,8 @@ namespace UnitTesting { Console.SetOut(sw); - Evenements.CommencerLaPartie(null, null); + + Evenements.CommencerLaPartie(null, new DebutPartieEventArgs()); string consoleOutput = sw.ToString().Trim(); @@ -51,10 +53,8 @@ namespace UnitTesting Couleur couleurJeton = Couleur.BLEU; Jeton jeton = new Jeton(couleurJeton); - AjouterJetonEventArgs eventArgs = new AjouterJetonEventArgs(jeton); - - - Evenements.AjouterJeton(null, eventArgs); + + Evenements.AjouterJeton(null, new AjouterJetonEventArgs(jeton)); string consoleOutput = sw.ToString().Trim(); @@ -74,7 +74,7 @@ namespace UnitTesting Console.SetOut(sw); - Evenements.SupprimerDernierJeton(null, null); + Evenements.SupprimerDernierJeton(null, new SupprimerDernierJetonEventArgs()); string consoleOutput = sw.ToString().Trim(); @@ -92,8 +92,9 @@ namespace UnitTesting { Console.SetOut(sw); + Code code = new Code(4); - Evenements.AjouterCode(null, null); + Evenements.AjouterCode(null, new AjouterCodeEventArgs(code)); string consoleOutput = sw.ToString().Trim(); @@ -103,6 +104,12 @@ namespace UnitTesting } } + + public static class Constants + { + public static readonly Joueur[] EmptyJoueurArray = Array.Empty(); + } + [Fact] public void TestPartieTerminee() { @@ -112,10 +119,10 @@ namespace UnitTesting Console.SetOut(sw); - Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { }, new Joueur[] { })); + Evenements.PartieTerminee(null, new PartieTermineeEventArgs(Constants.EmptyJoueurArray, Constants.EmptyJoueurArray)); - Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Camille", new Plateau(4, 12)) }, new Joueur[] { })); + Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Camille", new Plateau(4, 12)) }, Constants.EmptyJoueurArray)); Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Pauline", new Plateau(4, 12)), new Joueur("Celeste", new Plateau(4, 12)) }, new Joueur[] { })); From 5c5d0bf7a5b9e6af6c1af1690f48908e3112b891 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Wed, 15 May 2024 23:02:31 +0200 Subject: [PATCH 18/21] Correction code smell de EvenementUT --- Sources/UnitTesting/EvenementsUT.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/UnitTesting/EvenementsUT.cs b/Sources/UnitTesting/EvenementsUT.cs index f1ceebf..578af40 100644 --- a/Sources/UnitTesting/EvenementsUT.cs +++ b/Sources/UnitTesting/EvenementsUT.cs @@ -125,7 +125,7 @@ namespace UnitTesting Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Camille", new Plateau(4, 12)) }, Constants.EmptyJoueurArray)); - Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Pauline", new Plateau(4, 12)), new Joueur("Celeste", new Plateau(4, 12)) }, new Joueur[] { })); + Evenements.PartieTerminee(null, new PartieTermineeEventArgs(new Joueur[] { new Joueur("Pauline", new Plateau(4, 12)), new Joueur("Celeste", new Plateau(4, 12)) }, Constants.EmptyJoueurArray)); string consoleOutput = sw.ToString().Trim(); From e9d3410cd8af8a62ce127005aefdb3162ef0eef2 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 16 May 2024 16:29:20 +0200 Subject: [PATCH 19/21] Test class program.cs --- Sources/UnitTesting/ProgramUT.cs | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Sources/UnitTesting/ProgramUT.cs diff --git a/Sources/UnitTesting/ProgramUT.cs b/Sources/UnitTesting/ProgramUT.cs new file mode 100644 index 0000000..4c8229d --- /dev/null +++ b/Sources/UnitTesting/ProgramUT.cs @@ -0,0 +1,60 @@ +using ConsoleApp; +using CoreLibrary; +using CoreLibrary.Events; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace UnitTesting +{ + public class ProgramUT + { + [Fact] + public void TestPartieConfiguration() + { + // Créer une instance de Partie + IRegles regle = new ReglesClassiques(); + Partie maPartie = new Partie(new ReglesClassiques()); + + // Attacher des gestionnaires d'événements simulés + bool demanderJoueurCalled = false; + bool debutPartieCalled = false; + bool nouveauTourCalled = false; + bool demanderJetonCalled = false; + bool ajouterJetonCalled = false; + bool ajouterCodeCalled = false; + bool partieTermineeCalled = false; + + maPartie.DemanderJoueur += (sender, e) => + { + demanderJoueurCalled = true; + return $"Joueur {e.Numero}"; + }; + maPartie.DebutPartie += (sender, e) => debutPartieCalled = true; + maPartie.NouveauTour += (sender, e) => nouveauTourCalled = true; + maPartie.DemanderJeton += (sender, e) => + { + demanderJetonCalled = true; + return new Jeton(); + }; + maPartie.AjouterJeton += (sender, e) => ajouterJetonCalled = true; + maPartie.AjouterCode += (sender, e) => ajouterCodeCalled = true; + maPartie.PartieTerminee += (sender, e) => partieTermineeCalled = true; + + + maPartie.Jouer(); + + + Assert.True(demanderJoueurCalled); + Assert.True(debutPartieCalled); + Assert.True(nouveauTourCalled); + Assert.True(demanderJetonCalled); + Assert.True(ajouterJetonCalled); + Assert.True(ajouterCodeCalled); + Assert.True(partieTermineeCalled); + } + } +} From 6301e32cbcbac0301dff318f78a77c3011871176 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 16 May 2024 17:56:40 +0200 Subject: [PATCH 20/21] Test class ReglesClassique.cs --- Sources/UnitTesting/ProgramUT.cs | 4 +- Sources/UnitTesting/ReglesClassiquesUT.cs | 65 +++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/Sources/UnitTesting/ProgramUT.cs b/Sources/UnitTesting/ProgramUT.cs index 4c8229d..b143926 100644 --- a/Sources/UnitTesting/ProgramUT.cs +++ b/Sources/UnitTesting/ProgramUT.cs @@ -15,11 +15,11 @@ namespace UnitTesting [Fact] public void TestPartieConfiguration() { - // Créer une instance de Partie + IRegles regle = new ReglesClassiques(); Partie maPartie = new Partie(new ReglesClassiques()); - // Attacher des gestionnaires d'événements simulés + bool demanderJoueurCalled = false; bool debutPartieCalled = false; bool nouveauTourCalled = false; diff --git a/Sources/UnitTesting/ReglesClassiquesUT.cs b/Sources/UnitTesting/ReglesClassiquesUT.cs index ef4b834..2071f94 100644 --- a/Sources/UnitTesting/ReglesClassiquesUT.cs +++ b/Sources/UnitTesting/ReglesClassiquesUT.cs @@ -149,6 +149,47 @@ namespace UnitTesting Assert.False(estTerminee); } + [Fact] + public void TestEstTermineeVictoire() + { + ReglesClassiques regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + Joueur joueur1 = regles.AjouterJoueur("joueur1"); + Joueur joueur2 = regles.AjouterJoueur("joueur2"); + + regles.CommencerLaPartie(); + Plateau plateauj1 = regles.JoueurCourant().Plateau; + Type type = typeof(Plateau); + FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(fieldInfo); + Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1); + Assert.NotNull(codeSecret); + regles.JoueurCourant().Plateau.AjouterCode(codeSecret); + bool estTerminee = regles.EstTerminee(); + Assert.True(estTerminee); + } + + [Fact] + public void TestEstTermineeToursMaximesAtteints() + { + ReglesClassiques regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + regles.CommencerLaPartie(); + Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE)]); + for (int i = 1; i <= regles.TourMaximum*regles.NbJoueursMaximum; i++) + { + regles.JoueurCourant().Plateau.AjouterCode(code); + regles.PasserLaMain(); + + } + bool estTerminee = regles.EstTerminee(); + Assert.True(estTerminee); + } + + + [Fact] public void TestGagnantsAucunGagnants() { @@ -162,5 +203,29 @@ namespace UnitTesting Assert.Empty(gagnants); } + [Fact] + public void TestGagnantsGagnants() + { + + ReglesClassiques regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + + regles.CommencerLaPartie(); + Plateau plateauj1 = regles.JoueurCourant().Plateau; + Type type = typeof(Plateau); + FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(fieldInfo); + Code? codeSecret = (Code?)fieldInfo.GetValue(plateauj1); + Assert.NotNull(codeSecret); + regles.JoueurCourant().Plateau.AjouterCode(codeSecret); + var resulta = regles.Gagnants(); + Assert.Single(resulta); + Assert.Contains(regles.JoueurCourant(), resulta); + + } + } } From ed9047a35f3a21c832872ad8418967af92424ee3 Mon Sep 17 00:00:00 2001 From: "camille.turpin-etienne" Date: Thu, 16 May 2024 18:11:02 +0200 Subject: [PATCH 21/21] Test class UtilsUT du titre --- Sources/UnitTesting/EvenementsUT.cs | 3 +++ Sources/UnitTesting/UtilsUT.cs | 32 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Sources/UnitTesting/EvenementsUT.cs b/Sources/UnitTesting/EvenementsUT.cs index 578af40..c71b126 100644 --- a/Sources/UnitTesting/EvenementsUT.cs +++ b/Sources/UnitTesting/EvenementsUT.cs @@ -16,6 +16,9 @@ namespace UnitTesting { public class EvenementsUT { + + + [Fact] public void TestCommencerLaPartie() { diff --git a/Sources/UnitTesting/UtilsUT.cs b/Sources/UnitTesting/UtilsUT.cs index 1e4446f..0d4f25b 100644 --- a/Sources/UnitTesting/UtilsUT.cs +++ b/Sources/UnitTesting/UtilsUT.cs @@ -56,5 +56,37 @@ namespace UnitTesting Assert.Equal(expected, sw.ToString()); } } + + [Fact] + public void TestDessinerTitre() + { + // Capture de la sortie console + using (StringWriter sw = new StringWriter()) + { + Console.SetOut(sw); + + // Appel de la fonction à tester + Utils.DessinerTitre(); + + // Récupération de la sortie console dans une chaîne + string consoleOutput = sw.ToString(); + + // Chaîne attendue pour le titre + string titreAttendu = @" + __ __ _ _ _ +| \/ | __ _ ___| |_ ___ _ _ _ __ (_) _ _ __| | +| |\/| |/ _` |(_-<| _|/ -_)| '_|| ' \ | || ' \ / _` | +|_| |_|\__,_|/__/ \__|\___||_| |_|_|_||_||_||_|\__,_| + +───────────────────────────────────────────────────────"; + + consoleOutput = consoleOutput.Replace("\n", "").Replace("\r", ""); + titreAttendu = titreAttendu.Replace("\n", "").Replace("\r", ""); + // Assertion pour vérifier si la sortie console correspond au titre attendu + Assert.Equal(titreAttendu.Trim(), consoleOutput.Trim()); + } + } + } + }