From 1767b55c8a84bbf351cf70ee7ee8888a4d0df406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Thu, 13 Nov 2025 20:30:44 +0100 Subject: [PATCH] Updating to VS 2026 --- .gitlab-ci.yml | 4 +- Common.sln | 5 +- src/Directory.Build.props | 2 +- .../AMWD.Common.AspNetCore.Tests.csproj | 4 + .../BasicAuthenticationAttributeTest.cs | 4 +- .../Attributes/IPAllowListAttributeTest.cs | 24 +++--- .../Attributes/IPBlockListAttributeTest.cs | 22 ++--- .../Extensions/HttpContextExtensionsTest.cs | 2 +- .../ModelStateDictionaryExtensionsTest.cs | 8 +- .../Extensions/SessionExtensionsTest.cs | 4 +- .../BasicAuthenticationMiddlewareTest.cs | 12 +-- .../Utilities/HtmlHelperTest.cs | 44 +++++----- .../Utilities/PasswordHelperTest.cs | 2 +- .../AMWD.Common.Tests.csproj | 4 + .../Cli/CommandLineParserTest.cs | 82 +++++++------------ .../Cli/EnumerableWalkerTest.cs | 10 +-- .../Comparer/DomainComparerTest.cs | 6 +- .../Comparer/IPAddressComparerTest.cs | 6 +- .../Comparer/VersionStringComparerTest.cs | 6 +- .../Extensions/CollectionExtensionsTest.cs | 76 ++++++++--------- .../ReaderWriterLockSlimExtensionsTest.cs | 14 ++-- .../Extensions/StreamExtensionsTest.cs | 8 +- .../Extensions/StringExtensionsTest.cs | 22 ++--- .../Logging/FileLoggerTest.cs | 26 ++---- .../Utilities/AsyncQueueTest.cs | 66 ++++++++------- .../Utilities/CryptographyHelperTest.cs | 24 +++--- .../Utilities/DelayedTaskTest.cs | 9 +- test/Directory.Build.props | 10 +-- test/MSTestSettings.cs | 1 + 29 files changed, 230 insertions(+), 277 deletions(-) create mode 100644 test/MSTestSettings.cs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f9c326b..9d70913 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -47,7 +47,7 @@ test-debug: - dotnet tool install dotnet-reportgenerator-globaltool --tool-path /dotnet-tools script: - dotnet test -c Debug --nologo /p:CoverletOutputFormat=Cobertura - - /dotnet-tools/reportgenerator "-reports:${CI_PROJECT_DIR}/**/coverage.cobertura.xml" "-targetdir:/reports" -reportType:TextSummary + - /dotnet-tools/reportgenerator "-reports:${CI_PROJECT_DIR}/**/coverage.cobertura.xml" "-targetdir:/reports" "-reportType:TextSummary" after_script: - cat /reports/Summary.txt artifacts: @@ -107,7 +107,7 @@ test-release: - dotnet tool install dotnet-reportgenerator-globaltool --tool-path /dotnet-tools script: - dotnet test -c Release --nologo /p:CoverletOutputFormat=Cobertura - - /dotnet-tools/reportgenerator "-reports:${CI_PROJECT_DIR}/**/coverage.cobertura.xml" "-targetdir:/reports" -reportType:TextSummary + - /dotnet-tools/reportgenerator "-reports:${CI_PROJECT_DIR}/**/coverage.cobertura.xml" "-targetdir:/reports" "-reportType:TextSummary" after_script: - cat /reports/Summary.txt artifacts: diff --git a/Common.sln b/Common.sln index eb68539..45e6c8c 100644 --- a/Common.sln +++ b/Common.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.31903.59 +# Visual Studio Version 18 +VisualStudioVersion = 18.0.11205.157 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F2C7556A-99EB-43EB-8954-56A24AFE928F}" ProjectSection(SolutionItems) = preProject @@ -11,6 +11,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{E5DF156A-6C8B-4004-BA4C-A8DDE6FD3ECD}" ProjectSection(SolutionItems) = preProject test\Directory.Build.props = test\Directory.Build.props + test\MSTestSettings.cs = test\MSTestSettings.cs EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AMWD.Common", "src\AMWD.Common\AMWD.Common.csproj", "{F512C474-B670-4E47-911E-7C0674AA8E7E}" diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 0c023be..8a0bd0b 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -38,7 +38,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/AMWD.Common.AspNetCore.Tests/AMWD.Common.AspNetCore.Tests.csproj b/test/AMWD.Common.AspNetCore.Tests/AMWD.Common.AspNetCore.Tests.csproj index 59f2c67..17ec0fa 100644 --- a/test/AMWD.Common.AspNetCore.Tests/AMWD.Common.AspNetCore.Tests.csproj +++ b/test/AMWD.Common.AspNetCore.Tests/AMWD.Common.AspNetCore.Tests.csproj @@ -4,6 +4,10 @@ net8.0 + + + + diff --git a/test/AMWD.Common.AspNetCore.Tests/Attributes/BasicAuthenticationAttributeTest.cs b/test/AMWD.Common.AspNetCore.Tests/Attributes/BasicAuthenticationAttributeTest.cs index 4c50ae3..c462449 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Attributes/BasicAuthenticationAttributeTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Attributes/BasicAuthenticationAttributeTest.cs @@ -336,7 +336,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes var actionDescriptor = new ActionDescriptor { - EndpointMetadata = new List() + EndpointMetadata = [] }; if (isAnonymousAllowed) actionDescriptor.EndpointMetadata.Add(new AllowAnonymousAttribute()); @@ -346,7 +346,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes HttpContext = _contextMock.Object, RouteData = routeDataMock.Object, ActionDescriptor = actionDescriptor, - }, new List()); + }, []); } } } diff --git a/test/AMWD.Common.AspNetCore.Tests/Attributes/IPAllowListAttributeTest.cs b/test/AMWD.Common.AspNetCore.Tests/Attributes/IPAllowListAttributeTest.cs index 0cf7f04..38d116f 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Attributes/IPAllowListAttributeTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Attributes/IPAllowListAttributeTest.cs @@ -46,7 +46,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsTrue(context.Result is StatusCodeResult); Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -69,7 +69,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsTrue(context.Result is StatusCodeResult); Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -85,7 +85,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes // assert Assert.IsNull(context.Result); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } @@ -107,11 +107,11 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsTrue(context.Result is StatusCodeResult); Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } - [DataTestMethod] + [TestMethod] [DataRow("192.168.178.10")] [DataRow("192.168.178.20")] public void ShouldAllowSpecificAddress(string address) @@ -140,7 +140,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); } - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -164,7 +164,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes // assert Assert.IsNull(context.Result); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } @@ -191,11 +191,11 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsTrue(context.Result is StatusCodeResult); Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } - [DataTestMethod] + [TestMethod] [DataRow("192.168.178.10")] [DataRow("192.168.178.20")] public void ShouldAllowSpecificAddressConfig(string address) @@ -227,7 +227,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); } - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -252,7 +252,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsTrue(context.Result is StatusCodeResult); Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } @@ -333,7 +333,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes HttpContext = contextMock.Object, RouteData = routeDataMock.Object, ActionDescriptor = actionDescriptorMock.Object, - }, new List(), new Dictionary(), null); + }, [], new Dictionary(), null); } } } diff --git a/test/AMWD.Common.AspNetCore.Tests/Attributes/IPBlockListAttributeTest.cs b/test/AMWD.Common.AspNetCore.Tests/Attributes/IPBlockListAttributeTest.cs index 837b388..1c33275 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Attributes/IPBlockListAttributeTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Attributes/IPBlockListAttributeTest.cs @@ -43,7 +43,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes // assert Assert.IsNull(context.Result); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -63,7 +63,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes // assert Assert.IsNull(context.Result); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -83,7 +83,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes // assert Assert.IsNull(context.Result); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } @@ -106,11 +106,11 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsTrue(context.Result is StatusCodeResult); Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } - [DataTestMethod] + [TestMethod] [DataRow("192.168.178.10")] [DataRow("192.168.178.20")] public void ShouldBlockSpecificAddress(string address) @@ -139,7 +139,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsNull(context.Result); } - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -163,7 +163,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes // assert Assert.IsNull(context.Result); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } @@ -191,11 +191,11 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsTrue(context.Result is StatusCodeResult); Assert.AreEqual(403, ((StatusCodeResult)context.Result).StatusCode); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } - [DataTestMethod] + [TestMethod] [DataRow("192.168.178.10")] [DataRow("192.168.178.20")] public void ShouldBlockSpecificAddressConfig(string address) @@ -228,7 +228,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes Assert.IsNull(context.Result); } - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(remote, _itemsCallback["RemoteAddress"]); } @@ -251,7 +251,7 @@ namespace AMWD.Common.AspNetCore.Tests.Attributes // assert Assert.IsNull(context.Result); - Assert.AreEqual(1, _itemsCallback.Count); + Assert.HasCount(1, _itemsCallback); Assert.AreEqual(IPAddress.Loopback, _itemsCallback["RemoteAddress"]); } diff --git a/test/AMWD.Common.AspNetCore.Tests/Extensions/HttpContextExtensionsTest.cs b/test/AMWD.Common.AspNetCore.Tests/Extensions/HttpContextExtensionsTest.cs index c12fefa..74af153 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Extensions/HttpContextExtensionsTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Extensions/HttpContextExtensionsTest.cs @@ -110,7 +110,7 @@ namespace AMWD.Common.AspNetCore.Tests.Extensions Assert.AreEqual(_remote, result); } - [DataTestMethod] + [TestMethod] [DataRow("Cf-Connecting-Ip")] [DataRow("X-Real-IP")] [DataRow("X-Forwarded-For")] diff --git a/test/AMWD.Common.AspNetCore.Tests/Extensions/ModelStateDictionaryExtensionsTest.cs b/test/AMWD.Common.AspNetCore.Tests/Extensions/ModelStateDictionaryExtensionsTest.cs index a08ce77..05f5c68 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Extensions/ModelStateDictionaryExtensionsTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Extensions/ModelStateDictionaryExtensionsTest.cs @@ -55,25 +55,23 @@ namespace AMWD.Common.AspNetCore.Tests.Extensions } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNull() { // arrange ModelStateDictionary modelState = null; - // act - modelState.AddModelError(_testModel, m => m.SubModel.SubValueB, "ShitHappens"); + // act & assert + Assert.ThrowsExactly(() => modelState.AddModelError(_testModel, m => m.SubModel.SubValueB, "ShitHappens")); } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] public void ShouldThrowInvalidOperation() { // arrange var modelState = new ModelStateDictionary(); // act - modelState.AddModelError(_testModel, m => m, "ShitHappens"); + Assert.ThrowsExactly(() => modelState.AddModelError(_testModel, m => m, "ShitHappens")); } internal class TestModel diff --git a/test/AMWD.Common.AspNetCore.Tests/Extensions/SessionExtensionsTest.cs b/test/AMWD.Common.AspNetCore.Tests/Extensions/SessionExtensionsTest.cs index ce2b0a9..d67c0a4 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Extensions/SessionExtensionsTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Extensions/SessionExtensionsTest.cs @@ -113,8 +113,8 @@ namespace AMWD.Common.AspNetCore.Tests.Extensions // assert Assert.IsNotNull(result); - Assert.AreEqual(null, result.ValueA); - Assert.AreEqual(null, result.ValueB); + Assert.IsNull(result.ValueA); + Assert.IsNull(result.ValueB); } [TestMethod] diff --git a/test/AMWD.Common.AspNetCore.Tests/Security/BasicAuthentication/BasicAuthenticationMiddlewareTest.cs b/test/AMWD.Common.AspNetCore.Tests/Security/BasicAuthentication/BasicAuthenticationMiddlewareTest.cs index 3caad25..ca2a779 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Security/BasicAuthentication/BasicAuthenticationMiddlewareTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Security/BasicAuthentication/BasicAuthenticationMiddlewareTest.cs @@ -57,8 +57,8 @@ namespace AMWD.Common.AspNetCore.Tests.Security.BasicAuthentication // assert Assert.AreEqual(0, _responseStatusCodeCallback); // not triggered - Assert.AreEqual(0, _responseHeadersCallback.Count); - Assert.AreEqual(1, _validatorCallback.Count); + Assert.IsEmpty(_responseHeadersCallback); + Assert.HasCount(1, _validatorCallback); Assert.AreEqual(username, _validatorCallback.First().username); Assert.AreEqual(password, _validatorCallback.First().password); @@ -78,9 +78,9 @@ namespace AMWD.Common.AspNetCore.Tests.Security.BasicAuthentication // assert Assert.AreEqual(401, _responseStatusCodeCallback); - Assert.AreEqual(0, _validatorCallback.Count); + Assert.IsEmpty(_validatorCallback); - Assert.AreEqual(1, _responseHeadersCallback.Count); + Assert.HasCount(1, _responseHeadersCallback); Assert.AreEqual("WWW-Authenticate", _responseHeadersCallback.Keys.First()); Assert.AreEqual("Basic", _responseHeadersCallback.Values.First()); } @@ -106,11 +106,11 @@ namespace AMWD.Common.AspNetCore.Tests.Security.BasicAuthentication // assert Assert.AreEqual(401, _responseStatusCodeCallback); - Assert.AreEqual(1, _responseHeadersCallback.Count); + Assert.HasCount(1, _responseHeadersCallback); Assert.AreEqual("WWW-Authenticate", _responseHeadersCallback.Keys.First()); Assert.AreEqual($"Basic realm=\"{_validatorRealm}\"", _responseHeadersCallback.Values.First()); - Assert.AreEqual(1, _validatorCallback.Count); + Assert.HasCount(1, _validatorCallback); Assert.AreEqual(username, _validatorCallback.First().username); Assert.AreEqual(password, _validatorCallback.First().password); Assert.AreEqual(remote, _validatorCallback.First().ipAddr); diff --git a/test/AMWD.Common.AspNetCore.Tests/Utilities/HtmlHelperTest.cs b/test/AMWD.Common.AspNetCore.Tests/Utilities/HtmlHelperTest.cs index ac718db..d859405 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Utilities/HtmlHelperTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Utilities/HtmlHelperTest.cs @@ -7,32 +7,30 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities public class HtmlHelperTest { [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] - public void ShouldThrowErrorOnEmptyColor() + [DataRow(null)] + [DataRow("")] + [DataRow(" ")] + public void ShouldThrowErrorOnEmptyColor(string color) + { + // arrange + + // act & assert + Assert.ThrowsExactly(() => HtmlHelper.IsDarkColor(color)); + } + + [TestMethod] + public void ShouldThrowErrorOnUnsupportedColor() { // arrange // act - HtmlHelper.IsDarkColor(""); + Assert.ThrowsExactly(() => HtmlHelper.IsDarkColor("hsv(1, 2, 3)")); // assert // exception thrown } [TestMethod] - [ExpectedException(typeof(NotSupportedException))] - public void ShouldThrowErrorOnUnsupportedColor() - { - // arrange - - // act - HtmlHelper.IsDarkColor("hsv(1, 2, 3)"); - - // assert - // exception thrown - } - - [DataTestMethod] [DataRow("rgb(255, 255, 255)")] [DataRow("rgba(255, 255, 255, .5)")] public void ShouldReturnLightColorForWhiteRgb(string white) @@ -46,7 +44,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities Assert.IsFalse(isDark); } - [DataTestMethod] + [TestMethod] [DataRow("#ffFFff")] [DataRow("FFffFF")] public void ShouldReturnLightColorForWhiteFullHex(string white) @@ -60,7 +58,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities Assert.IsFalse(isDark); } - [DataTestMethod] + [TestMethod] [DataRow("#fFf")] [DataRow("FfF")] public void ShouldReturnLightColorForWhiteShortHex(string white) @@ -74,7 +72,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities Assert.IsFalse(isDark); } - [DataTestMethod] + [TestMethod] [DataRow("rgb(0, 0, 0)")] [DataRow("rgba(0, 0, 0, .5)")] public void ShouldReturnDarkColorForBlackRgb(string black) @@ -88,7 +86,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities Assert.IsTrue(isDark); } - [DataTestMethod] + [TestMethod] [DataRow("#000000")] [DataRow("000000")] public void ShouldReturnDarkColorForBlackFullHex(string black) @@ -102,7 +100,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities Assert.IsTrue(isDark); } - [DataTestMethod] + [TestMethod] [DataRow("#000")] [DataRow("000")] public void ShouldReturnDarkColorForBlackShortHex(string black) @@ -116,7 +114,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities Assert.IsTrue(isDark); } - [DataTestMethod] + [TestMethod] [DataRow("rgb(255, 88, 0)")] [DataRow("rgb(0, 218, 0)")] [DataRow("rgb(0, 168, 255)")] @@ -133,7 +131,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities Assert.IsFalse(isDark); } - [DataTestMethod] + [TestMethod] [DataRow("rgb(253, 88, 0)")] [DataRow("rgb(0, 217, 0)")] [DataRow("rgb(0, 168, 253)")] diff --git a/test/AMWD.Common.AspNetCore.Tests/Utilities/PasswordHelperTest.cs b/test/AMWD.Common.AspNetCore.Tests/Utilities/PasswordHelperTest.cs index 7c8838f..246d6ca 100644 --- a/test/AMWD.Common.AspNetCore.Tests/Utilities/PasswordHelperTest.cs +++ b/test/AMWD.Common.AspNetCore.Tests/Utilities/PasswordHelperTest.cs @@ -42,7 +42,7 @@ namespace AMWD.Common.AspNetCore.Tests.Utilities string hash = PasswordHelper.HashPassword(password); // assert - Assert.IsTrue(!string.IsNullOrWhiteSpace(hash)); + Assert.IsFalse(string.IsNullOrWhiteSpace(hash)); } [TestMethod] diff --git a/test/AMWD.Common.Tests/AMWD.Common.Tests.csproj b/test/AMWD.Common.Tests/AMWD.Common.Tests.csproj index f8301d0..d1fd1e9 100644 --- a/test/AMWD.Common.Tests/AMWD.Common.Tests.csproj +++ b/test/AMWD.Common.Tests/AMWD.Common.Tests.csproj @@ -4,6 +4,10 @@ net8.0 + + + + diff --git a/test/AMWD.Common.Tests/Cli/CommandLineParserTest.cs b/test/AMWD.Common.Tests/Cli/CommandLineParserTest.cs index 21c88ca..d2b2d3d 100644 --- a/test/AMWD.Common.Tests/Cli/CommandLineParserTest.cs +++ b/test/AMWD.Common.Tests/Cli/CommandLineParserTest.cs @@ -19,7 +19,7 @@ namespace AMWD.Common.Tests.Cli // assert Assert.IsNotNull(result); - Assert.AreEqual(9, result.Length); + Assert.HasCount(9, result); Assert.AreEqual("Option1", result[0]); Assert.AreEqual("Option 2", result[1]); @@ -44,7 +44,7 @@ namespace AMWD.Common.Tests.Cli // assert Assert.IsNotNull(args); - Assert.AreEqual(2, args.Length); + Assert.HasCount(2, args); Assert.AreEqual("Option1", args[0]); Assert.AreEqual("Option 2", args[1]); @@ -66,27 +66,27 @@ namespace AMWD.Common.Tests.Cli // assert Assert.IsNotNull(options); - Assert.AreEqual(4, options.Count); + Assert.HasCount(4, options); - Assert.AreEqual(1, options.ElementAt(0).Names.Count); + Assert.HasCount(1, options.ElementAt(0).Names); Assert.AreEqual("opt1", options.ElementAt(0).Names.First()); Assert.AreEqual(0, options.ElementAt(0).ParameterCount); Assert.IsFalse(options.ElementAt(0).IsSingle); Assert.IsFalse(options.ElementAt(0).IsRequired); - Assert.AreEqual(1, options.ElementAt(1).Names.Count); + Assert.HasCount(1, options.ElementAt(1).Names); Assert.AreEqual("opt2", options.ElementAt(1).Names.First()); Assert.AreEqual(1, options.ElementAt(1).ParameterCount); Assert.IsFalse(options.ElementAt(1).IsSingle); Assert.IsFalse(options.ElementAt(1).IsRequired); - Assert.AreEqual(1, options.ElementAt(2).Names.Count); + Assert.HasCount(1, options.ElementAt(2).Names); Assert.AreEqual("opt3", options.ElementAt(2).Names.First()); Assert.AreEqual(2, options.ElementAt(2).ParameterCount); Assert.IsTrue(options.ElementAt(2).IsSingle); Assert.IsTrue(options.ElementAt(2).IsRequired); - Assert.AreEqual(2, options.ElementAt(3).Names.Count); + Assert.HasCount(2, options.ElementAt(3).Names); Assert.AreEqual("opt4", options.ElementAt(3).Names.First()); Assert.AreEqual("option4", options.ElementAt(3).Names.Last()); Assert.AreEqual(0, options.ElementAt(3).ParameterCount); @@ -115,45 +115,45 @@ namespace AMWD.Common.Tests.Cli // assert Assert.IsFalse(opt.IsSet); - Assert.AreEqual(7, parser.Arguments.Length); - Assert.AreEqual(2, parser.FreeArguments.Length); + Assert.HasCount(7, parser.Arguments); + Assert.HasCount(2, parser.FreeArguments); Assert.AreEqual("foo", parser.FreeArguments.First()); Assert.AreEqual("bar", parser.FreeArguments.Last()); Assert.IsTrue(parser.Arguments.ElementAt(0).Option.IsSet); Assert.IsNull(parser.Arguments.ElementAt(0).Option.Value); - Assert.AreEqual(0, parser.Arguments.ElementAt(0).Values.Length); + Assert.IsEmpty(parser.Arguments.ElementAt(0).Values); Assert.IsTrue(parser.Arguments.ElementAt(1).Option.IsSet); Assert.AreEqual("two", parser.Arguments.ElementAt(1).Option.Value); - Assert.AreEqual(1, parser.Arguments.ElementAt(1).Values.Length); + Assert.HasCount(1, parser.Arguments.ElementAt(1).Values); Assert.AreEqual("two", parser.Arguments.ElementAt(1).Values.First()); Assert.IsTrue(parser.Arguments.ElementAt(2).Option.IsSet); Assert.AreEqual("three1", parser.Arguments.ElementAt(2).Option.Value); - Assert.AreEqual(2, parser.Arguments.ElementAt(2).Values.Length); + Assert.HasCount(2, parser.Arguments.ElementAt(2).Values); Assert.AreEqual("three1", parser.Arguments.ElementAt(2).Values.First()); Assert.AreEqual("three2", parser.Arguments.ElementAt(2).Values.Last()); Assert.IsTrue(parser.Arguments.ElementAt(3).Option.IsSet); Assert.AreEqual("four", parser.Arguments.ElementAt(3).Option.Value); - Assert.AreEqual(1, parser.Arguments.ElementAt(3).Values.Length); + Assert.HasCount(1, parser.Arguments.ElementAt(3).Values); Assert.AreEqual("four", parser.Arguments.ElementAt(3).Values.First()); Assert.IsTrue(parser.Arguments.ElementAt(4).Option.IsSet); Assert.AreEqual("testing", parser.Arguments.ElementAt(4).Option.Names.First()); Assert.AreEqual("done", parser.Arguments.ElementAt(4).Option.Value); - Assert.AreEqual(1, parser.Arguments.ElementAt(4).Values.Length); + Assert.HasCount(1, parser.Arguments.ElementAt(4).Values); Assert.AreEqual("done", parser.Arguments.ElementAt(4).Values.First()); Assert.IsNull(parser.Arguments.ElementAt(5).Option); Assert.AreEqual("foo", parser.Arguments.ElementAt(5).Value); - Assert.AreEqual(1, parser.Arguments.ElementAt(5).Values.Length); + Assert.HasCount(1, parser.Arguments.ElementAt(5).Values); Assert.IsNull(parser.Arguments.ElementAt(6).Option); Assert.AreEqual("bar", parser.Arguments.ElementAt(6).Value); - Assert.AreEqual(1, parser.Arguments.ElementAt(6).Values.Length); + Assert.HasCount(1, parser.Arguments.ElementAt(6).Values); } [TestMethod] @@ -193,28 +193,23 @@ namespace AMWD.Common.Tests.Cli var opts = parser.SetOptions; // assert - Assert.AreEqual(2, opts.Length); + Assert.HasCount(2, opts); Assert.AreEqual("opt1", opts.First().Names.First()); Assert.AreEqual("opt3", opts.Last().Names.First()); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowExceptionOnNullArgs() { string[] args = null; var parser = new CommandLineParser(); - // act - parser.Parse(args); - - // assert - ArgumentNullException - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => parser.Parse(args)); } [TestMethod] - [ExpectedException(typeof(Exception))] public void ShouldThrowExceptionOnMultipleAutocomplete() { // arrange @@ -226,15 +221,11 @@ namespace AMWD.Common.Tests.Cli parser.RegisterOption("Option1", 1); parser.RegisterOption("Option2", 1); - // act - parser.Parse(args); - - // assert - Exception - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => parser.Parse(args)); } [TestMethod] - [ExpectedException(typeof(Exception))] public void ShouldThrowExceptionOnMissingOption() { // arrange @@ -246,15 +237,11 @@ namespace AMWD.Common.Tests.Cli parser.RegisterOption("Opt1", 1); parser.RegisterOption("Opt2", 1); - // act - parser.Parse(args); - - // assert - Exception - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => parser.Parse(args)); } [TestMethod] - [ExpectedException(typeof(Exception))] public void ShouldTrhowExceptionOnDuplicateOption() { // arrange @@ -262,15 +249,11 @@ namespace AMWD.Common.Tests.Cli var parser = new CommandLineParser(); parser.RegisterOption("opt", 1).Single(); - // act - parser.Parse(args); - - // assert - Exception - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => parser.Parse(args)); } [TestMethod] - [ExpectedException(typeof(Exception))] public void ShouldThrowExceptionOnMissingArgument() { // arrange @@ -278,15 +261,11 @@ namespace AMWD.Common.Tests.Cli var parser = new CommandLineParser(); parser.RegisterOption("option", 1); - // act - parser.Parse(args); - - // assert - Exception - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => parser.Parse(args)); } [TestMethod] - [ExpectedException(typeof(Exception))] public void ShouldThrowExceptionForMissingRequiredOption() { // arrange @@ -295,11 +274,8 @@ namespace AMWD.Common.Tests.Cli parser.RegisterOption("opt").Required(); parser.RegisterOption("foo").Required(); - // act - parser.Parse(args); - - // assert - Exception - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => parser.Parse(args)); } } } diff --git a/test/AMWD.Common.Tests/Cli/EnumerableWalkerTest.cs b/test/AMWD.Common.Tests/Cli/EnumerableWalkerTest.cs index 1e76d1c..a6395a3 100644 --- a/test/AMWD.Common.Tests/Cli/EnumerableWalkerTest.cs +++ b/test/AMWD.Common.Tests/Cli/EnumerableWalkerTest.cs @@ -16,16 +16,12 @@ namespace AMWD.Common.Tests.Cli } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowExceptionOnNullReference() { // arrange - // act - _ = new EnumerableWalker(null); - - // assert - ArgumentNullException - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => new EnumerableWalker(null)); } [TestMethod] @@ -76,7 +72,7 @@ namespace AMWD.Common.Tests.Cli public void ShouldReturnDefaultWhenNothingLeft() { // arrange - var walker = new EnumerableWalker(Array.Empty()); + var walker = new EnumerableWalker([]); _ = walker.GetEnumerator(); // act diff --git a/test/AMWD.Common.Tests/Comparer/DomainComparerTest.cs b/test/AMWD.Common.Tests/Comparer/DomainComparerTest.cs index a2a3ee6..3686ffb 100644 --- a/test/AMWD.Common.Tests/Comparer/DomainComparerTest.cs +++ b/test/AMWD.Common.Tests/Comparer/DomainComparerTest.cs @@ -5,7 +5,7 @@ namespace AMWD.Common.Tests.Comparer [TestClass] public class DomainComparerTest { - [DataTestMethod] + [TestMethod] [DataRow("int", "internal")] [DataRow("int", "dom.int")] [DataRow("a.ins", "a.int")] @@ -22,7 +22,7 @@ namespace AMWD.Common.Tests.Comparer Assert.AreEqual(-1, result); } - [DataTestMethod] + [TestMethod] [DataRow("internal", "int")] [DataRow("dom.int", "int")] [DataRow("a.int", "a.ins")] @@ -39,7 +39,7 @@ namespace AMWD.Common.Tests.Comparer Assert.AreEqual(1, result); } - [DataTestMethod] + [TestMethod] [DataRow("internal", "internal")] [DataRow("dom.int", "dom.int")] [DataRow("a.internal", "a.internal")] diff --git a/test/AMWD.Common.Tests/Comparer/IPAddressComparerTest.cs b/test/AMWD.Common.Tests/Comparer/IPAddressComparerTest.cs index 4ec5da1..dec0352 100644 --- a/test/AMWD.Common.Tests/Comparer/IPAddressComparerTest.cs +++ b/test/AMWD.Common.Tests/Comparer/IPAddressComparerTest.cs @@ -6,7 +6,7 @@ namespace AMWD.Common.Tests.Comparer [TestClass] public class IPAddressComparerTest { - [DataTestMethod] + [TestMethod] [DataRow("127.0.0.0", "127.0.0.1")] [DataRow("fe80::", "fe80::1")] [DataRow("::ffff:7f00:0", "127.0.0.1")] @@ -22,7 +22,7 @@ namespace AMWD.Common.Tests.Comparer Assert.AreEqual(-1, result); } - [DataTestMethod] + [TestMethod] [DataRow("127.0.0.1", "127.0.0.0")] [DataRow("fe80::1", "fe80::")] [DataRow("::ffff:7f00:1", "127.0.0.0")] @@ -38,7 +38,7 @@ namespace AMWD.Common.Tests.Comparer Assert.AreEqual(1, result); } - [DataTestMethod] + [TestMethod] [DataRow("127.0.0.1", "127.0.0.1")] [DataRow("fe80::1", "fe80::1")] [DataRow("::ffff:7f00:1", "127.0.0.1")] diff --git a/test/AMWD.Common.Tests/Comparer/VersionStringComparerTest.cs b/test/AMWD.Common.Tests/Comparer/VersionStringComparerTest.cs index 80dc487..298f4bc 100644 --- a/test/AMWD.Common.Tests/Comparer/VersionStringComparerTest.cs +++ b/test/AMWD.Common.Tests/Comparer/VersionStringComparerTest.cs @@ -5,7 +5,7 @@ namespace AMWD.Common.Tests.Comparer [TestClass] public class VersionStringComparerTest { - [DataTestMethod] + [TestMethod] [DataRow(null, "0")] [DataRow("", "0")] [DataRow("0", "1")] @@ -25,7 +25,7 @@ namespace AMWD.Common.Tests.Comparer Assert.AreEqual(-1, result); } - [DataTestMethod] + [TestMethod] [DataRow("0", null)] [DataRow("0", "")] [DataRow("1", "0")] @@ -45,7 +45,7 @@ namespace AMWD.Common.Tests.Comparer Assert.AreEqual(1, result); } - [DataTestMethod] + [TestMethod] [DataRow(null, null)] [DataRow("", "")] [DataRow("1", "1")] diff --git a/test/AMWD.Common.Tests/Extensions/CollectionExtensionsTest.cs b/test/AMWD.Common.Tests/Extensions/CollectionExtensionsTest.cs index b49c7af..8cf485b 100644 --- a/test/AMWD.Common.Tests/Extensions/CollectionExtensionsTest.cs +++ b/test/AMWD.Common.Tests/Extensions/CollectionExtensionsTest.cs @@ -11,19 +11,19 @@ namespace AMWD.Common.Tests.Extensions { // Arrange var item = new TestItem { Number = 10, Text = "Ten" }; - ICollection list = new List - { + ICollection list = + [ new() { Number = 1, Text = "One" } - }; + ]; // Act list.AddIfNotNull(item); // Assert - Assert.AreEqual(2, list.Count); + Assert.HasCount(2, list); } [TestMethod] @@ -31,105 +31,95 @@ namespace AMWD.Common.Tests.Extensions { // Arrange TestItem item = null; - ICollection list = new List - { + ICollection list = + [ new() { Number = 1, Text = "One" } - }; + ]; // Act list.AddIfNotNull(item); // Assert - Assert.AreEqual(1, list.Count); + Assert.HasCount(1, list); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionForNullList() { // Arrange var item = new TestItem { Number = 10, Text = "Ten" }; ICollection list = null; - // Act - list.AddIfNotNull(item); - - // Assert - ArgumentNullException + // Act & Assert + Assert.ThrowsExactly(() => list.AddIfNotNull(item)); } [TestMethod] public void ShouldAddRange() { // Arrange - ICollection items = new List - { + ICollection items = + [ new() { Number = 10, Text = "Ten" }, new() { Number = 11, Text = "Eleven" }, - }; - ICollection list = new List - { + ]; + ICollection list = + [ new() { Number = 1, Text = "One" }, - }; - - // Act - list.AddRange(items); + // Act + .. items, + ]; // Assert - Assert.AreEqual(3, list.Count); + Assert.HasCount(3, list); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionForList() { // Arrange - ICollection items = new List - { + ICollection items = + [ new() { Number = 10, Text = "Ten" }, new() { Number = 11, Text = "Eleven" }, - }; + ]; ICollection list = null; - // Act - list.AddRange(items); - - // Assert - ArgumentNullException + // Act & Assert + Assert.ThrowsExactly(() => list.AddRange(items)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionForItems() { // Arrange ICollection items = null; - ICollection list = new List - { + ICollection list = + [ new() { Number = 1, Text = "One" }, - }; + ]; - // Act - list.AddRange(items); - - // Assert - ArgumentNullException + // Act & Assert + Assert.ThrowsExactly(() => list.AddRange(items)); } [TestMethod] public void ShouldNotAddRange() { // Arrange - ICollection list = new List - { + ICollection list = + [ new() { Number = 1, Text = "One" }, - }; + ]; // Act list.AddRange(list); // Assert - Assert.AreEqual(1, list.Count); + Assert.HasCount(1, list); } private class TestItem diff --git a/test/AMWD.Common.Tests/Extensions/ReaderWriterLockSlimExtensionsTest.cs b/test/AMWD.Common.Tests/Extensions/ReaderWriterLockSlimExtensionsTest.cs index 421491d..9d48df2 100644 --- a/test/AMWD.Common.Tests/Extensions/ReaderWriterLockSlimExtensionsTest.cs +++ b/test/AMWD.Common.Tests/Extensions/ReaderWriterLockSlimExtensionsTest.cs @@ -7,6 +7,8 @@ namespace AMWD.Common.Tests.Extensions [TestClass] public class ReaderWriterLockSlimExtensionsTest { + public TestContext TestContext { get; set; } + [TestMethod] public void ShouldEnterReadLock() { @@ -151,8 +153,8 @@ namespace AMWD.Common.Tests.Extensions } catch (Exception) { /* keep it quiet */ } - }); - awaitableTask.Wait(); + }, TestContext.CancellationToken); + awaitableTask.Wait(TestContext.CancellationToken); // assert Assert.IsTrue(isTimeout); @@ -180,8 +182,8 @@ namespace AMWD.Common.Tests.Extensions } catch (Exception) { /* keep it quiet */ } - }); - awaitableTask.Wait(); + }, TestContext.CancellationToken); + awaitableTask.Wait(TestContext.CancellationToken); // assert Assert.IsTrue(isTimeout); @@ -209,8 +211,8 @@ namespace AMWD.Common.Tests.Extensions } catch (Exception) { /* keep it quiet */ } - }); - awaitableTask.Wait(); + }, TestContext.CancellationToken); + awaitableTask.Wait(TestContext.CancellationToken); // assert Assert.IsTrue(isTimeout); diff --git a/test/AMWD.Common.Tests/Extensions/StreamExtensionsTest.cs b/test/AMWD.Common.Tests/Extensions/StreamExtensionsTest.cs index 8df5061..17a1c11 100644 --- a/test/AMWD.Common.Tests/Extensions/StreamExtensionsTest.cs +++ b/test/AMWD.Common.Tests/Extensions/StreamExtensionsTest.cs @@ -8,6 +8,8 @@ namespace AMWD.Common.Tests.Extensions [TestClass] public class StreamExtensionsTest { + public TestContext TestContext { get; set; } + [TestMethod] public void ShouldReadLineFromStreamSynchronous() { @@ -69,7 +71,7 @@ namespace AMWD.Common.Tests.Extensions var stream = new MemoryStream(buffer); // act - string line = await stream.ReadLineAsync(); + string line = await stream.ReadLineAsync(cancellationToken: TestContext.CancellationToken); // assert Assert.AreEqual("First Line", line); @@ -85,7 +87,7 @@ namespace AMWD.Common.Tests.Extensions var stream = new MemoryStream(buffer); // act - string line = await stream.ReadLineAsync(); + string line = await stream.ReadLineAsync(cancellationToken: TestContext.CancellationToken); // assert Assert.AreEqual("Single Line", line); @@ -100,7 +102,7 @@ namespace AMWD.Common.Tests.Extensions var stream = new WriteOnlyStream(); // act - string line = await stream.ReadLineAsync(); + string line = await stream.ReadLineAsync(cancellationToken: TestContext.CancellationToken); // assert Assert.IsNull(line); diff --git a/test/AMWD.Common.Tests/Extensions/StringExtensionsTest.cs b/test/AMWD.Common.Tests/Extensions/StringExtensionsTest.cs index 87f718a..2818959 100644 --- a/test/AMWD.Common.Tests/Extensions/StringExtensionsTest.cs +++ b/test/AMWD.Common.Tests/Extensions/StringExtensionsTest.cs @@ -9,16 +9,13 @@ namespace AMWD.Common.Tests.Extensions public class StringExtensionsTest { [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void ShouldThrowArgumentNullExceptionWhenNull() { // arrange string hex = null; - // act - var bytes = hex.HexToBytes(); - - // assert - ArgumentNullException + // act & assert + Assert.ThrowsExactly(() => hex.HexToBytes()); } [TestMethod] @@ -35,19 +32,16 @@ namespace AMWD.Common.Tests.Extensions Assert.IsFalse(bytes.Any()); } - [DataTestMethod] + [TestMethod] [DataRow("aff", null)] [DataRow("de:ad:be:e", ":")] [DataRow("hell", "")] - [ExpectedException(typeof(FormatException))] public void ShouldThrowFormatExceptionWhenInvalid(string hex, string delimiter) { // arrange - // act - var bytes = hex.HexToBytes(delimiter); - - // assert - FormatException + // act & assert + Assert.ThrowsExactly(() => hex.HexToBytes(delimiter)); } [TestMethod] @@ -308,8 +302,8 @@ namespace AMWD.Common.Tests.Extensions var nameserver = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 53); // act - bool valid = validEmail.IsValidEmailAddress(new[] { nameserver }); - bool invalid = !invalidEmail.IsValidEmailAddress(new[] { nameserver }); + bool valid = validEmail.IsValidEmailAddress([nameserver]); + bool invalid = !invalidEmail.IsValidEmailAddress([nameserver]); // assert Assert.IsTrue(valid); @@ -349,7 +343,7 @@ namespace AMWD.Common.Tests.Extensions // assert Assert.AreEqual($"{value}\r{value}\r", sb.ToString()); - Assert.IsFalse(sb.ToString().Contains('\n')); + Assert.DoesNotContain('\n', sb.ToString()); } } } diff --git a/test/AMWD.Common.Tests/Logging/FileLoggerTest.cs b/test/AMWD.Common.Tests/Logging/FileLoggerTest.cs index 39dea90..5815480 100644 --- a/test/AMWD.Common.Tests/Logging/FileLoggerTest.cs +++ b/test/AMWD.Common.Tests/Logging/FileLoggerTest.cs @@ -83,51 +83,39 @@ namespace AMWD.Common.Tests.Logging } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public void ShouldThrowDisposedOnIsEnabled() { // arrange var logger = GetFileLogger(); - - // act logger.Dispose(); - logger.IsEnabled(LogLevel.Error); - // assert - ObjectDisposedException - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => logger.IsEnabled(LogLevel.Error)); } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public void ShouldThrowDisposedOnLog() { // arrange var logger = GetFileLogger(); - - // act logger.Dispose(); - logger.Log(LogLevel.None, "Some Message"); - // assert - ObjectDisposedException - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => logger.Log(LogLevel.None, "Some Message")); } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public void ShouldThrowDisposedOnBeginScope() { // arrange var logger = GetFileLogger(); - - // act logger.Dispose(); - logger.BeginScope("foo"); - // assert - ObjectDisposedException - Assert.Fail(); + // act & assert + Assert.ThrowsExactly(() => logger.BeginScope("foo")); } - [DataTestMethod] + [TestMethod] [DataRow(LogLevel.Trace, false)] [DataRow(LogLevel.Debug, false)] [DataRow(LogLevel.Information, false)] diff --git a/test/AMWD.Common.Tests/Utilities/AsyncQueueTest.cs b/test/AMWD.Common.Tests/Utilities/AsyncQueueTest.cs index 1d67706..143cc1a 100644 --- a/test/AMWD.Common.Tests/Utilities/AsyncQueueTest.cs +++ b/test/AMWD.Common.Tests/Utilities/AsyncQueueTest.cs @@ -8,6 +8,8 @@ namespace AMWD.Common.Tests.Utilities [TestClass] public class AsyncQueueTest { + public TestContext TestContext { get; set; } + private Queue _internalQueue; private TestElement _queueElement1; @@ -52,7 +54,7 @@ namespace AMWD.Common.Tests.Utilities queue.Enqueue(element); // assert - Assert.AreEqual(1, _internalQueue.Count); + Assert.HasCount(1, _internalQueue); Assert.AreEqual(_internalQueue.Count, queue.Count); } @@ -69,11 +71,11 @@ namespace AMWD.Common.Tests.Utilities // act var task = Task.Run(async () => { - await queue.WaitAsync(); + await queue.WaitAsync(TestContext.CancellationToken); available = true; - }); + }, TestContext.CancellationToken); queue.Enqueue(element); - task.Wait(); + task.Wait(TestContext.CancellationToken); // assert Assert.IsTrue(available); @@ -92,10 +94,10 @@ namespace AMWD.Common.Tests.Utilities // act var task = Task.Run(async () => { - callback = await queue.DequeueAsync(); - }); + callback = await queue.DequeueAsync(TestContext.CancellationToken); + }, TestContext.CancellationToken); queue.Enqueue(element); - task.Wait(); + task.Wait(TestContext.CancellationToken); // assert Assert.IsNotNull(callback); @@ -118,8 +120,8 @@ namespace AMWD.Common.Tests.Utilities queue.Enqueue(elements); // assert - Assert.AreEqual(2, _internalQueue.Count); - Assert.AreEqual(queue.Count, _internalQueue.Count); + Assert.HasCount(2, _internalQueue); + Assert.HasCount(queue.Count, _internalQueue); } [TestMethod] @@ -229,12 +231,12 @@ namespace AMWD.Common.Tests.Utilities var task = Task.Run(async () => { - await Task.Delay(1000); - queue.Enqueue(new[] { _queueElement1, _queueElement2, _queueElement3 }); - }); + await Task.Delay(1000, TestContext.CancellationToken); + queue.Enqueue([_queueElement1, _queueElement2, _queueElement3]); + }, TestContext.CancellationToken); // act - var item = await queue.DequeueAsync(); + var item = await queue.DequeueAsync(TestContext.CancellationToken); // assert Assert.AreEqual(2, queue.Count); @@ -251,17 +253,17 @@ namespace AMWD.Common.Tests.Utilities var task = Task.Run(async () => { - await Task.Delay(1000); - queue.Enqueue(new[] { _queueElement1, _queueElement2, _queueElement3 }); - }); + await Task.Delay(1000, TestContext.CancellationToken); + queue.Enqueue([_queueElement1, _queueElement2, _queueElement3]); + }, TestContext.CancellationToken); // act - var items = await queue.DequeueManyAsync(2); + var items = await queue.DequeueManyAsync(2, TestContext.CancellationToken); // assert Assert.AreEqual(1, queue.Count); Assert.IsNotNull(items); - Assert.AreEqual(2, items.Length); + Assert.HasCount(2, items); Assert.AreEqual(_queueElement1, items[0]); Assert.AreEqual(_queueElement2, items[1]); } @@ -275,17 +277,17 @@ namespace AMWD.Common.Tests.Utilities var task = Task.Run(async () => { - await Task.Delay(1000); - queue.Enqueue(new[] { _queueElement1, _queueElement2, _queueElement3 }); - }); + await Task.Delay(1000, TestContext.CancellationToken); + queue.Enqueue([_queueElement1, _queueElement2, _queueElement3]); + }, TestContext.CancellationToken); // act - var items = await queue.DequeueAvailableAsync(); + var items = await queue.DequeueAvailableAsync(cancellationToken: TestContext.CancellationToken); // assert Assert.AreEqual(0, queue.Count); Assert.IsNotNull(items); - Assert.AreEqual(3, items.Length); + Assert.HasCount(3, items); Assert.AreEqual(_queueElement1, items[0]); Assert.AreEqual(_queueElement2, items[1]); Assert.AreEqual(_queueElement3, items[2]); @@ -300,34 +302,30 @@ namespace AMWD.Common.Tests.Utilities var task = Task.Run(async () => { - await Task.Delay(1000); - queue.Enqueue(new[] { _queueElement1, _queueElement2, _queueElement3 }); - }); + await Task.Delay(1000, TestContext.CancellationToken); + queue.Enqueue([_queueElement1, _queueElement2, _queueElement3]); + }, TestContext.CancellationToken); // act - var items = await queue.DequeueAvailableAsync(2); + var items = await queue.DequeueAvailableAsync(2, TestContext.CancellationToken); // assert Assert.AreEqual(1, queue.Count); Assert.IsNotNull(items); - Assert.AreEqual(2, items.Length); + Assert.HasCount(2, items); Assert.AreEqual(_queueElement1, items[0]); Assert.AreEqual(_queueElement2, items[1]); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public async Task ShouldThrowArumentOutOfRangeException() { // arrange _internalQueue.Clear(); var queue = GetQueue(); - // act - await queue.DequeueManyAsync(-2); - - // assert - ArgumentOutOfRangeException expected - Assert.Fail(); + // act & assert + await Assert.ThrowsExactlyAsync(() => queue.DequeueManyAsync(-2, TestContext.CancellationToken)); } private AsyncQueue GetQueue() diff --git a/test/AMWD.Common.Tests/Utilities/CryptographyHelperTest.cs b/test/AMWD.Common.Tests/Utilities/CryptographyHelperTest.cs index e50652c..8d027a0 100644 --- a/test/AMWD.Common.Tests/Utilities/CryptographyHelperTest.cs +++ b/test/AMWD.Common.Tests/Utilities/CryptographyHelperTest.cs @@ -114,8 +114,8 @@ namespace UnitTests.Common.Utilities // assert CollectionAssert.AreNotEqual(cipher1, cipher2); - Assert.AreEqual(24, cipher1.Length); - Assert.AreEqual(24, cipher2.Length); + Assert.HasCount(24, cipher1); + Assert.HasCount(24, cipher2); CollectionAssert.AreEqual(plain, plain1); CollectionAssert.AreEqual(plain, plain2); } @@ -228,8 +228,8 @@ namespace UnitTests.Common.Utilities // assert CollectionAssert.AreNotEqual(cipher1, cipher2); - Assert.AreEqual(16, cipher1.Length); - Assert.AreEqual(16, cipher2.Length); + Assert.HasCount(16, cipher1); + Assert.HasCount(16, cipher2); CollectionAssert.AreEqual(plain, plain1); CollectionAssert.AreEqual(plain, plain2); } @@ -368,11 +368,11 @@ namespace UnitTests.Common.Utilities byte[] bytes3 = CryptographyHelper.GetRandomBytes(length3); // assert - Assert.AreEqual(length1, bytes1.Length); - Assert.AreEqual(length2, bytes2.Length); - Assert.AreEqual(length3, bytes3.Length); + Assert.HasCount(length1, bytes1); + Assert.HasCount(length2, bytes2); + Assert.HasCount(length3, bytes3); - Assert.IsTrue(bytes1.Length == bytes2.Length); + Assert.HasCount(bytes2.Length, bytes1); CollectionAssert.AreNotEqual(bytes1, bytes2); } @@ -394,8 +394,8 @@ namespace UnitTests.Common.Utilities Assert.AreEqual(length2, str2.Length); Assert.AreEqual(length3, str3.Length); - Assert.IsTrue(str1.Length == str2.Length); - Assert.IsFalse(str1 == str2); + Assert.HasCount(str2.Length, str2); + Assert.AreNotEqual(str2, str1); } [TestMethod] @@ -412,7 +412,7 @@ namespace UnitTests.Common.Utilities // assert Assert.AreEqual(length, str1.Length); Assert.AreEqual(length, str2.Length); - Assert.IsFalse(str1 == str2); + Assert.AreNotEqual(str2, str1); Assert.IsFalse(RandomStringWithPoolRegex().IsMatch(str1)); Assert.IsFalse(RandomStringWithPoolRegex().IsMatch(str2)); } @@ -532,7 +532,7 @@ namespace UnitTests.Common.Utilities Assert.IsFalse(fileExistsBefore); Assert.IsNotNull(helper); Assert.IsTrue(fileExistsAfter); - Assert.IsTrue(!string.IsNullOrWhiteSpace(content)); + Assert.IsFalse(string.IsNullOrWhiteSpace(content)); } [TestMethod] diff --git a/test/AMWD.Common.Tests/Utilities/DelayedTaskTest.cs b/test/AMWD.Common.Tests/Utilities/DelayedTaskTest.cs index fb60c5d..06e1a92 100644 --- a/test/AMWD.Common.Tests/Utilities/DelayedTaskTest.cs +++ b/test/AMWD.Common.Tests/Utilities/DelayedTaskTest.cs @@ -9,6 +9,8 @@ namespace AMWD.Common.Tests.Utilities [TestClass] public class DelayedTaskTest { + public TestContext TestContext { get; set; } + [TestMethod] public void ShouldCreateNewDelayedTaskNotStarting() { @@ -113,7 +115,7 @@ namespace AMWD.Common.Tests.Utilities var cts = new CancellationTokenSource(delay.Add(TimeSpan.FromSeconds(1))); sw.Start(); var delayedTask = DelayedTask.Run(Action, delay); - await Task.Delay(50); + await Task.Delay(50, TestContext.CancellationToken); delayedTask.Reset(); SpinWait.SpinUntil(() => executionCount > 0 || cts.IsCancellationRequested); @@ -142,7 +144,7 @@ namespace AMWD.Common.Tests.Utilities var cts = new CancellationTokenSource(delay.Add(TimeSpan.FromSeconds(1))); sw.Start(); var delayedTask = DelayedTask.Create(Action, delay); - await Task.Delay(50); + await Task.Delay(50, TestContext.CancellationToken); bool isSuccess = delayedTask.ExecutePending(); SpinWait.SpinUntil(() => executionCount > 0 || cts.IsCancellationRequested); sw.Stop(); @@ -173,7 +175,7 @@ namespace AMWD.Common.Tests.Utilities var cts = new CancellationTokenSource(delay.Add(TimeSpan.FromSeconds(1))); sw.Start(); var delayedTask = DelayedTask.Run(Action, delay); - await Task.Delay(50); + await Task.Delay(50, TestContext.CancellationToken); bool isSuccess = delayedTask.ExecutePending(); SpinWait.SpinUntil(() => executionCount > 0 || cts.IsCancellationRequested); @@ -205,7 +207,6 @@ namespace AMWD.Common.Tests.Utilities // assert Assert.IsNotNull(delayedTask); - Assert.IsNotNull(awaiter); Assert.IsFalse(delayedTask.IsRunning); Assert.IsFalse(delayedTask.IsWaitingToRun); Assert.IsNull(delayedTask.Exception); diff --git a/test/Directory.Build.props b/test/Directory.Build.props index ade4169..2d00e5f 100644 --- a/test/Directory.Build.props +++ b/test/Directory.Build.props @@ -6,14 +6,14 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + + diff --git a/test/MSTestSettings.cs b/test/MSTestSettings.cs new file mode 100644 index 0000000..b8ff7f7 --- /dev/null +++ b/test/MSTestSettings.cs @@ -0,0 +1 @@ +[assembly: Parallelize(Scope = ExecutionScope.ClassLevel)]