-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutofixtureGenerator.pas
879 lines (710 loc) · 23.3 KB
/
AutofixtureGenerator.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
unit AutofixtureGenerator;
interface
uses
RTTI,
Generics.Collections,
AutoFixtureSetup;
type
{$SCOPEDENUMS ON}
IValueGenerator = interface
function getValue(APropertyName: String; AType: TRttiType; AReferenceDepth: integer = -1): TValue;
end;
IObjectGenerator = interface(IValueGenerator)
function getObject(AType: TRttiType; AReferenceDepth: Integer): TObject;
end;
TInjectDelegate<T> = reference to function(): T;
TInjectNameDelegate<T> = reference to function(aPropertyName: String): T;
TObjectModifyProcedure<T> = reference to procedure(AObject: T);
TPropertySelector<TObjectType, TPropertyType> = reference to function(AObject: TObjectType): TPropertyType;
TValueGenerator<T> = class(TInterfacedObject, IValueGenerator)
protected
FTypeInfo: TRttiType;
FPropertyName: String;
FDelegate: TInjectDelegate<T>;
FNameDelegate: TInjectNameDelegate<T>;
public
constructor Create(); virtual;
destructor Destroy; override;
procedure Register(aPropertyName: String; aDelegate: TInjectDelegate<T>); overload;
procedure Register(aDelegate: TInjectNameDelegate<T>); overload;
function canGenerate(aPropertyName: String): Boolean;
function getValue(aPropertyName: String; aType:TRttiType; AReferenceDepth: integer = -1): TValue;
end;
TProcessProperty = (Uninitialized, Omit, ValueSet);
TPropertyConfig = class
private
FFieldType: TRttiField;
FType: TRttiType;
FPropertyName: String;
FConfiguredPropertyValue: TValue; // If any!
FPropertyIdentifier: TValue;
FProcessProperty: TProcessProperty;
end;
TBaseConfig = class(TInterfacedObject, IObjectGenerator)
protected
FType: TRttiType;
FPreviousGenerator: IObjectGenerator;
FProperties: TDictionary<String, TPropertyConfig>;
FPropertiesByType: TDictionary<TRttiType, TList<TPropertyConfig>>;
FInitializedTypes: TList<TRttiType>;
procedure InitProperties;
procedure CallConstructor(AType: TRttiType; AObject: TObject; AReferenceDepth: Integer = -1); Virtual; Abstract;
procedure ObjectConstructed(AObject: TObject); Virtual; Abstract;
public
constructor Create(AType: TRttiType; APreviousConfig: IObjectGenerator);
destructor Destroy; override;
function GetValue(APropertyName: String; AType:TRttiType; AReferenceDepth: integer = -1): TValue; virtual;
function GetObject(AType: TRttiType; AReferenceDepth: Integer): TObject; virtual;
end;
TObjectConfig<T: Class> = class(TBaseConfig)
protected
FSetup: TAutofixtureSetup;
FRttiContext: TRttiContext;
FExecList: TList<TObjectModifyProcedure<T>>;
FObject: T;
//function GetPropertiesOfType<Typ>: TList<TPropertyConfig>;
function GetPropertyFromSelector<Typ>(AProperty: TPropertySelector<T, Typ>): TPropertyConfig;
function GenerateIndexValue(AType: TRttiType; AIndex: Integer): TValue;
function GetIndexFromValue<Typ>(AValue: Typ): Integer;
//procedure InitProperties();
procedure InitType(AType: TRttiType);
procedure CallConstructor(AType: TRttiType; AObject: TObject; AReferenceDepth: Integer = -1); override;
function CallMethod(AOnObject: TObject; vMethod: TRttiMethod; AReferenceDepth: Integer = -1): TValue;
procedure ObjectConstructed(AObject: TObject); override;
public
constructor Create(AAutofixtueSetup: TAutoFixtureSetup; APreviousConfig: IObjectGenerator);
destructor Destroy; override;
// function getValue(aPropertyName: String; aType:TRttiType): TValue;
// function getObject(AType: TRttiType; AReferenceDepth: Integer): TObject;
function OmitAutoProperties: TObjectConfig<T>;
function WithValue<Typ>(AProperty: String; AValue: Typ): TObjectConfig<T>; overload;
function WithValue<Typ>(AProperty: TPropertySelector<T, Typ>; AValue: Typ): TObjectConfig<T>; overload;
function Without(AProperty: String): TObjectConfig<T>; overload;
function Without<Typ>(AProperty: TPropertySelector<T, Typ>): TObjectConfig<T>; overload;
function Omit<Typ>(AProperty: String): TObjectConfig<T>; overload;
function Omit<Typ>(AProperty: TPropertySelector<T, Typ>): TObjectConfig<T>; overload;
function Exec(AProcedureModifier: TObjectModifyProcedure<T>): TObjectConfig<T>; overload;
function New: T;
end;
TRandomGenerator = class(TinterfacedObject, IValueGenerator)
function getValue(aPropertyName: String; aType: TRttiType; AReferenceDepth: integer = -1): TValue;
end;
TCollectionItemGenerator = class(TBaseConfig)
protected
procedure CallConstructor(AType: TRttiType; AObject: TObject; AReferenceDepth: Integer = -1); override;
procedure ObjectConstructed(AObject: TObject); override;
public
constructor Create(AType: TRttiType; APreviousConfig: IObjectGenerator); virtual;
destructor Destroy; override;
function GetValue(APropertyName: String; AType:TRttiType; AReferenceDepth: integer = -1): TValue; override;
// function getObject(AType: TRttiType; AReferenceDepth: Integer): TObject;
end;
implementation
uses SysUtils,
System.Classes,
Dateutils,
Spring.Mocking.Matching,
AutofixtureLibrary;
{ TValueGenerator }
function TValueGenerator<T>.canGenerate(aPropertyName: String): Boolean;
begin
if typeInfo(T) = FTypeInfo then begin
Result := True;
end
else begin
Result := False;
end;
end;
constructor TValueGenerator<T>.Create;
var ctx: TRttiContext;
begin
ctx := TRttiContext.Create;
FTypeInfo := ctx.GetType(TypeInfo(T));
end;
destructor TValueGenerator<T>.Destroy;
begin
FDelegate := nil;
FNameDelegate := nil;
inherited;
end;
function TValueGenerator<T>.getValue(aPropertyName: String; aType:TRttiType; AReferenceDepth: integer = -1): TValue;
var vGetByName: TInjectNameDelegate<T>;
vSimpleGet: TInjectDelegate<T>;
vType: TRttiType;
begin
if FTypeInfo = aType then begin
if FPropertyName = 'A' + aPropertyName then begin
Result := TValue.From<T>(FDelegate());
end
else if FPropertyName = '' then begin
Result := TValue.From<T>(FNameDelegate(aPropertyName));
end
else begin
Result := TValue.Empty;
end;
end
else begin
Result := nil;
end;
end;
procedure TValueGenerator<T>.Register(aPropertyName: String; aDelegate: TInjectDelegate<T>);
begin
FPropertyName := 'A' + aPropertyName;
FDelegate := aDelegate;
end;
procedure TValueGenerator<T>.Register(ADelegate: TInjectNameDelegate<T>);
begin
FPropertyName := '';
FNameDelegate := aDelegate;
end;
{ TRandomGenerator }
function TRandomGenerator.getValue(APropertyName: String; AType: TRttiType; AReferenceDepth: integer = -1): TValue;
var i: Integer;
vGuid: TGuid;
vClassRef: TClass;
vType: TRttiClassRefType;
begin
Result := TValue.Empty;
if Assigned(AType) then begin
case aType.TypeKind of
tkInteger: begin
Result := TValue.From<Integer>(1+Random(255));
end;
tkInt64: begin
Result := TValue.From<Int64>(1+Random(255));
end;
tkChar, TkWChar: begin
i := Random(100);
if i<40 then begin
Result := TValue.From(Chr(ord('a') + Random(26)));
end
else if i<80 then begin
Result := TValue.From(Chr(ord('A') + Random(26)));
end
else begin
Result := TValue.From(Chr(ord('0') + Random(10)));
end;
end;
tkWString, tkUString, tkLString: begin
CreateGuid(vGuid);
Result := TValue.From(aPropertyName + ' ' + GuidToString(vGuid))
end;
tkEnumeration: begin
Result := TValue.FromOrdinal(aType.Handle, aType.AsOrdinal.MinValue + Random(aType.AsOrdinal.MaxValue + 1 - aType.AsOrdinal.MinValue));
end;
tkFloat: Begin
if AType.Name='TDateTime' then begin
Result := TValue.From(Now - Random(300))
end
else begin
Result := TValue.From(0.1 + Random(300));
end;
End;
tkVariant: Begin
i := Random(100); // Assign variants as integer, string or date
if i<34 then begin
Result := TValue.FromVariant(1+Random(300))
end
else if i<67 then begin
CreateGuid(vGuid);
Result := TValue.FromVariant(aPropertyName + ' ' + GuidToString(vGuid));
end
else begin
Result := TValue.FromVariant(IncDay(Now, -Random(300)));
end;
End;
tkSet: begin
// Not really possible to generate a random set, just leave blank
end;
tkInterface: begin
Result := TValue.From(nil); {TODO -oJEHYK -cAutoMock : Make mock object dynamically}
end;
tkClassRef: begin
vType := AType as TRttiClassRefType;
vClassRef := vType.MetaclassType;
Result := TValue.From(vClassRef); // Gets the topmost classref that it's possible to assign - not always correct, but the best we can do
end;
end;
{
tkSet
Set types
tkClass
Class types
tkMethod
Procedure and function method types
tkLString
Delphi 2+ long strings (made of AnsiChars)
tkLWString
Delphi 2 constant added in preparation of long wide strings for Unicode, which were planned for Delphi 3
tkWString
The Delphi 3 constant which replaces tkLWString
tkVariant
Variant type, new in Delphi 2
tkArray
Array types, new in Delphi 3
tkRecord
Record types, new in Delphi 3
tkInterface
Interface types, new in Delphi 3
tkDynArray
}
end;
end;
{ TBaseConfig<T> }
{$WARN UNSAFE_CAST OFF}
procedure TObjectConfig<T>.CallConstructor(AType: TRttiType; AObject: TObject; AReferenceDepth: Integer = -1);
var
vConstructor, vAddMethod: TRttiMethod;
begin
TAutofixtureLibrary.GetMethods(FSetup, AType, vConstructor, vAddMethod);
if vConstructor <> nil then begin
CallMethod(AObject, vConstructor, AReferenceDepth);
end;
end;
function TObjectConfig<T>.CallMethod(AOnObject: TObject; vMethod: TRttiMethod; AReferenceDepth: Integer = -1): TValue;
var
vValueList: TList<TValue>;
vParam: TRttiParameter;
vValue: TValue;
begin
if AReferenceDepth = -1 then begin
AReferenceDepth := FSetup.ReferenceDepth;
end;
vValueList := TList<TValue>.Create;
try
for vParam in vMethod.GetParameters do
begin
vValue := getValue(vParam.Name, vParam.ParamType, AReferenceDepth);
vValueList.Add(vValue);
end;
Result := vMethod.Invoke(AOnObject, vValueList.ToArray);
finally
FreeAndNil(vValueList);
end;
end;
constructor TObjectConfig<T>.Create(AAutofixtueSetup: TAutoFixtureSetup; APreviousConfig: IObjectGenerator);
var
vObject: T;
vPByte: ^Byte;
i: Integer;
vList: TList<TPropertyConfig>;
vValue: TValue;
vConfig: TPropertyConfig;
begin
FSetup := AAutofixtueSetup;
FExecList := TList<TObjectModifyProcedure<T>>.Create;
// Find all properties
FRttiContext := TRttiContext.Create;
inherited Create(FRttiContext.GetType(TypeInfo(T)), APreviousConfig);
// TODO: Make lazy!
// Initialize "Object property finder"
FObject := T(FType.AsInstance.MetaclassType.Create);
vList := nil;
end;
destructor TObjectConfig<T>.Destroy;
begin
FreeAndNil(FExecList);
inherited;
end;
function TObjectConfig<T>.Exec(AProcedureModifier: TObjectModifyProcedure<T>): TObjectConfig<T>;
begin
Result := Self;
FExecList.Add(AProcedureModifier);
end;
function TObjectConfig<T>.GetIndexFromValue<Typ>(AValue: Typ): Integer;
var
vValue: TValue;
begin
Result := -1; // Obviously invalid index
vValue := TValue.From(AValue);
case FRttiContext.getType(TypeInfo(Typ)).TypeKind of
tkInteger: begin
Result := vValue.AsInteger;
end;
tkInt64: begin
Result := vValue.AsInt64;
end;
tkChar, TkWChar: begin
Result := Ord(vValue.AsString[1]);
end;
tkWString, tkUString, tkLString: begin
Result := StrToInt(vValue.AsString);
end;
tkEnumeration: begin
Result := vValue.AsOrdinal;
end;
tkFloat: Begin
Result := Trunc(vValue.AsExtended);
End;
tkVariant: Begin
Result := Integer(vValue.AsVariant);
End;
tkSet: begin
// Not really possible to generate a random set, just leave blank
end;
tkInterface, tkClass: begin
Result := Integer(vValue.AsObject);
end;
{
tkSet
Set types
tkClass
Class types
tkMethod
Procedure and function method types
tkWString
The Delphi 3 constant which replaces tkLWString
tkArray
Array types, new in Delphi 3
tkRecord
Record types, new in Delphi 3
tkInterface
Interface types, new in Delphi 3
tkDynArray
}
end;
end;
function TObjectConfig<T>.GenerateIndexValue(AType: TRttiType; AIndex: Integer): TValue;
var i: Integer;
vObject: TObject;
vPointer: ^TObject;
begin
Result := TValue.Empty;
case AType.TypeKind of
tkInteger: begin
Result := TValue.From<Integer>(AIndex);
end;
tkInt64: begin
Result := TValue.From<Int64>(AIndex);
end;
tkChar, TkWChar: begin
Result := TValue.From(Chr(Aindex));
end;
tkWString, tkUString, tkLString: begin
Result := TValue.From(IntToStr(AIndex))
end;
tkEnumeration: begin
Result := TValue.FromOrdinal(aType.Handle, AIndex);
end;
tkFloat: Begin
Result := TValue.From(0.1 + AIndex);
End;
tkVariant: Begin
Result := TValue.FromVariant(AIndex);
End;
tkSet: begin
// Not really possible to generate a random set, just leave blank
end;
tkInterface, tkClass: begin
vObject := nil;
vPointer := @vObject;
inc(vPointer, AIndex);
vObject := vPointer^;
Result := TValue.From(vObject);
end;
{
tkSet
Set types
tkClass
Class types
tkMethod
Procedure and function method types
tkWString
The Delphi 3 constant which replaces tkLWString
tkArray
Array types, new in Delphi 3
tkRecord
Record types, new in Delphi 3
tkInterface
Interface types, new in Delphi 3
tkDynArray
}
end;
end;
function TObjectConfig<T>.New: T;
var
vConfig: TPropertyConfig;
vValue: TValue;
begin
Result := Self.GetObject(FType, FSetup.ReferenceDepth) as T;
// Result := FType.AsInstance.MetaclassType.Create as T;
//
// // Now loop through properties
// for vConfig in Self.FProperties.Values do begin
// vValue := getValue(vConfig.FPropertyName, vConfig.FType);
// if not vValue.IsEmpty then begin
// vConfig.FFieldType.SetValue(Pointer(Result), vValue);
// end;
// end;
end;
procedure TBaseConfig.InitProperties();
var
vField: TRttiField;
vPropertyConfig: TPropertyConfig;
vList: TList<TPropertyConfig>;
begin
for vField in FType.GetFields do
begin
vPropertyConfig := TPropertyConfig.Create;
vPropertyConfig.FFieldType := vField;
vPropertyConfig.FType := vField.FieldType;
vPropertyConfig.FPropertyName := vField.Name;
if (vField.Name = 'FRefCount') then begin
vPropertyConfig.FProcessProperty := TProcessProperty.Omit;
end;
if not FProperties.ContainsKey(vPropertyConfig.FPropertyName.ToUpper) then begin
FProperties.Add(vPropertyConfig.FPropertyName.ToUpper, vPropertyConfig);
end;
if Assigned(vPropertyConfig.FType) and FPropertiesByType.TryGetValue(vPropertyConfig.FType, vList) then
begin
vList.Add(vPropertyConfig);
end
else begin
vList := TList<TPropertyConfig>.Create;
vList.Add(vPropertyConfig);
if Assigned(vPropertyConfig.FType) then begin
FPropertiesByType.Add(vPropertyConfig.FType, vList);
end;
end;
end;
end;
procedure TObjectConfig<T>.InitType(AType: TRttiType);
var
//vField: TRttiField;
vConfig: TPropertyConfig;
vList: TList<TPropertyConfig>;
vValue: TValue;
i: Integer;
begin
if not FInitializedTypes.Contains(AType) then begin
FInitializedTypes.Add(AType);
vList := FPropertiesByType[AType];
for i := 0 to vList.Count - 1 do begin
vConfig := vList[i];
vValue := GenerateIndexValue(vConfig.FType, i);
if not vValue.IsEmpty then begin
// Write value to object
vConfig.FFieldType.SetValue(pointer(FObject), vValue);
end;
// Now get value we check against
vConfig.FPropertyIdentifier := vConfig.FFieldType.GetValue(pointer(FObject));
end;
end;
end;
procedure TObjectConfig<T>.ObjectConstructed(AObject: TObject);
var
vModificator: TObjectModifyProcedure<T>;
begin
for vModificator in FExecList do begin
vModificator(AObject);
end;
end;
function TObjectConfig<T>.Omit<Typ>(AProperty: TPropertySelector<T, Typ>): TObjectConfig<T>;
var
vConfig: TPropertyConfig;
begin
Result := Self;
vConfig := GetPropertyFromSelector<Typ>(AProperty);
if Assigned(vConfig) then begin
vConfig.FProcessProperty := TProcessProperty.Omit;
end;
end;
function TObjectConfig<T>.Omit<Typ>(AProperty: String): TObjectConfig<T>;
var
vConfig: TPropertyConfig;
begin
Result := Self;
if FProperties.TryGetValue(AProperty.ToUpper, vConfig) then begin
vConfig.FProcessProperty := TProcessProperty.Omit;
end
else begin
raise Exception.Create('Property ' + AProperty + ' not found in class ' + FType.QualifiedName);
end;
end;
function TObjectConfig<T>.OmitAutoProperties: TObjectConfig<T>;
var
vConfig: TPropertyConfig;
begin
Result := Self;
if FPreviousGenerator is TObjectConfig<T> then begin
TObjectConfig<T>(FPreviousGenerator).OmitAutoProperties;
end
else begin
for vConfig in FProperties.Values do begin
if vConfig.FProcessProperty = TProcessProperty.Uninitialized then begin
vConfig.FProcessProperty := TProcessProperty.Omit;
end;
end;
end;
end;
function TObjectConfig<T>.Without<Typ>(AProperty: TPropertySelector<T, Typ>): TObjectConfig<T>;
var
vConfig: TPropertyConfig;
begin
Result := Self;
vConfig := GetPropertyFromSelector<Typ>(AProperty);
if Assigned(vConfig) then begin
vConfig.FProcessProperty := TProcessProperty.Omit;
end;
end;
function TObjectConfig<T>.Without(AProperty: String): TObjectConfig<T>;
var
vConfig: TPropertyConfig;
begin
Result := Self;
if FProperties.TryGetValue(AProperty.ToUpper, vConfig) then begin
vConfig.FProcessProperty := TProcessProperty.Omit;
end;
end;
function TObjectConfig<T>.GetPropertyFromSelector<Typ>(AProperty: TPropertySelector<T, Typ>): TPropertyConfig;
var
vActualValue: Typ;
vValue: TValue;
vIndex: Integer;
vType: TRttiType;
vList: TList<TPropertyConfig>;
vProperty: TPropertyConfig;
begin
Result := nil;
vType := FRttiContext.GetType(TypeInfo(Typ));
if not Assigned(vType) then begin
raise Exception.Create('Autofixture is unable to get RTTI type information for this type');
end;
InitType(vType);
if not FPropertiesByType.ContainsKey(vType) then begin
raise Exception.Create('The class ' + Self.FType.Name + ' doesn''t contain any properties with type ' + vType.Name);
end;
vActualValue := AProperty(FObject);
vValue := TValue.From(vActualValue);
vIndex := GetIndexFromValue(vActualValue);
vList := FPropertiesByType[vType];
if vIndex >= 0 then begin
if vIndex < vList.Count then begin
vProperty := vList[vIndex];
if vProperty.FPropertyIdentifier.ToString = vValue.ToString then begin
Result := vProperty;
Exit;
end;
end;
end;
for vIndex := 0 to vList.Count - 1 do begin
vProperty := vList[vIndex];
if vProperty.FPropertyIdentifier.ToString = vValue.ToString then begin
Result := vProperty;
Exit;
end;
end;
end;
function TObjectConfig<T>.WithValue<Typ>(AProperty: TPropertySelector<T, Typ>; AValue: Typ): TObjectConfig<T>;
var
vConfig: TPropertyConfig;
begin
Result := Self;
vConfig := GetPropertyFromSelector<Typ>(AProperty);
if Assigned(vConfig) then begin
vConfig.FProcessProperty := TProcessProperty.ValueSet;
vConfig.FConfiguredPropertyValue := TValue.From(AValue);
end;
end;
function TObjectConfig<T>.WithValue<Typ>(AProperty: String; AValue: Typ): TObjectConfig<T>;
var
vConfig: TPropertyConfig;
begin
Result := Self;
if FProperties.TryGetValue(AProperty.ToUpper, vConfig) then begin
vConfig.FProcessProperty := TProcessProperty.ValueSet;
vConfig.FConfiguredPropertyValue := TValue.From(AValue);
if vConfig.FConfiguredPropertyValue.isEmpty then begin
if not Assigned(vConfig.FType) then begin
raise Exception.Create('Autofixture is unable to get RTTI type information for this type');
end;
end;
end
else begin
raise Exception.Create('Property named ' + AProperty + ' not found inside class ' + FType.Name);
end;
end;
{ TCollectionItemGenerator }
procedure TCollectionItemGenerator.CallConstructor(AType: TRttiType; AObject: TObject; AReferenceDepth: Integer);
begin
// Constructor not called for collectionItems
end;
constructor TCollectionItemGenerator.Create(AType: TRttiType; APreviousConfig: IObjectGenerator);
begin
inherited Create(AType, APreviousConfig);
// Ignore Index and Collection properties
if FProperties.ContainsKey('INDEX') then begin
FProperties['INDEX'].FProcessProperty := TProcessProperty.Omit;
end;
if FProperties.ContainsKey('COLLECTION') then begin
FProperties['COLLECTION'].FProcessProperty := TProcessProperty.Omit;
end;
end;
destructor TCollectionItemGenerator.Destroy;
begin
inherited;
end;
function TCollectionItemGenerator.getValue(APropertyName: String; AType: TRttiType; AReferenceDepth: integer = -1): TValue;
begin
if (aPropertyName.ToUpper = 'INDEX') or (aPropertyName.ToUpper = 'COLLECTION') then begin
Result := TValue.Empty;
end
else begin
Result := inherited getValue(APropertyName, AType, AReferenceDepth);
end;
end;
procedure TCollectionItemGenerator.ObjectConstructed(AObject: TObject);
begin
// Do nothing
end;
{ TBaseConfig }
constructor TBaseConfig.Create(AType: TRttiType; APreviousConfig: IObjectGenerator);
begin
FType := AType;
FPreviousGenerator := APreviousConfig;
FProperties := TDictionary<String, TPropertyConfig>.Create;
FPropertiesByType := TDictionary<TRttiType, TList<TPropertyConfig>>.Create;
FInitializedTypes := TList<TRttiType>.Create;
InitProperties;
end;
destructor TBaseConfig.Destroy;
begin
FreeAndNil(FProperties);
FreeAndNil(FPropertiesByType);
inherited;
end;
function TBaseConfig.getObject(AType: TRttiType; AReferenceDepth: Integer): TObject;
var
vConfig: TPropertyConfig;
vValue: TValue;
begin
Result := FType.AsInstance.MetaclassType.Create;
CallConstructor(AType, Result, AReferenceDepth);
// Now loop through properties
for vConfig in Self.FProperties.Values do begin
vValue := getValue(vConfig.FPropertyName, vConfig.FType, AReferenceDepth - 1);
if not vValue.IsEmpty then begin
vConfig.FFieldType.SetValue(Pointer(Result), vValue);
end
end;
ObjectConstructed(Result);
end;
function TBaseConfig.getValue(APropertyName: String; AType: TRttiType; AReferenceDepth: integer = -1): TValue;
var
vConfig: TPropertyConfig;
begin
Result := TValue.Empty;
if AReferenceDepth > -1 then begin
if FProperties.TryGetValue(aPropertyName.ToUpper, vConfig) then begin
case vConfig.FProcessProperty of
TProcessProperty.ValueSet: Result := vConfig.FConfiguredPropertyValue;
TProcessProperty.Uninitialized: Result := FPreviousGenerator.getValue(aPropertyName, aType, AReferenceDepth);
end;
end
else begin
// property unknown, try previous config
Result := FPreviousGenerator.getValue(APropertyName, AType, AReferenceDepth);
end;
end;
end;
end.