TestRunner.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Threading;
  4. using MoonSharp.Interpreter.Tests;
  5. using MoonSharp.Interpreter;
  6. using MoonSharp.Interpreter.Loaders;
  7. public class TestRunner : MonoBehaviour
  8. {
  9. string m_Text = "";
  10. object m_Lock = new object();
  11. bool m_LastWasLine = true;
  12. // Use this for initialization
  13. void Start()
  14. {
  15. Debug.Log("STARTED!");
  16. StartCoroutine(DoTests());
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. }
  22. IEnumerator DoTests()
  23. {
  24. MoonSharp.Interpreter.Tests.TestRunner tr = new MoonSharp.Interpreter.Tests.TestRunner(Log);
  25. foreach (var r in tr.IterateOnTests())
  26. {
  27. Log(r);
  28. yield return null;
  29. }
  30. }
  31. void Log(TestResult r)
  32. {
  33. if (r.Type == TestResultType.Fail)
  34. {
  35. Console_WriteLine("[FAIL] | {0} - {1} - {2}", r.TestName, r.Message, r.Exception);
  36. }
  37. else if (r.Type == TestResultType.Ok)
  38. {
  39. Console_Write(".");
  40. }
  41. else if (r.Type == TestResultType.Skipped)
  42. {
  43. Console_Write("s");
  44. }
  45. else
  46. {
  47. Console_WriteLine("{0}", r.Message);
  48. }
  49. }
  50. private void Console_Write(string message)
  51. {
  52. lock (m_Lock)
  53. {
  54. m_Text = m_Text + message;
  55. m_LastWasLine = false;
  56. }
  57. }
  58. private void Console_WriteLine(string message, params object[] args)
  59. {
  60. lock (m_Lock)
  61. {
  62. if (!m_LastWasLine)
  63. {
  64. m_Text = m_Text + "\n";
  65. m_LastWasLine = true;
  66. }
  67. m_Text = m_Text + string.Format(message, args) + "\n";
  68. }
  69. }
  70. void OnGUI()
  71. {
  72. string text = "";
  73. lock (m_Lock)
  74. text = m_Text;
  75. GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "MoonSharp Test Runner");
  76. GUI.TextArea(new Rect(0, 30, Screen.width, Screen.height - 30), text);
  77. }
  78. }