-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTests.Injection.pas
390 lines (344 loc) · 11 KB
/
Tests.Injection.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
unit Tests.Injection;
interface
uses
DUnitX.TestFramework,
System.Classes, System.SysUtils,
Pattern.Command;
{$M+}
type
[TestFixture]
TestInjection_SingleParam = class(TObject)
private
FInteger101: integer;
FStrings: TStringList;
FOwnerComponent: TComponent;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
published
procedure ParameterStringList;
procedure ParameterStringList_ToStringsProperty;
procedure ParameterStringList_ToObjectProperty;
procedure ParameterInteger;
procedure ParameterBoolean;
procedure ParameterDouble;
procedure ParameterDateTime;
procedure ParameterWord;
procedure ParameterValueBoolean;
procedure ParameterValueFloat;
procedure ParameterValueInt;
procedure ParameterInterface;
procedure UnsupportedProperty_Exception;
end;
[TestFixture]
TestInjection_MultipleParams = class(TObject)
private
FStrings1: TStringList;
FStrings2: TStringList;
FOwnerComponent: TComponent;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
published
procedure TwoStringLists;
procedure InjectAll;
end;
implementation
// ------------------------------------------------------------------------
// sample interfaces injected into components
// ------------------------------------------------------------------------
type
ISample1 = interface (IInvokable)
['{AB5F0562-A0E6-4E93-910C-DD592FF02ADE}']
function GetValue: integer;
end;
ISample2 = interface (IInvokable)
['{D0562FD6-5393-4CA8-8285-46308C21B532}']
function GetValue(aValue: integer): integer;
end;
TSampleClass = class (TInterfacedObject,ISample1)
function GetValue: integer;
end;
TAnotherClass = class (TInterfacedObject,ISample2)
function GetValue(aValue: integer): integer;
end;
function TSampleClass.GetValue: integer;
begin
Exit(0);
end;
function TAnotherClass.GetValue(aValue: integer): integer;
begin
Result := aValue;
end;
// ------------------------------------------------------------------------
// sample components used in the tests
// ------------------------------------------------------------------------
type
TStringsComponent = class(TComponent)
strict private
FStringList: TStringList;
FStrings: TStrings;
FSameObject: TObject;
published
property StringList: TStringList read FStringList write FStringList;
property Strings: TStrings read FStrings write FStrings;
property SameObject: TObject read FSameObject write FSameObject;
end;
TIntegerComponent = class(TComponent)
strict private
FNumber: integer;
published
property Number: integer read FNumber write FNumber;
end;
TSimpleComponent = class(TComponent)
strict private
FNumber: integer;
FIsTrue: boolean;
FFloatNumber: Double;
FStartDate: TDateTime;
FSample1: ISample1;
published
property Number: integer read FNumber write FNumber;
property IsTrue: boolean read FIsTrue write FIsTrue;
property FloatNumber: Double read FFloatNumber write FFloatNumber;
property StartDate: TDateTime read FStartDate write FStartDate;
property Sample1: ISample1 read FSample1 write FSample1;
end;
// ------------------------------------------------------------------------
// tests: inject single parameter
// ------------------------------------------------------------------------
procedure TestInjection_SingleParam.Setup;
begin
FOwnerComponent := TComponent.Create(nil); // used as Owner for TCommand-s
FStrings := TStringList.Create();
FInteger101 := 101;
end;
procedure TestInjection_SingleParam.TearDown;
begin
FOwnerComponent.Free;
FreeAndNil(FStrings);
end;
procedure TestInjection_SingleParam.ParameterStringList;
var
StringsComponent: TStringsComponent;
begin
StringsComponent := TStringsComponent.Create(FOwnerComponent);
TComponentInjector.InjectProperties(StringsComponent, [FStrings]);
Assert.IsNotNull(StringsComponent.StringList);
Assert.AreSame(FStrings, StringsComponent.StringList);
end;
procedure TestInjection_SingleParam.ParameterStringList_ToStringsProperty;
var
StringsComponent: TStringsComponent;
FStrings2: TStringList;
begin
StringsComponent := TStringsComponent.Create(FOwnerComponent);
FStrings2 := TStringList.Create;
try
TComponentInjector.InjectProperties(StringsComponent, [FStrings,FStrings2]);
Assert.IsNotNull(StringsComponent.Strings);
Assert.AreSame(FStrings2, StringsComponent.Strings);
finally
FStrings2.Free;
end;
end;
procedure TestInjection_SingleParam.ParameterStringList_ToObjectProperty;
var
StringsComponent: TStringsComponent;
FStrings2: TStringList;
FStrings3: TStringList;
begin
StringsComponent := TStringsComponent.Create(FOwnerComponent);
FStrings2 := TStringList.Create;
FStrings3 := TStringList.Create;
try
TComponentInjector.InjectProperties(StringsComponent, [FStrings,FStrings2,FStrings3]);
Assert.IsNotNull(StringsComponent.SameObject);
Assert.AreSame(FStrings3, StringsComponent.SameObject);
finally
FStrings2.Free;
FStrings3.Free;
end;
end;
procedure TestInjection_SingleParam.ParameterInteger;
var
IntegerComponent: TIntegerComponent;
begin
IntegerComponent := TIntegerComponent.Create(FOwnerComponent);
TComponentInjector.InjectProperties(IntegerComponent, [FInteger101]);
Assert.AreEqual(FInteger101, IntegerComponent.Number);
end;
procedure TestInjection_SingleParam.ParameterBoolean;
var
SimpleComponent: TSimpleComponent;
b: boolean;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
b := True;
TComponentInjector.InjectProperties(SimpleComponent, [b]);
Assert.AreEqual(b, SimpleComponent.IsTrue);
end;
procedure TestInjection_SingleParam.ParameterDouble;
var
SimpleComponent: TSimpleComponent;
val: Double;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
val := Pi;
TComponentInjector.InjectProperties(SimpleComponent, [val]);
Assert.AreEqual(val, SimpleComponent.FloatNumber);
end;
procedure TestInjection_SingleParam.ParameterDateTime;
var
SimpleComponent: TSimpleComponent;
FloatVal: Single;
Date: TDateTime;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
FloatVal := 2.1;
Date := EncodeDate(2019, 02, 01) + EncodeTime(18, 50, 0, 0);
TComponentInjector.InjectProperties(SimpleComponent, [FloatVal, Date]);
Assert.AreEqual(Double(FloatVal), SimpleComponent.FloatNumber);
Assert.AreEqual(Date, SimpleComponent.StartDate);
end;
procedure TestInjection_SingleParam.ParameterWord;
var
SimpleComponent: TSimpleComponent;
Value: word;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
Value := 999;
TComponentInjector.InjectProperties(SimpleComponent, [Value]);
Assert.AreEqual(999, SimpleComponent.Number);
end;
procedure TestInjection_SingleParam.ParameterValueBoolean;
var
SimpleComponent: TSimpleComponent;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
TComponentInjector.InjectProperties(SimpleComponent, [True]);
Assert.AreEqual(True, SimpleComponent.IsTrue);
end;
procedure TestInjection_SingleParam.ParameterValueInt;
var
SimpleComponent: TSimpleComponent;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
TComponentInjector.InjectProperties(SimpleComponent, [55]);
Assert.AreEqual(55, SimpleComponent.Number);
end;
procedure TestInjection_SingleParam.ParameterValueFloat;
var
SimpleComponent: TSimpleComponent;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
TComponentInjector.InjectProperties(SimpleComponent, [99.99]);
Assert.AreEqual(99.99, Extended(SimpleComponent.FloatNumber));
end;
procedure TestInjection_SingleParam.ParameterInterface;
var
SimpleComponent: TSimpleComponent;
sample1: ISample1;
begin
SimpleComponent := TSimpleComponent.Create(FOwnerComponent);
sample1 := TSampleClass.Create;
TComponentInjector.InjectProperties(SimpleComponent, [sample1]);
Assert.IsTrue(SimpleComponent.Sample1 = sample1);
end;
procedure TestInjection_SingleParam.UnsupportedProperty_Exception;
type
TMyRec = record
a: integer;
b: boolean;
end;
var
aRec1: TMyRec;
StringsComponent: TStringsComponent;
begin
StringsComponent := TStringsComponent.Create(FOwnerComponent);
Assert.WillRaise(
procedure
begin
TComponentInjector.InjectProperties(StringsComponent, [@aRec1]);
end);
end;
// ------------------------------------------------------------------------
// Test_MoreInjections - tests component with many injected properties
// * 2x TStringList, 1x TComponent
// ------------------------------------------------------------------------
type
TManyPropComponent = class(TComponent)
strict private
FCount: integer;
FEvenLines: TStringList;
FOddLines: TStringList;
FComponent: TComponent;
FStream: TStream;
FSample1: ISample1;
FSample2: ISample2;
public
property Count: integer read FCount write FCount;
property Stream: TStream read FStream write FStream;
published
property OddLines: TStringList read FOddLines write FOddLines;
property Component: TComponent read FComponent write FComponent;
property Sample1: ISample1 read FSample1 write FSample1;
property EvenLines: TStringList read FEvenLines write FEvenLines;
property Sample2: ISample2 read FSample2 write FSample2;
end;
procedure TestInjection_MultipleParams.Setup;
begin
FStrings1 := TStringList.Create;
FStrings2 := TStringList.Create;
FOwnerComponent := TComponent.Create(nil);
end;
procedure TestInjection_MultipleParams.TearDown;
begin
FreeAndNil(FStrings1);
FreeAndNil(FStrings2);
FreeAndNil(FOwnerComponent);
end;
procedure TestInjection_MultipleParams.TwoStringLists;
var
ManyPropComponent: TManyPropComponent;
begin
// --
ManyPropComponent := TManyPropComponent.Create(FOwnerComponent);
// --
TComponentInjector.InjectProperties(ManyPropComponent,
[FStrings1, FStrings2]);
// --
Assert.AreSame(FStrings1, ManyPropComponent.OddLines);
Assert.AreSame(FStrings2, ManyPropComponent.EvenLines);
end;
procedure TestInjection_MultipleParams.InjectAll;
var
ManyPropComponent: TManyPropComponent;
sample1: ISample1;
sample2: ISample2;
begin
// Arrange:
ManyPropComponent := TManyPropComponent.Create(FOwnerComponent);
sample1 := TSampleClass.Create;
sample2 := TAnotherClass.Create;
// Act:
TComponentInjector.InjectProperties(ManyPropComponent,
[FStrings1, FStrings2, FOwnerComponent, sample2, sample1]);
// Assert
Assert.AreSame(FStrings1, ManyPropComponent.OddLines);
Assert.AreSame(FStrings2, ManyPropComponent.EvenLines);
Assert.AreSame(FOwnerComponent, ManyPropComponent.Component);
Assert.AreSame(sample1, ManyPropComponent.Sample1);
Assert.AreSame(sample2, ManyPropComponent.Sample2);
Assert.AreEqual(99, ManyPropComponent.Sample2.GetValue(99));
end;
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
initialization
TDUnitX.RegisterTestFixture(TestInjection_SingleParam);
TDUnitX.RegisterTestFixture(TestInjection_MultipleParams);
end.