From 12ce53faa687112f929ecbe596530e709971fbf7 Mon Sep 17 00:00:00 2001 From: Rafael Teixeira Date: Tue, 13 Feb 2024 10:08:35 -0300 Subject: [PATCH] Upgrade to NUnit4.x - Moved assertions to Constraint model where needed --- ...InterlockLedger.Commons.NUnit.Tests.csproj | 8 ++- .../Types/SingleEnumerableTests.cs | 42 +++++++----- .../System.IO/TextReaderExtensionsTests.cs | 2 +- .../System/Helpers.cs | 49 ++++++++++++++ .../System/LimitedRangeTests.cs | 30 +++++---- .../System/ObjectExtensionsTests.cs | 11 +--- .../System/StringExtensionsTests.cs | 2 +- .../System/StringSuffixExtensionsTests.cs | 66 ++++++++++--------- 8 files changed, 137 insertions(+), 73 deletions(-) create mode 100644 InterlockLedger.Commons.NUnit.Tests/System/Helpers.cs diff --git a/InterlockLedger.Commons.NUnit.Tests/InterlockLedger.Commons.NUnit.Tests.csproj b/InterlockLedger.Commons.NUnit.Tests/InterlockLedger.Commons.NUnit.Tests.csproj index 156e2ab..223bcbe 100644 --- a/InterlockLedger.Commons.NUnit.Tests/InterlockLedger.Commons.NUnit.Tests.csproj +++ b/InterlockLedger.Commons.NUnit.Tests/InterlockLedger.Commons.NUnit.Tests.csproj @@ -10,9 +10,13 @@ - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/InterlockLedger.Commons.NUnit.Tests/System.Collections.Generic/Types/SingleEnumerableTests.cs b/InterlockLedger.Commons.NUnit.Tests/System.Collections.Generic/Types/SingleEnumerableTests.cs index 2043184..033926b 100644 --- a/InterlockLedger.Commons.NUnit.Tests/System.Collections.Generic/Types/SingleEnumerableTests.cs +++ b/InterlockLedger.Commons.NUnit.Tests/System.Collections.Generic/Types/SingleEnumerableTests.cs @@ -38,23 +38,33 @@ public class SingleEnumerableTests [Test] public void SingleEnumerableTest() { var single = new SingleEnumerable(42); - Assert.NotNull(single); - Assert.AreEqual(42, single.First()); - Assert.AreEqual(42, single.Last()); - Assert.AreEqual(1, single.Count()); + Assert.That(single, Is.Not.Null); + Assert.Multiple(() => { + Assert.That(single.First(), Is.EqualTo(42)); + Assert.That(single.Last(), Is.EqualTo(42)); + Assert.That(single.Count(), Is.EqualTo(1)); + }); var enumerator = single.GetEnumerator(); - Assert.NotNull(enumerator); - Assert.IsInstanceOf>(enumerator); - Assert.AreEqual(0, enumerator.Current); - Assert.IsTrue(enumerator.MoveNext()); - Assert.AreEqual(42, enumerator.Current); - Assert.IsFalse(enumerator.MoveNext()); - Assert.AreEqual(0, enumerator.Current); + Assert.That(enumerator, Is.Not.Null); + Assert.That(enumerator, Is.InstanceOf>()); + Assert.Multiple(() => { + Assert.That(enumerator.Current, Is.EqualTo(0)); + Assert.That(enumerator.MoveNext()); + }); + Assert.Multiple(() => { + Assert.That(enumerator.Current, Is.EqualTo(42)); + Assert.That(enumerator.MoveNext(), Is.False); + }); + Assert.That(enumerator.Current, Is.EqualTo(0)); enumerator.Reset(); - Assert.AreEqual(0, enumerator.Current); - Assert.IsTrue(enumerator.MoveNext()); - Assert.AreEqual(42, enumerator.Current); - Assert.IsFalse(enumerator.MoveNext()); - Assert.AreEqual(0, enumerator.Current); + Assert.Multiple(() => { + Assert.That(enumerator.Current, Is.EqualTo(0)); + Assert.That(enumerator.MoveNext()); + }); + Assert.Multiple(() => { + Assert.That(enumerator.Current, Is.EqualTo(42)); + Assert.That(enumerator.MoveNext(), Is.False); + }); + Assert.That(enumerator.Current, Is.EqualTo(0)); } } diff --git a/InterlockLedger.Commons.NUnit.Tests/System.IO/TextReaderExtensionsTests.cs b/InterlockLedger.Commons.NUnit.Tests/System.IO/TextReaderExtensionsTests.cs index f3a7d3f..2acf889 100644 --- a/InterlockLedger.Commons.NUnit.Tests/System.IO/TextReaderExtensionsTests.cs +++ b/InterlockLedger.Commons.NUnit.Tests/System.IO/TextReaderExtensionsTests.cs @@ -46,7 +46,7 @@ public class TextReaderExtensionsTests [TestCase(" -123 ", -123)] [TestCase("2000000000", 2000000000)] [TestCase("-2000000000", -2000000000)] - public void TestReadInt32(string input, int expected) => Assert.AreEqual(expected, new StringReader(input).ReadInt32()); + public void TestReadInt32(string input, int expected) => Assert.That(new StringReader(input).ReadInt32(), Is.EqualTo(expected)); [TestCase("")] [TestCase("1B2C3")] diff --git a/InterlockLedger.Commons.NUnit.Tests/System/Helpers.cs b/InterlockLedger.Commons.NUnit.Tests/System/Helpers.cs new file mode 100644 index 0000000..2822677 --- /dev/null +++ b/InterlockLedger.Commons.NUnit.Tests/System/Helpers.cs @@ -0,0 +1,49 @@ +// ****************************************************************************************************************************** +// +// Copyright (c) 2018-2023 InterlockLedger Network +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// ****************************************************************************************************************************** + +namespace Test; + +internal static class Helpers +{ + internal const string _expectedExceptionMessageStart = "Required"; + + internal static bool AssertArgumentException(string name, TestDelegate code) where T : ArgumentException { + var ex = Assert.Throws(code); + Assert.That(ex, Is.Not.Null); + Assert.Multiple(() => { + Assert.That(ex!.ParamName, Is.EqualTo(name)); + Assert.That(ex.Message, Does.StartWith(_expectedExceptionMessageStart)); + Assert.That(ex.InnerException, Is.Null); + }); + return true; + } +} diff --git a/InterlockLedger.Commons.NUnit.Tests/System/LimitedRangeTests.cs b/InterlockLedger.Commons.NUnit.Tests/System/LimitedRangeTests.cs index f7dcd14..6b0327a 100644 --- a/InterlockLedger.Commons.NUnit.Tests/System/LimitedRangeTests.cs +++ b/InterlockLedger.Commons.NUnit.Tests/System/LimitedRangeTests.cs @@ -38,18 +38,24 @@ public class LimitedRangeTests { [Test] +#pragma warning disable NUnit2009 // The same value has been provided as both the actual and the expected argument public void Equality() { - Assert.AreEqual(LimitedRange.Empty, LimitedRange.Empty); + Assert.That(LimitedRange.Empty, Is.EqualTo(LimitedRange.Empty)); var invalidByCause1 = LimitedRange.InvalidBy("Cause1"); var invalidByCause2 = LimitedRange.InvalidBy("Cause2"); - Assert.AreEqual(invalidByCause1, invalidByCause2); - Assert.AreEqual(invalidByCause2, invalidByCause1); - Assert.AreEqual(new LimitedRange(1, 10), new LimitedRange(1, 10)); - Assert.AreNotEqual(LimitedRange.Empty, invalidByCause1); - Assert.AreNotEqual(invalidByCause1, LimitedRange.Empty); - Assert.AreNotEqual(new LimitedRange(1, 10), new LimitedRange(1, 11)); - Assert.AreNotEqual(new LimitedRange(1, 11), new LimitedRange(1, 10)); + Assert.Multiple(() => { + Assert.That(invalidByCause2, Is.EqualTo(invalidByCause1)); + Assert.That(invalidByCause1, Is.EqualTo(invalidByCause2)); + Assert.That(new LimitedRange(1, 10), Is.EqualTo(new LimitedRange(1, 10))); + }); + Assert.Multiple(() => { + Assert.That(invalidByCause1, Is.Not.EqualTo(LimitedRange.Empty)); + Assert.That(LimitedRange.Empty, Is.Not.EqualTo(invalidByCause1)); + Assert.That(new LimitedRange(1, 11), Is.Not.EqualTo(new LimitedRange(1, 10))); + Assert.That(new LimitedRange(1, 10), Is.Not.EqualTo(new LimitedRange(1, 11))); + }); } +#pragma warning restore NUnit2009 // The same value has been provided as both the actual and the expected argument [TestCase("\"[]\"", false, true, "")] [TestCase("\"[1]\"", false, false, "")] @@ -87,14 +93,16 @@ public void Build(string text, bool isInvalid, bool isEmpty, string? cause) { } private static void AssertLimitedRange(LimitedRange lr, string text, bool isInvalid, bool isEmpty, string? cause = null, bool unwrapped = false) { - Assert.AreEqual(isInvalid, lr.Textual.IsInvalid, nameof(isInvalid)); - Assert.AreEqual(isEmpty, lr.IsEmpty, nameof(isEmpty)); + Assert.Multiple(() => { + Assert.That(lr.Textual.IsInvalid, Is.EqualTo(isInvalid), nameof(isInvalid)); + Assert.That(lr.IsEmpty, Is.EqualTo(isEmpty), nameof(isEmpty)); + }); if (!lr.Textual.IsInvalid && !unwrapped) { Assert.That(lr.TextualRepresentation, Is.EqualTo(text)); string lrAsString = lr; // implicit string conversion Assert.That(lrAsString, Is.EqualTo(text)); } else if (!cause.IsBlank()) - StringAssert.AreEqualIgnoringCase(cause, lr.InvalidityCause); + Assert.That(lr.InvalidityCause, Is.EqualTo(cause).IgnoreCase); TestContext.WriteLine(lr.ToString()); } diff --git a/InterlockLedger.Commons.NUnit.Tests/System/ObjectExtensionsTests.cs b/InterlockLedger.Commons.NUnit.Tests/System/ObjectExtensionsTests.cs index 2310331..5391ce0 100644 --- a/InterlockLedger.Commons.NUnit.Tests/System/ObjectExtensionsTests.cs +++ b/InterlockLedger.Commons.NUnit.Tests/System/ObjectExtensionsTests.cs @@ -33,6 +33,7 @@ #nullable enable using static System.ObjectExtensions; +using static Test.Helpers; namespace System; @@ -58,14 +59,4 @@ private static bool TestRequiredUsing(T? value, string name) where T : class && AssertArgumentException(nameof(value), () => value.RequiredUsing(n => new ArgumentNullException(n, _expectedExceptionMessageStart))) && AssertArgumentException(nameof(value), () => value.RequiredUsing(ArgNullRequired)); - internal const string _expectedExceptionMessageStart = "Required"; - - internal static bool AssertArgumentException(string name, TestDelegate code) where T : ArgumentException { - var ex = Assert.Throws(code); - Assert.That(ex, Is.Not.Null); - Assert.That(ex!.ParamName, Is.EqualTo(name)); - Assert.That(ex.Message, Does.StartWith(_expectedExceptionMessageStart)); - Assert.That(ex.InnerException, Is.Null); - return true; - } } diff --git a/InterlockLedger.Commons.NUnit.Tests/System/StringExtensionsTests.cs b/InterlockLedger.Commons.NUnit.Tests/System/StringExtensionsTests.cs index ae87803..bc103df 100644 --- a/InterlockLedger.Commons.NUnit.Tests/System/StringExtensionsTests.cs +++ b/InterlockLedger.Commons.NUnit.Tests/System/StringExtensionsTests.cs @@ -33,7 +33,7 @@ #nullable enable using static System.ObjectExtensions; -using static System.ObjectExtensionsTests; +using static Test.Helpers; namespace System; diff --git a/InterlockLedger.Commons.NUnit.Tests/System/StringSuffixExtensionsTests.cs b/InterlockLedger.Commons.NUnit.Tests/System/StringSuffixExtensionsTests.cs index 7f17a35..3844086 100644 --- a/InterlockLedger.Commons.NUnit.Tests/System/StringSuffixExtensionsTests.cs +++ b/InterlockLedger.Commons.NUnit.Tests/System/StringSuffixExtensionsTests.cs @@ -36,38 +36,40 @@ namespace System; public class StringSuffixExtensionsTests { [Test] - public void WithSuffix() { - Assert.IsNull(((string?)null).WithSuffix(".json")); - Assert.AreEqual(".json", "".WithSuffix(".json")); - Assert.AreEqual("a.json", "a".WithSuffix(".json")); - Assert.AreEqual("b.JSON", "b.JSON".WithSuffix(".json")); - Assert.AreEqual("c", "c".WithSuffix(null)); - Assert.AreEqual("c", "c".WithSuffix("")); - Assert.AreEqual("c", "c".WithSuffix(" ")); - Assert.AreEqual("d!KKK", "d".WithSuffix("KKK", '!')); - Assert.AreEqual("d!KKK", "d".WithSuffix("!KKK", '!')); - Assert.AreEqual("d!KKK!", "d".WithSuffix("!!KKK!", '!')); - Assert.AreEqual("e#001", "e# ".WithSuffix("001", '#')); - Assert.AreEqual("file.txt", "file ".WithSuffix("txt")); - Assert.AreEqual(".file.txt", ".file ".WithSuffix("txt", '.')); - Assert.AreEqual("file.txt", "file. ".WithSuffix("txt")); - } + public void WithSuffix() => + Assert.Multiple(() => { + Assert.That(((string?)null).WithSuffix(".json"), Is.Null); + Assert.That("".WithSuffix(".json"), Is.EqualTo(".json")); + Assert.That("a".WithSuffix(".json"), Is.EqualTo("a.json")); + Assert.That("b.JSON".WithSuffix(".json"), Is.EqualTo("b.JSON")); + Assert.That("c".WithSuffix(null), Is.EqualTo("c")); + Assert.That("c".WithSuffix(""), Is.EqualTo("c")); + Assert.That("c".WithSuffix(" "), Is.EqualTo("c")); + Assert.That("d".WithSuffix("KKK", '!'), Is.EqualTo("d!KKK")); + Assert.That("d".WithSuffix("!KKK", '!'), Is.EqualTo("d!KKK")); + Assert.That("d".WithSuffix("!!KKK!", '!'), Is.EqualTo("d!KKK!")); + Assert.That("e# ".WithSuffix("001", '#'), Is.EqualTo("e#001")); + Assert.That("file ".WithSuffix("txt"), Is.EqualTo("file.txt")); + Assert.That(".file ".WithSuffix("txt", '.'), Is.EqualTo(".file.txt")); + Assert.That("file. ".WithSuffix("txt"), Is.EqualTo("file.txt")); + }); [Test] - public void WithSuffixReplaced() { - Assert.IsNull(((string?)null).WithSuffixReplaced(".jsonc")); - Assert.AreEqual(".jsonc", "".WithSuffixReplaced(".jsonc")); - Assert.AreEqual("a.jsonc", "a.json".WithSuffixReplaced(".jsonc")); - Assert.AreEqual("b.jsonc", "b.JSONC".WithSuffixReplaced(".jsonc")); - Assert.AreEqual("c.json", "c.json".WithSuffixReplaced(null)); - Assert.AreEqual("c.json", "c.json".WithSuffixReplaced("")); - Assert.AreEqual("c.json", "c.json".WithSuffixReplaced(" ")); - Assert.AreEqual("d!KKK", "d!a".WithSuffixReplaced("KKK", '!')); - Assert.AreEqual("d!KKK", "d!a".WithSuffixReplaced("!KKK", '!')); - Assert.AreEqual("d!KKK!", "d!a".WithSuffixReplaced("!!KKK!", '!')); - Assert.AreEqual("e#001", "e# ".WithSuffixReplaced("001", '#')); - Assert.AreEqual("file.txt", "file.doc ".WithSuffixReplaced("txt")); - Assert.AreEqual(".file.txt", ".file.doc ".WithSuffixReplaced("txt", '.')); - Assert.AreEqual("file.txt", "file. ".WithSuffixReplaced("txt")); - } + public void WithSuffixReplaced() => + Assert.Multiple(() => { + Assert.That(((string?)null).WithSuffixReplaced(".jsonc"), Is.Null); + Assert.That("".WithSuffixReplaced(".jsonc"), Is.EqualTo(".jsonc")); + Assert.That("a.json".WithSuffixReplaced(".jsonc"), Is.EqualTo("a.jsonc")); + Assert.That("b.JSONC".WithSuffixReplaced(".jsonc"), Is.EqualTo("b.jsonc")); + Assert.That("c.json".WithSuffixReplaced(null), Is.EqualTo("c.json")); + Assert.That("c.json".WithSuffixReplaced(""), Is.EqualTo("c.json")); + Assert.That("c.json".WithSuffixReplaced(" "), Is.EqualTo("c.json")); + Assert.That("d!a".WithSuffixReplaced("KKK", '!'), Is.EqualTo("d!KKK")); + Assert.That("d!a".WithSuffixReplaced("!KKK", '!'), Is.EqualTo("d!KKK")); + Assert.That("d!a".WithSuffixReplaced("!!KKK!", '!'), Is.EqualTo("d!KKK!")); + Assert.That("e# ".WithSuffixReplaced("001", '#'), Is.EqualTo("e#001")); + Assert.That("file.doc ".WithSuffixReplaced("txt"), Is.EqualTo("file.txt")); + Assert.That(".file.doc ".WithSuffixReplaced("txt", '.'), Is.EqualTo(".file.txt")); + Assert.That("file. ".WithSuffixReplaced("txt"), Is.EqualTo("file.txt")); + }); }