A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.7KB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Runtime.CompilerServices;
  5. namespace GamecraftModdingAPI.Tests
  6. {
  7. /// <summary>
  8. /// API test system assertion utilities.
  9. /// </summary>
  10. public static class Assert
  11. {
  12. private static StreamWriter logFile = null;
  13. private static ConcurrentDictionary<string, string> callbacks = new ConcurrentDictionary<string, string>();
  14. private const string PASS = "SUCCESS: ";
  15. private const string FAIL = "FAILURE: ";
  16. private const string WARN = "WARNING: ";
  17. private const string INFO = "DEBUG: ";
  18. /// <summary>
  19. /// Log a message to the test log.
  20. /// </summary>
  21. /// <param name="msg">Message.</param>
  22. /// <param name="end">Message ending.</param>
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public static void Log(string msg, string end = "\n")
  25. {
  26. if (logFile == null) openTestLog();
  27. logFile.Write(msg + end);
  28. logFile.Flush();
  29. }
  30. /// <summary>
  31. /// Asserts that the event receives a callback... eventually.
  32. /// Add the eventhandler returned by this method to the relevant event.
  33. /// This does not assert that the callback happens under that event's intended circumstances.
  34. /// Add another event handler to assert specific circumstance requirements.
  35. /// </summary>
  36. /// <returns>The callback event handler.</returns>
  37. /// <param name="eventName">Event name.</param>
  38. /// <param name="eventMsg">Event error message.</param>
  39. /// <typeparam name="T">The event handler callback argument object.</typeparam>
  40. public static EventHandler<T> CallsBack<T>(string eventName, string eventMsg = null)
  41. {
  42. if (eventMsg == null) eventMsg = $"expected callback to {eventName} but it never occurred...";
  43. callbacks[eventName] = eventMsg;
  44. return (sender, args) =>
  45. {
  46. string value = null;
  47. if (!callbacks.TryRemove(eventName, out value)) { Log(WARN + $"callback to {eventName} occurred again or a related error occurred... (Received '{args.ToString()}' from '{(sender == null ? (string)sender : sender.ToString())}')"); }
  48. Log(PASS + $"callback to {eventName} occurred... (Received '{args.ToString()}' from '{(sender == null ? (string)sender : sender.ToString())}')");
  49. TestRoot.TestsPassed = true;
  50. };
  51. }
  52. internal static void CallsComplete()
  53. {
  54. foreach(string key in callbacks.Keys)
  55. {
  56. Log(FAIL + callbacks[key]);
  57. TestRoot.TestsPassed = false;
  58. }
  59. }
  60. internal static void CloseLog()
  61. {
  62. if (logFile != null) logFile.Close();
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. private static void openTestLog()
  66. {
  67. logFile = File.CreateText(TestRoot.ReportFile);
  68. }
  69. }
  70. }