Using NUnit instead of MSTest

This commit is contained in:
2023-04-26 22:14:39 +02:00
parent d6059e724b
commit 6acdbee154
6 changed files with 52 additions and 25 deletions

View File

@ -4,7 +4,7 @@ VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elwig", "Elwig\Elwig.csproj", "{00868460-16F6-4B48-AA9B-998F6263693B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elwig", "Elwig\Elwig.csproj", "{00868460-16F6-4B48-AA9B-998F6263693B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{AF4B84F8-08E6-409C-9E94-D7D990469597}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{30D7700A-7B0A-4E5D-B839-B4C1D95E307E}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -16,10 +16,10 @@ Global
{00868460-16F6-4B48-AA9B-998F6263693B}.Debug|Any CPU.Build.0 = Debug|Any CPU {00868460-16F6-4B48-AA9B-998F6263693B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00868460-16F6-4B48-AA9B-998F6263693B}.Release|Any CPU.ActiveCfg = Release|Any CPU {00868460-16F6-4B48-AA9B-998F6263693B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00868460-16F6-4B48-AA9B-998F6263693B}.Release|Any CPU.Build.0 = Release|Any CPU {00868460-16F6-4B48-AA9B-998F6263693B}.Release|Any CPU.Build.0 = Release|Any CPU
{AF4B84F8-08E6-409C-9E94-D7D990469597}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {30D7700A-7B0A-4E5D-B839-B4C1D95E307E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF4B84F8-08E6-409C-9E94-D7D990469597}.Debug|Any CPU.Build.0 = Debug|Any CPU {30D7700A-7B0A-4E5D-B839-B4C1D95E307E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF4B84F8-08E6-409C-9E94-D7D990469597}.Release|Any CPU.ActiveCfg = Release|Any CPU {30D7700A-7B0A-4E5D-B839-B4C1D95E307E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF4B84F8-08E6-409C-9E94-D7D990469597}.Release|Any CPU.Build.0 = Release|Any CPU {30D7700A-7B0A-4E5D-B839-B4C1D95E307E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -5,7 +5,7 @@ using System.Windows.Controls;
using Elwig.Models; using Elwig.Models;
namespace Elwig.Helpers { namespace Elwig.Helpers {
static class Validator { public static class Validator {
private static readonly Dictionary<string, string[][]> PHONE_NRS = new() { private static readonly Dictionary<string, string[][]> PHONE_NRS = new() {
{ "43", new string[][] { { "43", new string[][] {
@ -31,10 +31,10 @@ namespace Elwig.Helpers {
}; };
public static ValidationResult CheckInteger(TextBox input, bool required) { public static ValidationResult CheckInteger(TextBox input, bool required) {
return CheckNumeric(input, required, -1); return CheckInteger(input, required, -1);
} }
private static ValidationResult CheckNumeric(TextBox input, bool required, int maxLen) { public static ValidationResult CheckInteger(TextBox input, bool required, int maxLen) {
string text = ""; string text = "";
int pos = input.CaretIndex; int pos = input.CaretIndex;
for (int i = 0; i < input.Text.Length; i++) { for (int i = 0; i < input.Text.Length; i++) {
@ -60,7 +60,7 @@ namespace Elwig.Helpers {
} }
public static ValidationResult CheckPlz(TextBox input, bool required, AppDbContext ctx) { public static ValidationResult CheckPlz(TextBox input, bool required, AppDbContext ctx) {
CheckNumeric(input, false, 4); CheckInteger(input, false, 4);
if (!required && input.Text.Length == 0) { if (!required && input.Text.Length == 0) {
return new(true, null); return new(true, null);
} else if (input.Text.Length != 4) { } else if (input.Text.Length != 4) {
@ -237,7 +237,7 @@ namespace Elwig.Helpers {
} }
public static ValidationResult CheckLfbisNr(TextBox input, bool required) { public static ValidationResult CheckLfbisNr(TextBox input, bool required) {
var res = CheckNumeric(input, false, 7); var res = CheckInteger(input, false, 7);
if (!res.IsValid) { if (!res.IsValid) {
return res; return res;
} else if (!required && input.Text.Length == 0) { } else if (!required && input.Text.Length == 0) {

View File

@ -1,7 +1,7 @@
using Elwig.Helpers; using Elwig.Helpers;
namespace Tests { namespace Tests {
[TestClass] [TestFixture]
public class HelpersUtilsTest { public class HelpersUtilsTest {
private static readonly double[,] Gradation = new double[,] { private static readonly double[,] Gradation = new double[,] {
@ -15,28 +15,28 @@ namespace Tests {
{ 30.0, 156.0 }, { 30.0, 156.0 },
}; };
[TestMethod] [Test]
public void Test_KmwToOe() { public void Test_KmwToOe() {
for (int i = 0; i < Gradation.GetLength(0); i++) { for (int i = 0; i < Gradation.GetLength(0); i++) {
Assert.AreEqual(Gradation[i, 1], Utils.KmwToOe(Gradation[i, 0])); Assert.AreEqual(Gradation[i, 1], Utils.KmwToOe(Gradation[i, 0]));
} }
} }
[TestMethod] [Test]
public void Test_OeToKmw() { public void Test_OeToKmw() {
for (int i = 0; i < Gradation.GetLength(0); i++) { for (int i = 0; i < Gradation.GetLength(0); i++) {
Assert.AreEqual(Gradation[i, 0], Utils.OeToKmw(Gradation[i, 1])); Assert.AreEqual(Gradation[i, 0], Utils.OeToKmw(Gradation[i, 1]));
} }
} }
[TestMethod] [Test]
public void Test_DecFromDb() { public void Test_DecFromDb() {
Assert.AreEqual(10.67M, Utils.DecFromDb(10670, 3)); Assert.AreEqual(10.67M, Utils.DecFromDb(10670, 3));
Assert.AreEqual(-100.9999M, Utils.DecFromDb(-1009999, 4)); Assert.AreEqual(-100.9999M, Utils.DecFromDb(-1009999, 4));
Assert.AreEqual(0.01M, Utils.DecFromDb(1, 2)); Assert.AreEqual(0.01M, Utils.DecFromDb(1, 2));
} }
[TestMethod] [Test]
public void Test_DecToDb() { public void Test_DecToDb() {
Assert.AreEqual(21948, Utils.DecToDb(219.48M, 2)); Assert.AreEqual(21948, Utils.DecToDb(219.48M, 2));
Assert.AreEqual(-12345, Utils.DecToDb(-1.2345M, 4)); Assert.AreEqual(-12345, Utils.DecToDb(-1.2345M, 4));
@ -45,18 +45,18 @@ namespace Tests {
Assert.AreEqual(-561894, Utils.DecToDb(-5618.944M, 2)); Assert.AreEqual(-561894, Utils.DecToDb(-5618.944M, 2));
} }
[TestMethod] [Test]
public void Test_Modulo() { public void Test_Modulo() {
Assert.AreEqual(1, Utils.Modulo("1", 2)); Assert.AreEqual(1, Utils.Modulo("1", 2));
Assert.AreEqual(1, Utils.Modulo("12", 11)); Assert.AreEqual(1, Utils.Modulo("12", 11));
Assert.AreEqual(1, Utils.Modulo("65", 16)); Assert.AreEqual(1, Utils.Modulo("65", 16));
Assert.AreEqual(4, Utils.Modulo("91746381048364", 10)); Assert.AreEqual(4, Utils.Modulo("91746381048364", 10));
Assert.AreEqual(1, Utils.Modulo("210501700012345678131468", 97)); Assert.AreEqual(1, Utils.Modulo("210501700012345678131468", 97));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("", 4)); Assert.Throws<ArgumentException>(() => Utils.Modulo("", 4));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("1ab", 5)); Assert.Throws<ArgumentException>(() => Utils.Modulo("1ab", 5));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("123", 1)); Assert.Throws<ArgumentException>(() => Utils.Modulo("123", 1));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("456", 0)); Assert.Throws<ArgumentException>(() => Utils.Modulo("456", 0));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("789", -1)); Assert.Throws<ArgumentException>(() => Utils.Modulo("789", -1));
} }
} }
} }

View File

@ -0,0 +1,26 @@
using Elwig.Helpers;
using System.Windows.Controls;
namespace Tests {
[TestFixture]
[Apartment(ApartmentState.STA)]
public class HelpersValidatorTest {
private static TextBox CreateTextBox(string value, int caret = 0) {
return new() {
Text = value,
CaretIndex = caret,
};
}
[Test]
public void Test_CheckInteger_Simple() {
Assert.IsFalse(Validator.CheckInteger(CreateTextBox(""), true).IsValid);
Assert.IsTrue(Validator.CheckInteger(CreateTextBox(""), false).IsValid);
Assert.IsTrue(Validator.CheckInteger(CreateTextBox("123"), true).IsValid);
Assert.IsTrue(Validator.CheckInteger(CreateTextBox("456"), false).IsValid);
Assert.IsTrue(Validator.CheckInteger(CreateTextBox("1234"), false, 4).IsValid);
Assert.IsTrue(Validator.CheckInteger(CreateTextBox("4567"), false, 3).IsValid);
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
@ -11,8 +11,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" /> <PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" /> <PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.5.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" /> <PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup> </ItemGroup>

View File

@ -1 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting; global using NUnit.Framework;