-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathSelfReferenceMeTest.java
498 lines (446 loc) · 14.6 KB
/
SelfReferenceMeTest.java
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
package com.sap.adt.abapcleaner.rules.syntax;
import org.junit.jupiter.api.Test;
import com.sap.adt.abapcleaner.rulebase.RuleID;
import com.sap.adt.abapcleaner.rulebase.RuleTestBase;
class SelfReferenceMeTest extends RuleTestBase {
SelfReferenceMeTest() {
super(RuleID.SELF_REFERENCE_ME);
}
private void buildStart() {
// the following CLASS DEFINITION is used for all the tests:
buildSrc("CLASS any_class DEFINITION.");
buildSrc(" PUBLIC SECTION.");
buildSrc(" METHODS:");
buildSrc(" any_method IMPORTING !a TYPE i");
buildSrc(" !b TYPE string,");
buildSrc(" other_method IMPORTING !num TYPE i");
buildSrc(" EXPORTING !text TYPE string");
buildSrc(" RETURNING VALUE(result) TYPE i.");
buildSrc(" METHODS redefined REDEFINITION.");
buildSrc("");
buildSrc(" PRIVATE SECTION.");
buildSrc(" DATA num TYPE i.");
buildSrc(" DATA prio TYPE i.");
buildSrc("ENDCLASS.");
buildSrc("");
buildSrc("CLASS any_class IMPLEMENTATION.");
buildSrc(" METHOD any_method.");
buildSrc(" ENDMETHOD.");
buildSrc("");
buildSrc(" METHOD other_method.");
copyExpFromSrc();
}
private void buildEnd() {
buildSrc(" ENDMETHOD.");
buildSrc("ENDCLASS.");
buildExp(" ENDMETHOD.");
buildExp("ENDCLASS.");
}
@Test
void testMethodCallWithParams() {
buildStart();
buildSrc(" me->any_method( a = 5");
buildSrc(" b = `abc` ).");
buildExp(" any_method( a = 5");
buildExp(" b = `abc` ).");
buildEnd();
testRule();
}
@Test
void testAttributeAccess() {
buildStart();
buildSrc(" result = me->prio.");
buildExp(" result = prio.");
buildEnd();
testRule();
}
@Test
void testMethodCallAsOperand() {
buildStart();
buildSrc(" result += me->get_count( ).");
buildExp(" result += get_count( ).");
buildEnd();
testRule();
}
@Test
void testExprInParameterAssignment() {
buildStart();
buildSrc(" ets_result = VALUE #( ( line_id = 1 price = me->mv_price_1 )");
buildSrc(" ( line_id = 2 price = me->create_price( iv_currency = me->get_currency( )");
buildSrc(" iv_amount = me->mv_amount ) ) ).");
buildExp(" ets_result = VALUE #( ( line_id = 1 price = mv_price_1 )");
buildExp(" ( line_id = 2 price = create_price( iv_currency = get_currency( )");
buildExp(" iv_amount = mv_amount ) ) ).");
buildEnd();
testRule();
}
@Test
void testLocalVariableWithSameName() {
buildStart();
buildSrc(" DATA prio TYPE i.");
buildSrc(" prio = 2.");
buildSrc(" me->prio = 3.");
buildEnd();
copyExpFromSrc();
testRule();
}
@Test
void testKnownParameterWithSameName() {
buildStart();
buildSrc(" me->num = 1.");
buildSrc(" me->text = `abc`.");
buildSrc(" me->result = 42.");
buildSrc(" me->num = ( me->result + num ) * result + ( result + me->num ) * me->result.");
buildEnd();
copyExpFromSrc();
testRule();
}
@Test
void testAssumedParameterWithSameName() {
// expect the nothing to happen when the method signature is unknown
// here, we do NOT use buildStart() and buildEnd()!
buildSrc(" me->num = num.");
buildSrc(" me->num = num-component.");
buildSrc(" me->num = num+4(2).");
buildSrc(" me->num = num->get_next_num( ).");
buildSrc(" me->num = any_method( text = |text with { num }| ).");
copyExpFromSrc();
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testRedefinedMethod() {
// as the signature of the redefined method is out of sight, expect me-> to remain unchanged,
// since 'num', 'text' and 'result' may be parameters
buildStart();
buildSrc(" ENDMETHOD.");
buildSrc("");
buildSrc(" METHOD redefined.");
buildSrc(" me->num = 1.");
buildSrc(" me->text = `abc`.");
buildSrc(" me->result = 42.");
buildEnd();
copyExpFromSrc();
testRule();
}
@Test
void testLocalInterfaceMethodConsidered() {
// expect the signature of method LIF_ANY_INTERFACE~ANY_METHOD to be correctly identified,
// meaning that ME can safely be removed from ME->MV_VALUE, but NOT from ME->RESULT
buildSrc("INTERFACE lif_any_interface.");
buildSrc(" METHODS any_method");
buildSrc(" EXPORTING result TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("CLASS lcl_test DEFINITION.");
buildSrc(" PUBLIC SECTION.");
buildSrc(" INTERFACES lif_any_interface.");
buildSrc(" PRIVATE SECTION.");
buildSrc(" DATA mv_value TYPE i.");
buildSrc(" DATA result TYPE i.");
buildSrc("ENDCLASS.");
buildSrc("");
buildSrc("CLASS lcl_test IMPLEMENTATION.");
buildSrc(" METHOD lif_any_interface~any_method.");
buildSrc(" me->mv_value = 1.");
buildSrc(" me->result = 2.");
buildSrc(" ENDMETHOD.");
buildSrc("ENDCLASS.");
buildExp("INTERFACE lif_any_interface.");
buildExp(" METHODS any_method");
buildExp(" EXPORTING result TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("CLASS lcl_test DEFINITION.");
buildExp(" PUBLIC SECTION.");
buildExp(" INTERFACES lif_any_interface.");
buildExp(" PRIVATE SECTION.");
buildExp(" DATA mv_value TYPE i.");
buildExp(" DATA result TYPE i.");
buildExp("ENDCLASS.");
buildExp("");
buildExp("CLASS lcl_test IMPLEMENTATION.");
buildExp(" METHOD lif_any_interface~any_method.");
buildExp(" mv_value = 1.");
buildExp(" me->result = 2.");
buildExp(" ENDMETHOD.");
buildExp("ENDCLASS.");
testRule();
}
@Test
void testLocalInterfacesWithCascadingAliases() {
// expect the cascading ALIASES to be correctly interpreted, meaning that in method M1, "ME->V1" must remain,
// in method M2, "ME->V2" must remain, and in method M3, "ME->V3" must remain, while otherwise, ME-> can be removed
buildSrc("INTERFACE i1.");
buildSrc(" METHODS meth EXPORTING v1 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("INTERFACE i2.");
buildSrc(" INTERFACES i1.");
buildSrc(" ALIASES m1 FOR i1~meth.");
buildSrc(" METHODS meth EXPORTING v2 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("INTERFACE i3.");
buildSrc(" INTERFACES i2.");
buildSrc(" ALIASES: m1 FOR i2~m1,");
buildSrc(" m2 FOR i2~meth.");
buildSrc("");
buildSrc(" METHODS meth EXPORTING v3 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("CLASS c1 DEFINITION.");
buildSrc(" PUBLIC SECTION.");
buildSrc(" INTERFACES i3.");
buildSrc(" ALIASES: m1 FOR i3~m1,");
buildSrc(" m2 FOR i3~m2,");
buildSrc(" m3 FOR i3~meth.");
buildSrc("");
buildSrc(" DATA: v1 TYPE i,");
buildSrc(" v2 TYPE i,");
buildSrc(" v3 TYPE i.");
buildSrc("ENDCLASS.");
buildSrc("");
buildSrc("CLASS c1 IMPLEMENTATION.");
buildSrc(" METHOD m1.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("");
buildSrc(" METHOD m2.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("");
buildSrc(" METHOD m3.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("ENDCLASS.");
buildExp("INTERFACE i1.");
buildExp(" METHODS meth EXPORTING v1 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("INTERFACE i2.");
buildExp(" INTERFACES i1.");
buildExp(" ALIASES m1 FOR i1~meth.");
buildExp(" METHODS meth EXPORTING v2 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("INTERFACE i3.");
buildExp(" INTERFACES i2.");
buildExp(" ALIASES: m1 FOR i2~m1,");
buildExp(" m2 FOR i2~meth.");
buildExp("");
buildExp(" METHODS meth EXPORTING v3 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("CLASS c1 DEFINITION.");
buildExp(" PUBLIC SECTION.");
buildExp(" INTERFACES i3.");
buildExp(" ALIASES: m1 FOR i3~m1,");
buildExp(" m2 FOR i3~m2,");
buildExp(" m3 FOR i3~meth.");
buildExp("");
buildExp(" DATA: v1 TYPE i,");
buildExp(" v2 TYPE i,");
buildExp(" v3 TYPE i.");
buildExp("ENDCLASS.");
buildExp("");
buildExp("CLASS c1 IMPLEMENTATION.");
buildExp(" METHOD m1.");
buildExp(" me->v1 = 1.");
buildExp(" v2 = 2.");
buildExp(" v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("");
buildExp(" METHOD m2.");
buildExp(" v1 = 1.");
buildExp(" me->v2 = 2.");
buildExp(" v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("");
buildExp(" METHOD m3.");
buildExp(" v1 = 1.");
buildExp(" v2 = 2.");
buildExp(" me->v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("ENDCLASS.");
testRule();
}
@Test
void testGlobalAndRecursiveLocalInterfaces() {
// expect that the method signatures from the recursively implemented local interfaces are correctly identified
// and ME-> is removed where possible, while nothing is changed in the global interface method, because its
// signature is 'out of sight'
buildSrc("INTERFACE i1.");
buildSrc(" METHODS meth EXPORTING v1 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("INTERFACE i2.");
buildSrc(" INTERFACES i1.");
buildSrc(" METHODS meth EXPORTING v2 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("INTERFACE i3.");
buildSrc(" INTERFACES i2.");
buildSrc(" METHODS meth EXPORTING v3 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("CLASS c1 DEFINITION.");
buildSrc(" PUBLIC SECTION.");
buildSrc(" INTERFACES: i3,");
buildSrc(" if_global.");
buildSrc("");
buildSrc(" DATA: v1 TYPE i,");
buildSrc(" v2 TYPE i,");
buildSrc(" v3 TYPE i.");
buildSrc("ENDCLASS.");
buildSrc("");
buildSrc("CLASS c1 IMPLEMENTATION.");
buildSrc(" METHOD i1~meth.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("");
buildSrc(" METHOD i2~meth.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("");
buildSrc(" METHOD i3~meth.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("");
buildSrc(" METHOD if_global~meth.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("ENDCLASS.");
buildExp("INTERFACE i1.");
buildExp(" METHODS meth EXPORTING v1 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("INTERFACE i2.");
buildExp(" INTERFACES i1.");
buildExp(" METHODS meth EXPORTING v2 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("INTERFACE i3.");
buildExp(" INTERFACES i2.");
buildExp(" METHODS meth EXPORTING v3 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("CLASS c1 DEFINITION.");
buildExp(" PUBLIC SECTION.");
buildExp(" INTERFACES: i3,");
buildExp(" if_global.");
buildExp("");
buildExp(" DATA: v1 TYPE i,");
buildExp(" v2 TYPE i,");
buildExp(" v3 TYPE i.");
buildExp("ENDCLASS.");
buildExp("");
buildExp("CLASS c1 IMPLEMENTATION.");
buildExp(" METHOD i1~meth.");
buildExp(" me->v1 = 1.");
buildExp(" v2 = 2.");
buildExp(" v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("");
buildExp(" METHOD i2~meth.");
buildExp(" v1 = 1.");
buildExp(" me->v2 = 2.");
buildExp(" v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("");
buildExp(" METHOD i3~meth.");
buildExp(" v1 = 1.");
buildExp(" v2 = 2.");
buildExp(" me->v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("");
buildExp(" METHOD if_global~meth.");
buildExp(" me->v1 = 1.");
buildExp(" me->v2 = 2.");
buildExp(" me->v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("ENDCLASS.");
testRule();
}
@Test
void testEndlessLoopAvoided() {
// ensure an endless loop is avoided despite the syntax error of interface i3 implementing itself
buildSrc("INTERFACE i1.");
buildSrc(" METHODS meth EXPORTING v1 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("INTERFACE i2.");
buildSrc(" INTERFACES i1.");
buildSrc(" METHODS meth EXPORTING v2 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("INTERFACE i3.");
buildSrc(" \" syntax error: i3 cannot implement itself");
buildSrc(" INTERFACES: i3, i2.");
buildSrc(" METHODS meth EXPORTING v3 TYPE i.");
buildSrc("ENDINTERFACE.");
buildSrc("");
buildSrc("CLASS c1 DEFINITION.");
buildSrc(" PUBLIC SECTION.");
buildSrc(" INTERFACES i3.");
buildSrc("");
buildSrc(" DATA: v1 TYPE i,");
buildSrc(" v2 TYPE i,");
buildSrc(" v3 TYPE i.");
buildSrc("ENDCLASS.");
buildSrc("");
buildSrc("CLASS c1 IMPLEMENTATION.");
buildSrc(" METHOD i1~meth.");
buildSrc(" me->v1 = 1.");
buildSrc(" me->v2 = 2.");
buildSrc(" me->v3 = 3.");
buildSrc(" ENDMETHOD.");
buildSrc("ENDCLASS.");
buildExp("INTERFACE i1.");
buildExp(" METHODS meth EXPORTING v1 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("INTERFACE i2.");
buildExp(" INTERFACES i1.");
buildExp(" METHODS meth EXPORTING v2 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("INTERFACE i3.");
buildExp(" \" syntax error: i3 cannot implement itself");
buildExp(" INTERFACES: i3, i2.");
buildExp(" METHODS meth EXPORTING v3 TYPE i.");
buildExp("ENDINTERFACE.");
buildExp("");
buildExp("CLASS c1 DEFINITION.");
buildExp(" PUBLIC SECTION.");
buildExp(" INTERFACES i3.");
buildExp("");
buildExp(" DATA: v1 TYPE i,");
buildExp(" v2 TYPE i,");
buildExp(" v3 TYPE i.");
buildExp("ENDCLASS.");
buildExp("");
buildExp("CLASS c1 IMPLEMENTATION.");
buildExp(" METHOD i1~meth.");
buildExp(" me->v1 = 1.");
buildExp(" v2 = 2.");
buildExp(" v3 = 3.");
buildExp(" ENDMETHOD.");
buildExp("ENDCLASS.");
testRule();
}
}