From 3b72d209de483a14527acf279d2f6760dfe05a37 Mon Sep 17 00:00:00 2001 From: "pauline.prady" Date: Mon, 13 May 2024 11:48:19 +0200 Subject: [PATCH] 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);