1 | unit TestCase;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Generics.Collections;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
37 | resourcestring
|
---|
38 | SNone = 'None';
|
---|
39 | SPassed = 'Passed';
|
---|
40 | SFailed = 'Failed';
|
---|
41 |
|
---|
42 | const
|
---|
43 | ResultText: array[TTestResult] of string = (SNone, SPassed, SFailed);
|
---|
44 |
|
---|
45 | procedure Translate;
|
---|
46 |
|
---|
47 |
|
---|
48 | implementation
|
---|
49 |
|
---|
50 | procedure Translate;
|
---|
51 | begin
|
---|
52 | ResultText[trNone] := SNone;
|
---|
53 | ResultText[trPassed] := SPassed;
|
---|
54 | ResultText[trFailed] := SFailed;
|
---|
55 | end;
|
---|
56 |
|
---|
57 |
|
---|
58 | { TTestCase }
|
---|
59 |
|
---|
60 | procedure TTestCase.Initialize;
|
---|
61 | begin
|
---|
62 | TestResult := trNone;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure TTestCase.Run;
|
---|
66 | begin
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure TTestCase.Finalize;
|
---|
70 | begin
|
---|
71 | end;
|
---|
72 |
|
---|
73 | procedure TTestCase.Evaluate(Passed: Boolean);
|
---|
74 | begin
|
---|
75 | if Passed then TestResult := trPassed
|
---|
76 | else TestResult := trFailed;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | procedure TTestCase.Pass;
|
---|
80 | begin
|
---|
81 | TestResult := trPassed;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | procedure TTestCase.Fail;
|
---|
85 | begin
|
---|
86 | TestResult := trFailed;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | constructor TTestCase.Create;
|
---|
90 | begin
|
---|
91 | end;
|
---|
92 |
|
---|
93 | { TTestCases }
|
---|
94 |
|
---|
95 | function TTestCases.AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
|
---|
96 | begin
|
---|
97 | Result := TestClass.Create;
|
---|
98 | Result.Name := Name;
|
---|
99 | Add(Result);
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TTestCases.Run;
|
---|
103 | var
|
---|
104 | I: Integer;
|
---|
105 | Passed: Integer;
|
---|
106 | Failed: Integer;
|
---|
107 | begin
|
---|
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));
|
---|
126 | end;
|
---|
127 |
|
---|
128 | end.
|
---|