Skip to content

Commit

Permalink
Upgrade to NUnit4.x - Moved assertions to Constraint model where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Feb 13, 2024
1 parent 6a2c2f8 commit 12ce53f
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
<Target Name="TagSources" />
<Target Name="NugetOrg" />
<ItemGroup>
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InterlockLedger.Commons\InterlockLedger.Commons.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,33 @@ public class SingleEnumerableTests
[Test]
public void SingleEnumerableTest() {
var single = new SingleEnumerable<int>(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<IEnumerator<int>>(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<IEnumerator<int>>());
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
49 changes: 49 additions & 0 deletions InterlockLedger.Commons.NUnit.Tests/System/Helpers.cs
Original file line number Diff line number Diff line change
@@ -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<T>(string name, TestDelegate code) where T : ArgumentException {
var ex = Assert.Throws<T>(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;
}
}
30 changes: 19 additions & 11 deletions InterlockLedger.Commons.NUnit.Tests/System/LimitedRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")]
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#nullable enable

using static System.ObjectExtensions;
using static Test.Helpers;

namespace System;

Expand All @@ -58,14 +59,4 @@ private static bool TestRequiredUsing<T>(T? value, string name) where T : class
&& AssertArgumentException<ArgumentNullException>(nameof(value), () => value.RequiredUsing(n => new ArgumentNullException(n, _expectedExceptionMessageStart)))
&& AssertArgumentException<ArgumentNullException>(nameof(value), () => value.RequiredUsing(ArgNullRequired));

internal const string _expectedExceptionMessageStart = "Required";

internal static bool AssertArgumentException<T>(string name, TestDelegate code) where T : ArgumentException {
var ex = Assert.Throws<T>(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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#nullable enable

using static System.ObjectExtensions;
using static System.ObjectExtensionsTests;
using static Test.Helpers;

namespace System;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
}

0 comments on commit 12ce53f

Please sign in to comment.