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.

65 lines
1.8KB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Runtime.CompilerServices;
  5. namespace GamecraftModdingAPI.Tests
  6. {
  7. public static class Assert
  8. {
  9. private static StreamWriter logFile = null;
  10. private static ConcurrentDictionary<string, string> callbacks = new ConcurrentDictionary<string, string>();
  11. private const string PASS = "SUCCESS: ";
  12. private const string FAIL = "FAILURE: ";
  13. private const string WARN = "WARNING: ";
  14. private const string INFO = "DEBUG: ";
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static void Log(string msg, string end = "\n")
  17. {
  18. if (logFile == null) openTestLog();
  19. logFile.Write(msg + end);
  20. logFile.Flush();
  21. }
  22. public static EventHandler<T> CallsBack<T>(string eventName, string eventMsg = null)
  23. {
  24. if (eventMsg == null) eventMsg = $"expected callback to {eventName} but it never occurred...";
  25. callbacks[eventName] = eventMsg;
  26. return (sender, args) =>
  27. {
  28. string value = null;
  29. 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())}')"); }
  30. Log(PASS + $"callback to {eventName} occurred... (Received '{args.ToString()}' from '{(sender == null ? (string)sender : sender.ToString())}')");
  31. TestRoot.TestsPassed = true;
  32. };
  33. }
  34. internal static void CallsComplete()
  35. {
  36. foreach(string key in callbacks.Keys)
  37. {
  38. Log(FAIL + callbacks[key]);
  39. TestRoot.TestsPassed = false;
  40. }
  41. }
  42. internal static void CloseLog()
  43. {
  44. if (logFile != null) logFile.Close();
  45. }
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. private static void openTestLog()
  48. {
  49. logFile = File.CreateText(TestRoot.ReportFile);
  50. }
  51. }
  52. }