y_testing.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*----------------------------------------------------------------------------*-
  2. ====================================
  3. y_testing - Run unit tests easilly
  4. ====================================
  5. Description:
  6. Runs any functions named as tests when the Testing_Run function is called.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the SA:MP script information include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2008
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk
  23. Thanks:
  24. Peter, Cam - Support.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN.
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 1.0
  43. Changelog:
  44. 25/10/10:
  45. Integrated in to YSI.
  46. 06/08/10:
  47. First version
  48. -*----------------------------------------------------------------------------*/
  49. #include <YSI\internal\y_version>
  50. #include <YSI\y_scripting>
  51. #include <YSI\y_debug>
  52. #if !defined RUN_TESTS
  53. #if _DEBUG > 0 || defined _YSI_SPECIAL_DEBUG
  54. #define RUN_TESTS
  55. #if defined _YSI_SPECIAL_DEBUG
  56. #define TP printf
  57. #define TC(%0); %0
  58. #else
  59. #define TP P:1
  60. #define TC C:1
  61. #endif
  62. #else
  63. #define Tests:%1() stock bool:Tests_@%1()
  64. #define Test:%1() stock bool:Test_@%1()
  65. #define TestInit:%1() stock Init_@%1()
  66. #define TestClose:%1() stock Shut_@%1()
  67. #endif
  68. #endif
  69. #define TEST_TRUE(%0) Testing_Test(bool:(%0))
  70. #define TEST_FALSE(%0) Testing_Test(!(%0))
  71. #define TEST_NULL(%0) Testing_Test((%0) == 0)
  72. #define TEST_N(%0,%1) Testing_Test((%0) == (%1))
  73. #define TEST_TRUE_EX(%0,%2) Testing_Test(bool:(%0), (%2))
  74. #define TEST_FALSE_EX(%0,%2) Testing_Test(!(%0), (%2))
  75. #define TEST_NULL_EX(%0,%2) Testing_Test((%0) == 0, (%2))
  76. #define TEST_N_EX(%0,%1,%2) Testing_Test((%0) == (%1), (%2))
  77. static stock
  78. YSI_g_sTests,
  79. YSI_g_sFails;
  80. stock Testing_Test(bool:x, const str[] = "")
  81. {
  82. ++YSI_g_sTests;
  83. if (!x)
  84. {
  85. ++YSI_g_sFails;
  86. TP("*** Test failed: %s", str);
  87. }
  88. TC(else printf("*** Test passed: %s", str););
  89. }
  90. /*----------------------------------------------------------------------------*-
  91. Function:
  92. Testing_Run
  93. Params:
  94. &tests - Number of tests run.
  95. &fails - Number of tests which failed.
  96. buffer[33] - The name of the first test which failed.
  97. Return:
  98. Wether all tests were sucessful or not.
  99. Notes:
  100. -
  101. native Testing_Run(&tests, &fails, buffer[33] = "");
  102. -*----------------------------------------------------------------------------*/
  103. stock bool:Testing_Run(&tests, &fails, lastfail[33] = "", bool:p = false)
  104. {
  105. #pragma unused p
  106. #if defined RUN_TESTS
  107. P:3("Testsing_Run() called");
  108. new
  109. idx,
  110. buffer[32];
  111. while ((idx = Scripting_GetPublic(idx, buffer, "Test_@")))
  112. {
  113. ++YSI_g_sTests;
  114. // Call the setup function if there is one.
  115. buffer[0] = 'I';
  116. buffer[1] = 'n';
  117. buffer[2] = 'i';
  118. buffer[3] = 't';
  119. CallLocalFunction(buffer, "");
  120. // Call the test.
  121. buffer[0] = 'T';
  122. buffer[1] = 'e';
  123. buffer[2] = 's';
  124. buffer[3] = 't';
  125. TP("Testsing_Run(): Calling %s", buffer[6]);
  126. if (!CallLocalFunction(buffer, ""))
  127. {
  128. if (YSI_g_sFails)
  129. {
  130. ++YSI_g_sFails;
  131. }
  132. else
  133. {
  134. fails = 0;
  135. // Copy the string over.
  136. while ((lastfail[fails] = buffer[fails + 6])) ++fails;
  137. YSI_g_sFails = 1;
  138. }
  139. TC(if (p) printf("*** Test failed: %s", buffer[fails + 6]););
  140. }
  141. // Call the shutdown function if there is one.
  142. buffer[0] = 'S';
  143. buffer[1] = 'h';
  144. buffer[2] = 'u';
  145. buffer[3] = 't';
  146. CallLocalFunction(buffer, "");
  147. }
  148. while ((idx = Scripting_GetPublic(idx, buffer, "Tezt_@")))
  149. {
  150. //++YSI_g_sTests;
  151. // Call the setup function if there is one.
  152. buffer[0] = 'I';
  153. buffer[1] = 'n';
  154. buffer[2] = 'i';
  155. buffer[3] = 't';
  156. CallLocalFunction(buffer, "");
  157. // Call the test.
  158. buffer[0] = 'T';
  159. buffer[1] = 'e';
  160. buffer[2] = 'z';
  161. buffer[3] = 't';
  162. fails = YSI_g_sFails;
  163. TP("Testsing_Run(): Calling %s", buffer[6]);
  164. CallLocalFunction(buffer, "");
  165. /*if (YSI_g_sFails != fails)
  166. {
  167. if (YSI_g_sFails)
  168. {
  169. ++YSI_g_sFails;
  170. }
  171. else
  172. {
  173. fails = 0;
  174. // Copy the string over.
  175. while ((lastfail[fails] = buffer[fails + 6])) ++fails;
  176. YSI_g_sFails = 1;
  177. }
  178. //C:1(if (p) printf("*** Test failed: %s", buffer[fails + 6]););
  179. }*/
  180. // Call the shutdown function if there is one.
  181. buffer[0] = 'S';
  182. buffer[1] = 'h';
  183. buffer[2] = 'u';
  184. buffer[3] = 't';
  185. CallLocalFunction(buffer, "");
  186. }
  187. tests = YSI_g_sTests;
  188. fails = YSI_g_sFails;
  189. return fails == 0;
  190. #else
  191. #pragma unused tests, fails, lastfail
  192. return true;
  193. #endif
  194. }
  195. #if defined RUN_TESTS
  196. #define Tests:%1() forward bool:Tezt_@%1(); public bool:Tezt_@%1()
  197. #define Test:%1() forward bool:Test_@%1(); public bool:Test_@%1()
  198. #define TestInit:%1() forward Init_@%1(); public Init_@%1()
  199. #define TestClose:%1() forward Shut_@%1(); public Shut_@%1()
  200. #if _DEBUG > 0 || defined _YSI_SPECIAL_DEBUG
  201. #if !defined FILTERSCRIPT
  202. // Hook main in gamemodes.
  203. main()
  204. {
  205. new
  206. tests,
  207. fails;
  208. Testing_Run(tests, fails, _, true);
  209. printf("*** Tests: %d, Fails: %d", tests, fails);
  210. Testing_main();
  211. }
  212. #define main Testing_main
  213. #endif
  214. #endif
  215. #endif