summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-fpcunit/src/tests/suitetest.pp
blob: b8e9855e60d6b495a8623659bf6263ab55b92fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{$mode objfpc}
{$h+}
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 2004 by Dean Zobec

    Port to Free Pascal of the JUnit framework.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit suitetest;

interface

uses
  fpcunit, testreport, testregistry;

type

  TNoTestCases = class(TTestCase)
  public
    procedure NoTestCase;
  end;

  {$M+}
  TNoTestCaseClass = class(TObject)
  published
    procedure TestSuccess;
  end;
  {$M-}

  TOneTestCase = class(TTestCase)
  public
    procedure NoTestCase;
  published
    procedure OnlyOneTestCase; virtual;
  end;

  TOverrideTestCase = class(TOneTestCase)
  published
    procedure OnlyOneTestCase; override;
  end;


  TInheritedTestCase = class(TOneTestCase)
  published
    procedure Test2;
  end;

  TSuiteTest = class(TTestCase)
  private
    FResult: TTestResult;
  protected
    procedure Setup; override;
    procedure Teardown; override;
  public
    class function Suite: TTestSuite;
  published
    procedure testNoTestCaseClass;
    procedure testNoTestCases;
    procedure testOneTestCase;
    procedure testInheritedTests;
    procedure testNotExistingTestCase;
    procedure testShadowedTests;
    procedure testAddTestSuiteFromClass;
    procedure testCreateTestSuiteFromArray;
  end;


implementation

procedure TNoTestCases.NoTestCase;
begin
end;

procedure TNoTestCaseClass.TestSuccess;
begin
end;

procedure TOneTestCase.NoTestCase;
begin
end;

procedure TOneTestCase.OnlyOneTestCase;
begin
end;

procedure TOverrideTestCase.OnlyOneTestCase;
begin
end;

procedure TInheritedTestCase.Test2;
begin
end;

procedure TSuiteTest.Setup;
begin
  FResult := TTestResult.Create;
end;

procedure TSuiteTest.Teardown;
begin
  FResult.Free;
end;

class function TSuiteTest.Suite: TTestSuite;
begin
  Result := TTestSuite.Create('TSuiteTest');
  Result.AddTest(TSuiteTest.CreateWithName('testNoTestCaseClass'));
  Result.AddTest(TSuiteTest.CreateWithName('testNoTestCases'));
  Result.AddTest(TSuiteTest.CreateWithName('testOneTestCase'));
  Result.AddTest(TSuiteTest.CreateWithName('testInheritedTests'));
  Result.AddTest(TSuiteTest.CreateWithName('testNotExistingTestCase'));
  Result.AddTest(TSuiteTest.CreateWithName('testShadowedTests'));
  Result.AddTest(TSuiteTest.CreateWithName('testAddTestSuiteFromClass'));
  Result.AddTest(TSuiteTest.CreateWithName('testCreateTestSuiteFromArray'));
end;

procedure TSuiteTest.testNoTestCaseClass;
var
  ts: TTestSuite;
begin
  ts := TTestSuite.Create(TNoTestCaseClass);
  ts.Run(FResult);
  ts.Free;
  AssertEquals(1, FResult.RunTests);
  AssertTrue(not FResult.WasSuccessful);
end;

procedure TSuiteTest.testNoTestCases;
var
  ts: TTestSuite;
begin
  ts := TTestSuite.Create(TNoTestCases);
  ts.Run(FResult);
  ts.Free;
  AssertTrue(FResult.RunTests = 1);
  AssertTrue(FResult.NumberOfFailures = 1);
  AssertTrue(not FResult.WasSuccessful);
end;

procedure TSuiteTest.testOneTestCase;
var
  ts: TTestSuite;
begin
  ts := TTestSuite.Create(TOneTestCase);
  ts.Run(FResult);
  ts.Free;
  AssertTrue(FResult.RunTests = 1);
  AssertTrue(FResult.NumberOfFailures = 0);
  AssertTrue(FResult.NumberOfErrors = 0);
  AssertTrue(FResult.WasSuccessful);
end;

procedure TSuiteTest.testInheritedTests;
var
  ts: TTestSuite;
begin
  ts := TTestSuite.Create(TInheritedTestCase);
  ts.Run(FResult);
  ts.Free;
  AssertEquals(2, FResult.RunTests);
  AssertTrue(FResult.WasSuccessful);
end;

procedure TSuiteTest.testNotExistingTestCase;
var
  t: TTestCase;
begin
  t := TSuiteTest.CreateWithName('notExistingMethod');
  t.Run(FResult);
  t.Free;
  AssertTrue(FResult.RunTests = 1);
  AssertTrue(FResult.NumberOfFailures = 1);
  AssertTrue(FResult.NumberOfErrors = 0);
end;

procedure TSuiteTest.testShadowedTests;
var
  ts: TTestSuite;
begin
  ts := TTestSuite.Create(TOverrideTestCase);
  ts.Run(FResult);
  ts.Free;
  AssertEquals(1, FResult.RunTests);
end;

procedure TSuiteTest.testAddTestSuiteFromClass;
var
  ts: TTestSuite;
begin
  ts := TTestSuite.Create;
  ts.AddTestSuiteFromClass(TOneTestCase);
  ts.Run(FResult);
  ts.Free;
  AssertEquals(1, FResult.RunTests);
end;

procedure TSuiteTest.testCreateTestSuiteFromArray;
var
  ts: TTestSuite;
begin
  ts := TTestSuite.Create([TOneTestCase, TInheritedTestCase]);
  try
    AssertEquals(3, ts.CountTestCases);
    AssertEquals(2, ts.Tests.Count);
    AssertEquals('TOneTestCase', ts[0].TestName);
    AssertEquals('TInheritedTestCase', ts[1].TestName);
  finally
    ts.Free;
  end;
end;

initialization

  RegisterTests([TSuiteTest]);

end.