1 | unit UTest;
|
---|
2 |
|
---|
3 | {$mode Delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, fgl, UContact;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TTestResult = (trNone, trPassed, trFailed);
|
---|
12 |
|
---|
13 | { TTestCase }
|
---|
14 |
|
---|
15 | TTestCase = class
|
---|
16 | public
|
---|
17 | Name: string;
|
---|
18 | Result: TTestResult;
|
---|
19 | Log: string;
|
---|
20 | procedure Run; virtual;
|
---|
21 | procedure Evaluate(Passed: Boolean);
|
---|
22 | procedure Pass;
|
---|
23 | procedure Fail;
|
---|
24 | end;
|
---|
25 |
|
---|
26 | TTestCaseClass = class of TTestCase;
|
---|
27 |
|
---|
28 | { TTestCases }
|
---|
29 |
|
---|
30 | TTestCases = class(TFPGObjectList<TTestCase>)
|
---|
31 | function AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
|
---|
32 | end;
|
---|
33 |
|
---|
34 | { TTestCaseLoadSave }
|
---|
35 |
|
---|
36 | TTestCaseLoadSave = class(TTestCase)
|
---|
37 | Input: string;
|
---|
38 | Output: string;
|
---|
39 | procedure Run; override;
|
---|
40 | end;
|
---|
41 |
|
---|
42 | { TTestCaseCheckProperty }
|
---|
43 |
|
---|
44 | TTestCaseCheckProperty = class(TTestCase)
|
---|
45 | Input: string;
|
---|
46 | ContactIndex: Integer;
|
---|
47 | Index: TContactFieldIndex;
|
---|
48 | Value: string;
|
---|
49 | procedure Run; override;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | const
|
---|
53 | ResultText: array[TTestResult] of string = ('None', 'Passed', 'Failed');
|
---|
54 |
|
---|
55 |
|
---|
56 | implementation
|
---|
57 |
|
---|
58 | { TTestCaseCheckProperty }
|
---|
59 |
|
---|
60 | procedure TTestCaseCheckProperty.Run;
|
---|
61 | var
|
---|
62 | Lines: TStringList;
|
---|
63 | PropertyValue: string;
|
---|
64 | begin
|
---|
65 | Lines := TStringList.Create;
|
---|
66 | try
|
---|
67 | with TContactsFile.Create do
|
---|
68 | try
|
---|
69 | Lines.Text := Input;
|
---|
70 | LoadFromStrings(Lines);
|
---|
71 | if ContactIndex < Contacts.Count then begin
|
---|
72 | PropertyValue := Contacts[ContactIndex].Fields[Index];
|
---|
73 | Evaluate(PropertyValue = Value);
|
---|
74 | end else Fail;
|
---|
75 | Log := 'Expected:' + LineEnding +
|
---|
76 | '"' + Value + '"' + LineEnding + LineEnding +
|
---|
77 | 'Output:' + LineEnding +
|
---|
78 | '"' + PropertyValue + '"';
|
---|
79 | finally
|
---|
80 | Free;
|
---|
81 | end;
|
---|
82 | finally
|
---|
83 | Lines.Free;
|
---|
84 | end;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | { TTestCaseLoadSave }
|
---|
88 |
|
---|
89 | procedure TTestCaseLoadSave.Run;
|
---|
90 | var
|
---|
91 | Lines: TStringList;
|
---|
92 | begin
|
---|
93 | Lines := TStringList.Create;
|
---|
94 | try
|
---|
95 | with TContactsFile.Create do
|
---|
96 | try
|
---|
97 | Lines.Text := Input;
|
---|
98 | LoadFromStrings(Lines);
|
---|
99 | Lines.Text := '';
|
---|
100 | SaveToStrings(Lines);
|
---|
101 | Evaluate(Lines.Text = Output);
|
---|
102 | Log := 'Expected:' + LineEnding +
|
---|
103 | '"' + Output + '"' + LineEnding + LineEnding +
|
---|
104 | 'Output:' + LineEnding +
|
---|
105 | '"' + Lines.Text + '"';
|
---|
106 | finally
|
---|
107 | Free;
|
---|
108 | end;
|
---|
109 | finally
|
---|
110 | Lines.Free;
|
---|
111 | end;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | { TTestCase }
|
---|
115 |
|
---|
116 | procedure TTestCase.Run;
|
---|
117 | begin
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TTestCase.Evaluate(Passed: Boolean);
|
---|
121 | begin
|
---|
122 | if Passed then Result := trPassed
|
---|
123 | else Result := trFailed;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure TTestCase.Pass;
|
---|
127 | begin
|
---|
128 | Result := trPassed;
|
---|
129 | end;
|
---|
130 |
|
---|
131 | procedure TTestCase.Fail;
|
---|
132 | begin
|
---|
133 | Result := trFailed;
|
---|
134 | end;
|
---|
135 |
|
---|
136 | { TTestCases }
|
---|
137 |
|
---|
138 | function TTestCases.AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
|
---|
139 | begin
|
---|
140 | Result := TestClass.Create;
|
---|
141 | Result.Name := Name;
|
---|
142 | Add(Result);
|
---|
143 | end;
|
---|
144 |
|
---|
145 | end.
|
---|
146 |
|
---|