source: tags/1.4.0/UTest.pas

Last change on this file was 138, checked in by chronos, 21 months ago
  • Added: Allow to configure visible columns in contacts table.
File size: 3.1 KB
Line 
1unit UTest;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, UVCard;
7
8type
9 TTestResult = (trNone, trPassed, trFailed);
10
11 { TTestCase }
12
13 TTestCase = class
14 public
15 Name: string;
16 Result: TTestResult;
17 Log: string;
18 procedure Run; virtual;
19 procedure Evaluate(Passed: Boolean);
20 procedure Pass;
21 procedure Fail;
22 end;
23
24 TTestCaseClass = class of TTestCase;
25
26 { TTestCases }
27
28 TTestCases = class(TObjectList<TTestCase>)
29 function AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
30 end;
31
32 TTestCaseActionKind = (akNone, akRemoveExactDuplicates);
33
34 { TTestCaseLoadSave }
35
36 TTestCaseLoadSave = class(TTestCase)
37 Input: string;
38 Output: string;
39 Action: TTestCaseActionKind;
40 procedure Run; override;
41 end;
42
43 { TTestCaseCheckProperty }
44
45 TTestCaseCheckProperty = class(TTestCase)
46 Input: string;
47 ContactIndex: Integer;
48 Index: TContactFieldIndex;
49 Value: string;
50 procedure Run; override;
51 end;
52
53resourcestring
54 SNone = 'None';
55 SPassed = 'Passed';
56 SFailed = 'Failed';
57
58const
59 ResultText: array[TTestResult] of string = (SNone, SPassed, SFailed);
60
61procedure Translate;
62
63
64implementation
65
66uses
67 UVCardFile;
68
69resourcestring
70 SExpected = 'Expected:';
71 SOutput = 'Output:';
72
73procedure Translate;
74begin
75 ResultText[trNone] := SNone;
76 ResultText[trPassed] := SPassed;
77 ResultText[trFailed] := SFailed;
78end;
79
80{ TTestCaseCheckProperty }
81
82procedure TTestCaseCheckProperty.Run;
83var
84 Lines: TStringList;
85 PropertyValue: string;
86begin
87 Lines := TStringList.Create;
88 try
89 with TVCardFile.Create(nil) do
90 try
91 Lines.Text := Input;
92 VCard.LoadFromStrings(Lines);
93 if ContactIndex < VCard.Contacts.Count then begin
94 PropertyValue := VCard.Contacts[ContactIndex].Fields[Index];
95 Evaluate(PropertyValue = Value);
96 end else Fail;
97 Log := SExpected + LineEnding +
98 '"' + Value + '"' + LineEnding + LineEnding +
99 SOutput + LineEnding +
100 '"' + PropertyValue + '"';
101 finally
102 Free;
103 end;
104 finally
105 Lines.Free;
106 end;
107end;
108
109{ TTestCaseLoadSave }
110
111procedure TTestCaseLoadSave.Run;
112var
113 Lines: TStringList;
114begin
115 Lines := TStringList.Create;
116 try
117 with TVCardFile.Create(nil) do
118 try
119 Lines.Text := Input;
120 VCard.LoadFromStrings(Lines);
121
122 if Action = akRemoveExactDuplicates then
123 VCard.Contacts.RemoveExactDuplicates;
124
125 Lines.Text := '';
126 VCard.SaveToStrings(Lines);
127 Evaluate(Lines.Text = Output);
128 Log := SExpected + LineEnding +
129 '"' + Output + '"' + LineEnding + LineEnding +
130 SOutput + LineEnding +
131 '"' + Lines.Text + '"';
132 finally
133 Free;
134 end;
135 finally
136 Lines.Free;
137 end;
138end;
139
140{ TTestCase }
141
142procedure TTestCase.Run;
143begin
144end;
145
146procedure TTestCase.Evaluate(Passed: Boolean);
147begin
148 if Passed then Result := trPassed
149 else Result := trFailed;
150end;
151
152procedure TTestCase.Pass;
153begin
154 Result := trPassed;
155end;
156
157procedure TTestCase.Fail;
158begin
159 Result := trFailed;
160end;
161
162{ TTestCases }
163
164function TTestCases.AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
165begin
166 Result := TestClass.Create;
167 Result.Name := Name;
168 Add(Result);
169end;
170
171end.
172
Note: See TracBrowser for help on using the repository browser.