tests sur robot
continuous-integration/drone/push Build is failing Details

master
Céleste BARBOSA 1 year ago
parent 204d7f1e5d
commit ad6dc4e85d

@ -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,78 +1,73 @@
using CoreLibrary.Exceptions; using CoreLibrary.Exceptions;
using System.Reflection; using System.Reflection;
using CoreLibrary.Core; using CoreLibrary.Core;
using Xunit; using Xunit;
using CoreLibrary.Joueurs;
using CoreLibrary.Regles; namespace UnitTesting
using CoreLibrary; {
public class PlateauUT
namespace UnitTesting {
{ [Fact]
public class PlateauUT public void TestConstructeurValide()
{ {
[Fact] Plateau plateau = new Plateau(4,12);
public void TestConstructeurValide() Assert.NotNull(plateau);
{ Assert.False(plateau.Victoire);
Plateau plateau = new Plateau(4,12); }
Assert.NotNull(plateau);
Assert.False(plateau.Victoire); [Fact]
} public void TestConstructeurInvalide()
{
[Fact] Assert.Throws<TailleCodeException>(() => new Plateau(-1, 10));
public void TestConstructeurInvalide() Assert.Throws<TailleGrilleException>(() => new Plateau(3, -1));
{ }
Assert.Throws<TailleCodeException>(() => new Plateau(-1, 10));
Assert.Throws<TailleGrilleException>(() => new Plateau(3, -1)); [Fact]
} public void TestEstCompletTrue()
{
[Fact] Plateau plateau = new Plateau(4, 3);
public void TestEstCompletTrue() Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)];
{ Code code = new Code(4);
Plateau plateau = new Plateau(4, 3); code.AjouterJeton(jetons[0]);
Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)]; code.AjouterJeton(jetons[1]);
Code code = new Code(4); code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[0]); code.AjouterJeton(jetons[3]);
code.AjouterJeton(jetons[1]); plateau.AjouterCode(code);
code.AjouterJeton(jetons[2]); plateau.AjouterCode(code);
code.AjouterJeton(jetons[3]); plateau.AjouterCode(code);
plateau.AjouterCode(code);
plateau.AjouterCode(code); bool estComplet = plateau.Complet;
plateau.AjouterCode(code);
Assert.True(estComplet);
bool estComplet = plateau.Complet; }
Assert.True(estComplet); [Fact]
} public void TestEstCompletFalse()
{
[Fact] Plateau plateau = new Plateau(4, 3);
public void TestEstCompletFalse() Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)];
{ Code code = new Code(4);
Plateau plateau = new Plateau(4, 3); code.AjouterJeton(jetons[0]);
Jeton[] jetons = [new Jeton(Couleur.Rouge), new Jeton(Couleur.Bleu), new Jeton(Couleur.Blanc), new Jeton(Couleur.Jaune)]; code.AjouterJeton(jetons[1]);
Code code = new Code(4); code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[0]); code.AjouterJeton(jetons[3]);
code.AjouterJeton(jetons[1]); plateau.AjouterCode(code);
code.AjouterJeton(jetons[2]); plateau.AjouterCode(code);
code.AjouterJeton(jetons[3]);
plateau.AjouterCode(code); bool estComplet = plateau.Complet;
plateau.AjouterCode(code);
Assert.False(estComplet);
}
bool estComplet = plateau.Complet;
Assert.False(estComplet);
}
[Fact] [Fact]
public void TestAjouterCodeTailleIncorrecte() public void TestAjouterCodeTailleIncorrecte()
{ {
Plateau plateau = new Plateau(4, 10); Plateau plateau = new Plateau(4, 10);
Jeton[] jetons = new Jeton[] Jeton[] jetons = new Jeton[]
{ {
new Jeton(Couleur.Rouge), new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu), new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc), new Jeton(Couleur.Blanc),
new Jeton(Couleur.Jaune) new Jeton(Couleur.Jaune)
}; };
Code code = new Code(4); Code code = new Code(4);
@ -83,45 +78,45 @@ namespace UnitTesting
Assert.Throws<CodeCompletException>(() => code.AjouterJeton(new Jeton(Couleur.Bleu))); Assert.Throws<CodeCompletException>(() => code.AjouterJeton(new Jeton(Couleur.Bleu)));
} }
[Fact] [Fact]
public void TestAjouterCodeIncomplet() public void TestAjouterCodeIncomplet()
{ {
Plateau plateau = new Plateau(4, 10); Plateau plateau = new Plateau(4, 10);
Code code = new Code(4); Code code = new Code(4);
Assert.Throws<CodeIncompletException>(() => plateau.AjouterCode(code)); Assert.Throws<CodeIncompletException>(() => plateau.AjouterCode(code));
} }
[Fact] [Fact]
public void TestAjouterCodeBonCode() public void TestAjouterCodeBonCode()
{ {
Plateau plateau = new Plateau(4, 10); Plateau plateau = new Plateau(4, 10);
Type type = typeof(Plateau); Type type = typeof(Plateau);
FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance); FieldInfo? fieldInfo = type.GetField("codeSecret", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(fieldInfo); Assert.NotNull(fieldInfo);
Code? codeSecret = (Code?)fieldInfo.GetValue(plateau); Code? codeSecret = (Code?)fieldInfo.GetValue(plateau);
Assert.NotNull(codeSecret); Assert.NotNull(codeSecret);
plateau.AjouterCode(codeSecret); plateau.AjouterCode(codeSecret);
Assert.True(plateau.Victoire); Assert.True(plateau.Victoire);
} }
[Fact] [Fact]
public void TestGrilleAjouterCode() public void TestGrilleAjouterCode()
{ {
Code code = new Code(4); Code code = new Code(4);
Couleur[] couleurs = Enum.GetValues<Couleur>(); Couleur[] couleurs = Enum.GetValues<Couleur>();
Plateau plateau = new Plateau(4, 12); Plateau plateau = new Plateau(4, 12);
Jeton[] jetons = new Jeton[] Jeton[] jetons = new Jeton[]
{ {
new Jeton(Couleur.Rouge), new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu), new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc), new Jeton(Couleur.Blanc),
new Jeton(Couleur.Jaune) new Jeton(Couleur.Jaune)
}; };
code.AjouterJeton(jetons[0]); code.AjouterJeton(jetons[0]);
@ -129,59 +124,59 @@ namespace UnitTesting
code.AjouterJeton(jetons[2]); code.AjouterJeton(jetons[2]);
code.AjouterJeton(jetons[3]); code.AjouterJeton(jetons[3]);
plateau.AjouterCode(code); plateau.AjouterCode(code);
(IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille; (IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille;
var (jetonsGrille, indicateurs) = grille; var (jetonsGrille, indicateurs) = grille;
Assert.Single(jetonsGrille); Assert.Single(jetonsGrille);
Assert.Equal(4, jetonsGrille.First().Count()); Assert.Equal(4, jetonsGrille.First().Count());
Assert.Single(indicateurs); Assert.Single(indicateurs);
} }
[Fact] [Fact]
public void TestGrilleEstVide() public void TestGrilleEstVide()
{ {
Plateau plateau = new Plateau(4, 12); Plateau plateau = new Plateau(4, 12);
(IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille; (IEnumerable<IEnumerable<Jeton>>, IEnumerable<IEnumerable<Indicateur>>) grille = plateau.Grille;
var (jetons, indicateurs) = grille; var (jetons, indicateurs) = grille;
Assert.Empty(jetons); Assert.Empty(jetons);
Assert.Empty(indicateurs); Assert.Empty(indicateurs);
} }
[Fact] [Fact]
public void TestAjouterCode_GrilleComplete_ThrowsGrilleCompleteException() public void TestAjouterCode_GrilleComplete_ThrowsGrilleCompleteException()
{ {
Plateau plateau = new Plateau(4, 2); Plateau plateau = new Plateau(4, 2);
Code codeComplet1 = new Code(4); Code codeComplet1 = new Code(4);
Code codeComplet2 = new Code(4); Code codeComplet2 = new Code(4);
Code codeComplet3 = new Code(4); Code codeComplet3 = new Code(4);
Jeton[] jetons = new Jeton[] Jeton[] jetons = new Jeton[]
{ {
new Jeton(Couleur.Rouge), new Jeton(Couleur.Rouge),
new Jeton(Couleur.Bleu), new Jeton(Couleur.Bleu),
new Jeton(Couleur.Blanc), new Jeton(Couleur.Blanc),
new Jeton(Couleur.Jaune) new Jeton(Couleur.Jaune)
}; };
foreach (Jeton jeton in jetons) foreach (Jeton jeton in jetons)
{ {
codeComplet1.AjouterJeton(jeton); codeComplet1.AjouterJeton(jeton);
codeComplet2.AjouterJeton(jeton); codeComplet2.AjouterJeton(jeton);
codeComplet3.AjouterJeton(jeton); codeComplet3.AjouterJeton(jeton);
} }
plateau.AjouterCode(codeComplet1); plateau.AjouterCode(codeComplet1);
plateau.AjouterCode(codeComplet2); plateau.AjouterCode(codeComplet2);
Assert.Throws<GrilleCompleteException>(() => plateau.AjouterCode(codeComplet3)); Assert.Throws<GrilleCompleteException>(() => plateau.AjouterCode(codeComplet3));
} }
[Fact] [Fact]
public void TestPlateauEcoute() public void TestPlateauEcoute()
{ {
Plateau plateau = new Plateau(4, 2); Plateau plateau = new Plateau(4, 2);
@ -198,10 +193,10 @@ namespace UnitTesting
QuandPlateauAjouterCodeInfo?.Invoke(plateau, []); QuandPlateauAjouterCodeInfo?.Invoke(plateau, []);
Assert.True(appel); Assert.True(appel);
} }
[Fact] [Fact]
public void TestAjouterCodeVictoire() public void TestAjouterCodeVictoire()
{ {
// Cas 1 : Victoire : false, code correct : false // Cas 1 : Victoire : false, code correct : false
@ -271,6 +266,6 @@ namespace UnitTesting
VictoireInfo4.SetValue(plateau4, true); VictoireInfo4.SetValue(plateau4, true);
plateau4.AjouterCode(code4); 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 EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistance", "Persistance\Persistance.csproj", "{B9E8C10D-9E0E-42F1-8F2C-1E2200DD96FA}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistance", "Persistance\Persistance.csproj", "{B9E8C10D-9E0E-42F1-8F2C-1E2200DD96FA}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{2C86B541-6E8A-4C92-BC64-582413B22A49}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{B9E8C10D-9E0E-42F1-8F2C-1E2200DD96FA}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save