Skip to content

Commit

Permalink
Upgrade to NUnit 4
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanSchoenfeld committed May 9, 2024
1 parent 4f8a281 commit f6ad131
Show file tree
Hide file tree
Showing 118 changed files with 2,255 additions and 2,078 deletions.
7 changes: 4 additions & 3 deletions src/Hydrogen.NUnit.DB/DACTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using Hydrogen.Data;
using Tools;
using Object = Tools.Object;
using NUnit.Framework.Legacy;

namespace Hydrogen.NUnit {
public abstract class DACTestFixture {
Expand Down Expand Up @@ -110,7 +111,7 @@ protected virtual void DropDatabase(IDAC dac) {
protected virtual void AssertSameTableRowCount(IDAC source, IDAC dest, string tableName) {
var sourceCount = source.Count(tableName);
var destCount = dest.Count(tableName);
Assert.AreEqual(sourceCount, destCount);
ClassicAssert.AreEqual(sourceCount, destCount);
}

// ReSharper disable once InconsistentNaming
Expand Down Expand Up @@ -156,8 +157,8 @@ private void AssertSameTableDataInternal(IDAC source, IDAC dest, string tableNam
var sourceTableSchema = source.GetSchemaCached()[tableName];
var destTableSchema = dest.GetSchemaCached()[tableName];

Assert.AreNotEqual(DBKeyType.None, sourceTableSchema.PrimaryKey.KeyType, "Tables without primary keys are not supported");
Assert.AreEqual(sourceTableSchema.PrimaryKey.KeyType, destTableSchema.PrimaryKey.KeyType, "Table primary key types do not match");
ClassicAssert.AreNotEqual(DBKeyType.None, sourceTableSchema.PrimaryKey.KeyType, "Tables without primary keys are not supported");
ClassicAssert.AreEqual(sourceTableSchema.PrimaryKey.KeyType, destTableSchema.PrimaryKey.KeyType, "Table primary key types do not match");

var sourceData = source.Select(tableName).Rows.Cast<DataRow>().Select(r => r.ItemArray.Select(Object.SanitizeObject).ToArray()).ToArray();
var destData = dest.Select(tableName).Rows.Cast<DataRow>().Select(r => r.ItemArray.Select(Object.SanitizeObject).ToArray()).ToArray();
Expand Down
2 changes: 1 addition & 1 deletion src/Hydrogen.NUnit.DB/Hydrogen.NUnit.DB.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down
39 changes: 20 additions & 19 deletions src/Hydrogen.NUnit/AssertEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace Hydrogen.NUnit;

Expand Down Expand Up @@ -727,7 +728,7 @@ public static void StreamIntegrationTests(int maxSize, Stream actualStream, Stre
expectedStream.Seek(segment.Start, SeekOrigin.Begin);
actualStream.Seek(segment.Start, SeekOrigin.Begin);
if (runAsserts) {
Assert.AreEqual(expectedStream.ReadBytes(count), actualStream.ReadBytes(count));
ClassicAssert.AreEqual(expectedStream.ReadBytes(count), actualStream.ReadBytes(count));
AreEqual(expectedStream, actualStream);
}
extraTest?.Invoke();
Expand Down Expand Up @@ -774,18 +775,18 @@ public static void AreEqual(Stream expected, Stream actual) {


public static void AreEqual(IDynamicMerkleTree expected, IDynamicMerkleTree actual) {
Assert.AreEqual(expected.Size, actual.Size);
Assert.AreEqual(expected.Leafs.Count, actual.Leafs.Count);
Assert.AreEqual(expected.Leafs.ToArray(), actual.Leafs.ToArray());
Assert.AreEqual(expected.Root, actual.Root);
ClassicAssert.AreEqual(expected.Size, actual.Size);
ClassicAssert.AreEqual(expected.Leafs.Count, actual.Leafs.Count);
ClassicAssert.AreEqual(expected.Leafs.ToArray(), actual.Leafs.ToArray());
ClassicAssert.AreEqual(expected.Root, actual.Root);
}

public static void NotHasFlags<T>(T expected, T actual) where T : Enum {
Assert.IsFalse(actual.HasFlag(expected));
ClassicAssert.IsFalse(actual.HasFlag(expected));
}

public static void HasFlags<T>(T expected, T actual) where T : Enum {
Assert.IsTrue(actual.HasFlag(expected));
ClassicAssert.IsTrue(actual.HasFlag(expected));
}

public static void HasAllFlags<T>(T actual, params T[] flags) where T : Enum {
Expand All @@ -796,21 +797,21 @@ public static void HasAllFlags<T>(T actual, params T[] flags) where T : Enum {
public static void RegexMatch(string expected, string regexPattern, params Tuple<string, string>[] expectedCaptures) {
var regex = new Regex(regexPattern);
var match = regex.Match(expected);
Assert.AreEqual(expected, match.Value);
ClassicAssert.AreEqual(expected, match.Value);
foreach (var expectedCapture in expectedCaptures) {
if (expectedCapture.Item2 == null)
Assert.IsFalse(match.Groups[expectedCapture.Item1].Success);
ClassicAssert.IsFalse(match.Groups[expectedCapture.Item1].Success);
else
Assert.AreEqual(expectedCapture.Item2, match.Groups[expectedCapture.Item1]?.Value);
ClassicAssert.AreEqual(expectedCapture.Item2, match.Groups[expectedCapture.Item1]?.Value);
}
}

public static void RegexNotMatch(string badInput, string regexPattern, params Tuple<string, string>[] expectedCaptures) {
var regex = new Regex(regexPattern);
var match = regex.Match(badInput);
Assert.AreNotEqual(badInput, match.Value);
ClassicAssert.AreNotEqual(badInput, match.Value);
if (expectedCaptures.Any())
Assert.IsFalse(expectedCaptures.All(c => c.Item2 == match.Groups[c.Item1]?.Value));
ClassicAssert.IsFalse(expectedCaptures.All(c => c.Item2 == match.Groups[c.Item1]?.Value));

}

Expand All @@ -820,9 +821,9 @@ public static void AssertSame2DArrays<T>(IEnumerable<IEnumerable<T>> expectedRow

var preText = String.Format("{0}{1}{2}{0}", Environment.NewLine, Tools.NUnit.Convert2DArrayToString(expectedName, expectedRowsArr), Tools.NUnit.Convert2DArrayToString(actualName, actualRowsArr));

Assert.AreEqual(expectedRowsArr.Count(), actualRowsArr.Count(), "{4}{0} has {1} row(s) but {2} has {3} row(s)", actualName, expectedRowsArr.Count(), expectedName, actualRowsArr.Count(), preText);
ClassicAssert.AreEqual(expectedRowsArr.Count(), actualRowsArr.Count(), "{4}{0} has {1} row(s) but {2} has {3} row(s)", actualName, expectedRowsArr.Count(), expectedName, actualRowsArr.Count(), preText);
foreach (var rowExpectation in expectedRowsArr.WithDescriptions().Zip(actualRowsArr, Tuple.Create)) {
Assert.AreEqual(rowExpectation.Item1.Item.Count(),
ClassicAssert.AreEqual(rowExpectation.Item1.Item.Count(),
rowExpectation.Item2.Count(),
"{5}{0} row {1} had {2} column(s) but {3} row {1} had {4} column(s)",
expectedName,
Expand All @@ -832,7 +833,7 @@ public static void AssertSame2DArrays<T>(IEnumerable<IEnumerable<T>> expectedRow
rowExpectation.Item2.Count(),
preText);
foreach (var colExpectation in rowExpectation.Item1.Item.WithDescriptions().Zip(rowExpectation.Item2, Tuple.Create)) {
Assert.AreEqual(colExpectation.Item1.Item,
ClassicAssert.AreEqual(colExpectation.Item1.Item,
colExpectation.Item2,
"{6}{0} row {1} col {2} had value {3} but {4} row {1} col {2} had value {5}",
expectedName,
Expand All @@ -849,22 +850,22 @@ public static void AssertSame2DArrays<T>(IEnumerable<IEnumerable<T>> expectedRow
public static void ApproxEqual(System.DateTime expected, System.DateTime actual, TimeSpan? tolerance = null, string errorMessage = null) {
var approxEqual = expected.ApproxEqual(actual, tolerance);
if (!approxEqual)
Assert.Fail(errorMessage ?? "Dates not approximately equal.{0}Expected: {1:yyyy-MM-dd HH:mm:ss.fff}{0}Actual: {2:yyyy-MM-dd HH:mm:ss.fff}", Environment.NewLine, expected, actual);
ClassicAssert.Fail(errorMessage ?? ("Dates not approximately equal.{0}Expected: {1:yyyy-MM-dd HH:mm:ss.fff}{0}Actual: {2:yyyy-MM-dd HH:mm:ss.fff}").FormatWith(Environment.NewLine, expected, actual));
}

public static void ApproxEqual<T>(T expected, T actual, T tolerance, string message = null) {
var lowerBound = Tools.OperatorTool.Subtract(expected, tolerance);
var upperBound = Tools.OperatorTool.Add(expected, tolerance);
var inRange = Tools.OperatorTool.LessThanOrEqual(lowerBound, actual) && Tools.OperatorTool.LessThanOrEqual(actual, upperBound);
Assert.IsTrue(inRange, message ?? $"Value '{actual}' was not approx equal to '{expected}' (tolerance '{tolerance}')");
ClassicAssert.IsTrue(inRange, message ?? $"Value '{actual}' was not approx equal to '{expected}' (tolerance '{tolerance}')");
}

public static void HasLoadedPages<TItem>(PagedListBase<TItem> list, params long[] pageNos) {
Assert.IsEmpty(list.Pages.Where(p => p.State == PageState.Loaded).Select(p => p.Number).Except(pageNos), "Unexpected pages were open");
ClassicAssert.IsEmpty(list.Pages.Where(p => p.State == PageState.Loaded).Select(p => p.Number).Except(pageNos), "Unexpected pages were open");
}

public static void HasDirtyPages<TItem, TPage>(PagedListBase<TItem> list, params long[] pageNos) {
Assert.IsEmpty(list.Pages.Where(p => p.Dirty).Select(p => p.Number).Except(pageNos), "Unexpected pages were dirty");
ClassicAssert.IsEmpty(list.Pages.Where(p => p.Dirty).Select(p => p.Number).Except(pageNos), "Unexpected pages were dirty");
}

}
4 changes: 2 additions & 2 deletions src/Hydrogen.NUnit/Hydrogen.NUnit.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand All @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 5 additions & 4 deletions src/Hydrogen.NUnit/NUnitTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text;
using NUnit.Framework;
using Hydrogen;
using NUnit.Framework.Legacy;

namespace Tools;

Expand All @@ -29,16 +30,16 @@ public static string Convert2DArrayToString<T>(string header, IEnumerable<IEnume

public static void IsEmpty<T>(IEnumerable<T> collection, string message = null) {
if (!string.IsNullOrWhiteSpace(message))
Assert.IsEmpty(collection, message);
ClassicAssert.IsEmpty(collection, message);
else
Assert.IsEmpty(collection);
ClassicAssert.IsEmpty(collection);
}

public static void IsNotEmpty<T>(IEnumerable<T> collection, string message = null) {
if (!string.IsNullOrWhiteSpace(message))
Assert.IsNotEmpty(collection, message);
ClassicAssert.IsNotEmpty(collection, message);
else
Assert.IsNotEmpty(collection);
ClassicAssert.IsNotEmpty(collection);
}


Expand Down
2 changes: 1 addition & 1 deletion tests/Hydrogen.Communications.Tests/RPCComplexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
// //test for 5 seonds
// int TestRoundCount = 5;
// MiningManager.SimulateBlockCreation(TestRoundCount);
// Assert.AreEqual(RhMinerMock.Accepted, TestRoundCount * 4 / 2);
// ClassicAssert.AreEqual(RhMinerMock.Accepted, TestRoundCount * 4 / 2);

// StopStratumServer();
// StopMiningManager();
Expand Down
Loading

0 comments on commit f6ad131

Please sign in to comment.