asserttest.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {$mode objfpc}
  2. {$h+}
  3. {
  4. $Id$
  5. This file is part of the Free Component Library (FCL)
  6. Copyright (c) 2004 by Dean Zobec
  7. Port to Free Pascal of the JUnit framework.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit asserttest;
  15. interface
  16. uses
  17. fpcunit;
  18. type
  19. TAssertTest = class(TTestCase)
  20. published
  21. procedure TestFail;
  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. implementation
  34. procedure TAssertTest.TestFail;
  35. begin
  36. try
  37. fail('Wrong or no exception raised with fail');
  38. except
  39. on E: EAssertionfailedError do
  40. Exit;
  41. end;
  42. raise EAssertionFailedError.Create;
  43. end;
  44. procedure TAssertTest.TestAssertSame;
  45. var
  46. o: TObject;
  47. o1: TObject;
  48. begin
  49. o := TObject.Create;
  50. AssertSame(o, o);
  51. o1 := TObject.Create;
  52. try
  53. AssertSame(o, o1);
  54. except
  55. on E: EAssertionFailedError do
  56. begin
  57. o.Free;
  58. o1.Free;
  59. Exit;
  60. end;
  61. end;
  62. o.Free;
  63. o1.Free;
  64. Fail('Wrong or no exception raised');
  65. end;
  66. procedure TAssertTest.TestAssertSameNull;
  67. var
  68. a, b: TObject;
  69. begin
  70. a := nil;
  71. b := nil;
  72. AssertSame(a, b);
  73. AssertSame(nil, a);
  74. AssertSame(a, nil);
  75. end;
  76. procedure TAssertTest.TestAssertNotSameFailsNull;
  77. var
  78. a, b: TObject;
  79. begin
  80. a := nil;
  81. b := nil;
  82. try
  83. assertNotSame(a, b);
  84. except
  85. on E: EAssertionFailedError do
  86. Exit;
  87. end;
  88. fail('error: nil should equal nil');
  89. end;
  90. procedure TAssertTest.TestAssertStringEquals;
  91. begin
  92. AssertEquals('a', 'a')
  93. end;
  94. procedure TAssertTest.TestNullNotSameObject;
  95. var
  96. obj: TObject;
  97. begin
  98. obj := TObject.Create;
  99. try
  100. AssertSame(nil, obj);
  101. except
  102. on E: EAssertionFailedError do
  103. begin
  104. obj.Free;
  105. Exit;
  106. end;
  107. end;
  108. Fail('error comparing a valid obj instance with nil');
  109. end;
  110. procedure TAssertTest.TestAssertNull;
  111. var
  112. obj: TObject;
  113. begin
  114. AssertNull(nil);
  115. obj := TObject.Create;
  116. try
  117. AssertNull(obj);
  118. except
  119. on E: EAssertionFailedError do
  120. begin
  121. obj.Free;
  122. Exit;
  123. end;
  124. end;
  125. obj.Free;
  126. Fail('failure: obj is not null!');
  127. end;
  128. procedure TAssertTest.TestAssertNotNull;
  129. var
  130. obj: TObject;
  131. begin
  132. obj := TObject.Create;
  133. AssertNotNull(obj);
  134. try
  135. AssertNotNull(nil);
  136. except
  137. on E: EAssertionFailedError do
  138. begin
  139. obj.Free;
  140. Exit;
  141. end;
  142. end;
  143. obj.Free;
  144. Fail('error: nil is not a valid object');
  145. end;
  146. procedure TAssertTest.TestAssertTrue;
  147. begin
  148. assertTrue(true);
  149. try
  150. assertTrue(false);
  151. except
  152. on E: EAssertionFailedError do
  153. Exit;
  154. end;
  155. fail('error asserting true');
  156. end;
  157. procedure TAssertTest.TestAssertFalse;
  158. begin
  159. assertFalse(false);
  160. try
  161. assertFalse(true);
  162. except
  163. on E: EAssertionFailedError do
  164. Exit;
  165. end;
  166. fail('error asserting false');
  167. end;
  168. procedure TAssertTest.TestAssertNotSame;
  169. var
  170. obj: TObject;
  171. obj1: TObject;
  172. begin
  173. obj := TObject.Create;
  174. obj1 := TObject.Create;
  175. AssertNotSame(obj, nil);
  176. AssertNotSame(nil, obj);
  177. AssertNotSame(obj, obj1);
  178. try
  179. AssertNotSame(obj, obj)
  180. except
  181. on E: EAssertionFailedError do
  182. begin
  183. obj.Free;
  184. obj1.Free;
  185. Exit;
  186. end;
  187. end;
  188. obj.Free;
  189. obj1.Free;
  190. Fail('Error: Objects are the same!');
  191. end;
  192. end.