source: trunk/Packages/Common/TestCase.pas

Last change on this file was 2, checked in by chronos, 16 months ago
  • Added: Initial development version.
File size: 2.2 KB
Line 
1unit TestCase;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Generics.Collections;
7
8type
9 TTestResult = (trNone, trPassed, trFailed);
10
11 { TTestCase }
12
13 TTestCase = class
14 public
15 Name: string;
16 TestResult: TTestResult;
17 Log: string;
18 procedure Initialize; virtual;
19 procedure Run; virtual;
20 procedure Finalize; virtual;
21 procedure Evaluate(Passed: Boolean);
22 procedure Pass;
23 procedure Fail;
24 constructor Create; virtual;
25 end;
26
27 TTestCaseClass = class of TTestCase;
28
29 { TTestCases }
30
31 TTestCases = class(TObjectList<TTestCase>)
32 function AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
33 procedure Run;
34 end;
35
36
37resourcestring
38 SNone = 'None';
39 SPassed = 'Passed';
40 SFailed = 'Failed';
41
42const
43 ResultText: array[TTestResult] of string = (SNone, SPassed, SFailed);
44
45procedure Translate;
46
47
48implementation
49
50procedure Translate;
51begin
52 ResultText[trNone] := SNone;
53 ResultText[trPassed] := SPassed;
54 ResultText[trFailed] := SFailed;
55end;
56
57
58{ TTestCase }
59
60procedure TTestCase.Initialize;
61begin
62 TestResult := trNone;
63end;
64
65procedure TTestCase.Run;
66begin
67end;
68
69procedure TTestCase.Finalize;
70begin
71end;
72
73procedure TTestCase.Evaluate(Passed: Boolean);
74begin
75 if Passed then TestResult := trPassed
76 else TestResult := trFailed;
77end;
78
79procedure TTestCase.Pass;
80begin
81 TestResult := trPassed;
82end;
83
84procedure TTestCase.Fail;
85begin
86 TestResult := trFailed;
87end;
88
89constructor TTestCase.Create;
90begin
91end;
92
93{ TTestCases }
94
95function TTestCases.AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
96begin
97 Result := TestClass.Create;
98 Result.Name := Name;
99 Add(Result);
100end;
101
102procedure TTestCases.Run;
103var
104 I: Integer;
105 Passed: Integer;
106 Failed: Integer;
107begin
108 Passed := 0;
109 Failed := 0;
110 for I := 0 to Count - 1 do
111 with Items[I] do begin
112 WriteLn('== ' + Name + ' ======= ');
113 Initialize;
114 Run;
115 Finalize;
116 if TestResult = trPassed then Inc(Passed);
117 if TestResult = trFailed then Inc(Failed);
118 end;
119
120 for I := 0 to Count - 1 do
121 with Items[I] do begin
122 WriteLn(Name + ': ' + ResultText[TestResult]);
123 end;
124 WriteLn('Total: ' + IntToStr(Count) + ', Passed: ' + IntToStr(Passed) +
125 ', Failed: ' + IntToStr(Failed));
126end;
127
128end.
Note: See TracBrowser for help on using the repository browser.