Merge branch 'UT' of https://codefirst.iut.uca.fr/git/nicolas.barbosa/mastermind into UT
continuous-integration/drone/push Build is passing Details

master
commit 7f40e9a3b2

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CoreLibrary\CoreLibrary.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,27 @@
// Cas 3 - Plateau rempli, code mauvaise couleur
using CoreLibrary.Core;
using System.Reflection;
Code code3 = new Code(1);
code3.AjouterJeton(new Jeton(Couleur.Rouge));
Plateau plateau3;
Code? codeSecret3;
do
{
plateau3 = new Plateau(1, 1);
FieldInfo? codeSecretInfo3 = typeof(Plateau).GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
codeSecret3 = codeSecretInfo3.GetValue(plateau2) as Code;
Assert.NotNull(codeSecret3);
} while (codeSecret3.RecupererJeton(0).Equals(code3.RecupererJeton(0)));
plateau3.AjouterCode(codeSecret3);
MethodInfo? EstCodePossible3 = typeof(Robot).GetMethod("EstCodePossible", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
Assert.NotNull(EstCodePossible3);
object? estPossibleObj3 = EstCodePossible3.Invoke(new Robot(), [plateau3, code3]);
Assert.NotNull(estPossibleObj3);
Assert.IsType<bool>(estPossibleObj3);
Assert.False((bool)estPossibleObj3);

@ -1,49 +0,0 @@
using CoreLibrary.Joueurs;
using System.Runtime.Serialization;
namespace CoreLibrary.Exceptions
{
/// <summary>
/// Exception levée lorsqu'un joueur se connecte alors qu'il l'est déjà.
/// </summary>
[Serializable]
public class JoueurDejaConnecteException : Exception
{
// Message par défaut
private const string messageDefaut = "Le joueur est déjà connecté.";
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="JoueurDejaConnecteException"/> avec le message par défaut.
/// </summary>
public JoueurDejaConnecteException() : base(messageDefaut)
{}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="JoueurDejaConnecteException"/> avec le message spécifié.
/// </summary>
public JoueurDejaConnecteException(string message) : base(message)
{}
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="JoueurDejaConnecteException"/> avec le joueur spécifié.
/// </summary>
public JoueurDejaConnecteException(Joueur joueur) : base($"Le joueur {joueur.Nom} est déjà connecté.")
{ }
/// <summary>
/// Initialise une nouvelle instance de la classe <see cref="JoueurDejaConnecteException"/> avec le message et l'exception parent spécifiés.
/// </summary>
public JoueurDejaConnecteException(string message, Exception exception) : base(message, exception)
{}
[Obsolete("This method is obsolete. Use alternative methods for data retrieval.", DiagnosticId = "SYSLIB0051")]
protected JoueurDejaConnecteException(SerializationInfo info, StreamingContext contexte) : base(info, contexte)
{}
[Obsolete("This method is obsolete. Use alternative methods for data retrieval.", DiagnosticId = "SYSLIB0051")]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
}
}

@ -1,83 +0,0 @@
using CoreLibrary.Exceptions;
using System.Reflection;
using System.Runtime.Serialization;
using Xunit;
namespace UnitTesting
{
public class JoueurDejaConnecteExceptionUT
{
[Fact]
public void ExceptionDefaut()
{
Assert.ThrowsAsync<JoueurDejaConnecteException>(() => throw new JoueurDejaConnecteException());
}
[Fact]
public void ExceptionMessage()
{
string message = "Mon super gros problème.";
Assert.ThrowsAsync<JoueurDejaConnecteException>(() => throw new JoueurDejaConnecteException(message));
try
{
throw new JoueurDejaConnecteException(message);
}
catch (JoueurDejaConnecteException 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<JoueurDejaConnecteException>(() => throw new JoueurDejaConnecteException(message, parent));
try
{
throw new JoueurDejaConnecteException(message, parent);
}
catch (JoueurDejaConnecteException e)
{
Assert.Equal(message, e.Message);
Assert.NotNull(e.InnerException);
Assert.IsType<InvalidOperationException>(e.InnerException);
Assert.Equal(message2, e.InnerException.Message);
}
}
[Fact]
public void ExceptionSerialisation()
{
JoueurDejaConnecteException exception = new JoueurDejaConnecteException();
#pragma warning disable SYSLIB0050
SerializationInfo info = new SerializationInfo(typeof(JoueurDejaConnecteException), 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
JoueurDejaConnecteException exceptionSerialisee =
(JoueurDejaConnecteException)FormatterServices.GetUninitializedObject(typeof(JoueurDejaConnecteException));
#pragma warning restore SYSLIB0050
ConstructorInfo? constructeur = typeof(JoueurDejaConnecteException).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);
}
}
}

@ -77,15 +77,6 @@ namespace UnitTesting
Assert.Equal(joueur2, eventArgs?.Joueur);
}
[Fact]
public void TestSeConnecterThrowException()
{
Joueur joueur = new Joueur("Joueur");
joueur.SeConnecter(joueur);
Assert.Throws<JoueurDejaConnecteException>(() => joueur.SeConnecter(joueur));
}
[Fact]
public void TestToStringValide()
{

@ -1,78 +1,73 @@
using CoreLibrary.Exceptions;
using System.Reflection;
using CoreLibrary.Core;
using Xunit;
using CoreLibrary.Joueurs;
using CoreLibrary.Regles;
using CoreLibrary;
namespace UnitTesting
{
public class PlateauUT
{
[Fact]
public void TestConstructeurValide()
{
Plateau plateau = new Plateau(4,12);
Assert.NotNull(plateau);
Assert.False(plateau.Victoire);
}
[Fact]
public void TestConstructeurInvalide()
{
Assert.Throws<TailleCodeException>(() => new Plateau(-1, 10));
Assert.Throws<TailleGrilleException>(() => new Plateau(3, -1));
}
[Fact]
public void TestEstCompletTrue()
{
Plateau plateau = new Plateau(4, 3);
Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)];
Code code = new Code(4);
code.AjouterJeton(jetons[0]);
code.AjouterJeton(jetons[1]);
code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[3]);
plateau.AjouterCode(code);
plateau.AjouterCode(code);
plateau.AjouterCode(code);
bool estComplet = plateau.Complet;
Assert.True(estComplet);
}
[Fact]
public void TestEstCompletFalse()
{
Plateau plateau = new Plateau(4, 3);
Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)];
Code code = new Code(4);
code.AjouterJeton(jetons[0]);
code.AjouterJeton(jetons[1]);
code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[3]);
plateau.AjouterCode(code);
plateau.AjouterCode(code);
bool estComplet = plateau.Complet;
Assert.False(estComplet);
}
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.False(plateau.Victoire);
}
[Fact]
public void TestConstructeurInvalide()
{
Assert.Throws<TailleCodeException>(() => new Plateau(-1, 10));
Assert.Throws<TailleGrilleException>(() => new Plateau(3, -1));
}
[Fact]
public void TestEstCompletTrue()
{
Plateau plateau = new Plateau(4, 3);
Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)];
Code code = new Code(4);
code.AjouterJeton(jetons[0]);
code.AjouterJeton(jetons[1]);
code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[3]);
plateau.AjouterCode(code);
plateau.AjouterCode(code);
plateau.AjouterCode(code);
bool estComplet = plateau.Complet;
Assert.True(estComplet);
}
[Fact]
public void TestEstCompletFalse()
{
Plateau plateau = new Plateau(4, 3);
Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)];
Code code = new Code(4);
code.AjouterJeton(jetons[0]);
code.AjouterJeton(jetons[1]);
code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[3]);
plateau.AjouterCode(code);
plateau.AjouterCode(code);
bool estComplet = plateau.Complet;
Assert.False(estComplet);
}
[Fact]
public void TestAjouterCodeTailleIncorrecte()
{
Plateau plateau = new Plateau(4, 10);
Jeton[] jetons = new Jeton[]
{
new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc),
{
new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc),
new Jeton(Couleur.Jaune)
};
Code code = new Code(4);
@ -83,45 +78,45 @@ namespace UnitTesting
Assert.Throws<CodeCompletException>(() => code.AjouterJeton(new Jeton(Couleur.Bleu)));
}
[Fact]
public void TestAjouterCodeIncomplet()
{
Plateau plateau = new Plateau(4, 10);
Code code = new Code(4);
Assert.Throws<CodeIncompletException>(() => 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 TestGrilleAjouterCode()
{
Code code = new Code(4);
Couleur[] couleurs = Enum.GetValues<Couleur>();
}
[Fact]
public void TestAjouterCodeIncomplet()
{
Plateau plateau = new Plateau(4, 10);
Code code = new Code(4);
Assert.Throws<CodeIncompletException>(() => 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 TestGrilleAjouterCode()
{
Code code = new Code(4);
Couleur[] couleurs = Enum.GetValues<Couleur>();
Plateau plateau = new Plateau(4, 12);
Jeton[] jetons = new Jeton[]
{
new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc),
{
new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc),
new Jeton(Couleur.Jaune)
};
code.AjouterJeton(jetons[0]);
@ -129,59 +124,59 @@ namespace UnitTesting
code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[3]);
plateau.AjouterCode(code);
(IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille;
var (jetonsGrille, indicateurs) = grille;
Assert.Single(jetonsGrille);
Assert.Equal(4, jetonsGrille.First().Count());
Assert.Single(indicateurs);
}
[Fact]
public void TestGrilleEstVide()
{
Plateau plateau = new Plateau(4, 12);
(IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille;
var (jetons, indicateurs) = grille;
Assert.Empty(jetons);
Assert.Empty(indicateurs);
}
[Fact]
public void TestAjouterCode_GrilleComplete_ThrowsGrilleCompleteException()
{
Plateau plateau = new Plateau(4, 2);
Code codeComplet1 = new Code(4);
Code codeComplet2 = new Code(4);
Code codeComplet3 = new Code(4);
Jeton[] jetons = new Jeton[]
{
new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc),
new Jeton(Couleur.Jaune)
};
foreach (Jeton jeton in jetons)
{
codeComplet1.AjouterJeton(jeton);
codeComplet2.AjouterJeton(jeton);
codeComplet3.AjouterJeton(jeton);
}
plateau.AjouterCode(codeComplet1);
plateau.AjouterCode(codeComplet2);
Assert.Throws<GrilleCompleteException>(() => plateau.AjouterCode(codeComplet3));
}
[Fact]
plateau.AjouterCode(code);
(IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille;
var (jetonsGrille, indicateurs) = grille;
Assert.Single(jetonsGrille);
Assert.Equal(4, jetonsGrille.First().Count());
Assert.Single(indicateurs);
}
[Fact]
public void TestGrilleEstVide()
{
Plateau plateau = new Plateau(4, 12);
(IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille;
var (jetons, indicateurs) = grille;
Assert.Empty(jetons);
Assert.Empty(indicateurs);
}
[Fact]
public void TestAjouterCode_GrilleComplete_ThrowsGrilleCompleteException()
{
Plateau plateau = new Plateau(4, 2);
Code codeComplet1 = new Code(4);
Code codeComplet2 = new Code(4);
Code codeComplet3 = new Code(4);
Jeton[] jetons = new Jeton[]
{
new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc),
new Jeton(Couleur.Jaune)
};
foreach (Jeton jeton in jetons)
{
codeComplet1.AjouterJeton(jeton);
codeComplet2.AjouterJeton(jeton);
codeComplet3.AjouterJeton(jeton);
}
plateau.AjouterCode(codeComplet1);
plateau.AjouterCode(codeComplet2);
Assert.Throws<GrilleCompleteException>(() => plateau.AjouterCode(codeComplet3));
}
[Fact]
public void TestPlateauEcoute()
{
Plateau plateau = new Plateau(4, 2);
@ -198,10 +193,10 @@ namespace UnitTesting
QuandPlateauAjouterCodeInfo?.Invoke(plateau, []);
Assert.True(appel);
}
[Fact]
}
[Fact]
public void TestAjouterCodeVictoire()
{
// Cas 1 : Victoire : false, code correct : false
@ -271,6 +266,6 @@ namespace UnitTesting
VictoireInfo4.SetValue(plateau4, true);
plateau4.AjouterCode(code4);
}
}
}
}
}
}

@ -0,0 +1,73 @@
using CoreLibrary;
using CoreLibrary.Core;
using CoreLibrary.Evenements;
using CoreLibrary.Joueurs;
using System.Reflection;
using Xunit;
namespace UnitTesting
{
public class RobotUT
{
[Fact]
public void TestEstCodePossible()
{
// Cas 1 - Plateau vide
Plateau plateau1 = new Plateau(1, 1);
Code code1 = new Code(1);
code1.AjouterJeton(new Jeton(Couleur.Rouge));
MethodInfo? EstCodePossible1 = typeof(Robot).GetMethod("EstCodePossible", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
Assert.NotNull(EstCodePossible1);
object? estPossibleObj1 = EstCodePossible1.Invoke(new Robot(), [plateau1, code1]);
Assert.NotNull(estPossibleObj1);
Assert.IsType<bool>(estPossibleObj1);
Assert.True((bool)estPossibleObj1);
// Cas 2 - Plateau rempli, code juste
Plateau plateau2 = new Plateau(1, 1);
FieldInfo? codeSecretInfo2 = typeof(Plateau).GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(codeSecretInfo2);
Code? codeSecret2 = codeSecretInfo2.GetValue(plateau2) as Code;
Assert.NotNull(codeSecret2);
plateau2.AjouterCode(codeSecret2);
MethodInfo? EstCodePossible2 = typeof(Robot).GetMethod("EstCodePossible", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
Assert.NotNull(EstCodePossible2);
object? estPossibleObj2 = EstCodePossible2.Invoke(new Robot(), [plateau2, codeSecret2]);
Assert.NotNull(estPossibleObj2);
Assert.IsType<bool>(estPossibleObj2);
Assert.True((bool)estPossibleObj2);
// Cas 3 - Plateau rempli, code mauvaise couleur
Code code3 = new Code(1);
code3.AjouterJeton(new Jeton(Couleur.Rouge));
Plateau plateau3;
Code? codeSecret3;
do
{
plateau3 = new Plateau(1, 1);
FieldInfo? codeSecretInfo3 = typeof(Plateau).GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(codeSecretInfo3);
codeSecret3 = codeSecretInfo3.GetValue(plateau3) as Code;
Assert.NotNull(codeSecret3);
} while (codeSecret3.RecupererJeton(0).Equals(code3.RecupererJeton(0)));
plateau3.AjouterCode(codeSecret3);
MethodInfo? EstCodePossible3 = typeof(Robot).GetMethod("EstCodePossible", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
Assert.NotNull(EstCodePossible3);
object? estPossibleObj3 = EstCodePossible3.Invoke(new Robot(), [plateau3, code3]);
Assert.NotNull(estPossibleObj3);
Assert.IsType<bool>(estPossibleObj3);
Assert.False((bool)estPossibleObj3);
}
}
}

@ -16,6 +16,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTesting", "UnitTesting\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistance", "Persistance\Persistance.csproj", "{B9E8C10D-9E0E-42F1-8F2C-1E2200DD96FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{2C86B541-6E8A-4C92-BC64-582413B22A49}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -44,6 +46,10 @@ Global
{B9E8C10D-9E0E-42F1-8F2C-1E2200DD96FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9E8C10D-9E0E-42F1-8F2C-1E2200DD96FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9E8C10D-9E0E-42F1-8F2C-1E2200DD96FA}.Release|Any CPU.Build.0 = Release|Any CPU
{2C86B541-6E8A-4C92-BC64-582413B22A49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C86B541-6E8A-4C92-BC64-582413B22A49}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C86B541-6E8A-4C92-BC64-582413B22A49}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C86B541-6E8A-4C92-BC64-582413B22A49}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save