-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTests.TCommandAction.pas
179 lines (154 loc) · 3.8 KB
/
Tests.TCommandAction.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
unit Tests.TCommandAction;
interface
uses
DUnitX.TestFramework,
System.Classes,
System.SysUtils,
Pattern.Command,
Pattern.CommandAction,
Vcl.StdCtrls,
Vcl.Forms;
{$M+}
type
[TestFixture]
TestCommandAction = class(TObject)
private
fOwnerComponent: TComponent;
fStringList: TStringList;
fAction: TCommandAction;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
published
procedure ActionWithCaption;
procedure ActionWithCommand;
procedure ActionWithShotcut;
procedure ActionWitnEventOnUpdate;
procedure ActionWitEventAfterExecution;
procedure ActionWithInjections;
procedure ActionCaption_WillChangeButtonCaption;
end;
implementation
uses
Vcl.Menus;
type
TTestCommand = class(TCommand)
const
DefaultRange = 10;
private
FRandomNumbers: TStringList;
FRange: integer;
protected
procedure DoGuard; override;
procedure DoExecute; override;
public
constructor Create(AOwner: TComponent); override;
published
property Range: integer read FRange write FRange;
property RandomNumbers: TStringList read FRandomNumbers
write FRandomNumbers;
end;
{$REGION 'implementation TTestCommand -----------------'}
constructor TTestCommand.Create(AOwner: TComponent);
begin
inherited;
Randomize;
Range := DefaultRange;
end;
procedure TTestCommand.DoGuard;
begin
System.Assert(RandomNumbers <> nil);
end;
procedure TTestCommand.DoExecute;
begin
RandomNumbers.Add((1 + Random(Range)).ToString);
end;
{$ENDREGION --------------------------------------------}
procedure TestCommandAction.Setup;
begin
fOwnerComponent := TComponent.Create(nil);
fAction := TCommandAction.Create(fOwnerComponent);
fStringList := TStringList.Create;
end;
procedure TestCommandAction.TearDown;
begin
fOwnerComponent.Free;
fStringList.Free;
end;
procedure TestCommandAction.ActionWithCaption;
begin
// Arrage & Act:
fAction.WithCaption('Execute test command');
// Assert
Assert.AreEqual('Execute test command', fAction.Caption);
end;
procedure TestCommandAction.ActionWithCommand;
var
cmd: TTestCommand;
begin
// Arrage:
cmd := TTestCommand.Create(fOwnerComponent);
cmd.WithInjections([fStringList]);
// Act:
fAction.WithCommand(cmd);
fAction.Execute;
fAction.Execute;
// Assert
Assert.AreEqual(2, cmd.RandomNumbers.Count);
end;
procedure TestCommandAction.ActionWithShotcut;
var
aShortCut: TShortCut;
begin
aShortCut := TextToShortCut('CTRL+K');
fAction.WithShortCut(aShortCut);
Assert.AreEqual(ShortCutToText(aShortCut), ShortCutToText(fAction.ShortCut));
end;
procedure TestCommandAction.ActionWitnEventOnUpdate;
begin
fAction.WithEventOnUpdate(
procedure(act: TCommandAction)
begin
act.Tag := act.Tag + 1;
end);
fAction.Update;
Assert.AreEqual(1, fAction.Tag);
end;
procedure TestCommandAction.ActionWitEventAfterExecution;
begin
fAction.Tag := -1;
fAction.Command := TTestCommand.Create(fOwnerComponent);
fAction.Command.WithInjections([fStringList]);
fAction.WitEventAfterExecution(
procedure(act: TCommandAction)
begin
act.Tag := 99;
end);
fAction.Execute;
Assert.AreEqual(99, fAction.Tag);
end;
procedure TestCommandAction.ActionWithInjections;
var
actualNumbers: integer;
begin
fAction // --+
.WithCommand(TTestCommand.Create(fOwnerComponent)) //--+
.WithInjections([fStringList]);
fAction.Execute;
fAction.Execute;
fAction.Execute;
actualNumbers := (fAction.Command as TTestCommand).RandomNumbers.Count;
Assert.AreEqual(3, actualNumbers);
end;
procedure TestCommandAction.ActionCaption_WillChangeButtonCaption;
var
aButton: TButton;
begin
aButton := TButton.Create(fOwnerComponent);
fAction.WithCaption('Sample caption');
aButton.Action := fAction;
Assert.AreEqual('Sample caption', aButton.Caption);
end;
end.