diff --git a/Sources/UnitTesting/AjouterCodeEventArgsUT.cs b/Sources/UnitTesting/AjouterCodeEventArgsUT.cs index 4c35c6f..6c3b27f 100644 --- a/Sources/UnitTesting/AjouterCodeEventArgsUT.cs +++ b/Sources/UnitTesting/AjouterCodeEventArgsUT.cs @@ -9,7 +9,7 @@ namespace UnitTesting [Fact] public void TestConstructeurValide() { - Code monCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU)]); + Code monCode = new Code([new Jeton(Couleur.VERT), new Jeton(Couleur.BLEU)]); AjouterCodeEventArgs evenement = new AjouterCodeEventArgs(monCode); diff --git a/Sources/UnitTesting/CodeCompletExceptionUT.cs b/Sources/UnitTesting/CodeCompletExceptionUT.cs index 06ebf97..0ac5325 100644 --- a/Sources/UnitTesting/CodeCompletExceptionUT.cs +++ b/Sources/UnitTesting/CodeCompletExceptionUT.cs @@ -1,4 +1,8 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +using System.Text.Json; using Xunit; namespace UnitTesting @@ -22,7 +26,7 @@ namespace UnitTesting { throw new CodeCompletException(message); } - catch(CodeCompletException e) + catch (CodeCompletException e) { Assert.Equal(message, e.Message); } @@ -50,5 +54,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + CodeCompletException exception = new CodeCompletException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(CodeCompletException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + CodeCompletException exceptionSerialisee = + (CodeCompletException) FormatterServices.GetUninitializedObject(typeof(CodeCompletException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(CodeCompletException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } } diff --git a/Sources/UnitTesting/CodeIncompletExceptionUT.cs b/Sources/UnitTesting/CodeIncompletExceptionUT.cs index b6bed0f..0476bb1 100644 --- a/Sources/UnitTesting/CodeIncompletExceptionUT.cs +++ b/Sources/UnitTesting/CodeIncompletExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -50,5 +52,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + CodeIncompletException exception = new CodeIncompletException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(CodeIncompletException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + CodeIncompletException exceptionSerialisee = + (CodeIncompletException)FormatterServices.GetUninitializedObject(typeof(CodeIncompletException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(CodeIncompletException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } } diff --git a/Sources/UnitTesting/CodeInvalideExceptionUT.cs b/Sources/UnitTesting/CodeInvalideExceptionUT.cs index 7fdf9c0..87242c6 100644 --- a/Sources/UnitTesting/CodeInvalideExceptionUT.cs +++ b/Sources/UnitTesting/CodeInvalideExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -66,5 +68,33 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + CodeInvalideException exception = new CodeInvalideException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(CodeInvalideException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + CodeInvalideException exceptionSerialisee = + (CodeInvalideException)FormatterServices.GetUninitializedObject(typeof(CodeInvalideException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(CodeInvalideException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } + } } diff --git a/Sources/UnitTesting/CodeVideExceptionUT.cs b/Sources/UnitTesting/CodeVideExceptionUT.cs index 650dc3a..2ad8544 100644 --- a/Sources/UnitTesting/CodeVideExceptionUT.cs +++ b/Sources/UnitTesting/CodeVideExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -50,5 +52,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + CodeVideException exception = new CodeVideException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(CodeVideException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + CodeVideException exceptionSerialisee = + (CodeVideException)FormatterServices.GetUninitializedObject(typeof(CodeVideException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(CodeVideException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } } diff --git a/Sources/UnitTesting/GrilleCompleteExceptionUT.cs b/Sources/UnitTesting/GrilleCompleteExceptionUT.cs index 592fd65..c083a7d 100644 --- a/Sources/UnitTesting/GrilleCompleteExceptionUT.cs +++ b/Sources/UnitTesting/GrilleCompleteExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -50,5 +52,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + GrilleCompleteException exception = new GrilleCompleteException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(GrilleCompleteException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + GrilleCompleteException exceptionSerialisee = + (GrilleCompleteException)FormatterServices.GetUninitializedObject(typeof(GrilleCompleteException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(GrilleCompleteException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } } diff --git a/Sources/UnitTesting/IndiceCodeExceptionUT.cs b/Sources/UnitTesting/IndiceCodeExceptionUT.cs index 50e40c4..a14c524 100644 --- a/Sources/UnitTesting/IndiceCodeExceptionUT.cs +++ b/Sources/UnitTesting/IndiceCodeExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -66,5 +68,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + IndiceCodeException exception = new IndiceCodeException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(IndiceCodeException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + IndiceCodeException exceptionSerialisee = + (IndiceCodeException)FormatterServices.GetUninitializedObject(typeof(IndiceCodeException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(IndiceCodeException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } } diff --git a/Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs b/Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs index c0733cf..e5d0c2d 100644 --- a/Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs +++ b/Sources/UnitTesting/PartieNonCommenceeExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -50,5 +52,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + PartieNonCommenceeException exception = new PartieNonCommenceeException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(PartieNonCommenceeException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + PartieNonCommenceeException exceptionSerialisee = + (PartieNonCommenceeException)FormatterServices.GetUninitializedObject(typeof(PartieNonCommenceeException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(PartieNonCommenceeException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } } diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs index 6c62f38..55ab6e7 100644 --- a/Sources/UnitTesting/PartieUT.cs +++ b/Sources/UnitTesting/PartieUT.cs @@ -2,6 +2,8 @@ using CoreLibrary.Regles; using CoreLibrary.Core; using Xunit; +using System.Reflection; +using CoreLibrary.Events; namespace UnitTesting { @@ -164,5 +166,28 @@ namespace UnitTesting partie.Jouer(); Assert.True(eventAppelle); } + + [Fact] + public void TestQuandSupprimerDernierJeton() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool appele = false; + + partie.DemanderJeton += (sender, e) => + { + return e.Indice == 0 || appele ? new Jeton() : null; + }; + + partie.SupprimerDernierJeton += (sender, e) => + { + appele = true; + }; + + partie.Jouer(); + + + Assert.True(appele); + } } } diff --git a/Sources/UnitTesting/PlateauUT.cs b/Sources/UnitTesting/PlateauUT.cs index c2a9736..0e69ba8 100644 --- a/Sources/UnitTesting/PlateauUT.cs +++ b/Sources/UnitTesting/PlateauUT.cs @@ -133,26 +133,40 @@ namespace UnitTesting [Fact] public void TestEstBonCodeFalse() { - List couleurs = new List((Couleur[])Enum.GetValues(typeof(Couleur))); - - Plateau plateau = new Plateau(4, 10); + Plateau plateau = new Plateau(2, 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; + int i = 0; + int j = 1; + + while (jetons[i].Couleur == jetons[j].Couleur) + { + ++i; + ++j; + + if (j == jetons.Length) + { + plateau = new Plateau(2, 10); + codeSecret = (Code?)fieldInfo.GetValue(plateau); + Assert.NotNull(codeSecret); + jetons = codeSecret.Jetons().Where(jeton => jeton.HasValue).Select(jeton => jeton!.Value).ToArray(); + + i = 0; + j = 1; + } + } + - jetons[0] = new Jeton(couleurs[indice]); + Jeton tmp = jetons[0]; + jetons[0] = jetons[1]; + jetons[1] = tmp; Code code = new Code(jetons); diff --git a/Sources/UnitTesting/ReglesClassiquesUT.cs b/Sources/UnitTesting/ReglesClassiquesUT.cs index 615974a..dba5535 100644 --- a/Sources/UnitTesting/ReglesClassiquesUT.cs +++ b/Sources/UnitTesting/ReglesClassiquesUT.cs @@ -65,11 +65,28 @@ namespace UnitTesting regles.AjouterJoueur("joueur2"); regles.CommencerLaPartie(); - Joueur joueurCourantInitial = regles.JoueurCourant(); + + Type type = typeof(ReglesClassiques); + FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(fieldInfo); + + int? joueurCourantAvant = (int?)fieldInfo.GetValue(regles); + Assert.NotNull(joueurCourantAvant); + + if (joueurCourantAvant >= regles.NbJoueursMaximum) + joueurCourantAvant = 0; + regles.PasserLaMain(); Joueur joueurCourantSuivant = regles.JoueurCourant(); - Assert.NotEqual(joueurCourantInitial, joueurCourantSuivant); + Assert.Equal(regles.JoueurCourant(), joueurCourantSuivant); + + int? joueurCourantApres = (int?)fieldInfo.GetValue(regles); + Assert.NotNull(joueurCourantApres); + + Assert.Equal(joueurCourantAvant, joueurCourantApres-1); + Assert.Equal(joueurCourantAvant+1, joueurCourantApres); + Assert.NotEqual(joueurCourantAvant, joueurCourantApres); Assert.Equal("joueur2", joueurCourantSuivant.Nom); } @@ -127,12 +144,43 @@ namespace UnitTesting } [Fact] - public void TestEstTermineeFalseUnJoueur() + public void TestEstTermineeFalseJoueurCourantNull() { ReglesClassiques regles = new ReglesClassiques(); regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); regles.CommencerLaPartie(); - regles.PasserLaMain(); + + Type type = typeof(ReglesClassiques); + FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(fieldInfo); + + int? joueurCourant = (int?)fieldInfo.GetValue(regles); + + joueurCourant = null; + Assert.Null(joueurCourant); + + bool result = regles.EstTerminee(); + + Assert.False(result); + } + + [Fact] + public void TestEstTermineeFalseJoueurCourantDiffDeZero() + { + ReglesClassiques regles = new ReglesClassiques(); + regles.AjouterJoueur("joueur1"); + regles.AjouterJoueur("joueur2"); + regles.CommencerLaPartie(); + + Type type = typeof(ReglesClassiques); + FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(fieldInfo); + + int? joueurCourant = (int?)fieldInfo.GetValue(regles); + + joueurCourant = -1; + Assert.Equal(-1, joueurCourant); bool result = regles.EstTerminee(); @@ -173,7 +221,7 @@ namespace UnitTesting } [Fact] - public void TestEstTermineeToursMaximesAtteints() + public void TestEstTermineeToursMaximumAtteints() { ReglesClassiques regles = new ReglesClassiques(); Partie partie = new Partie(regles); @@ -191,8 +239,6 @@ namespace UnitTesting Assert.True(estTerminee); } - - [Fact] public void TestGagnantsAucunGagnants() { diff --git a/Sources/UnitTesting/TailleCodeExceptionUT.cs b/Sources/UnitTesting/TailleCodeExceptionUT.cs index eda2056..7c1bfbe 100644 --- a/Sources/UnitTesting/TailleCodeExceptionUT.cs +++ b/Sources/UnitTesting/TailleCodeExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -65,5 +67,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + TailleCodeException exception = new TailleCodeException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(TailleCodeException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + TailleCodeException exceptionSerialisee = + (TailleCodeException)FormatterServices.GetUninitializedObject(typeof(TailleCodeException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(TailleCodeException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } } diff --git a/Sources/UnitTesting/TailleGrilleExceptionUT.cs b/Sources/UnitTesting/TailleGrilleExceptionUT.cs index 8063a17..7d4bf33 100644 --- a/Sources/UnitTesting/TailleGrilleExceptionUT.cs +++ b/Sources/UnitTesting/TailleGrilleExceptionUT.cs @@ -1,4 +1,6 @@ using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; using Xunit; namespace UnitTesting @@ -65,5 +67,32 @@ namespace UnitTesting } } + [Fact] + public void ExceptionSerialisation() + { + TailleGrilleException exception = new TailleGrilleException(); + +#pragma warning disable SYSLIB0050 + SerializationInfo info = new SerializationInfo(typeof(TailleGrilleException), new FormatterConverter()); + StreamingContext contexte = new StreamingContext(StreamingContextStates.All); +#pragma warning restore SYSLIB0050 + +#pragma warning disable SYSLIB0051 + exception.GetObjectData(info, contexte); +#pragma warning restore SYSLIB0051 + + Assert.Equal(exception.Message, info.GetString("Message")); + +#pragma warning disable SYSLIB0050 + TailleGrilleException exceptionSerialisee = + (TailleGrilleException)FormatterServices.GetUninitializedObject(typeof(TailleGrilleException)); +#pragma warning restore SYSLIB0050 + + ConstructorInfo? constructeur = typeof(TailleGrilleException).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, [typeof(SerializationInfo), typeof(StreamingContext)], null); + Assert.NotNull(constructeur); + constructeur.Invoke(exceptionSerialisee, [info, contexte]); + + Assert.Equal(exception.Message, exceptionSerialisee.Message); + } } }