diff --git a/Sources/UnitTesting/AjouterCodeEventArgsUT.cs b/Sources/UnitTesting/AjouterCodeEventArgsUT.cs new file mode 100644 index 0000000..6c3b27f --- /dev/null +++ b/Sources/UnitTesting/AjouterCodeEventArgsUT.cs @@ -0,0 +1,20 @@ +using CoreLibrary.Core; +using CoreLibrary.Events; +using Xunit; + +namespace UnitTesting +{ + public class AjouterCodeEventArgsUT + { + [Fact] + public void TestConstructeurValide() + { + Code monCode = new Code([new Jeton(Couleur.VERT), new Jeton(Couleur.BLEU)]); + + AjouterCodeEventArgs evenement = new AjouterCodeEventArgs(monCode); + + Assert.Equal(monCode, evenement.Code); + } + + } +} diff --git a/Sources/UnitTesting/AjouterJetonEventArgsUT.cs b/Sources/UnitTesting/AjouterJetonEventArgsUT.cs new file mode 100644 index 0000000..f621091 --- /dev/null +++ b/Sources/UnitTesting/AjouterJetonEventArgsUT.cs @@ -0,0 +1,20 @@ +using CoreLibrary.Core; +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..5433ab5 --- /dev/null +++ b/Sources/UnitTesting/AjouterJoueurEventArgs.cs @@ -0,0 +1,21 @@ +using CoreLibrary.Core; +using CoreLibrary.Events; +using CoreLibrary.Joueurs; +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/CodeCompletExceptionUT.cs b/Sources/UnitTesting/CodeCompletExceptionUT.cs new file mode 100644 index 0000000..0ac5325 --- /dev/null +++ b/Sources/UnitTesting/CodeCompletExceptionUT.cs @@ -0,0 +1,85 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +using System.Text.Json; +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); + } + } + + [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 new file mode 100644 index 0000000..0476bb1 --- /dev/null +++ b/Sources/UnitTesting/CodeIncompletExceptionUT.cs @@ -0,0 +1,83 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +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); + } + } + + [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 new file mode 100644 index 0000000..87242c6 --- /dev/null +++ b/Sources/UnitTesting/CodeInvalideExceptionUT.cs @@ -0,0 +1,100 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +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); + } + } + + [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/CodeUT.cs b/Sources/UnitTesting/CodeUT.cs new file mode 100644 index 0000000..d42d23f --- /dev/null +++ b/Sources/UnitTesting/CodeUT.cs @@ -0,0 +1,225 @@ +using CoreLibrary.Core; +using CoreLibrary.Exceptions; +using Xunit; + +namespace UnitTesting +{ + public class CodeUT + { + [Fact] + public void TestPremierConstructeurValide() + { + Code code = new Code(4); + Assert.NotNull(code); + Assert.Equal(4, code.Jetons().Count()); + Assert.Equal(0, code.NbJetons); + } + + [Fact] + public void TestPremierConstructeurInvalide() + { + Assert.Throws(() => new Code(0)); + Assert.Throws(() => new Code(-1)); + } + + [Fact] + public void TestDeuxiemeConstructeurValide() + { + Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; + + Code code = new Code(jetons); + Assert.NotNull(code); + Assert.Equal(3, code.Jetons().Count()); + Assert.Equal(3, code.NbJetons); + } + + [Fact] + public void TestDeuxiemeConstructeurInvalide() + { + Assert.Throws(() => new Code([])); + } + + [Fact] + public void TestAjouterJetonValide() + { + Jeton jeton = new Jeton(Couleur.JAUNE); + Code code = new Code(3); + code.AjouterJeton(jeton); + Assert.Equal(1, code.NbJetons); + Assert.Equal(jeton, code.Jetons().ElementAt(0)); + } + + [Fact] + public void TestAjouterJetonInvalide() + { + Code code = new Code([new Jeton(Couleur.NOIR)]); + Assert.Throws(() => code.AjouterJeton(new Jeton(Couleur.ROUGE))); + } + + [Fact] + public void TestSupprimerDernierJetonValide() + { + Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); + code.SupprimerDernierJeton(); + Assert.Equal(2, code.NbJetons); + } + + [Fact] + public void TestSupprimerDernierJetonInvalide() + { + Code code = new Code(4); + Assert.Throws(() => code.SupprimerDernierJeton()); + } + + [Fact] + 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); + Jeton jeton = code.RecupererJeton(1); + Assert.Equal(jetonAttendu.Couleur, jeton.Couleur); + } + + [Fact] + public void TestRecupererJetonInvalide() + { + Code code = new Code(4); + Assert.Throws(() => code.RecupererJeton(-1)); + Assert.Throws(() => code.RecupererJeton(4)); + } + + [Fact] + public void TestRecupererJetonNull() + { + Code code = new Code(4); + Assert.Throws(() => code.RecupererJeton(1)); + } + + [Fact] + public void TestJetonsValide() + { + Jeton[] jetonsAttendus = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; + Code code = new Code(jetonsAttendus); + IEnumerable lesJetons = code.Jetons(); + + Assert.Equal(jetonsAttendus.Length, lesJetons.Count()); + + int index = 0; + foreach (Jeton jetonAttendu in jetonsAttendus) + { + Assert.Equal(jetonAttendu.Couleur, lesJetons.ElementAt(index)?.Couleur); + index++; + } + } + + [Fact] + public void TestEstCompletValide() + { + Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); + bool estComplet = code.EstComplet(); + Assert.True(estComplet); + } + + [Fact] + public void TestEstCompletInvalide() + { + Code code = new Code(3); + bool estComplet = code.EstComplet(); + Assert.False(estComplet); + } + + [Fact] + public void TestTailleMaximaleValide() + { + Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; + Code code = new Code(jetons); + int tailleMaximale = code.TailleMaximale(); + + Assert.Equal(jetons.Length, tailleMaximale); + } + + [Fact] + 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)]); + + IEnumerable indicateurs = code.Comparer(autreCode); + + Assert.Equal(3, indicateurs.Count()); + Assert.Contains(Indicateur.BONNEPLACE, indicateurs); + Assert.DoesNotContain(Indicateur.BONNECOULEUR, 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.Contains(Indicateur.BONNEPLACE, indicateurs); + Assert.DoesNotContain(Indicateur.BONNECOULEUR, indicateurs); + } + + [Fact] + 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)]); + + IEnumerable indicateurs = code.Comparer(autreCode); + + Assert.Equal(2, indicateurs.Count()); + Assert.Contains(Indicateur.BONNECOULEUR, indicateurs); + Assert.DoesNotContain(Indicateur.BONNEPLACE, indicateurs); + } + + [Fact] + 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.BLANC), new Jeton(Couleur.BLEU)]); + + IEnumerable indicateurs = code.Comparer(autreCode); + + Assert.Equal(3, indicateurs.Count()); + Assert.Contains(Indicateur.BONNEPLACE, indicateurs); + Assert.Contains(Indicateur.BONNECOULEUR, indicateurs); + } + + [Fact] + 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.VERT), new Jeton(Couleur.JAUNE), new Jeton(Couleur.NOIR)]); + IEnumerable indicateurs = code.Comparer(autreCode); + + Assert.Empty(indicateurs); + } + + [Fact] + public void TestComparerMonCodeIncomplet() + { + 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 TestComparerSonCodeIncomplet() + { + 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/CodeVideExceptionUT.cs b/Sources/UnitTesting/CodeVideExceptionUT.cs new file mode 100644 index 0000000..2ad8544 --- /dev/null +++ b/Sources/UnitTesting/CodeVideExceptionUT.cs @@ -0,0 +1,83 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +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); + } + } + + [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/DemanderJoueurEventArgsUT.cs b/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs new file mode 100644 index 0000000..0aca2be --- /dev/null +++ b/Sources/UnitTesting/DemanderJoueurEventArgsUT.cs @@ -0,0 +1,16 @@ +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/GrilleCompleteExceptionUT.cs b/Sources/UnitTesting/GrilleCompleteExceptionUT.cs new file mode 100644 index 0000000..c083a7d --- /dev/null +++ b/Sources/UnitTesting/GrilleCompleteExceptionUT.cs @@ -0,0 +1,83 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +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); + } + } + + [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 new file mode 100644 index 0000000..a14c524 --- /dev/null +++ b/Sources/UnitTesting/IndiceCodeExceptionUT.cs @@ -0,0 +1,99 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +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); + } + } + + [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/JetonUT.cs b/Sources/UnitTesting/JetonUT.cs new file mode 100644 index 0000000..4af8a63 --- /dev/null +++ b/Sources/UnitTesting/JetonUT.cs @@ -0,0 +1,20 @@ +using CoreLibrary.Core; +using Xunit; + +namespace UnitTesting +{ + public class JetonUT + { + [Fact] + public void TestConstructeurValide() + { + Couleur[] listeCouleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); + + for (int i=0; i(() => 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); + } + } + + [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/PartieTermineeEventArgsUT.cs b/Sources/UnitTesting/PartieTermineeEventArgsUT.cs new file mode 100644 index 0000000..9b96d40 --- /dev/null +++ b/Sources/UnitTesting/PartieTermineeEventArgsUT.cs @@ -0,0 +1,24 @@ +using CoreLibrary.Joueurs; +using CoreLibrary.Core; +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); + } + + } +} diff --git a/Sources/UnitTesting/PartieUT.cs b/Sources/UnitTesting/PartieUT.cs new file mode 100644 index 0000000..3a2c993 --- /dev/null +++ b/Sources/UnitTesting/PartieUT.cs @@ -0,0 +1,207 @@ +using CoreLibrary; +using CoreLibrary.Regles; +using CoreLibrary.Core; +using Xunit; +using System.Reflection; +using CoreLibrary.Events; + +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); + } + + + [Fact] + public void TestSupprimerDernierJeton() + { + IRegles regles = new ReglesClassiques(); + Partie partie = new Partie(regles); + bool appele = false; + + partie.DemanderJeton += (sender, e) => + { + if (e.Indice == 0 || appele) + return new Jeton(); + return null; + }; + + partie.SupprimerDernierJeton += (sender, e) => + { + appele = true; + }; + + partie.Jouer(); + + Assert.True(appele); + } + + [Fact] + public void TestSupprimerDernierJetonNull() + { + Partie partie = new Partie(new ReglesClassiques()); + + MethodInfo? methode = typeof(Partie).GetMethod("QuandSupprimerDernierJeton", BindingFlags.NonPublic | BindingFlags.Instance); + + Assert.NotNull(methode); + + methode.Invoke(partie, null); + } + } +} diff --git a/Sources/UnitTesting/PlateauUT.cs b/Sources/UnitTesting/PlateauUT.cs new file mode 100644 index 0000000..48189fa --- /dev/null +++ b/Sources/UnitTesting/PlateauUT.cs @@ -0,0 +1,269 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using CoreLibrary.Core; +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() + { + 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); + Assert.True(plateau.EstBonCode(codeSecret)); + } + + [Fact] + public void TestEstBonCodeFalse() + { + 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(); + + 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; + } + } + + + Jeton tmp = jetons[0]; + jetons[0] = jetons[1]; + jetons[1] = tmp; + + 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() + { + Plateau plateau = new Plateau(4, 5); + + foreach(Indicateur[] indicateur in plateau.Indicateurs()) + { + 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)]); + 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); + + Assert.NotNull(plateau.Indicateurs().ElementAt(0)); + Assert.NotNull(plateau.Indicateurs().ElementAt(1)); + Assert.Null(plateau.Indicateurs().ElementAt(2)); + Assert.Null(plateau.Indicateurs().ElementAt(3)); + Assert.Null(plateau.Indicateurs().ElementAt(4)); + } + } +} diff --git a/Sources/UnitTesting/ProgramUT.cs b/Sources/UnitTesting/ProgramUT.cs new file mode 100644 index 0000000..5aae04c --- /dev/null +++ b/Sources/UnitTesting/ProgramUT.cs @@ -0,0 +1,55 @@ +using CoreLibrary; +using CoreLibrary.Regles; +using CoreLibrary.Core; +using Xunit; + +namespace UnitTesting +{ + public class ProgramUT + { + [Fact] + public void TestPartieConfiguration() + { + + IRegles regle = new ReglesClassiques(); + Partie maPartie = new Partie(new ReglesClassiques()); + + + 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); + } + } +} diff --git a/Sources/UnitTesting/TailleCodeExceptionUT.cs b/Sources/UnitTesting/TailleCodeExceptionUT.cs new file mode 100644 index 0000000..7c1bfbe --- /dev/null +++ b/Sources/UnitTesting/TailleCodeExceptionUT.cs @@ -0,0 +1,98 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +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); + } + } + + [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 new file mode 100644 index 0000000..7d4bf33 --- /dev/null +++ b/Sources/UnitTesting/TailleGrilleExceptionUT.cs @@ -0,0 +1,98 @@ +using CoreLibrary.Exceptions; +using System.Reflection; +using System.Runtime.Serialization; +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); + } + } + + [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); + } + } +}