forked from VSoftTechnologies/Delphi-Mocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delphi.Mocks.AutoMock.pas
76 lines (59 loc) · 1.79 KB
/
Delphi.Mocks.AutoMock.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
unit Delphi.Mocks.AutoMock;
interface
uses
TypInfo,
System.Generics.Collections,
Delphi.Mocks,
Delphi.Mocks.WeakReference;
type
TAutoMock = class(TWeakReferencedObject, IAutoMock)
private
FMocks : TList<IProxy>;
public
function Mock(const ATypeInfo : PTypeInfo) : IProxy;
procedure Add(const ATypeName : string; const AMock: IProxy);
constructor Create;
destructor Destroy; override;
end;
//TODO: Add getting out a previously added mock. This would be done in the RecordHit of the method data object.
implementation
uses
Windows,
Delphi.Mocks.Validation,
Delphi.Mocks.Proxy.TypeInfo;
{ TAutoMock }
procedure TAutoMock.Add(const ATypeName : string; const AMock: IProxy);
begin
FMocks.Add(AMock);
end;
constructor TAutoMock.Create;
begin
inherited Create;
FMocks := TList<IProxy>.Create;
end;
destructor TAutoMock.Destroy;
var
I: Integer;
begin
for I := 0 to FMocks.Count - 1 do
FMocks[I] := nil;
FMocks.Clear;
inherited;
end;
function TAutoMock.Mock(const ATypeInfo : PTypeInfo) : IProxy;
var
proxy: IProxy;
proxyAsType: IProxy;
begin
//Raise exceptions if the mock doesn't meet the requirements.
TMocksValidation.CheckMockType(ATypeInfo);
//We create new mocks using ourself as the auto mocking reference
proxy := TProxy.Create(ATypeInfo, Self, false);
proxyAsType := proxy.ProxyFromType(ATypeInfo);
FMocks.Add(proxy);
//Push the proxy into the result we are returning.
if proxyAsType.QueryInterface(GetTypeData(TypeInfo(IProxy)).Guid, result) <> 0 then
//TODO: This raise seems superfluous as the only types which are created are controlled by us above. They all implement IProxy<T>
raise EMockNoProxyException.Create('Error casting to interface ' + ATypeInfo.NameStr + ' , proxy does not appear to implememnt IProxy');
end;
end.