OSDN Git Service

Microsoft.NET port: Fix Mono compatibility of unit tests.
authorLoRd_MuldeR <mulder2@gmx.de>
Thu, 13 Feb 2020 21:21:33 +0000 (22:21 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Thu, 13 Feb 2020 21:25:37 +0000 (22:25 +0100)
ports/dotnet/test/Compat.cs [new file with mode: 0644]
ports/dotnet/test/MHash384Test.cs
ports/dotnet/test/MHash384Test.csproj
ports/dotnet/test/Main.cs

diff --git a/ports/dotnet/test/Compat.cs b/ports/dotnet/test/Compat.cs
new file mode 100644 (file)
index 0000000..94e4dc4
--- /dev/null
@@ -0,0 +1,49 @@
+/* ---------------------------------------------------------------------------------------------- */
+/* MHash-384 - Simple fast portable secure hashing library                                        */
+/* Copyright(c) 2016-2020 LoRd_MuldeR <mulder2@gmx.de>                                            */
+/*                                                                                                */
+/* 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
index 263521a..7eb2489 100644 (file)
 
 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)
index b8e1a80..dd40ed3 100644 (file)
@@ -70,6 +70,7 @@
     <Compile Include="Main.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="MHash384Test.cs" />
+    <Compile Include="Compat.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\libmhash384\MHash384.csproj">
index 74c7191..6fb5b05 100644 (file)
 /* 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);
                     }
                 }
             }