source: tags/1.3.0/UTest.pas

Last change on this file was 94, checked in by chronos, 2 years ago
  • Added: Validation tests for contact properties.
File size: 2.6 KB
Line 
1unit UTest;
2
3{$mode Delphi}
4
5interface
6
7uses
8 Classes, SysUtils, fgl, UContact;
9
10type
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
52const
53 ResultText: array[TTestResult] of string = ('None', 'Passed', 'Failed');
54
55
56implementation
57
58{ TTestCaseCheckProperty }
59
60procedure TTestCaseCheckProperty.Run;
61var
62 Lines: TStringList;
63 PropertyValue: string;
64begin
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;
85end;
86
87{ TTestCaseLoadSave }
88
89procedure TTestCaseLoadSave.Run;
90var
91 Lines: TStringList;
92begin
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;
112end;
113
114{ TTestCase }
115
116procedure TTestCase.Run;
117begin
118end;
119
120procedure TTestCase.Evaluate(Passed: Boolean);
121begin
122 if Passed then Result := trPassed
123 else Result := trFailed;
124end;
125
126procedure TTestCase.Pass;
127begin
128 Result := trPassed;
129end;
130
131procedure TTestCase.Fail;
132begin
133 Result := trFailed;
134end;
135
136{ TTestCases }
137
138function TTestCases.AddNew(Name: string; TestClass: TTestCaseClass): TTestCase;
139begin
140 Result := TestClass.Create;
141 Result.Name := Name;
142 Add(Result);
143end;
144
145end.
146
Note: See TracBrowser for help on using the repository browser.