asserttest.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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;
  17. type
  18. TAssertTest = class(TTestCase)
  19. published
  20. procedure TestFail;
  21. procedure TestAssertSame;
  22. procedure TestAssertSameNull;
  23. procedure TestAssertNotSameFailsNull;
  24. procedure TestAssertStringEquals;
  25. procedure TestNullNotSameObject;
  26. procedure TestAssertNull;
  27. procedure TestAssertNotNull;
  28. procedure TestAssertTrue;
  29. procedure TestAssertFalse;
  30. procedure TestAssertNotSame;
  31. end;
  32. implementation
  33. procedure TAssertTest.TestFail;
  34. begin
  35. try
  36. fail('Wrong or no exception raised with fail');
  37. except
  38. on E: EAssertionfailedError do
  39. Exit;
  40. end;
  41. raise EAssertionFailedError.Create;
  42. end;
  43. procedure TAssertTest.TestAssertSame;
  44. var
  45. o: TObject;
  46. o1: TObject;
  47. begin
  48. o := TObject.Create;
  49. AssertSame(o, o);
  50. o1 := TObject.Create;
  51. try
  52. AssertSame(o, o1);
  53. except
  54. on E: EAssertionFailedError do
  55. begin
  56. o.Free;
  57. o1.Free;
  58. Exit;
  59. end;
  60. end;
  61. o.Free;
  62. o1.Free;
  63. Fail('Wrong or no exception raised');
  64. end;
  65. procedure TAssertTest.TestAssertSameNull;
  66. var
  67. a, b: TObject;
  68. begin
  69. a := nil;
  70. b := nil;
  71. AssertSame(a, b);
  72. AssertSame(nil, a);
  73. AssertSame(a, nil);
  74. end;
  75. procedure TAssertTest.TestAssertNotSameFailsNull;
  76. var
  77. a, b: TObject;
  78. begin
  79. a := nil;
  80. b := nil;
  81. try
  82. assertNotSame(a, b);
  83. except
  84. on E: EAssertionFailedError do
  85. Exit;
  86. end;
  87. fail('error: nil should equal nil');
  88. end;
  89. procedure TAssertTest.TestAssertStringEquals;
  90. begin
  91. AssertEquals('a', 'a')
  92. end;
  93. procedure TAssertTest.TestNullNotSameObject;
  94. var
  95. obj: TObject;
  96. begin
  97. obj := TObject.Create;
  98. try
  99. AssertSame(nil, obj);
  100. except
  101. on E: EAssertionFailedError do
  102. begin
  103. obj.Free;
  104. Exit;
  105. end;
  106. end;
  107. Fail('error comparing a valid obj instance with nil');
  108. end;
  109. procedure TAssertTest.TestAssertNull;
  110. var
  111. obj: TObject;
  112. begin
  113. AssertNull(nil);
  114. obj := TObject.Create;
  115. try
  116. AssertNull(obj);
  117. except
  118. on E: EAssertionFailedError do
  119. begin
  120. obj.Free;
  121. Exit;
  122. end;
  123. end;
  124. obj.Free;
  125. Fail('failure: obj is not null!');
  126. end;
  127. procedure TAssertTest.TestAssertNotNull;
  128. var
  129. obj: TObject;
  130. begin
  131. obj := TObject.Create;
  132. AssertNotNull(obj);
  133. try
  134. AssertNotNull(nil);
  135. except
  136. on E: EAssertionFailedError do
  137. begin
  138. obj.Free;
  139. Exit;
  140. end;
  141. end;
  142. obj.Free;
  143. Fail('error: nil is not a valid object');
  144. end;
  145. procedure TAssertTest.TestAssertTrue;
  146. begin
  147. assertTrue(true);
  148. try
  149. assertTrue(false);
  150. except
  151. on E: EAssertionFailedError do
  152. Exit;
  153. end;
  154. fail('error asserting true');
  155. end;
  156. procedure TAssertTest.TestAssertFalse;
  157. begin
  158. assertFalse(false);
  159. try
  160. assertFalse(true);
  161. except
  162. on E: EAssertionFailedError do
  163. Exit;
  164. end;
  165. fail('error asserting false');
  166. end;
  167. procedure TAssertTest.TestAssertNotSame;
  168. var
  169. obj: TObject;
  170. obj1: TObject;
  171. begin
  172. obj := TObject.Create;
  173. obj1 := TObject.Create;
  174. AssertNotSame(obj, nil);
  175. AssertNotSame(nil, obj);
  176. AssertNotSame(obj, obj1);
  177. try
  178. AssertNotSame(obj, obj)
  179. except
  180. on E: EAssertionFailedError do
  181. begin
  182. obj.Free;
  183. obj1.Free;
  184. Exit;
  185. end;
  186. end;
  187. obj.Free;
  188. obj1.Free;
  189. Fail('Error: Objects are the same!');
  190. end;
  191. initialization
  192. RegisterTests([TAssertTest]);
  193. end.