Test.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit Test;
  26. {$I Tests.inc}
  27. interface
  28. uses
  29. SysUtils;
  30. type
  31. ErrorCode = {$IFDEF FPC}LongInt{$ELSE}Cardinal{$ENDIF};
  32. TTestRoutine = procedure;
  33. TTestRoutineArgs = procedure(const AArgs: array of const);
  34. EOSErrorClass = class of EOSError;
  35. { args }
  36. procedure AssertExcept(ARoutine: TTestRoutineArgs; AExceptClass: ExceptClass;
  37. const AError: string; const AArgs: array of const); overload;
  38. procedure AssertExcept(ARoutine: TTestRoutineArgs; AExceptClass: ExceptClass;
  39. const AArgs: array of const); overload;
  40. procedure AssertOSExcept(ARoutine: TTestRoutineArgs; const AError: string;
  41. ACode: ErrorCode; const AArgs: array of const); overload;
  42. procedure AssertOSExcept(ARoutine: TTestRoutineArgs; const AError: string;
  43. const AArgs: array of const); overload;
  44. procedure AssertOSExcept(ARoutine: TTestRoutineArgs; ACode: ErrorCode;
  45. const AArgs: array of const); overload;
  46. procedure AssertOSExcept(ARoutine: TTestRoutineArgs;
  47. const AArgs: array of const); overload;
  48. { no args }
  49. procedure AssertExcept(ARoutine: TTestRoutine; AExceptClass: ExceptClass;
  50. const AError: string); overload;
  51. procedure AssertExcept(ARoutine: TTestRoutine;
  52. AExceptClass: ExceptClass); overload;
  53. procedure AssertOSExcept(ARoutine: TTestRoutine; const AError: string;
  54. ACode: ErrorCode); overload;
  55. procedure AssertOSExcept(ARoutine: TTestRoutine;
  56. const AError: string); overload;
  57. procedure AssertOSExcept(ARoutine: TTestRoutine; ACode: ErrorCode); overload;
  58. procedure AssertOSExcept(ARoutine: TTestRoutine); overload;
  59. implementation
  60. { args }
  61. procedure AssertExcept(ARoutine: TTestRoutineArgs; AExceptClass: ExceptClass;
  62. const AError: string; const AArgs: array of const);
  63. var
  64. OK: Boolean;
  65. begin
  66. Assert(Assigned(ARoutine));
  67. OK := False;
  68. try
  69. ARoutine(AArgs);
  70. except
  71. on E: Exception do
  72. OK := (E.ClassType = AExceptClass) and (E.Message = AError);
  73. end;
  74. Assert(OK);
  75. end;
  76. procedure AssertExcept(ARoutine: TTestRoutineArgs; AExceptClass: ExceptClass;
  77. const AArgs: array of const);
  78. var
  79. OK: Boolean;
  80. begin
  81. Assert(Assigned(ARoutine));
  82. OK := False;
  83. try
  84. ARoutine(AArgs);
  85. except
  86. on E: Exception do
  87. OK := E.ClassType = AExceptClass;
  88. end;
  89. Assert(OK);
  90. end;
  91. procedure AssertOSExcept(ARoutine: TTestRoutineArgs; const AError: string;
  92. ACode: ErrorCode; const AArgs: array of const);
  93. var
  94. OK: Boolean;
  95. begin
  96. Assert(Assigned(ARoutine));
  97. OK := False;
  98. try
  99. ARoutine(AArgs);
  100. except
  101. on E: EOSError do
  102. OK := (E.ErrorCode = ACode) and (E.Message = AError);
  103. end;
  104. Assert(OK);
  105. end;
  106. procedure AssertOSExcept(ARoutine: TTestRoutineArgs; const AError: string;
  107. const AArgs: array of const);
  108. var
  109. OK: Boolean;
  110. begin
  111. Assert(Assigned(ARoutine));
  112. OK := False;
  113. try
  114. ARoutine(AArgs);
  115. except
  116. on E: EOSError do
  117. OK := E.Message = AError;
  118. end;
  119. Assert(OK);
  120. end;
  121. procedure AssertOSExcept(ARoutine: TTestRoutineArgs; ACode: ErrorCode;
  122. const AArgs: array of const);
  123. var
  124. OK: Boolean;
  125. begin
  126. Assert(Assigned(ARoutine));
  127. OK := False;
  128. try
  129. ARoutine(AArgs);
  130. except
  131. on E: EOSError do
  132. OK := E.ErrorCode = ACode;
  133. end;
  134. Assert(OK);
  135. end;
  136. procedure AssertOSExcept(ARoutine: TTestRoutineArgs;
  137. const AArgs: array of const);
  138. var
  139. OK: Boolean;
  140. begin
  141. Assert(Assigned(ARoutine));
  142. OK := False;
  143. try
  144. ARoutine(AArgs);
  145. except
  146. on E: EOSError do
  147. OK := True;
  148. end;
  149. Assert(OK);
  150. end;
  151. { no args }
  152. procedure AssertExcept(ARoutine: TTestRoutine; AExceptClass: ExceptClass;
  153. const AError: string);
  154. var
  155. OK: Boolean;
  156. begin
  157. Assert(Assigned(ARoutine));
  158. OK := False;
  159. try
  160. ARoutine;
  161. except
  162. on E: Exception do
  163. OK := (E.ClassType = AExceptClass) and (E.Message = AError);
  164. end;
  165. Assert(OK);
  166. end;
  167. procedure AssertExcept(ARoutine: TTestRoutine; AExceptClass: ExceptClass);
  168. var
  169. OK: Boolean;
  170. begin
  171. Assert(Assigned(ARoutine));
  172. OK := False;
  173. try
  174. ARoutine;
  175. except
  176. on E: Exception do
  177. OK := E.ClassType = AExceptClass;
  178. end;
  179. Assert(OK);
  180. end;
  181. procedure AssertOSExcept(ARoutine: TTestRoutine; const AError: string;
  182. ACode: ErrorCode);
  183. var
  184. OK: Boolean;
  185. begin
  186. Assert(Assigned(ARoutine));
  187. OK := False;
  188. try
  189. ARoutine;
  190. except
  191. on E: EOSError do
  192. OK := (E.ErrorCode = ACode) and (E.Message = AError);
  193. end;
  194. Assert(OK);
  195. end;
  196. procedure AssertOSExcept(ARoutine: TTestRoutine; const AError: string);
  197. var
  198. OK: Boolean;
  199. begin
  200. Assert(Assigned(ARoutine));
  201. OK := False;
  202. try
  203. ARoutine;
  204. except
  205. on E: EOSError do
  206. OK := E.Message = AError;
  207. end;
  208. Assert(OK);
  209. end;
  210. procedure AssertOSExcept(ARoutine: TTestRoutine; ACode: ErrorCode);
  211. var
  212. OK: Boolean;
  213. begin
  214. Assert(Assigned(ARoutine));
  215. OK := False;
  216. try
  217. ARoutine;
  218. except
  219. on E: EOSError do
  220. OK := E.ErrorCode = ACode;
  221. end;
  222. Assert(OK);
  223. end;
  224. procedure AssertOSExcept(ARoutine: TTestRoutine);
  225. var
  226. OK: Boolean;
  227. begin
  228. Assert(Assigned(ARoutine));
  229. OK := False;
  230. try
  231. ARoutine;
  232. except
  233. on E: EOSError do
  234. OK := True;
  235. end;
  236. Assert(OK);
  237. end;
  238. end.