Correction des jeux de tests

master
Céleste BARBOSA 1 year ago
commit 4ddb803580

@ -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);
}
}
}

@ -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);
}
}
}

@ -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);
}
}
}

@ -0,0 +1,54 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class CodeCompletExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<CodeCompletException>(() => throw new CodeCompletException());
}
[Fact]
public void ExceptionMessage()
{
string message = "Mon super gros problème.";
Assert.ThrowsAsync<CodeCompletException>(() => 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<CodeCompletException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -0,0 +1,54 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class CodeIncompletExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<CodeIncompletException>(() => throw new CodeIncompletException());
}
[Fact]
public void ExceptionMessage()
{
string message = "Mon super gros problème.";
Assert.ThrowsAsync<CodeIncompletException>(() => 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<CodeIncompletException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -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<CodeInvalideException>(() => throw new CodeInvalideException());
}
[Fact]
public void ExceptionAttributs()
{
Assert.ThrowsAsync<CodeInvalideException>(() => 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<CodeInvalideException>(() => 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<CodeInvalideException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -7,7 +7,7 @@ namespace UnitTesting
public class CodeUT public class CodeUT
{ {
[Fact] [Fact]
public void TestFirstConstructorValidArguments() public void TestPremierConstructeurValide()
{ {
Code code = new Code(4); Code code = new Code(4);
Assert.NotNull(code); Assert.NotNull(code);
@ -16,14 +16,14 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestConstructorInvalidArguments() public void TestPremierConstructeurInvalide()
{ {
Assert.Throws<TailleCodeException>(() => new Code(0)); Assert.Throws<TailleCodeException>(() => new Code(0));
Assert.Throws<TailleCodeException>(() => new Code(-1)); Assert.Throws<TailleCodeException>(() => new Code(-1));
} }
[Fact] [Fact]
public void TestSecondConstructorValidArguments() public void TestDeuxiemeConstructeurValide()
{ {
Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)];
@ -34,13 +34,13 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestSecondConstructorInvalidArguments() public void TestDeuxiemeConstructeurInvalide()
{ {
Assert.Throws<TailleCodeException>(() => new Code([])); Assert.Throws<TailleCodeException>(() => new Code([]));
} }
[Fact] [Fact]
public void TestAjouterJetonValid() public void TestAjouterJetonValide()
{ {
Jeton jeton = new Jeton(Couleur.JAUNE); Jeton jeton = new Jeton(Couleur.JAUNE);
Code code = new Code(3); Code code = new Code(3);
@ -50,14 +50,14 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestAjouterJetonInvalid() public void TestAjouterJetonInvalide()
{ {
Code code = new Code([new Jeton(Couleur.NOIR)]); Code code = new Code([new Jeton(Couleur.NOIR)]);
Assert.Throws<CodeCompletException>(() => code.AjouterJeton(new Jeton(Couleur.ROUGE))); Assert.Throws<CodeCompletException>(() => code.AjouterJeton(new Jeton(Couleur.ROUGE)));
} }
[Fact] [Fact]
public void TestSupprimerDernierJetonValid() public void TestSupprimerDernierJetonValide()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]);
code.SupprimerDernierJeton(); code.SupprimerDernierJeton();
@ -65,14 +65,14 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestSupprimerDernierJetonInvalid() public void TestSupprimerDernierJetonInvalide()
{ {
Code code = new Code(4); Code code = new Code(4);
Assert.Throws<CodeVideException>(() => code.SupprimerDernierJeton()); Assert.Throws<CodeVideException>(() => code.SupprimerDernierJeton());
} }
[Fact] [Fact]
public void TestRecupererJetonValid() public void TestRecupererJetonValide()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]);
Jeton jetonAttendu = new Jeton(Couleur.BLEU); Jeton jetonAttendu = new Jeton(Couleur.BLEU);
@ -81,7 +81,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestRecupererJetonInvalid() public void TestRecupererJetonInvalide()
{ {
Code code = new Code(4); Code code = new Code(4);
Assert.Throws<IndiceCodeException>(() => code.RecupererJeton(-1)); Assert.Throws<IndiceCodeException>(() => code.RecupererJeton(-1));
@ -89,7 +89,14 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestJetonsValid() public void TestRecupererJetonNull()
{
Code code = new Code(4);
Assert.Throws<IndiceCodeException>(() => code.RecupererJeton(1));
}
[Fact]
public void TestJetonsValide()
{ {
Jeton[] jetonsAttendus = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; Jeton[] jetonsAttendus = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)];
Code code = new Code(jetonsAttendus); Code code = new Code(jetonsAttendus);
@ -106,7 +113,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestEstCompletValid() public void TestEstCompletValide()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]);
bool estComplet = code.EstComplet(); bool estComplet = code.EstComplet();
@ -114,7 +121,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestEstCompletInvalid() public void TestEstCompletInvalide()
{ {
Code code = new Code(3); Code code = new Code(3);
bool estComplet = code.EstComplet(); bool estComplet = code.EstComplet();
@ -122,7 +129,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestTailleMaximaleValid() public void TestTailleMaximaleValide()
{ {
Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)]; Jeton[] jetons = [new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLEU)];
Code code = new Code(jetons); Code code = new Code(jetons);
@ -132,7 +139,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestComparerValid() public void TestComparerValide()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); 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)]); Code autreCode = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]);
@ -145,7 +152,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestComparerGoodPlace() public void TestComparerBonnePlace()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); 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.BLEU), new Jeton(Couleur.NOIR)]);
@ -158,7 +165,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestComparerGoodColor() public void TestComparerBonneCouleur()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); 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)]); Code autreCode = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.ROUGE), new Jeton(Couleur.NOIR)]);
@ -171,7 +178,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestComparerGoodPlaceAndColor() public void TestComparerBonnePlaceEtCouleur()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); 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.ROUGE), new Jeton(Couleur.BLANC), new Jeton(Couleur.BLEU)]);
@ -184,7 +191,7 @@ namespace UnitTesting
} }
[Fact] [Fact]
public void TestComparerNoMatch() public void TestComparerDifferent()
{ {
Code code = new Code([new Jeton(Couleur.ROUGE), new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC)]); 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 autreCode = new Code([new Jeton(Couleur.VERT), new Jeton(Couleur.JAUNE), new Jeton(Couleur.NOIR)]);
@ -192,6 +199,27 @@ namespace UnitTesting
Assert.Empty(indicateurs); 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<Indicateur> 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<Indicateur> indicateurs = code.Comparer(autreCode);
Assert.Empty(indicateurs);
}
} }
} }

@ -0,0 +1,54 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class CodeVideExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<CodeVideException>(() => throw new CodeVideException());
}
[Fact]
public void ExceptionMessage()
{
string message = "Mon super gros problème.";
Assert.ThrowsAsync<CodeVideException>(() => 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<CodeVideException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -0,0 +1,17 @@
using CoreLibrary.Events;
using Xunit;
namespace UnitTesting
{
public class DemanderJoueurEventArgsUT
{
[Fact]
public void TestConstructeurValide()
{
DemanderJoueurEventArgs evenement = new DemanderJoueurEventArgs(3);
Assert.Equal(3, evenement.Numero);
}
}
}

@ -0,0 +1,143 @@
using ConsoleApp;
using CoreLibrary;
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;
using System.Threading.Tasks;
using Xunit;
namespace UnitTesting
{
public class EvenementsUT
{
[Fact]
public void TestCommencerLaPartie()
{
using (var sw = new StringWriter())
{
Console.SetOut(sw);
Evenements.CommencerLaPartie(null, new DebutPartieEventArgs());
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);
Evenements.AjouterJeton(null, new AjouterJetonEventArgs(jeton));
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, new SupprimerDernierJetonEventArgs());
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);
Code code = new Code(4);
Evenements.AjouterCode(null, new AjouterCodeEventArgs(code));
string consoleOutput = sw.ToString().Trim();
Assert.Equal("───────────────────────────────────────────────────────", consoleOutput);
}
}
public static class Constants
{
public static readonly Joueur[] EmptyJoueurArray = Array.Empty<Joueur>();
}
[Fact]
public void TestPartieTerminee()
{
using (StringWriter sw = new StringWriter())
{
Console.SetOut(sw);
Evenements.PartieTerminee(null, new PartieTermineeEventArgs(Constants.EmptyJoueurArray, Constants.EmptyJoueurArray));
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)) }, Constants.EmptyJoueurArray));
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);
}
}
}
}

@ -0,0 +1,54 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class GrilleCompleteExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<GrilleCompleteException>(() => throw new GrilleCompleteException());
}
[Fact]
public void ExceptionMessage()
{
string message = "Mon super gros problème.";
Assert.ThrowsAsync<GrilleCompleteException>(() => 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<GrilleCompleteException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -0,0 +1,70 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class IndiceCodeExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<IndiceCodeException>(() => throw new IndiceCodeException());
}
[Fact]
public void ExceptionAttributs()
{
Assert.ThrowsAsync<IndiceCodeException>(() => 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<IndiceCodeException>(() => 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<IndiceCodeException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -6,7 +6,7 @@ namespace UnitTesting
public class JetonUT public class JetonUT
{ {
[Fact] [Fact]
public void TestConstructorValid() public void TestConstructeurValide()
{ {
Couleur[] listeCouleurs = (Couleur[])Enum.GetValues(typeof(Couleur)); Couleur[] listeCouleurs = (Couleur[])Enum.GetValues(typeof(Couleur));

@ -7,7 +7,7 @@ namespace UnitTesting
public class JoueurUT public class JoueurUT
{ {
[Fact] [Fact]
public void TestConstructorWithValidArguments() public void TestConstructeurValide()
{ {
Plateau plateau = new Plateau(4, 10); Plateau plateau = new Plateau(4, 10);
Joueur joueur = new Joueur("MonJoueur", plateau); Joueur joueur = new Joueur("MonJoueur", plateau);

@ -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);
}
}
}

@ -0,0 +1,54 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class PartieNonCommenceeExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<PartieNonCommenceeException>(() => throw new PartieNonCommenceeException());
}
[Fact]
public void ExceptionMessage()
{
string message = "Mon super gros problème.";
Assert.ThrowsAsync<PartieNonCommenceeException>(() => 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<PartieNonCommenceeException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -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);
}
}
}

@ -0,0 +1,168 @@
using CoreLibrary;
using CoreLibrary.Exceptions;
using System.Reflection;
using Xunit;
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);
}
}
}

@ -1,130 +1,255 @@
using CoreLibrary.Core; using CoreLibrary;
using CoreLibrary.Exceptions; using CoreLibrary.Exceptions;
using System.Linq; using System.Reflection;
using Xunit; using Xunit;
namespace UnitTesting namespace UnitTesting
{ {
public class PlateauUT public class PlateauUT
{ {
[Fact] [Fact]
public void TestConstructorValid() public void TestConstructeurValide()
{ {
Plateau plateau = new Plateau(4,12); Plateau plateau = new Plateau(4,12);
Assert.NotNull(plateau); Assert.NotNull(plateau);
Assert.Equal(1, plateau.Tour); Assert.Equal(1, plateau.Tour);
Assert.False(plateau.Victoire); Assert.False(plateau.Victoire);
} }
[Fact] [Fact]
public void TestConstructorInvalid() public void TestConstructeurInvalide()
{ {
Assert.Throws<TailleCodeException>(() => new Plateau(0, 10)); Assert.Throws<TailleCodeException>(() => new Plateau(0, 10));
} Assert.Throws<TailleGrilleException>(() => new Plateau(3, 0));
}
[Fact]
public void TestEstCompletTrue() [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 plateau = new Plateau(4, 3);
plateau.AjouterCode(code); 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); plateau.AjouterCode(code);
plateau.AjouterCode(code);
bool estComplet = plateau.EstComplet();
bool estComplet = plateau.EstComplet();
Assert.True(estComplet);
} Assert.True(estComplet);
}
[Fact]
public void TestEstCompletFalse() [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 plateau = new Plateau(4, 3);
plateau.AjouterCode(code); 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();
bool estComplet = plateau.EstComplet();
Assert.False(estComplet);
} Assert.False(estComplet);
}
[Fact]
public void TestAjouterCodeValid() [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 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);
plateau.AjouterCode(code);
Assert.Equal(2, plateau.Tour);
} Assert.Equal(2, plateau.Tour);
}
[Fact]
public void TestAjouterCodeIncorrectSize() [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)]); 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<CodeInvalideException>(() => plateau.AjouterCode(code));
} Assert.Throws<CodeInvalideException>(() => plateau.AjouterCode(code));
}
[Fact]
public void TestAjouterCodeIncomplete() [Fact]
{ public void TestAjouterCodeIncomplet()
Plateau plateau = new Plateau(4, 10); {
Code code = new Code(4); Plateau plateau = new Plateau(4, 10);
Code code = new Code(4);
Assert.Throws<CodeIncompletException>(() => plateau.AjouterCode(code));
} Assert.Throws<CodeIncompletException>(() => plateau.AjouterCode(code));
}
[Fact]
public void TestEstBonCodeTrue() [Fact]
{ public void TestAjouterCodeBonCode()
{
} Plateau plateau = new Plateau(4, 10);
[Fact] Type type = typeof(Plateau);
public void TestEstBonCodeFalse()
{ FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Plateau plateau = new Plateau(4, 10); Assert.NotNull(fieldInfo);
Code code = new Code([new Jeton(Couleur.BLEU), new Jeton(Couleur.BLANC), new Jeton(Couleur.JAUNE), new Jeton(Couleur.BLANC)]);
plateau.AjouterCode(code); Code? codeSecret = (Code?)fieldInfo.GetValue(plateau);
Assert.False(plateau.EstBonCode(code)); Assert.NotNull(codeSecret);
} plateau.AjouterCode(codeSecret);
Assert.True(plateau.Victoire);
[Fact] }
public void TestGrilleValid()
{ [Fact]
Plateau plateau = new Plateau(4, 3); public void TestEstBonCodeTailleException()
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 plateau = new Plateau(3, 5);
Code code = new Code(4);
plateau.AjouterCode(code1);
plateau.AjouterCode(code2); Assert.Throws<CodeInvalideException>(() => plateau.EstBonCode(code));
}
IEnumerable<IEnumerable<Jeton?>> grille = plateau.Grille();
[Fact]
Assert.Equal(4, grille.First().Count()); public void TestEstBonCodeIncomplet()
Assert.Equal(4, grille.Last().Count()); {
Assert.Equal(code1.Jetons(), grille.ElementAt(0)); Plateau plateau = new Plateau(3, 5);
Assert.Equal(code2.Jetons(), grille.ElementAt(1)); Code code = new Code(3);
} Assert.Throws<CodeIncompletException>(() => plateau.EstBonCode(code));
}
[Fact]
public void TestGrilleEmpty() [Fact]
{ public void TestEstBonCodeTrue()
Plateau plateau = new Plateau(4, 3); {
Plateau plateau = new Plateau(4, 10);
IEnumerable<IEnumerable<Jeton?>> grille = plateau.Grille();
Type type = typeof(Plateau);
foreach (IEnumerable<Jeton?> tour in grille)
{ FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.All(tour, jeton => Assert.Null(jeton)); Assert.NotNull(fieldInfo);
}
} Code? codeSecret = (Code?)fieldInfo.GetValue(plateau);
} Assert.NotNull(codeSecret);
} Assert.True(plateau.EstBonCode(codeSecret));
}
[Fact]
public void TestEstBonCodeFalse()
{
List<Couleur> couleurs = new List<Couleur>((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<Couleur> couleurs = new List<Couleur>((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<jetons.Length; ++i)
{
Couleur couleurJeton = jetons[i].Couleur;
couleurs.Remove(couleurJeton);
}
Code code = new Code(
[
new Jeton(couleurs[0]),
new Jeton(couleurs[0]),
new Jeton(couleurs[0]),
new Jeton(couleurs[0])
]
);
Assert.False(plateau.EstBonCode(code));
}
[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<IEnumerable<Jeton?>> 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<IEnumerable<Jeton?>> grille = plateau.Grille();
foreach (IEnumerable<Jeton?> 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));
}
}
}

@ -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()
{
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);
}
}
}

@ -1,121 +1,231 @@
using CoreLibrary.Core; using CoreLibrary;
using CoreLibrary.Exceptions; using CoreLibrary.Exceptions;
using CoreLibrary.Joueurs; using System.Reflection;
using CoreLibrary.Regles; using Xunit;
using Xunit;
namespace UnitTesting
namespace UnitTesting {
{ public class ReglesClassiquesUT
public class ReglesClassiquesUT {
{ [Fact]
[Fact] public void TestNom()
public void TestConstructor() {
{ ReglesClassiques regle = new ReglesClassiques();
ReglesClassiques regles = new ReglesClassiques(); Assert.Equal("Règles classiques", regle.Nom);
}
Assert.NotNull(regles); [Fact]
Assert.Equal(0, regles.NbJoueurs); public void TestConstructeur()
Assert.Equal(2, regles.NbJoueursMaximum); {
} ReglesClassiques regles = new ReglesClassiques();
[Fact] Assert.NotNull(regles);
public void TestAjouterJoueur() Assert.Equal(0, regles.NbJoueurs);
{ Assert.Equal(2, regles.NbJoueursMaximum);
ReglesClassiques regles = new ReglesClassiques(); }
regles.AjouterJoueur("MonJoueur"); [Fact]
public void TestAjouterJoueur()
Assert.Equal(1, regles.NbJoueurs); {
} ReglesClassiques regles = new ReglesClassiques();
[Fact] regles.AjouterJoueur("MonJoueur");
public void TestJoueurCourantWithPlayer()
{ Assert.Equal(1, regles.NbJoueurs);
ReglesClassiques regles = new ReglesClassiques(); }
regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2"); [Fact]
regles.CommencerLaPartie(); public void TestJoueurCourantAvecJoueur()
{
Joueur joueurCourant = regles.JoueurCourant(); ReglesClassiques regles = new ReglesClassiques();
regles.AjouterJoueur("joueur1");
Assert.NotNull(joueurCourant); regles.AjouterJoueur("joueur2");
Assert.Equal("joueur1", joueurCourant.Nom); regles.CommencerLaPartie();
}
Joueur joueurCourant = regles.JoueurCourant();
[Fact]
public void TestJoueurCourantNoPlayer() Assert.NotNull(joueurCourant);
{ Assert.Equal("joueur1", joueurCourant.Nom);
ReglesClassiques regles = new ReglesClassiques(); }
Assert.Throws<PartieNonCommenceeException>(() => regles.JoueurCourant());
} [Fact]
public void TestJoueurCourantSansJoueur()
[Fact] {
public void TestPasserLaMainValid() ReglesClassiques regles = new ReglesClassiques();
{ Assert.Throws<PartieNonCommenceeException>(() => regles.JoueurCourant());
ReglesClassiques regles = new ReglesClassiques(); }
regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2"); [Fact]
public void TestPasserLaMainValide()
regles.CommencerLaPartie(); {
Joueur joueurCourantInitial = regles.JoueurCourant(); ReglesClassiques regles = new ReglesClassiques();
regles.PasserLaMain(); regles.AjouterJoueur("joueur1");
Joueur joueurCourantSuivant = regles.JoueurCourant(); regles.AjouterJoueur("joueur2");
Assert.NotEqual(joueurCourantInitial, joueurCourantSuivant); regles.CommencerLaPartie();
Assert.Equal("joueur2", joueurCourantSuivant.Nom); Joueur joueurCourantInitial = regles.JoueurCourant();
} regles.PasserLaMain();
Joueur joueurCourantSuivant = regles.JoueurCourant();
[Fact]
public void TestPasserLaMainInvalid() Assert.NotEqual(joueurCourantInitial, joueurCourantSuivant);
{ Assert.Equal("joueur2", joueurCourantSuivant.Nom);
ReglesClassiques regles = new ReglesClassiques(); }
Assert.Throws<PartieNonCommenceeException>(() => regles.PasserLaMain());
} [Fact]
public void TestPasserLaMainCompteurReinitialise()
[Fact] {
public void TestGenererCode() ReglesClassiques regles = new ReglesClassiques();
{ Type type = typeof(ReglesClassiques);
ReglesClassiques regles = new ReglesClassiques();
Code code = regles.GenererCode(); regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2");
Assert.NotNull(code); regles.CommencerLaPartie();
Assert.Equal(regles.TailleCodeMaximum, code.TailleMaximale());
} FieldInfo? fieldInfo = type.GetField("joueurCourant", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo);
[Fact] int? joueurCourant = (int?)fieldInfo.GetValue(regles);
public void TestCommencerLaPartie()
{ regles.PasserLaMain();
ReglesClassiques regles = new ReglesClassiques(); regles.PasserLaMain();
regles.AjouterJoueur("joueur1"); regles.PasserLaMain();
regles.AjouterJoueur("joueur2");
Assert.NotNull(joueurCourant);
regles.CommencerLaPartie(); Assert.Equal(0, joueurCourant);
Joueur joueurCourant = regles.JoueurCourant(); }
Assert.NotNull(joueurCourant); [Fact]
Assert.Equal("joueur1", joueurCourant.Nom); public void TestPasserLaMainInvalide()
} {
ReglesClassiques regles = new ReglesClassiques();
[Fact] Assert.Throws<PartieNonCommenceeException>(() => regles.PasserLaMain());
public void TestEstTermineeTrue() }
{
// [Fact]
} public void TestGenererCode()
{
[Fact] ReglesClassiques regles = new ReglesClassiques();
public void TestEstTermineeFalse() Code code = regles.GenererCode();
{
ReglesClassiques regles = new ReglesClassiques(); Assert.NotNull(code);
regles.AjouterJoueur("joueur1"); Assert.Equal(regles.TailleCodeMaximum, code.TailleMaximale());
regles.AjouterJoueur("joueur2"); }
regles.CommencerLaPartie();
[Fact]
bool estTerminee = regles.EstTerminee(); public void TestCommencerLaPartie()
{
Assert.False(estTerminee); ReglesClassiques regles = new ReglesClassiques();
} regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2");
//Test Gagnants et Perdants + TestEstTermineeTrue + TestConstructor PB
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();
regles.PasserLaMain();
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]
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()
{
ReglesClassiques regles = new ReglesClassiques();
regles.AjouterJoueur("joueur1");
regles.AjouterJoueur("joueur2");
regles.CommencerLaPartie();
IEnumerable<Joueur> gagnants = regles.Gagnants();
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);
}
}
}

@ -0,0 +1,69 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class TailleCodeExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<TailleCodeException>(() => throw new TailleCodeException());
}
[Fact]
public void ExceptionAttributs()
{
Assert.ThrowsAsync<TailleCodeException>(() => 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<TailleCodeException>(() => 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<TailleCodeException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -0,0 +1,69 @@
using CoreLibrary.Exceptions;
using Xunit;
namespace UnitTesting
{
public class TailleGrilleExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<TailleGrilleException>(() => throw new TailleGrilleException());
}
[Fact]
public void ExceptionAttributs()
{
Assert.ThrowsAsync<TailleGrilleException>(() => 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<TailleGrilleException>(() => 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<TailleGrilleException>(() => 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<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
}
}

@ -23,6 +23,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ConsoleApp\ConsoleApp.csproj" />
<ProjectReference Include="..\CoreLibrary\CoreLibrary.csproj" /> <ProjectReference Include="..\CoreLibrary\CoreLibrary.csproj" />
</ItemGroup> </ItemGroup>

@ -0,0 +1,92 @@
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, ConsoleColor>()
{
{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<Indicateur, ConsoleColor> expectedIndicateursTerminal = new Dictionary<Indicateur, ConsoleColor>()
{
{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());
}
}
[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());
}
}
}
}
Loading…
Cancel
Save