1 | unit UTest;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections, UVCard;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
53 | resourcestring
|
---|
54 | SNone = 'None';
|
---|
55 | SPassed = 'Passed';
|
---|
56 | SFailed = 'Failed';
|
---|
57 |
|
---|
58 | const
|
---|
59 | ResultText: array[TTestResult] of string = (SNone, SPassed, SFailed);
|
---|
60 |
|
---|
61 | procedure Translate;
|
---|
62 |
|
---|
63 |
|
---|
64 | implementation
|
---|
65 |
|
---|
66 | uses
|
---|
67 | UVCardFile;
|
---|
68 |
|
---|
69 | resourcestring
|
---|
70 | SExpected = 'Expected:';
|
---|
71 | SOutput = 'Output:';
|
---|
72 |
|
---|
73 | procedure Translate;
|
---|
74 | begin
|
---|
75 | ResultText[trNone] := SNone;
|
---|
76 | ResultText[trPassed] := SPassed;
|
---|
77 | ResultText[trFailed] := SFailed;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | { TTestCaseCheckProperty }
|
---|
81 |
|
---|
82 | procedure TTestCaseCheckProperty.Run;
|
---|
83 | var
|
---|
84 | Lines: TStringList;
|
---|
85 | PropertyValue: string;
|
---|
86 | begin
|
---|
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;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | { TTestCaseLoadSave }
|
---|
110 |
|
---|
111 | procedure TTestCaseLoadSave.Run;
|
---|
112 | var
|
---|
113 | Lines: TStringList;
|
---|
114 | begin
|
---|
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;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | { TTestCase }
|
---|
141 |
|
---|
142 | procedure TTestCase.Run;
|
---|
143 | begin
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TTestCase.Evaluate(Passed: Boolean);
|
---|
147 | begin
|
---|
148 | if Passed then Result := trPassed
|
---|
149 | else Result := trFailed;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | procedure TTestCase.Pass;
|
---|
153 | begin
|
---|
154 | Result := trPassed;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | procedure TTestCase.Fail;
|
---|
158 | begin
|
---|
159 | Result := trFailed;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | { TTestCases }
|
---|
163 |
|
---|
164 | function TTestCases.AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
|
---|
165 | begin
|
---|
166 | Result := TestClass.Create;
|
---|
167 | Result.Name := Name;
|
---|
168 | Add(Result);
|
---|
169 | end;
|
---|
170 |
|
---|
171 | end.
|
---|
172 |
|
---|