DUnitCompatibleInterface.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. {%MainUnit fpcunit.pp}
  2. {
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2022 by Michael van Canney and other members of the
  5. Free Pascal development team
  6. DUnit compatibility
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$IFDEF read_interface}
  14. class procedure Check(pValue: boolean; const pMessage: string = '');
  15. class procedure CheckEquals(expected, actual: extended; const msg: string = ''); overload;
  16. class procedure CheckEquals(expected, actual: extended; delta: extended; const msg: string = ''); overload;
  17. class procedure CheckEquals(expected, actual: AnsiString; const msg: string = ''); overload;
  18. class procedure CheckEquals(expected, actual: unicodestring;const msg: string = ''); overload;
  19. class procedure CheckEquals(expected, actual: integer; const msg: string = ''); overload;
  20. class procedure CheckEquals(expected, actual: boolean; const msg: string = ''); overload;
  21. class procedure CheckEquals(expected, actual: TClass; const msg: string = ''); overload;
  22. class procedure CheckEquals(expected, actual: int64; const msg: string = ''); overload;
  23. class procedure CheckEquals(expected, actual: QWord; const msg: string = ''); overload;
  24. class procedure CheckNotEquals(expected, actual: Ansistring; const msg: string = ''); overload;
  25. class procedure CheckNotEquals(expected, actual: unicodestring; const msg: string = ''); overload;
  26. class procedure CheckNotEquals(expected, actual: integer; const msg: string = ''); overload; virtual;
  27. class procedure CheckNotEquals(expected, actual: boolean; const msg: string = ''); overload; virtual;
  28. class procedure CheckNotEquals(expected: extended; actual: extended; delta: extended = 0; const msg: string = ''); overload; virtual;
  29. class procedure CheckNotEquals(expected, actual: int64; const msg: string = ''); overload; virtual;
  30. class procedure CheckNotEquals(expected, actual: QWord; const msg: string = ''); overload; virtual;
  31. class procedure CheckNull(obj: IUnknown; const msg: string = ''); overload;
  32. class procedure CheckNull(obj: TObject; const msg: string = ''); overload;
  33. class procedure CheckNotNull(obj: TObject; const msg: string = ''); overload;
  34. class procedure CheckNotNull(obj: IUnknown; const msg: string = ''); overload; virtual;
  35. class procedure CheckIs(obj :TObject; pClass: TClass; const msg: string = ''); overload;
  36. class procedure CheckSame(expected, actual: TObject; const msg: string = ''); overload;
  37. class procedure CheckTrue(condition: Boolean; const msg: string = '');
  38. class procedure CheckFalse(condition: Boolean; const msg: string = '');
  39. class procedure CheckException(AMethod: TRunMethod; AExceptionClass: ExceptClass; const msg: string = '');
  40. class function EqualsErrorMessage(const expected, actual: string; const ErrorMsg: string): string;
  41. class function NotEqualsErrorMessage(const expected, actual: string; const ErrorMsg: string): string;
  42. class function Suite: TTest;
  43. {
  44. *** TODO ***
  45. procedure CheckEqualsBin(expected, actual: longword; const msg: string = ''; digits: integer=32); virtual;
  46. procedure CheckEqualsHex(expected, actual: longword; const msg: string = ''; digits: integer=8); virtual;
  47. procedure CheckNotEqualsBin(expected, actual: longword; const msg: string = ''; digits: integer=32); virtual;
  48. procedure CheckNotEqualsHex(expected, actual: longword; const msg: string = ''; digits: integer=8); virtual;
  49. procedure CheckNotNull(obj :IUnknown; const msg :string = ''); overload; virtual;
  50. procedure CheckSame(expected, actual: IUnknown; const msg: string = ''); overload; virtual;
  51. procedure CheckException(AMethod: TTestMethod; AExceptionClass: TClass; msg :string = '');
  52. procedure CheckInherits(expected, actual: TClass; const msg: string = ''); overload; virtual;
  53. }
  54. {$ENDIF read_interface}
  55. {$IFDEF read_implementation}
  56. class procedure TAssert.Check(pValue: boolean; const pMessage: string);
  57. begin
  58. AssertTrue(pMessage, pValue);
  59. end;
  60. class procedure TAssert.CheckEquals(expected, actual: extended; const msg: string);
  61. begin
  62. CheckEquals(expected, actual, 0, msg);
  63. end;
  64. class procedure TAssert.CheckEquals(expected, actual: extended;
  65. delta: extended; const msg: string);
  66. begin
  67. AssertEquals(msg, expected, actual, delta);
  68. end;
  69. class procedure TAssert.CheckEquals(expected, actual: Ansistring; const msg: string);
  70. begin
  71. AssertEquals(msg, expected, actual);
  72. end;
  73. class procedure TAssert.CheckEquals(expected, actual: unicodestring; const msg: string);
  74. begin
  75. AssertEquals(msg, expected, actual);
  76. end;
  77. class procedure TAssert.CheckEquals(expected, actual: integer; const msg: string);
  78. begin
  79. AssertEquals(msg, expected, actual);
  80. end;
  81. class procedure TAssert.CheckEquals(expected, actual: boolean; const msg: string);
  82. begin
  83. AssertEquals(msg, expected, actual);
  84. end;
  85. class procedure TAssert.CheckEquals(expected, actual: TClass; const msg: string);
  86. begin
  87. AssertEquals(msg, expected, actual);
  88. end;
  89. class procedure TAssert.CheckEquals(expected, actual: QWord;
  90. const msg: string);
  91. begin
  92. AssertEquals(msg, expected, actual);
  93. end;
  94. class procedure TAssert.CheckEquals(expected, actual: int64;
  95. const msg: string);
  96. begin
  97. AssertEquals(msg, expected, actual);
  98. end;
  99. class procedure TAssert.CheckNotEquals(expected, actual: Ansistring; const msg: string);
  100. begin
  101. if AnsiCompareStr(Expected, Actual) = 0 then
  102. Fail(ComparisonMsg(msg, Expected, Actual, false))
  103. else
  104. Inc(AssertCount);
  105. end;
  106. class procedure TAssert.CheckNotEquals(expected, actual: unicodestring; const msg: string ='');
  107. begin
  108. if (Expected=Actual) then
  109. Fail(ComparisonMsg(msg, Expected, Actual, false))
  110. else
  111. Inc(AssertCount);
  112. end;
  113. class procedure TAssert.CheckNotEquals(expected, actual: integer; const msg: string);
  114. begin
  115. if (expected = actual) then
  116. Fail(ComparisonMsg(msg, IntToStr(expected), IntToStr(actual), false))
  117. else
  118. Inc(AssertCount);
  119. end;
  120. class procedure TAssert.CheckNotEquals(expected, actual: boolean; const msg: string);
  121. begin
  122. if (expected = actual) then
  123. Fail(ComparisonMsg(msg, BoolToStr(expected), BoolToStr(actual), false))
  124. else
  125. Inc(AssertCount);
  126. end;
  127. class procedure TAssert.CheckNotEquals(expected: extended; actual: extended;
  128. delta: extended; const msg: string);
  129. begin
  130. if (abs(expected-actual) <= delta) then
  131. FailNotEquals(FloatToStr(expected), FloatToStr(actual), msg, nil)
  132. else
  133. Inc(AssertCount);
  134. end;
  135. class procedure TAssert.CheckNotEquals(expected, actual: QWord;
  136. const msg: string);
  137. begin
  138. if (expected = actual) then
  139. Fail(ComparisonMsg(msg, IntToStr(expected), IntToStr(actual), false))
  140. else
  141. Inc(AssertCount);
  142. end;
  143. class procedure TAssert.CheckNotEquals(expected, actual: int64;
  144. const msg: string);
  145. begin
  146. if (expected = actual) then
  147. Fail(ComparisonMsg(msg, IntToStr(expected), IntToStr(actual), false))
  148. else
  149. Inc(AssertCount);
  150. end;
  151. class procedure TAssert.CheckNull(obj: IUnknown; const msg: string);
  152. begin
  153. AssertNullIntf(msg, obj);
  154. end;
  155. class procedure TAssert.CheckNull(obj: TObject; const msg: string);
  156. begin
  157. AssertNull(msg, obj);
  158. end;
  159. class procedure TAssert.CheckNotNull(obj: TObject; const msg: string);
  160. begin
  161. AssertNotNull(msg, obj);
  162. end;
  163. class procedure TAssert.CheckNotNull(obj: IUnknown; const msg: string);
  164. begin
  165. AssertNotNullIntf(msg, obj);
  166. end;
  167. class procedure TAssert.CheckIs(obj: TObject; pClass: TClass; const msg: string);
  168. begin
  169. Assert(pClass <> nil);
  170. if obj = nil then
  171. Fail(ComparisonMsg(msg, pClass.ClassName, 'nil'))
  172. else if not obj.ClassType.InheritsFrom(pClass) then
  173. Fail(ComparisonMsg(msg, pClass.ClassName, obj.ClassName))
  174. else
  175. Inc(AssertCount);
  176. end;
  177. class procedure TAssert.CheckSame(expected, actual: TObject; const msg: string);
  178. begin
  179. AssertSame(msg, expected, actual);
  180. end;
  181. class procedure TAssert.CheckTrue(condition: Boolean; const msg: string);
  182. begin
  183. if (not condition) then
  184. FailNotEquals(BoolToStr(true, true), BoolToStr(false, true), msg, nil)
  185. else
  186. Inc(AssertCount);
  187. end;
  188. class procedure TAssert.CheckFalse(condition: Boolean; const msg: string);
  189. begin
  190. if (condition) then
  191. FailNotEquals(BoolToStr(false, true), BoolToStr(true, true), msg, nil)
  192. else
  193. Inc(AssertCount);
  194. end;
  195. class procedure TAssert.CheckException(AMethod: TRunMethod; AExceptionClass: ExceptClass; const msg: string = '');
  196. begin
  197. AssertException(msg, AExceptionClass, AMethod);
  198. end;
  199. class function TAssert.EqualsErrorMessage(const expected, actual: string;
  200. const ErrorMsg: string): string;
  201. begin
  202. if (ErrorMsg <> '') then
  203. Result := Format(sExpectedButWasAndMessageFmt, [ErrorMsg + ', ', expected, actual])
  204. else
  205. Result := Format(sExpectedButWasFmt, [expected, actual])
  206. end;
  207. class function TAssert.NotEqualsErrorMessage(const expected, actual: string;
  208. const ErrorMsg: string): string;
  209. begin
  210. if (ErrorMsg <> '') then
  211. Result := Format(sExpectedButWasAndMessageFmt, [ErrorMsg, expected, actual])
  212. else
  213. Result := Format(sExpectedButWasFmt, [expected, actual]);
  214. end;
  215. class function TAssert.Suite: TTest;
  216. begin
  217. result := TTestSuite.Create(self);
  218. end;
  219. {$ENDIF read_implementation}