LeanTest.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. }