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.

122 lines
3.2 KiB

4 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. public class LeanTester : MonoBehaviour {
  4. public float timeout = 15f;
  5. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2 && !UNITY_4_3 && !UNITY_4_5
  6. public void Start(){
  7. StartCoroutine( timeoutCheck() );
  8. }
  9. IEnumerator timeoutCheck(){
  10. float pauseEndTime = Time.realtimeSinceStartup + timeout;
  11. while (Time.realtimeSinceStartup < pauseEndTime)
  12. {
  13. yield return 0;
  14. }
  15. if(LeanTest.testsFinished==false){
  16. Debug.Log(LeanTest.formatB("Tests timed out!"));
  17. LeanTest.overview();
  18. }
  19. }
  20. #endif
  21. }
  22. public class LeanTest : object {
  23. public static int expected = 0;
  24. private static int tests = 0;
  25. private static int passes = 0;
  26. public static float timeout = 15f;
  27. public static bool timeoutStarted = false;
  28. public static bool testsFinished = false;
  29. public static void debug( string name, bool didPass, string failExplaination = null){
  30. expect( didPass, name, failExplaination);
  31. }
  32. public static void expect( bool didPass, string definition, string failExplaination = null){
  33. float len = printOutLength(definition);
  34. int paddingLen = 40-(int)(len*1.05f);
  35. #if UNITY_FLASH
  36. string padding = padRight(paddingLen);
  37. #else
  38. string padding = "".PadRight(paddingLen,"_"[0]);
  39. #endif
  40. string logName = formatB(definition) +" " + padding + " [ "+ (didPass ? formatC("pass","green") : formatC("fail","red")) +" ]";
  41. if(didPass==false && failExplaination!=null)
  42. logName += " - " + failExplaination;
  43. Debug.Log(logName);
  44. if(didPass)
  45. passes++;
  46. tests++;
  47. // Debug.Log("tests:"+tests+" expected:"+expected);
  48. if(tests==expected && testsFinished==false){
  49. overview();
  50. }else if(tests>expected){
  51. Debug.Log(formatB("Too many tests for a final report!") + " set LeanTest.expected = "+tests);
  52. }
  53. if(timeoutStarted==false){
  54. timeoutStarted = true;
  55. GameObject tester = new GameObject();
  56. tester.name = "~LeanTest";
  57. LeanTester test = tester.AddComponent(typeof(LeanTester)) as LeanTester;
  58. test.timeout = timeout;
  59. #if !UNITY_EDITOR
  60. tester.hideFlags = HideFlags.HideAndDontSave;
  61. #endif
  62. }
  63. }
  64. public static string padRight(int len){
  65. string str = "";
  66. for(int i = 0; i < len; i++){
  67. str += "_";
  68. }
  69. return str;
  70. }
  71. public static float printOutLength( string str ){
  72. float len = 0.0f;
  73. for(int i = 0; i < str.Length; i++){
  74. if(str[i]=="I"[0]){
  75. len += 0.5f;
  76. }else if(str[i]=="J"[0]){
  77. len += 0.85f;
  78. }else{
  79. len += 1.0f;
  80. }
  81. }
  82. return len;
  83. }
  84. public static string formatBC( string str, string color ){
  85. return formatC(formatB(str),color);
  86. }
  87. public static string formatB( string str ){
  88. #if UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2
  89. return str;
  90. #else
  91. return "<b>"+ str + "</b>";
  92. #endif
  93. }
  94. public static string formatC( string str, string color ){
  95. #if UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2
  96. return str;
  97. #else
  98. return "<color="+color+">"+ str + "</color>";
  99. #endif
  100. }
  101. public static void overview(){
  102. testsFinished = true;
  103. int failedCnt = (expected-passes);
  104. string failedStr = failedCnt > 0 ? formatBC(""+failedCnt,"red") : ""+failedCnt;
  105. Debug.Log(formatB("Final Report:")+" _____________________ PASSED: "+formatBC(""+passes,"green")+" FAILED: "+failedStr+" ");
  106. }
  107. }