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.

295 lines
8.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using System.Linq; // welcome to the dark side
  6. using Svelto.Tasks;
  7. using Svelto.Tasks.Lean;
  8. using Svelto.Tasks.Enumerators;
  9. using UnityEngine;
  10. using TechbloxModdingAPI.App;
  11. using TechbloxModdingAPI.Tasks;
  12. using TechbloxModdingAPI.Utility;
  13. namespace TechbloxModdingAPI.Tests
  14. {
  15. /// <summary>
  16. /// API test system root class.
  17. /// </summary>
  18. public static class TestRoot
  19. {
  20. public static bool AutoShutdown = true;
  21. public const string ReportFile = "TechbloxModdingAPI_tests.log";
  22. private static bool _testsPassed = false;
  23. private static uint _testsCount = 0;
  24. private static uint _testsCountPassed = 0;
  25. private static uint _testsCountFailed = 0;
  26. private static string state = "StartingUp";
  27. private static Stopwatch timer;
  28. private static List<Type> testTypes = null;
  29. public static bool TestsPassed
  30. {
  31. get => _testsPassed;
  32. set
  33. {
  34. _testsPassed = _testsPassed && value;
  35. _testsCount++;
  36. if (value)
  37. {
  38. _testsCountPassed++;
  39. }
  40. else
  41. {
  42. _testsCountFailed++;
  43. }
  44. }
  45. }
  46. private static void StartUp()
  47. {
  48. // init
  49. timer = Stopwatch.StartNew();
  50. _testsPassed = true;
  51. _testsCount = 0;
  52. _testsCountPassed = 0;
  53. _testsCountFailed = 0;
  54. // flow control
  55. Game.Enter += (sender, args) => { GameTests().RunOn(RobocraftX.Schedulers.Lean.EveryFrameStepRunner_TimeRunningAndStopped); };
  56. Game.Exit += (s, a) => state = "ReturningFromGame";
  57. Client.EnterMenu += (sender, args) =>
  58. {
  59. if (state == "EnteringMenu")
  60. {
  61. MenuTests().RunOn(Scheduler.leanRunner);
  62. state = "EnteringGame";
  63. }
  64. if (state == "ReturningFromGame")
  65. {
  66. TearDown().RunOn(Scheduler.leanRunner);
  67. state = "ShuttingDown";
  68. }
  69. };
  70. // init tests here
  71. foreach (Type t in testTypes)
  72. {
  73. foreach (MethodBase m in t.GetMethods())
  74. {
  75. if (m.GetCustomAttribute<APITestStartUpAttribute>() != null)
  76. {
  77. try
  78. {
  79. m.Invoke(null, new object[0]);
  80. }
  81. catch (Exception e)
  82. {
  83. Assert.Fail($"Start up method '{m}' raised an exception: {e.ToString()}");
  84. }
  85. }
  86. }
  87. }
  88. state = "EnteringMenu";
  89. }
  90. private static IEnumerator<TaskContract> MenuTests()
  91. {
  92. yield return Yield.It;
  93. // menu tests
  94. foreach (Type t in testTypes)
  95. {
  96. foreach (MethodBase m in t.GetMethods())
  97. {
  98. APITestCaseAttribute a = m.GetCustomAttribute<APITestCaseAttribute>();
  99. if (a != null && a.TestType == TestType.Menu)
  100. {
  101. try
  102. {
  103. m.Invoke(null, new object[0]);
  104. }
  105. catch (Exception e)
  106. {
  107. Assert.Fail($"Menu test '{m}' raised an exception: {e.ToString()}");
  108. }
  109. yield return Yield.It;
  110. }
  111. }
  112. }
  113. // load game
  114. yield return GoToGameTests().Continue();
  115. }
  116. private static IEnumerator<TaskContract> GoToGameTests()
  117. {
  118. Client app = new Client();
  119. int oldLength = 0;
  120. while (app.MyGames.Length == 0 || oldLength != app.MyGames.Length)
  121. {
  122. oldLength = app.MyGames.Length;
  123. yield return new WaitForSecondsEnumerator(1).Continue();
  124. }
  125. yield return Yield.It;
  126. app.MyGames[0].EnterGame();
  127. /*Game newGame = Game.NewGame();
  128. yield return new WaitForSecondsEnumerator(5).Continue(); // wait for sync
  129. newGame.EnterGame();*/
  130. }
  131. private static IEnumerator<TaskContract> GameTests()
  132. {
  133. yield return Yield.It;
  134. Game currentGame = Game.CurrentGame();
  135. // in-game tests
  136. yield return new WaitForSecondsEnumerator(5).Continue(); // wait for game to finish loading
  137. foreach (Type t in testTypes)
  138. {
  139. foreach (MethodBase m in t.GetMethods())
  140. {
  141. APITestCaseAttribute a = m.GetCustomAttribute<APITestCaseAttribute>();
  142. if (a != null && a.TestType == TestType.Game)
  143. {
  144. try
  145. {
  146. m.Invoke(null, new object[0]);
  147. }
  148. catch (Exception e)
  149. {
  150. Assert.Fail($"Game test '{m}' raised an exception: {e.ToString()}");
  151. }
  152. yield return Yield.It;
  153. }
  154. }
  155. }
  156. currentGame.ToggleTimeMode();
  157. yield return new WaitForSecondsEnumerator(5).Continue();
  158. // simulation tests
  159. foreach (Type t in testTypes)
  160. {
  161. foreach (MethodBase m in t.GetMethods())
  162. {
  163. APITestCaseAttribute a = m.GetCustomAttribute<APITestCaseAttribute>();
  164. if (a != null && a.TestType == TestType.SimulationMode)
  165. {
  166. try
  167. {
  168. m.Invoke(null, new object[0]);
  169. }
  170. catch (Exception e)
  171. {
  172. Assert.Fail($"Simulation test '{m}' raised an exception: {e.ToString()}");
  173. }
  174. yield return Yield.It;
  175. }
  176. }
  177. }
  178. currentGame.ToggleTimeMode();
  179. yield return new WaitForSecondsEnumerator(5).Continue();
  180. // build tests
  181. foreach (Type t in testTypes)
  182. {
  183. foreach (MethodBase m in t.GetMethods())
  184. {
  185. APITestCaseAttribute a = m.GetCustomAttribute<APITestCaseAttribute>();
  186. if (a != null && a.TestType == TestType.EditMode)
  187. {
  188. try
  189. {
  190. m.Invoke(null, new object[0]);
  191. }
  192. catch (Exception e)
  193. {
  194. Assert.Fail($"Build test '{m}' raised an exception: {e.ToString()}");
  195. }
  196. yield return Yield.It;
  197. }
  198. }
  199. }
  200. // exit game
  201. yield return new WaitForSecondsEnumerator(5).Continue();
  202. yield return ReturnToMenu().Continue();
  203. }
  204. private static IEnumerator<TaskContract> ReturnToMenu()
  205. {
  206. Logging.MetaLog("Returning to main menu");
  207. yield return Yield.It;
  208. Game.CurrentGame().ExitGame();
  209. }
  210. private static IEnumerator<TaskContract> TearDown()
  211. {
  212. yield return new WaitForSecondsEnumerator(5).Continue();
  213. Logging.MetaLog("Tearing down test run");
  214. // dispose tests here
  215. foreach (Type t in testTypes)
  216. {
  217. foreach (MethodBase m in t.GetMethods())
  218. {
  219. if (m.GetCustomAttribute<APITestTearDownAttribute>() != null)
  220. {
  221. try
  222. {
  223. m.Invoke(null, new object[0]);
  224. }
  225. catch (Exception e)
  226. {
  227. Assert.Warn($"Tear down method '{m}' raised an exception: {e.ToString()}");
  228. }
  229. yield return Yield.It;
  230. }
  231. }
  232. }
  233. // finish up
  234. Assert.CallsComplete();
  235. timer.Stop();
  236. string verdict = _testsPassed ? "--- PASSED :) ---" : "--- FAILED :( ---";
  237. Assert.Log($"VERDICT: {verdict} ({_testsCountPassed}/{_testsCountFailed}/{_testsCount} P/F/T in {timer.ElapsedMilliseconds}ms)");
  238. yield return Yield.It;
  239. // end game
  240. Logging.MetaLog("Completed test run: " + verdict);
  241. yield return Yield.It;
  242. Assert.CloseLog();
  243. if (AutoShutdown) Application.Quit();
  244. }
  245. private static void FindTests(Assembly asm)
  246. {
  247. testTypes = new List<Type>();
  248. foreach (Type t in asm.GetTypes())
  249. {
  250. if (t.GetCustomAttribute<APITestClassAttribute>() != null)
  251. {
  252. testTypes.Add(t);
  253. }
  254. }
  255. }
  256. /// <summary>
  257. /// Runs the tests.
  258. /// </summary>
  259. /// <param name="asm">Assembly to search for tests. When set to null, this uses the TechbloxModdingAPI assembly. </param>
  260. public static void RunTests(Assembly asm = null)
  261. {
  262. if (asm == null) asm = Assembly.GetExecutingAssembly();
  263. FindTests(asm);
  264. Logging.MetaLog("Starting test run");
  265. // log metadata
  266. Assert.Log($"Unity {Application.unityVersion}");
  267. Assert.Log($"Techblox {Application.version}");
  268. Assert.Log($"TechbloxModdingAPI {Assembly.GetExecutingAssembly().GetName().Version}");
  269. Assert.Log($"Testing {asm.GetName().Name} {asm.GetName().Version}");
  270. Assert.Log($"START: --- {DateTime.Now.ToString()} --- ({testTypes.Count} tests classes detected)");
  271. StartUp();
  272. Logging.MetaLog("Test StartUp complete");
  273. }
  274. }
  275. }