From 94db526da0812c765cdb7d220e1d448c750af7b7 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Thu, 13 Feb 2020 22:21:33 +0100 Subject: [PATCH] Microsoft.NET port: Fix Mono compatibility of unit tests. --- ports/dotnet/test/Compat.cs | 49 +++++++++++++++++++++++++++++++++++ ports/dotnet/test/MHash384Test.cs | 16 +++++++++--- ports/dotnet/test/MHash384Test.csproj | 1 + ports/dotnet/test/Main.cs | 12 ++++++--- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 ports/dotnet/test/Compat.cs diff --git a/ports/dotnet/test/Compat.cs b/ports/dotnet/test/Compat.cs new file mode 100644 index 0000000..94e4dc4 --- /dev/null +++ b/ports/dotnet/test/Compat.cs @@ -0,0 +1,49 @@ +/* ---------------------------------------------------------------------------------------------- */ +/* MHash-384 - Simple fast portable secure hashing library */ +/* Copyright(c) 2016-2020 LoRd_MuldeR */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */ +/* and associated documentation files (the "Software"), to deal in the Software without */ +/* restriction, including without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the */ +/* Software is furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in all copies or */ +/* substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */ +/* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ +/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */ +/* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* ---------------------------------------------------------------------------------------------- */ + +using System; + +namespace com.muldersoft.mhash384.test.mono_compat +{ + public sealed class TestClassAttribute : Attribute { } + + public sealed class TestMethodAttribute : Attribute { } + + public sealed class ClassInitializeAttribute : Attribute { } + + public sealed class ClassCleanupAttribute : Attribute { } + + public sealed class TestContext { } + + public class AssertFailedException : Exception + { + public AssertFailedException(string message) : base(message) + { + } + } + + public static class Assert + { + public static void Fail(String message) + { + throw new AssertFailedException(message); + } + } +} \ No newline at end of file diff --git a/ports/dotnet/test/MHash384Test.cs b/ports/dotnet/test/MHash384Test.cs index 263521a..7eb2489 100644 --- a/ports/dotnet/test/MHash384Test.cs +++ b/ports/dotnet/test/MHash384Test.cs @@ -20,9 +20,14 @@ using System; using System.Text; -using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Diagnostics; +#if !__MonoCS__ +using Microsoft.VisualStudio.TestTools.UnitTesting; +#else +using com.muldersoft.mhash384.test.mono_compat; +#endif + namespace com.muldersoft.mhash384.test { [TestClass] @@ -30,14 +35,14 @@ namespace com.muldersoft.mhash384.test { private static bool m_success; - [ClassInitialize()] + [ClassInitialize] public static void Initialize(TestContext testContext) { Trace.WriteLine(String.Format("MHash-384 v{0:D}.{1:D2}-{2:D}, Microsoft.NET v{3} [{4:D}-Bit]\n", MHash384.VERSION_MAJOR, MHash384.VERSION_MINOR, MHash384.VERSION_PATCH, System.Environment.Version, System.Environment.Is64BitProcess ? 64 : 32)); m_success = true; } - [ClassCleanup()] + [ClassCleanup] public static void Cleanup() { if(m_success) @@ -184,7 +189,10 @@ namespace com.muldersoft.mhash384.test bool matches = digest.Equals(expected, StringComparison.OrdinalIgnoreCase); m_success &= matches; Trace.WriteLine(String.Format("{0} - {1}", digest, matches ? "OK" : "Error!")); - Assert.IsTrue(matches); + if(!matches) + { + Assert.Fail("Computed digest does *not* match reference!"); + } } private static string GetDigest(uint repetitions, string test) diff --git a/ports/dotnet/test/MHash384Test.csproj b/ports/dotnet/test/MHash384Test.csproj index b8e1a80..dd40ed3 100644 --- a/ports/dotnet/test/MHash384Test.csproj +++ b/ports/dotnet/test/MHash384Test.csproj @@ -70,6 +70,7 @@ + diff --git a/ports/dotnet/test/Main.cs b/ports/dotnet/test/Main.cs index 74c7191..6fb5b05 100644 --- a/ports/dotnet/test/Main.cs +++ b/ports/dotnet/test/Main.cs @@ -18,13 +18,18 @@ /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* ---------------------------------------------------------------------------------------------- */ -using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; +#if !__MonoCS__ +using Microsoft.VisualStudio.TestTools.UnitTesting; +#else +using com.muldersoft.mhash384.test.mono_compat; +#endif + namespace com.muldersoft.mhash384.test { class TestDriver @@ -50,9 +55,10 @@ namespace com.muldersoft.mhash384.test } catch (TargetInvocationException targetInvocationException) { - if (typeof(AssertFailedException).IsAssignableFrom(targetInvocationException.GetType())) + Exception innerException = targetInvocationException.InnerException; + if (!Object.ReferenceEquals(innerException, null)) { - throw targetInvocationException; + Console.WriteLine(@"Test failed: ""{0}""", innerException.Message); } } } -- 2.11.0