From 6acdbee1545dfadc189d854e0f7f16da5733203c Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Wed, 26 Apr 2023 22:14:39 +0200 Subject: [PATCH] Using NUnit instead of MSTest --- Elwig.sln | 10 +++++----- Elwig/Helpers/Validator.cs | 10 +++++----- Tests/HelpersUtilsTest.cs | 22 +++++++++++----------- Tests/HelpersValidatorTest.cs | 26 ++++++++++++++++++++++++++ Tests/Tests.csproj | 7 ++++--- Tests/Usings.cs | 2 +- 6 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 Tests/HelpersValidatorTest.cs diff --git a/Elwig.sln b/Elwig.sln index d308563..e642619 100644 --- a/Elwig.sln +++ b/Elwig.sln @@ -4,7 +4,7 @@ VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elwig", "Elwig\Elwig.csproj", "{00868460-16F6-4B48-AA9B-998F6263693B}" 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 Global 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}.Release|Any CPU.ActiveCfg = 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 - {AF4B84F8-08E6-409C-9E94-D7D990469597}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AF4B84F8-08E6-409C-9E94-D7D990469597}.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}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {30D7700A-7B0A-4E5D-B839-B4C1D95E307E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {30D7700A-7B0A-4E5D-B839-B4C1D95E307E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {30D7700A-7B0A-4E5D-B839-B4C1D95E307E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Elwig/Helpers/Validator.cs b/Elwig/Helpers/Validator.cs index 96a51d1..af8c742 100644 --- a/Elwig/Helpers/Validator.cs +++ b/Elwig/Helpers/Validator.cs @@ -5,7 +5,7 @@ using System.Windows.Controls; using Elwig.Models; namespace Elwig.Helpers { - static class Validator { + public static class Validator { private static readonly Dictionary PHONE_NRS = new() { { "43", new string[][] { @@ -31,10 +31,10 @@ namespace Elwig.Helpers { }; 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 = ""; int pos = input.CaretIndex; 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) { - CheckNumeric(input, false, 4); + CheckInteger(input, false, 4); if (!required && input.Text.Length == 0) { return new(true, null); } else if (input.Text.Length != 4) { @@ -237,7 +237,7 @@ namespace Elwig.Helpers { } public static ValidationResult CheckLfbisNr(TextBox input, bool required) { - var res = CheckNumeric(input, false, 7); + var res = CheckInteger(input, false, 7); if (!res.IsValid) { return res; } else if (!required && input.Text.Length == 0) { diff --git a/Tests/HelpersUtilsTest.cs b/Tests/HelpersUtilsTest.cs index 33689f0..ef79750 100644 --- a/Tests/HelpersUtilsTest.cs +++ b/Tests/HelpersUtilsTest.cs @@ -1,7 +1,7 @@ using Elwig.Helpers; namespace Tests { - [TestClass] + [TestFixture] public class HelpersUtilsTest { private static readonly double[,] Gradation = new double[,] { @@ -15,28 +15,28 @@ namespace Tests { { 30.0, 156.0 }, }; - [TestMethod] + [Test] public void Test_KmwToOe() { for (int i = 0; i < Gradation.GetLength(0); i++) { Assert.AreEqual(Gradation[i, 1], Utils.KmwToOe(Gradation[i, 0])); } } - [TestMethod] + [Test] public void Test_OeToKmw() { for (int i = 0; i < Gradation.GetLength(0); i++) { Assert.AreEqual(Gradation[i, 0], Utils.OeToKmw(Gradation[i, 1])); } } - [TestMethod] + [Test] public void Test_DecFromDb() { Assert.AreEqual(10.67M, Utils.DecFromDb(10670, 3)); Assert.AreEqual(-100.9999M, Utils.DecFromDb(-1009999, 4)); Assert.AreEqual(0.01M, Utils.DecFromDb(1, 2)); } - [TestMethod] + [Test] public void Test_DecToDb() { Assert.AreEqual(21948, Utils.DecToDb(219.48M, 2)); Assert.AreEqual(-12345, Utils.DecToDb(-1.2345M, 4)); @@ -45,18 +45,18 @@ namespace Tests { Assert.AreEqual(-561894, Utils.DecToDb(-5618.944M, 2)); } - [TestMethod] + [Test] public void Test_Modulo() { Assert.AreEqual(1, Utils.Modulo("1", 2)); Assert.AreEqual(1, Utils.Modulo("12", 11)); Assert.AreEqual(1, Utils.Modulo("65", 16)); Assert.AreEqual(4, Utils.Modulo("91746381048364", 10)); Assert.AreEqual(1, Utils.Modulo("210501700012345678131468", 97)); - Assert.ThrowsException(() => Utils.Modulo("", 4)); - Assert.ThrowsException(() => Utils.Modulo("1ab", 5)); - Assert.ThrowsException(() => Utils.Modulo("123", 1)); - Assert.ThrowsException(() => Utils.Modulo("456", 0)); - Assert.ThrowsException(() => Utils.Modulo("789", -1)); + Assert.Throws(() => Utils.Modulo("", 4)); + Assert.Throws(() => Utils.Modulo("1ab", 5)); + Assert.Throws(() => Utils.Modulo("123", 1)); + Assert.Throws(() => Utils.Modulo("456", 0)); + Assert.Throws(() => Utils.Modulo("789", -1)); } } } diff --git a/Tests/HelpersValidatorTest.cs b/Tests/HelpersValidatorTest.cs new file mode 100644 index 0000000..800f651 --- /dev/null +++ b/Tests/HelpersValidatorTest.cs @@ -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); + } + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index d92d858..c301ed9 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,4 +1,4 @@ - + net7.0-windows @@ -11,8 +11,9 @@ - - + + + diff --git a/Tests/Usings.cs b/Tests/Usings.cs index ab67c7e..3244567 100644 --- a/Tests/Usings.cs +++ b/Tests/Usings.cs @@ -1 +1 @@ -global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file +global using NUnit.Framework;