y_testing.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 YSI testing include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  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, whose limits continue to amaze me!
  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. /*#if defined _inc_a_samp || defined _samp_included || defined _inc_y_version
  50. #error y_testing must be the VERY FIRST include in your mode if being used.
  51. #endif
  52. // Now we redefined "native" to do something clever!
  53. // "IsPlayerAttachedObjectSlotUsed" is an issue for us...
  54. // "y_renative" allows native functions to be declared without constantly trying
  55. // to redefine it.
  56. #include "internal\y_renative"
  57. #define native%0(%1); _YSI_NO_TAG_NATIVE:_YSI_RE_NATIVE<%0@|||%1|||%0>_YSI_RE_STOCK<%0|||%1|||_:%0@>
  58. //stock%0(%1){printf(_:_YSI_NO_SQUARE:"native "#%0" called");return _:%0@(%1);}
  59. #define _YSI_RE_STOCK<%0|||%1|||%2> stock%0(%1){print(_:_YSI_NO_SQUARE:"native "#%0" called");return %2(%1);}
  60. #define _YSI_NO_SQUARE:%0);%1(%2[%3]%4);} _YSI_NO_SQUARE:%0);%1(%2%4);}
  61. //#define _YSI_NO_TAG_NATIVE:_YSI_RE_NATIVE<%0:%1|||%2|||%3:%4>%5{%6;%7:%8:%9} _YSI_RE_NATIVE<%1|||%2|||%4>%5{%6;return%8:%9}
  62. #define _YSI_NO_TAG_NATIVE:_YSI_RE_NATIVE<%0:%1|||%2|||%3:%4>_YSI_RE_STOCK<%5:%6|||%7|||_:%8:%9> _YSI_RE_NATIVE<%1|||%2|||%4>_YSI_RE_STOCK<%5:%6|||%7|||%8:%9>
  63. #include <a_samp>*/
  64. #include "internal\y_version"
  65. #include "y_amx"
  66. #include "internal\y_shortfunc"
  67. #include "y_debug"
  68. #include "y_va"
  69. #include "internal\y_natives"
  70. #if defined RUN_TESTS
  71. #define Debug_PrintT va_printf
  72. #else
  73. #if _DEBUG > 0 || defined _YSI_SPECIAL_DEBUG
  74. #define RUN_TESTS
  75. #define _AUTO_RUN_TESTS
  76. //#if defined _YSI_SPECIAL_DEBUG
  77. // #define TP printf
  78. // //#define TC(%0); %0
  79. #define Debug_PrintT va_printf
  80. //#else
  81. // //#define TP P:0
  82. // //#define TC C:0
  83. //#endif
  84. #else
  85. #define Debug_PrintT(%0);
  86. // #define Tests:%1() stock bool:Tests_@%1()
  87. // #define Test:%1() stock bool:Test_@%1()
  88. // #define TestInit:%1() stock Init_@%1()
  89. // #define TestClose:%1() stock Shut_@%1()
  90. #endif
  91. #endif
  92. #define TEST_TRUE(%0) Testing_Test(bool:(%0))
  93. #define TEST_FALSE(%0) Testing_Test(!(%0))
  94. #define TEST_NULL(%0) Testing_Test((%0) == 0)
  95. #define TEST_N(%0,%1) Testing_Test((%0) == (%1))
  96. #define TEST_TRUE_EX(%0,%2) Testing_Test(bool:(%0), (%2))
  97. #define TEST_FALSE_EX(%0,%2) Testing_Test(!(%0), (%2))
  98. #define TEST_NULL_EX(%0,%2) Testing_Test((%0) == 0, (%2))
  99. #define TEST_N_EX(%0,%1,%2) Testing_Test((%0) == (%1), (%2))
  100. #define ASSERT(%0) Testing_Test(%0,__name)
  101. #define ASSERT_TRUE(%0) Testing_Test(!!%0,__name)
  102. #define ASSERT_FALSE(%0) Testing_Test(!%0,__name)
  103. #define ASSERT_NULL(%0) Testing_Test(0==%0,__name)
  104. #define ASSERT_N:%1(%0) Testing_Test(%1==%0,__name)
  105. #define TEST_FAILED "*** Test Failed:"
  106. #define TEST_PASSED "*** Test Passed:"
  107. static stock
  108. YSI_g_sTests,
  109. YSI_g_sFails;
  110. stock Testing_Test(bool:x, str[] = "", va_args<>)
  111. {
  112. P:3("Testing_Test called: %i, \"%s\"", x, str);
  113. ++YSI_g_sTests;
  114. if (!x)
  115. {
  116. if (numargs() == 2)
  117. {
  118. ++YSI_g_sFails;
  119. P:T(TEST_FAILED " %s", va_start<1>);
  120. //printf("*** Test failed: %s", str);
  121. }
  122. else
  123. {
  124. P:T(str, va_start<2>);
  125. }
  126. }
  127. else if (numargs() == 2)
  128. {
  129. P:T(TEST_PASSED " %s", va_start<1>);
  130. }
  131. else
  132. {
  133. P:T(str, va_start<2>);
  134. }
  135. }
  136. /*----------------------------------------------------------------------------*\
  137. Function:
  138. Testing_Run
  139. Params:
  140. &tests - Number of tests run.
  141. &fails - Number of tests which failed.
  142. buffer[33] - The name of the first test which failed.
  143. Return:
  144. Wether all tests were sucessful or not.
  145. Notes:
  146. -
  147. native Testing_Run(&tests, &fails, buffer[33] = "");
  148. \*----------------------------------------------------------------------------*/
  149. stock bool:Testing_Run(&tests, &fails, lastfail[33] = "", bool:p = false)
  150. {
  151. P:3("bool:Testing_Run called: %i, %i, \"%s\", %i", tests, fails, lastfail, p);
  152. #pragma unused p
  153. #if defined RUN_TESTS
  154. P:2("Testing_Run() called");
  155. new
  156. idx,
  157. buffer[32];
  158. while ((idx = AMX_GetPublicName(idx, buffer, "Test_@")))
  159. {
  160. strunpack(buffer, buffer);
  161. ++YSI_g_sTests;
  162. // Call the setup function if there is one.
  163. buffer[0] = 'I';
  164. buffer[1] = 'n';
  165. buffer[2] = 'i';
  166. buffer[3] = 't';
  167. CallLocalFunction(buffer, "");
  168. // Call the test.
  169. buffer[0] = 'T';
  170. buffer[1] = 'e';
  171. buffer[2] = 's';
  172. buffer[3] = 't';
  173. P:5("Testing_Run(): Calling %s", buffer[6]);
  174. if (!CallLocalFunction(buffer, ""))
  175. {
  176. if (YSI_g_sFails)
  177. {
  178. ++YSI_g_sFails;
  179. }
  180. else
  181. {
  182. fails = 0;
  183. // Copy the string over.
  184. while ((lastfail[fails] = buffer[fails + 6])) ++fails;
  185. YSI_g_sFails = 1;
  186. }
  187. //TC(if (p) printf("*** Test failed: %s", buffer[fails + 6]););
  188. }
  189. // Call the shutdown function if there is one.
  190. buffer[0] = 'S';
  191. buffer[1] = 'h';
  192. buffer[2] = 'u';
  193. buffer[3] = 't';
  194. CallLocalFunction(buffer, "");
  195. }
  196. while ((idx = AMX_GetPublicName(idx, buffer, "Tezt_@")))
  197. {
  198. strunpack(buffer, buffer);
  199. //++YSI_g_sTests;
  200. // Call the setup function if there is one.
  201. buffer[0] = 'I';
  202. buffer[1] = 'n';
  203. buffer[2] = 'i';
  204. buffer[3] = 't';
  205. CallLocalFunction(buffer, "");
  206. // Call the test.
  207. buffer[0] = 'T';
  208. buffer[1] = 'e';
  209. buffer[2] = 'z';
  210. buffer[3] = 't';
  211. fails = YSI_g_sFails;
  212. P:5("Testing_Run(): Calling %s", buffer[6]);
  213. CallLocalFunction(buffer, "");
  214. /*if (YSI_g_sFails != fails)
  215. {
  216. if (YSI_g_sFails)
  217. {
  218. ++YSI_g_sFails;
  219. }
  220. else
  221. {
  222. fails = 0;
  223. // Copy the string over.
  224. while ((lastfail[fails] = buffer[fails + 6])) ++fails;
  225. YSI_g_sFails = 1;
  226. }
  227. //C:1(if (p) printf("*** Test failed: %s", buffer[fails + 6]););
  228. }*/
  229. // Call the shutdown function if there is one.
  230. buffer[0] = 'S';
  231. buffer[1] = 'h';
  232. buffer[2] = 'u';
  233. buffer[3] = 't';
  234. CallLocalFunction(buffer, "");
  235. }
  236. tests = YSI_g_sTests;
  237. fails = YSI_g_sFails;
  238. return fails == 0;
  239. #else
  240. #pragma unused tests, fails, lastfail
  241. return true;
  242. #endif
  243. }
  244. #if defined RUN_TESTS
  245. #define Test:%1() forward bool:Tezt_@%1(); public bool:Tezt_@%1() for(new string:__name[]=#%1,bool:__once=(printf("*** Test %s start", __name) || TRUE);__once;__once=(printf(" ") && FALSE))
  246. #define Tests:%1() forward bool:Test_@%1(); public bool:Test_@%1() for(new string:__name[]=#%1,bool:__once=(printf("*** Test %s start", __name) || TRUE);__once;__once=(printf(" ") && FALSE))
  247. #define TestInit:%1() forward Init_@%1(); public Init_@%1() for(new string:__name[]=#%1,bool:__once=TRUE;__once;__once=(printf(" ", __name) && FALSE))
  248. #define TestClose:%1() forward Shut_@%1(); public Shut_@%1() for(new string:__name[]=#%1,bool:__once=TRUE;__once;__once=(printf(" ", __name) && FALSE))
  249. #if defined _AUTO_RUN_TESTS
  250. #if !defined FILTERSCRIPT
  251. // Hook main in gamemodes.
  252. main()
  253. {
  254. // Disable error messages (as we're likely to generate them).
  255. state ysi_debug : off;
  256. CallLocalFunction("Testing_main", "");
  257. new
  258. tests,
  259. fails;
  260. printf(" ");
  261. printf(" ||===================|| ");
  262. printf(" || STARTING TESTS... || ");
  263. printf(" ||===================|| ");
  264. printf(" ");
  265. Testing_Run(tests, fails, _, true);
  266. printf("*** Tests: %d, Fails: %d", tests, fails);
  267. if (!fails)
  268. {
  269. printf(" ");
  270. printf(" ||===================|| ");
  271. printf(" || ALL TESTS PASSED! || ");
  272. printf(" ||===================|| ");
  273. printf(" ");
  274. }
  275. state ysi_debug : on;
  276. }
  277. #define main forward Testing_main(); public Testing_main
  278. #endif
  279. #else
  280. Testing_RunAll()
  281. {
  282. // Disable error messages (as we're likely to generate them).
  283. state ysi_debug : off;
  284. new
  285. tests,
  286. fails;
  287. printf(" ");
  288. printf(" ||===================|| ");
  289. printf(" || STARTING TESTS... || ");
  290. printf(" ||===================|| ");
  291. printf(" ");
  292. Testing_Run(tests, fails, _, true);
  293. printf("*** Tests: %d, Fails: %d", tests, fails);
  294. if (!fails)
  295. {
  296. printf(" ");
  297. printf(" ||===================|| ");
  298. printf(" || ALL TESTS PASSED! || ");
  299. printf(" ||===================|| ");
  300. printf(" ");
  301. }
  302. state ysi_debug : on;
  303. }
  304. #endif
  305. #else
  306. #define Tests:%1() stock bool:Tests_@%1()
  307. #define Test:%1() stock bool:Test_@%1()
  308. #define TestInit:%1() stock Init_@%1()
  309. #define TestClose:%1() stock Shut_@%1()
  310. #endif