-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathAutoWrapEventHandlerTest.pas
150 lines (136 loc) · 3.87 KB
/
AutoWrapEventHandlerTest.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
unit AutoWrapEventHandlerTest;
interface
uses
Types,
DUnitX.TestFramework,
PythonEngine,
WrapDelphi, System.Classes;
type
TTestGetObjectEvent = procedure(Sender: TObject; var AObject: TObject) of object;
TTestGetValueEvent = procedure(Sender: TObject; var AValue: Double) of object;
TTestAuto = class(TComponent)
private
FObject: TObject;
FValue: Double;
FOnGetObject: TTestGetObjectEvent;
FOnGetValue: TTestGetValueEvent;
ProcessCalled: Boolean;
public
procedure Process;
published
property OnGetObject: TTestGetObjectEvent read FOnGetObject write FOnGetObject;
property OnGetValue: TTestGetValueEvent read FOnGetValue write FOnGetValue;
end;
[TestFixture]
TTestAutoWrapEventHandlers = class(TObject)
private
PythonEngine: TPythonEngine;
DelphiModule: TPythonModule;
DelphiWrapper: TPyDelphiWrapper;
public
[SetupFixture]
procedure SetupFixture;
[TearDownFixture]
procedure TearDownFixture;
[Test]
procedure TestProcessWithValue;
[Test]
procedure TestProcessWithObject;
end;
implementation
uses
System.Diagnostics,
System.SysUtils,
TypInfo;
{ TTest }
procedure TTestAuto.Process;
begin
ProcessCalled := True;
if Assigned(FOnGetObject) then
FOnGetObject(Self, FObject);
if Assigned(FOnGetValue) then
FOnGetValue(Self, FValue);
end;
{ TTestAutoWrapEventHandlers }
procedure TTestAutoWrapEventHandlers.SetupFixture;
begin
PythonEngine := TPythonEngine.Create(nil);
PythonEngine.Name := 'PythonEngine';
PythonEngine.AutoLoad := False;
PythonEngine.FatalAbort := True;
PythonEngine.FatalMsgDlg := True;
PythonEngine.UseLastKnownVersion := True;
PythonEngine.AutoFinalize := True;
PythonEngine.PyFlags := [pfInteractive];
DelphiModule := TPythonModule.Create(nil);
DelphiModule.Name := 'DelphiModule';
DelphiModule.Engine := PythonEngine;
DelphiModule.ModuleName := 'delphi';
DelphiWrapper := TPyDelphiWrapper.Create(nil);
DelphiWrapper.Name := 'PyDelphiWrapper';
DelphiWrapper.Engine := PythonEngine;
DelphiWrapper.Module := DelphiModule;
DelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<TTestAuto>);
PythonEngine.LoadDll;
end;
procedure TTestAutoWrapEventHandlers.TearDownFixture;
begin
PythonEngine.Free;
DelphiWrapper.Free;
DelphiModule.Free;
end;
procedure TTestAutoWrapEventHandlers.TestProcessWithValue;
var
Test: TTestAuto;
begin
Test := TTestAuto.Create(nil);
try
DelphiWrapper.DefineVar('test', Test);
PythonEngine.ExecString(
'import delphi' + LF +
'' + LF +
'def MyOnGetValue(sender, value):' + LF +
' value.Value = 3.14' + LF +
'' + LF +
'delphi.test.OnGetValue = MyOnGetValue' + LF +
'delphi.test.Process()' + LF +
''
);
Assert.IsTrue(Test.ProcessCalled);
Assert.AreEqual(Test.FValue, 3.14);
finally
Test.Free;
end;
end;
procedure TTestAutoWrapEventHandlers.TestProcessWithObject;
var
Test: TTestAuto;
begin
Test := TTestAuto.Create(nil);
try
DelphiWrapper.DefineVar('test', Test);
PythonEngine.ExecString(
'import delphi' + LF +
'' + LF +
'def MyOnGetObject(sender, value):' + LF +
' value.Value = sender' + LF +
'' + LF +
'delphi.test.OnGetObject = MyOnGetObject' + LF +
'delphi.test.Process()' + LF +
''
);
var StopWatch := TStopwatch.StartNew;
var Count := 100000;
for var I := 0 to Count do
Test.Process;
StopWatch.Stop;
WriteLn(Format('*********** Elaplsed time for %d event calls: %d', [Count, StopWatch.ElapsedMilliseconds]));
Assert.IsTrue(Test.ProcessCalled);
Assert.AreSame(Test, Test.FObject);
finally
Test.Free;
end;
end;
initialization
TDUnitX.RegisterTestFixture(TTestAutoWrapEventHandlers);
end.