From 60f231f939210485e9d258fd6da8d318f49deec8 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Tue, 30 Jun 2020 20:43:14 -0400 Subject: [PATCH] Add standard asserts --- GamecraftModdingAPI/Tests/Assert.cs | 158 ++++++++++++++++++++++++++ GamecraftModdingAPI/Tests/TestRoot.cs | 54 ++++++++- 2 files changed, 206 insertions(+), 6 deletions(-) diff --git a/GamecraftModdingAPI/Tests/Assert.cs b/GamecraftModdingAPI/Tests/Assert.cs index d888f45..eec9c5c 100644 --- a/GamecraftModdingAPI/Tests/Assert.cs +++ b/GamecraftModdingAPI/Tests/Assert.cs @@ -3,6 +3,8 @@ using System.Collections.Concurrent; using System.IO; using System.Runtime.CompilerServices; +using Unity.Mathematics; + namespace GamecraftModdingAPI.Tests { /// @@ -59,6 +61,162 @@ namespace GamecraftModdingAPI.Tests }; } + public static bool NotNull(T obj, string err = null, string success = null) + { + if (err == null) err = $"{nameof(T)} object was null."; + if (success == null) success = $"{nameof(T)} '{obj}' not null"; + if (obj == null) + { + Log(FAIL + err); + TestRoot.TestsPassed = false; + return false; + } + else + { + Log(PASS + success); + TestRoot.TestsPassed = true; + return true; + } + } + + public static bool Equal(T obj1, T obj2, string err = null, string success = null) + { + if (err == null) err = $"{nameof(T)} '{obj1}' is not equal to '{obj2}'."; + if (success == null) success = $"{nameof(T)} '{obj1}' is equal to '{obj2}'."; + if (obj1 == null && obj2 == null) + { + // pass + Log(PASS + success); + TestRoot.TestsPassed = true; + return true; + } + else if (!(obj1 == null && obj2 == null) && obj1.Equals(obj2) && obj2.Equals(obj1)) + { + // pass + Log(PASS + success); + TestRoot.TestsPassed = true; + return true; + } + else if (obj1 != null && (obj1 != null && !obj1.Equals(obj2))) + { + // pass + Log(PASS + success); + TestRoot.TestsPassed = true; + return true; + } + else if (obj2 != null && !obj2.Equals(obj1)) + { + // pass + Log(PASS + success); + TestRoot.TestsPassed = true; + return true; + } + else + { + // fail + Log(FAIL + err); + TestRoot.TestsPassed = false; + return false; + } + } + + public static bool Errorless(Action tryThis, string err = null, string success = null) + { + if (err == null) err = $"{tryThis} raised an exception: "; + if (success == null) success = $"{tryThis} completed without raising an exception."; + try + { + tryThis(); + } + catch (Exception e) + { + Log(FAIL + err + e); + TestRoot.TestsPassed = false; + return false; + } + TestRoot.TestsPassed = true; + Log(PASS + success); + return true; + } + + public static bool CloseTo(float a, float b, string err = null, string success = null, float delta = float.Epsilon) + { + if (err == null) err = $"{a} is not within {delta} of {b}."; + if (success == null) success = $"{a} is close enough to {b}."; + if (Math.Abs(a - b) > delta) + { + Log(FAIL + err); + TestRoot.TestsPassed = false; + return false; + } + else + { + TestRoot.TestsPassed = true; + Log(PASS + success); + return true; + } + } + + public static bool CloseTo(double a, double b, string err = null, string success = null, double delta = double.Epsilon) + { + if (err == null) err = $"{a} is not within {delta} of {b}."; + if (success == null) success = $"{a} is close enough to {b}."; + if (Math.Abs(a - b) > delta) + { + Log(FAIL + err); + TestRoot.TestsPassed = false; + return false; + } + else + { + TestRoot.TestsPassed = true; + Log(PASS + success); + return true; + } + } + + public static bool CloseTo(float3 a, float3 b, string err = null, string success = null, float delta = float.Epsilon) + { + if (err == null) err = $"{a} is not within {delta} of {b} in every direction."; + if (success == null) success = $"{a} is close enough to {b}."; + bool xClose = CloseTo(a.x, b.x, err, success, delta); + bool yClose = CloseTo(a.y, b.y, err, success, delta); + bool zClose = CloseTo(a.z, b.z, err, success, delta); + if (xClose && yClose && zClose) + { + //TestRoot.TestsPassed = true; + //Log(PASS + success); + return true; + } + else + { + //Log(FAIL + err); + //TestRoot.TestsPassed = false; + return false; + } + } + + public static void Fail(string msg = null) + { + if (msg == null) msg = $"Manual test failure with no message provided."; + Log(FAIL + msg); + TestRoot.TestsPassed = false; + } + + public static void Pass(string msg = null) + { + if (msg == null) msg = $"Manual test pass with no message provided."; + Log(PASS + msg); + TestRoot.TestsPassed = true; + } + + public static void Warn(string msg = null) + { + if (msg == null) msg = $"Manual test warning with no message provided."; + Log(WARN + msg); + TestRoot.TestsPassed = true; + } + internal static void CallsComplete() { foreach(string key in callbacks.Keys) diff --git a/GamecraftModdingAPI/Tests/TestRoot.cs b/GamecraftModdingAPI/Tests/TestRoot.cs index 99f77b7..4e9da73 100644 --- a/GamecraftModdingAPI/Tests/TestRoot.cs +++ b/GamecraftModdingAPI/Tests/TestRoot.cs @@ -87,7 +87,14 @@ namespace GamecraftModdingAPI.Tests { if (m.GetCustomAttribute() != null) { - m.Invoke(null, new object[0]); + try + { + m.Invoke(null, new object[0]); + } + catch (Exception e) + { + Assert.Fail($"Start up method '{m}' raised an exception: {e.ToString()}"); + } } } } @@ -105,7 +112,14 @@ namespace GamecraftModdingAPI.Tests APITestCaseAttribute a = m.GetCustomAttribute(); if (a != null && a.TestType == TestType.Menu) { - m.Invoke(null, new object[0]); + try + { + m.Invoke(null, new object[0]); + } + catch (Exception e) + { + Assert.Fail($"Menu test '{m}' raised an exception: {e.ToString()}"); + } yield return Yield.It; } } @@ -144,7 +158,14 @@ namespace GamecraftModdingAPI.Tests APITestCaseAttribute a = m.GetCustomAttribute(); if (a != null && a.TestType == TestType.Game) { - m.Invoke(null, new object[0]); + try + { + m.Invoke(null, new object[0]); + } + catch (Exception e) + { + Assert.Fail($"Game test '{m}' raised an exception: {e.ToString()}"); + } yield return Yield.It; } } @@ -159,7 +180,14 @@ namespace GamecraftModdingAPI.Tests APITestCaseAttribute a = m.GetCustomAttribute(); if (a != null && a.TestType == TestType.SimulationMode) { - m.Invoke(null, new object[0]); + try + { + m.Invoke(null, new object[0]); + } + catch (Exception e) + { + Assert.Fail($"Simulation test '{m}' raised an exception: {e.ToString()}"); + } yield return Yield.It; } } @@ -174,7 +202,14 @@ namespace GamecraftModdingAPI.Tests APITestCaseAttribute a = m.GetCustomAttribute(); if (a != null && a.TestType == TestType.EditMode) { - m.Invoke(null, new object[0]); + try + { + m.Invoke(null, new object[0]); + } + catch (Exception e) + { + Assert.Fail($"Build test '{m}' raised an exception: {e.ToString()}"); + } yield return Yield.It; } } @@ -202,7 +237,14 @@ namespace GamecraftModdingAPI.Tests { if (m.GetCustomAttribute() != null) { - m.Invoke(null, new object[0]); + try + { + m.Invoke(null, new object[0]); + } + catch (Exception e) + { + Assert.Warn($"Tear down method '{m}' raised an exception: {e.ToString()}"); + } yield return Yield.It; } }