Repository navigation
Expand file tree
/
Copy pathTestAntiPatterns.pas
More file actions
278 lines (246 loc) · 5.07 KB
/
Copy pathTestAntiPatterns.pas
File metadata and controls
278 lines (246 loc) · 5.07 KB
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
unit TestAntiPatterns;
{------------------------------------------------------------------------------
Test unit for DDevExtensions v3.7.0 Anti-Pattern Detection
This file intentionally contains code anti-patterns to test the
Code Style Checker's new detection features.
Expected detections:
- EmptyFinally: 2 instances
- NestedWith: 1 instance
- DeepNesting: 1 instance (5 levels deep)
- LongMethod: 1 instance (LongMethod procedure)
- LongParamList: 2 instances (7+ parameters)
DELETE THIS FILE AFTER TESTING
------------------------------------------------------------------------------}
interface
type
TAntiPatternTest = class
private
FValue: Integer;
FName: string;
public
constructor Create;
// LongParamList: 7 parameters (exceeds default threshold of 6)
procedure ProcessData(Param1: Integer; Param2: Integer; Param3: string;
Param4: Boolean; Param5: Double; Param6: Integer; Param7: string);
// LongParamList: 8 parameters
function CalculateResult(A, B, C, D, E, F, G, H: Integer): Integer;
procedure TestEmptyFinally;
procedure TestNestedWith;
procedure TestDeepNesting;
end;
implementation
uses
SysUtils, Classes;
constructor TAntiPatternTest.Create;
begin
inherited Create;
FValue := 0;
FName := '';
end;
procedure TAntiPatternTest.ProcessData(Param1: Integer; Param2: Integer;
Param3: string; Param4: Boolean; Param5: Double; Param6: Integer;
Param7: string);
begin
// Just a stub for testing long parameter list detection
FValue := Param1 + Param2;
end;
function TAntiPatternTest.CalculateResult(A, B, C, D, E, F, G, H: Integer): Integer;
begin
// Another long parameter list test
Result := A + B + C + D + E + F + G + H;
end;
procedure TAntiPatternTest.TestEmptyFinally;
var
Stream: TStringList;
begin
// EmptyFinally #1: This finally block is empty
Stream := TStringList.Create;
try
Stream.Add('Test');
finally
// Empty - should be flagged!
end;
// EmptyFinally #2: Another empty finally
try
FValue := 42;
finally
// Also empty - should be flagged!
end;
end;
procedure TAntiPatternTest.TestNestedWith;
var
List1: TStringList;
List2: TStringList;
begin
List1 := TStringList.Create;
List2 := TStringList.Create;
try
// NestedWith: with statements nested more than 1 level deep
with List1 do
begin
Add('Item 1');
with List2 do // Nested with - should be flagged!
begin
Add('Item 2');
Clear;
end;
end;
finally
List2.Free;
List1.Free;
end;
end;
procedure TAntiPatternTest.TestDeepNesting;
var
I, J, K: Integer;
Done: Boolean;
begin
Done := False;
// DeepNesting: 5 levels of control flow nesting (exceeds default of 4)
for I := 0 to 10 do // Level 1
begin
if I > 2 then // Level 2
begin
for J := 0 to 5 do // Level 3
begin
if J > 1 then // Level 4
begin
try // Level 5 - should be flagged!
K := I * J;
if K > 20 then
Done := True;
except
on E: Exception do
Done := False;
end;
end;
end;
end;
end;
if Done then
FValue := 1;
end;
// LongMethod: This procedure exceeds 100 lines (default threshold)
procedure LongMethod;
var
A, B, C, D, E: Integer;
S: string;
begin
// Line 1-10
A := 1;
B := 2;
C := 3;
D := 4;
E := 5;
S := 'Test';
A := A + 1;
B := B + 1;
C := C + 1;
D := D + 1;
// Line 11-20
E := E + 1;
A := A * 2;
B := B * 2;
C := C * 2;
D := D * 2;
E := E * 2;
S := S + '1';
S := S + '2';
S := S + '3';
S := S + '4';
// Line 21-30
A := 10;
B := 20;
C := 30;
D := 40;
E := 50;
S := 'Line 26';
S := 'Line 27';
S := 'Line 28';
S := 'Line 29';
S := 'Line 30';
// Line 31-40
A := 11;
B := 21;
C := 31;
D := 41;
E := 51;
S := 'Line 36';
S := 'Line 37';
S := 'Line 38';
S := 'Line 39';
S := 'Line 40';
// Line 41-50
A := 12;
B := 22;
C := 32;
D := 42;
E := 52;
S := 'Line 46';
S := 'Line 47';
S := 'Line 48';
S := 'Line 49';
S := 'Line 50';
// Line 51-60
A := 13;
B := 23;
C := 33;
D := 43;
E := 53;
S := 'Line 56';
S := 'Line 57';
S := 'Line 58';
S := 'Line 59';
S := 'Line 60';
// Line 61-70
A := 14;
B := 24;
C := 34;
D := 44;
E := 54;
S := 'Line 66';
S := 'Line 67';
S := 'Line 68';
S := 'Line 69';
S := 'Line 70';
// Line 71-80
A := 15;
B := 25;
C := 35;
D := 45;
E := 55;
S := 'Line 76';
S := 'Line 77';
S := 'Line 78';
S := 'Line 79';
S := 'Line 80';
// Line 81-90
A := 16;
B := 26;
C := 36;
D := 46;
E := 56;
S := 'Line 86';
S := 'Line 87';
S := 'Line 88';
S := 'Line 89';
S := 'Line 90';
// Line 91-100
A := 17;
B := 27;
C := 37;
D := 47;
E := 57;
S := 'Line 96';
S := 'Line 97';
S := 'Line 98';
S := 'Line 99';
S := 'Line 100';
// Line 101-105 - exceeds 100 line threshold
A := 18;
B := 28;
C := 38;
D := 48;
E := 58;
end;
end.