Commit 13314703 authored by Jason Ouellet's avatar Jason Ouellet

Ajouter des tests unitaires dans Here.Params

Correction warnings (Code Analysis)
parent f1c8a4d1
Pipeline #11450 canceled with stages
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1304:Spécifier CultureInfo", Justification = "<En attente>", Scope = "member", Target = "~P:Here.Params.ParamsEnum`1.ValeurStr")]
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Here.Params
......@@ -7,7 +9,9 @@ namespace Here.Params
{
public ParamsArray(string nom)
{
Nom = nom ?? throw new ArgumentNullException(nameof(nom));
if (string.IsNullOrEmpty(nom) || (string.IsNullOrWhiteSpace(nom.Trim())))
throw new ArgumentNullException(nameof(nom));
Nom = nom;
}
public ParamsArray(string nom, T[] valeurs) : this(nom)
......@@ -19,13 +23,13 @@ namespace Here.Params
public string Nom { get; set; }
public T[] Valeurs { get; set; }
public IEnumerable<T> Valeurs { get; set; }
public string ValeurStr => string.Join(";", Valeurs.Select(v => v.ToString()).ToArray());
public override string ToString()
{
return string.Format("{0}={1}", Nom, ValeurStr);
return string.Format(CultureInfo.InvariantCulture, "{0}={1}", Nom, ValeurStr);
}
}
......
using System;
using System.Globalization;
namespace Here.Params
{
......@@ -28,7 +29,7 @@ namespace Here.Params
public override string ToString()
{
return string.Format("{0}={1}", Nom, ValeurStr);
return string.Format(CultureInfo.InvariantCulture, "{0}={1}", Nom, ValeurStr);
}
}
}
\ No newline at end of file
namespace Here.Params
using System.Globalization;
namespace Here.Params
{
public class ParamsEnum<T> : ParamsBase<T>
{
......@@ -6,7 +8,7 @@
{
}
public override string ValeurStr => string.Format("{0}", Valeur.ToString().ToLower());
public override string ValeurStr => string.Format(CultureInfo.InvariantCulture, "{0}", Valeur.ToString().ToLower());
}
}
\ No newline at end of file
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Xunit;
namespace Here.Configuration.Tests
{
[ExcludeFromCodeCoverage]
public class ConfigurationExceptionTest
{
private const string MessageFormat = "La valeur {0} n'est pas configuré correctement";
private const string Valeur = "ConfigValeur";
private const string InnerException = "Inner exception.";
[Fact]
public void ConstructorTest()
{
Exception ex =
new ConfigurationException();
Assert.Equal("Exception of type 'Here.Configuration.ConfigurationException' was thrown.", ex.Message);
}
[Fact]
public void MessageTest()
{
Exception ex =
new ConfigurationException(Valeur);
Assert.Equal(string.Format(MessageFormat, Valeur), ex.Message);
}
[Fact]
public void InnerExceptionTest()
{
Exception exInner = new Exception(InnerException);
Exception ex =
new ConfigurationException(Valeur, exInner);
Assert.Equal(string.Format(MessageFormat, Valeur), ex.Message);
Assert.Equal(exInner.ToString(), ex.InnerException.ToString());
}
[Fact]
public void SerialisationDeserialisationTest()
{
Exception ex =
new ConfigurationException(
Valeur, new Exception(InnerException));
string exceptionToString = ex.ToString();
// Sérialiser et désérialiser l'exception avec un BinaryFormatter
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
// Enregistrer l'état de l'objet (sérialisation)
bf.Serialize(ms, ex);
// Réutiliser le même flux mémoire
ms.Seek(0, 0);
// Remplacer l'exception originale par la désérialisation de celui-ci
ex = (ConfigurationException)bf.Deserialize(ms);
}
// Valider que l'objets avant est équivalent à l'objet d'après la désérialisation
Assert.Equal(exceptionToString, ex.ToString());
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Xunit;
namespace Here.Configuration.Tests
{
[ExcludeFromCodeCoverage]
public class HereConfigTests
{
[Fact()]
public void TestConstructeur()
{
HereConfig instance;
instance = new HereConfig();
Assert.NotNull(instance);
}
[Fact()]
public void TestProprieteAppId()
{
var appIdString = "AppIdString";
HereConfig instance;
instance = new HereConfig() { AppId = appIdString };
Assert.Equal(appIdString, instance.AppId);
}
[Fact()]
public void TestProprieteAppCode()
{
var appCodeString = "AppCodeString";
HereConfig instance;
instance = new HereConfig() { AppCode = appCodeString };
Assert.Equal(appCodeString, instance.AppCode);
}
[Fact()]
public void TestProprieteUriServices()
{
var uriServicesDict = new Dictionary<string, string>() { };
HereConfig instance;
instance = new HereConfig() { UriServices = uriServicesDict };
Assert.Equal(uriServicesDict, instance.UriServices);
}
}
}
\ No newline at end of file
using Here.Configuration;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Xunit;
namespace Here.Tests
{
public class ConfigurationExceptionTest
{
private const string MessageExToString = "ex.ToString()";
private const string Message = "Message";
private const string InnerException = "Inner exception.";
public ConfigurationExceptionTest()
{
}
[Fact]
public void TestConfigurationException()
{
Exception ex =
new ConfigurationException(
Message, new Exception(InnerException));
// Save the full ToString() value, including the exception message and stack trace.
string exceptionToString = ex.ToString();
// Round-trip the exception: Serialize and de-serialize with a BinaryFormatter
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
// "Save" object state
bf.Serialize(ms, ex);
// Re-use the same stream for de-serialization
ms.Seek(0, 0);
// Replace the original exception with de-serialized one
ex = (ConfigurationException)bf.Deserialize(ms);
}
// Double-check that the exception message and stack trace (owned by the base Exception) are preserved
Assert.Equal(MessageExToString, ex.ToString());
Assert.Equal(MessageExToString, exceptionToString);
}
}
}
\ No newline at end of file
......@@ -7,7 +7,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoBogus" Version="2.7.3" />
<PackageReference Include="Bogus" Version="28.3.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="NaughtyStrings.Bogus" Version="2.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
......
using Xunit;
using Here;
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using Bogus;
namespace Here.Tests
{
[ExcludeFromCodeCoverage]
public class LocalisationTests
{
Faker faker = new Faker("fr");
private readonly decimal latitudeVal;
private readonly decimal longitudeVal;
private readonly string stringPoint;
public LocalisationTests()
{
latitudeVal = faker.Random.Decimal(100, 1000);
longitudeVal = faker.Random.Decimal(100, 1000);
stringPoint = string.Format("{0},{1}", latitudeVal,longitudeVal);
}
private Localisation ObtenirInstanceLocalisation()
{
Localisation instance;
instance = new Localisation(latitudeVal, longitudeVal);
return instance;
}
[Fact()]
public void ConstructeurTest()
{
ILocalisation instance;
instance = ObtenirInstanceLocalisation();
Assert.NotNull(instance);
Assert.Equal(latitudeVal, instance.Latitude);
Assert.Equal(longitudeVal, instance.Longitude);
}
[Fact]
public void ToStringTest()
{
var instance = ObtenirInstanceLocalisation();
Assert.Equal(stringPoint, instance.ToString());
}
}
}
\ No newline at end of file
using Xunit;
using Here.Params;
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Collections;
namespace Here.Params.Tests
{
[ExcludeFromCodeCoverage]
public class ParamsArrayTests
{
private const string nomValide = "nom";
private const string valeurValide = "valeur";
[Theory]
[InlineData(nomValide)]
public void ConstructeurStringNomValide(string nom)
{
var param = new ParamsArray<string>(nom);
Assert.Equal(nom, param.Nom);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void ConstructeurNomNullArgumentException(string nom)
{
Assert.Throws<ArgumentNullException>(() => new ParamsArray<string>(nom));
}
[Theory]
[InlineData(nomValide, null)]
public void ConstructeurValeurNullArgumentException<T>(string nom, T[] valeur)
{
Assert.Throws<ArgumentNullException>(() => new ParamsArray<T>(nom, valeur));
}
[Theory]
[InlineData(nomValide, new string[] { "ValeurA", "ValeurB", "ValeurC" }, "ValeurA;ValeurB;ValeurC")]
[InlineData(nomValide, new string[] { "ValeurA", "ValeurB" }, "ValeurA;ValeurB")]
[InlineData(nomValide, new string[] { "ValeurA", "ValeurB", "ValeurC", "ValeurD" }, "ValeurA;ValeurB;ValeurC;ValeurD")]
public void ValeurStringArrayValeurStrValide(string nom, string[] valeur, string reponse)
{
ParamsArray<string> param = new ParamsArray<string>(nom, valeur);
Assert.Equal(reponse, param.ValeurStr);
}
[Theory]
[InlineData(nomValide, new string[] { "ValeurA", "ValeurB", "ValeurC" }, "nom=ValeurA;ValeurB;ValeurC")]
[InlineData(nomValide, new string[] { "ValeurA", "ValeurB" }, "nom=ValeurA;ValeurB")]
[InlineData(nomValide, new string[] { "ValeurA", "ValeurB", "ValeurC", "ValeurD" }, "nom=ValeurA;ValeurB;ValeurC;ValeurD")]
public void ValeurStringArrayToStringValide(string nom, string[] valeur, string reponse)
{
ParamsArray<string> param = new ParamsArray<string>(nom, valeur);
Assert.Equal(reponse, param.ToString());
}
[Theory]
[InlineData(nomValide, new int[] { 1, 5, 10 }, "1;5;10")]
[InlineData(nomValide, new int[] { 1, 5 }, "1;5")]
[InlineData(nomValide, new int[] { 1, 5, 10, 15 }, "1;5;10;15")]
public void ValeurIntArrayValeurStrValide(string nom, int[] valeur, string reponse)
{
ParamsArray<int> param = new ParamsArray<int>(nom, valeur);
Assert.Equal(reponse, param.ValeurStr);
}
[Theory]
[InlineData(nomValide, new int[] { 1, 5, 10 }, "nom=1;5;10")]
[InlineData(nomValide, new int[] { 1, 5 }, "nom=1;5")]
[InlineData(nomValide, new int[] { 1, 5, 10, 15 }, "nom=1;5;10;15")]
public void ValeurIntArrayToStringValide(string nom, int[] valeur, string reponse)
{
ParamsArray<int> param = new ParamsArray<int>(nom, valeur);
Assert.Equal(reponse, param.ToString());
}
}
}
\ No newline at end of file
using Xunit;
using Here.Params;
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.CodeAnalysis;
namespace Here.Params.Tests
{
[ExcludeFromCodeCoverage]
public class ParamsBaseTests
{
private const string nomValide = "nom";
private const string valeurValide = "valeur";
[Theory]
[InlineData(nomValide)]
public void ConstructeurStringNomValide(string nom)
{
var param = new ParamsBase<string>(nom);
Assert.Equal(nom, param.Nom);
}
[Theory]
[InlineData(nomValide, valeurValide)]
public void ConstructeurStringNomValideValeurValide(string nom, string valeur)
{
var param = new ParamsBase<string>(nom, valeur);
Assert.Equal(nom, param.Nom);
Assert.Equal(valeur, param.Valeur);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void ConstructeurNomNullArgumentException(string nom)
{
Assert.Throws<ArgumentNullException>(() => new ParamsBase<string>(nom));
}
[Theory]
[InlineData(null, null)]
[InlineData("", null)]
[InlineData(nomValide, null)]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData(nomValide, "")]
[InlineData(null, valeurValide)]
[InlineData("", valeurValide)]
public void ConstructeurNomValeurNullArgumentException(string nom, string valeur)
{
Assert.Throws<ArgumentNullException>(() => new ParamsBase<string>(nom, valeur));
}
[Theory]
[InlineData(nomValide, valeurValide)]
public void ValeurStringValeurStrValide(string nom, string valeur)
{
var param = new ParamsBase<string>(nom, valeur);
Assert.Equal(valeur, param.ValeurStr);
}
[Theory]
[InlineData(nomValide, 10)]
[InlineData(nomValide, 0)]
[InlineData(nomValide, -10)]
public void ValeurIntValeurStrValide(string nom, int valeur)
{
var param = new ParamsBase<int>(nom, valeur);
Assert.Equal(valeur.ToString(), param.ValeurStr);
}
private const decimal valeurPositive = 10.25m;
private const decimal valeurZero = 10.25m;
private const decimal valeurNegative = -10.75m;
[Theory]
[InlineData(nomValide, valeurValide)]
public void ToStringValide(string nom, string valeur)
{
var param = new ParamsBase<string>(nom, valeur);
Assert.Equal(string.Format("{0}={1}", nom, valeur), param.ToString());
}
}
}
\ No newline at end of file
using Xunit;
using Here.Params;
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.CodeAnalysis;
namespace Here.Params.Tests
{
[ExcludeFromCodeCoverage]
public class ParamsEnumTests
{
private const string nomValide = "nom";
public enum TestEnum
{
ValeurA,
ValeurB,
ValeurC,
}
[Theory]
[InlineData(nomValide, TestEnum.ValeurA)]
[InlineData(nomValide, TestEnum.ValeurB)]
[InlineData(nomValide, TestEnum.ValeurC)]
public void ValeurEnumValeurStrValide(string nom, TestEnum valeur)
{
ParamsEnum<TestEnum> param = new ParamsEnum<TestEnum>(nom, valeur);
Assert.Equal(valeur.ToString().ToLower(), param.ValeurStr);
}
}
}
\ No newline at end of file
using Xunit;
using System.Collections.Generic;
namespace Here.Tests
{
public class UnitTest1
{
[Fact]
public void TestTrue()
{
Assert.True(true);
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment