asserttest.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. This file is part of the Free Component Library (FCL)
  5. Copyright (c) 2004 by Dean Zobec
  6. Port to Free Pascal of the JUnit framework.
  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. unit asserttest;
  14. interface
  15. uses
  16. fpcunit, testregistry, sysutils;
  17. type
  18. TAssertTest = class(TTestCase)
  19. published
  20. procedure TestFail;
  21. procedure TestIgnore;
  22. procedure TestAssertSame;
  23. procedure TestAssertSameNull;
  24. procedure TestAssertNotSameFailsNull;
  25. procedure TestAssertStringEquals;
  26. procedure TestNullNotSameObject;
  27. procedure TestAssertNull;
  28. procedure TestAssertNotNull;
  29. procedure TestAssertTrue;
  30. procedure TestAssertFalse;
  31. procedure TestAssertNotSame;
  32. end;
  33. TMyTest = class(TTestCase)
  34. published
  35. procedure RaiseIgnoreTest;
  36. end;
  37. TTestIgnore = class(TTestCase)
  38. published
  39. procedure TestIgnoreResult;
  40. procedure TestIgnoreActivation;
  41. procedure TestIgnoreSetting;
  42. end;
  43. implementation
  44. procedure TAssertTest.TestFail;
  45. begin
  46. try
  47. fail('Wrong or no exception raised with fail');
  48. except
  49. on E: EAssertionfailedError do
  50. Exit;
  51. end;
  52. raise EAssertionFailedError.Create;
  53. end;
  54. procedure TAssertTest.TestIgnore;
  55. begin
  56. try
  57. Ignore('Ignored Test');
  58. except
  59. on E: EIgnoredTest do
  60. Exit;
  61. end;
  62. fail('Wrong or no Exception raised with ignore');
  63. end;
  64. procedure TAssertTest.TestAssertSame;
  65. var
  66. o: TObject;
  67. o1: TObject;
  68. begin
  69. o := TObject.Create;
  70. AssertSame(o, o);
  71. o1 := TObject.Create;
  72. try
  73. AssertSame(o, o1);
  74. except
  75. on E: EAssertionFailedError do
  76. begin
  77. o.Free;
  78. o1.Free;
  79. Exit;
  80. end;
  81. end;
  82. o.Free;
  83. o1.Free;
  84. Fail('Wrong or no exception raised');
  85. end;
  86. procedure TAssertTest.TestAssertSameNull;
  87. var
  88. a, b: TObject;
  89. begin
  90. a := nil;
  91. b := nil;
  92. AssertSame(a, b);
  93. AssertSame(nil, a);
  94. AssertSame(a, nil);
  95. end;
  96. procedure TAssertTest.TestAssertNotSameFailsNull;
  97. var
  98. a, b: TObject;
  99. begin
  100. a := nil;
  101. b := nil;
  102. try
  103. assertNotSame(a, b);
  104. except
  105. on E: EAssertionFailedError do
  106. Exit;
  107. end;
  108. fail('error: nil should equal nil');
  109. end;
  110. procedure TAssertTest.TestAssertStringEquals;
  111. begin
  112. AssertEquals('a', 'a')
  113. end;
  114. procedure TAssertTest.TestNullNotSameObject;
  115. var
  116. obj: TObject;
  117. begin
  118. obj := TObject.Create;
  119. try
  120. AssertSame(nil, obj);
  121. except
  122. on E: EAssertionFailedError do
  123. begin
  124. obj.Free;
  125. Exit;
  126. end;
  127. end;
  128. Fail('error comparing a valid obj instance with nil');
  129. end;
  130. procedure TAssertTest.TestAssertNull;
  131. var
  132. obj: TObject;
  133. begin
  134. AssertNull(nil);
  135. obj := TObject.Create;
  136. try
  137. AssertNull(obj);
  138. except
  139. on E: EAssertionFailedError do
  140. begin
  141. obj.Free;
  142. Exit;
  143. end;
  144. end;
  145. obj.Free;
  146. Fail('failure: obj is not null!');
  147. end;
  148. procedure TAssertTest.TestAssertNotNull;
  149. var
  150. obj: TObject;
  151. begin
  152. obj := TObject.Create;
  153. AssertNotNull(obj);
  154. try
  155. AssertNotNull(nil);
  156. except
  157. on E: EAssertionFailedError do
  158. begin
  159. obj.Free;
  160. Exit;
  161. end;
  162. end;
  163. obj.Free;
  164. Fail('error: nil is not a valid object');
  165. end;
  166. procedure TAssertTest.TestAssertTrue;
  167. begin
  168. assertTrue(true);
  169. try
  170. assertTrue(false);
  171. except
  172. on E: EAssertionFailedError do
  173. Exit;
  174. end;
  175. fail('error asserting true');
  176. end;
  177. procedure TAssertTest.TestAssertFalse;
  178. begin
  179. assertFalse(false);
  180. try
  181. assertFalse(true);
  182. except
  183. on E: EAssertionFailedError do
  184. Exit;
  185. end;
  186. fail('error asserting false');
  187. end;
  188. procedure TAssertTest.TestAssertNotSame;
  189. var
  190. obj: TObject;
  191. obj1: TObject;
  192. begin
  193. obj := TObject.Create;
  194. obj1 := TObject.Create;
  195. AssertNotSame(obj, nil);
  196. AssertNotSame(nil, obj);
  197. AssertNotSame(obj, obj1);
  198. try
  199. AssertNotSame(obj, obj)
  200. except
  201. on E: EAssertionFailedError do
  202. begin
  203. obj.Free;
  204. obj1.Free;
  205. Exit;
  206. end;
  207. end;
  208. obj.Free;
  209. obj1.Free;
  210. Fail('Error: Objects are the same!');
  211. end;
  212. procedure TMyTest.RaiseIgnoreTest;
  213. begin
  214. Ignore('This is an ignored test');
  215. AssertEquals('the compiler can count', 3, 1+1);
  216. end;
  217. procedure TTestIgnore.TestIgnoreResult;
  218. var
  219. t: TMyTest;
  220. res: TTestResult;
  221. begin
  222. t := TMyTest.CreateWithName('RaiseIgnoreTest');
  223. res := t.CreateResultAndRun;
  224. assertEquals('no test was run', 1, res.RunTests);
  225. assertEquals('no Ignored Test present', 1, res.NumberOfIgnoredTests);
  226. assertTrue('failure is not signalled as Ignored Test', TTestFailure(res.IgnoredTests[0]).IsIgnoredTest);
  227. assertEquals('wrong failure name', 'EIgnoredTest', TTestFailure(res.IgnoredTests[0]).ExceptionClassName);
  228. assertEquals('wrong message', 'This is an ignored test', TTestFailure(res.IgnoredTests[0]).ExceptionMessage);
  229. t.Free;
  230. res.Free;
  231. end;
  232. procedure TTestIgnore.TestIgnoreActivation;
  233. var
  234. t: TMyTest;
  235. res: TTestResult;
  236. begin
  237. t := TMyTest.CreateWithName('RaiseIgnoreTest');
  238. t.EnableIgnores := false;
  239. res := t.CreateResultandRun;
  240. assertEquals('no test was run', 1, res.RunTests);
  241. assertEquals('Ignored Test reported even if the switch is not active', 0, res.NumberOfIgnoredTests);
  242. assertEquals('no failure caught', 1, res.NumberOfFailures);
  243. assertFalse('failure is signalled as Ignored Test and the switch is not active',
  244. TTestFailure(res.Failures[0]).IsIgnoredTest);
  245. assertEquals('wrong failure name', 'EAssertionFailedError', TTestFailure(res.Failures[0]).ExceptionClassName);
  246. assertEquals('wrong message', 'the compiler can count expected: <3> but was: <2>', TTestFailure(res.Failures[0]).ExceptionMessage);
  247. t.Free;
  248. res.Free;
  249. end;
  250. procedure TTestIgnore.TestIgnoreSetting;
  251. var
  252. ts: TTestSuite;
  253. i: integer;
  254. begin
  255. ts := TTestSuite.Create(TTestIgnore);
  256. try
  257. AssertTrue('EnableIgnores must be True at creation', ts.EnableIgnores);
  258. for i := 0 to ts.Tests.Count - 1 do
  259. AssertTrue('EnableIgnores of Test ' + IntToStr(i) + ' must be True at creation', TTest(ts.Tests[i]).EnableIgnores);
  260. ts.EnableIgnores := False;
  261. AssertFalse('EnableIgnores was not set to false', ts.EnableIgnores);
  262. for i := 0 to ts.Tests.Count - 1 do
  263. AssertFalse('EnableIgnores of Test ' + IntToStr(i) + ' was not set to False', TTest(ts.Tests[i]).EnableIgnores);
  264. finally
  265. ts.Free;
  266. end;
  267. end;
  268. initialization
  269. RegisterTests([TAssertTest, TTestIgnore]);
  270. end.