forked from SAP/abap-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalDeclarationOrderTest.java
1643 lines (1372 loc) · 53.5 KB
/
LocalDeclarationOrderTest.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.sap.adt.abapcleaner.rules.declarations;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.sap.adt.abapcleaner.rulebase.RuleID;
import com.sap.adt.abapcleaner.rulebase.RuleTestBase;
public class LocalDeclarationOrderTest extends RuleTestBase {
private LocalDeclarationOrderRule rule;
LocalDeclarationOrderTest() {
super(RuleID.LOCAL_DECLARATION_ORDER);
rule = (LocalDeclarationOrderRule)getRule();
}
@BeforeEach
void setUp() {
// setup default test configuration (may be modified in the individual test methods)
rule.configTypesOrder.setEnumValue(LocalTypesDeclarationOrder.METHOD_START_KEEP_ORDER);
rule.configConstantsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_KEEP_ORDER);
rule.configStaticsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configFieldSymbolsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configDistinctBlocks.setValue(true);
rule.configEmptyLine.setValue(true);
rule.configMoveComments.setValue(true);
rule.configRearrangeChains.setValue(true);
rule.configConsiderComments.setValue(false);
}
@Test
void testDistinctBlocksEmptyLineChangeOrder() {
rule.configConstantsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configStaticsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configFieldSymbolsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
buildSrc(" DATA lv_a8 TYPE i.");
buildSrc(" STATICS sv_b2 TYPE i.");
buildSrc(" FIELD-SYMBOLS <lv_c7> TYPE ty_s_data.");
buildSrc(" DATA: lv_d4 TYPE i.");
buildSrc(" FIELD-SYMBOLS <lv_e1> TYPE ty_s_data.");
buildSrc(" STATICS sv_f5 TYPE i.");
buildSrc(" CONSTANTS: lc_g6 TYPE i VALUE 1.");
buildSrc(" CONSTANTS lc_h3 TYPE i VALUE 1.");
buildSrc("");
buildSrc(" UNASSIGN <lv_e1>.");
buildSrc(" CLEAR sv_b2.");
buildSrc(" IF iv_case = lc_h3.");
buildSrc(" CLEAR lv_d4.");
buildSrc(" CLEAR sv_f5.");
buildSrc(" ELSEIF iv_case = lc_g6.");
buildSrc(" UNASSIGN <lv_c7>.");
buildSrc(" CLEAR lv_a8.");
buildSrc(" ENDIF.");
buildExp(" CONSTANTS lc_h3 TYPE i VALUE 1.");
buildExp(" CONSTANTS: lc_g6 TYPE i VALUE 1.");
buildExp("");
buildExp(" STATICS sv_b2 TYPE i.");
buildExp(" STATICS sv_f5 TYPE i.");
buildExp("");
buildExp(" DATA: lv_d4 TYPE i.");
buildExp(" DATA lv_a8 TYPE i.");
buildExp("");
buildExp(" FIELD-SYMBOLS <lv_e1> TYPE ty_s_data.");
buildExp(" FIELD-SYMBOLS <lv_c7> TYPE ty_s_data.");
buildExp("");
buildExp(" UNASSIGN <lv_e1>.");
buildExp(" CLEAR sv_b2.");
buildExp(" IF iv_case = lc_h3.");
buildExp(" CLEAR lv_d4.");
buildExp(" CLEAR sv_f5.");
buildExp(" ELSEIF iv_case = lc_g6.");
buildExp(" UNASSIGN <lv_c7>.");
buildExp(" CLEAR lv_a8.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testDistinctBlocksEmptyLineKeepOrder() {
rule.configConstantsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_KEEP_ORDER);
rule.configStaticsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_KEEP_ORDER);
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_KEEP_ORDER);
rule.configFieldSymbolsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_KEEP_ORDER);
buildSrc(" DATA lv_a8 TYPE i.");
buildSrc(" STATICS sv_b2 TYPE i.");
buildSrc(" FIELD-SYMBOLS <lv_c7> TYPE ty_s_data.");
buildSrc(" DATA lv_d4 TYPE i.");
buildSrc(" FIELD-SYMBOLS <lv_e1> TYPE ty_s_data.");
buildSrc(" STATICS sv_f5 TYPE i.");
buildSrc(" CONSTANTS lc_g6 TYPE i VALUE 1.");
buildSrc(" CONSTANTS lc_h3 TYPE i VALUE 1.");
buildSrc("");
buildSrc(" UNASSIGN <lv_e1>.");
buildSrc(" CLEAR sv_b2.");
buildSrc(" IF iv_case = lc_h3.");
buildSrc(" CLEAR lv_d4.");
buildSrc(" CLEAR sv_f5.");
buildSrc(" ELSEIF iv_case = lc_g6.");
buildSrc(" UNASSIGN <lv_c7>.");
buildSrc(" CLEAR lv_a8.");
buildSrc(" ENDIF.");
buildExp(" CONSTANTS lc_g6 TYPE i VALUE 1.");
buildExp(" CONSTANTS lc_h3 TYPE i VALUE 1.");
buildExp("");
buildExp(" STATICS sv_b2 TYPE i.");
buildExp(" STATICS sv_f5 TYPE i.");
buildExp("");
buildExp(" DATA lv_a8 TYPE i.");
buildExp(" DATA lv_d4 TYPE i.");
buildExp("");
buildExp(" FIELD-SYMBOLS <lv_c7> TYPE ty_s_data.");
buildExp(" FIELD-SYMBOLS <lv_e1> TYPE ty_s_data.");
buildExp("");
buildExp(" UNASSIGN <lv_e1>.");
buildExp(" CLEAR sv_b2.");
buildExp(" IF iv_case = lc_h3.");
buildExp(" CLEAR lv_d4.");
buildExp(" CLEAR sv_f5.");
buildExp(" ELSEIF iv_case = lc_g6.");
buildExp(" UNASSIGN <lv_c7>.");
buildExp(" CLEAR lv_a8.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testJointBlocksNoEmptyLineChangeOrder() {
rule.configConstantsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configStaticsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configFieldSymbolsOrder.setEnumValue(LocalDeclarationOrder.METHOD_START_CHANGE_ORDER);
rule.configDistinctBlocks.setValue(false);
rule.configEmptyLine.setValue(false);
buildSrc(" DATA lv_a8 TYPE i.");
buildSrc(" STATICS sv_b2 TYPE i.");
buildSrc(" FIELD-SYMBOLS <lv_c7> TYPE ty_s_data.");
buildSrc(" DATA lv_d4 TYPE i.");
buildSrc(" FIELD-SYMBOLS <lv_e1> TYPE ty_s_data.");
buildSrc(" STATICS sv_f5 TYPE i.");
buildSrc(" CONSTANTS lc_g6 TYPE i VALUE 1.");
buildSrc(" CONSTANTS lc_h3 TYPE i VALUE 1.");
buildSrc("");
buildSrc(" UNASSIGN <lv_e1>.");
buildSrc(" CLEAR sv_b2.");
buildSrc(" IF iv_case = lc_h3.");
buildSrc(" CLEAR lv_d4.");
buildSrc(" CLEAR sv_f5.");
buildSrc(" ELSEIF iv_case = lc_g6.");
buildSrc(" UNASSIGN <lv_c7>.");
buildSrc(" CLEAR lv_a8.");
buildSrc(" ENDIF.");
buildExp(" CONSTANTS lc_h3 TYPE i VALUE 1.");
buildExp(" CONSTANTS lc_g6 TYPE i VALUE 1.");
buildExp("");
buildExp(" FIELD-SYMBOLS <lv_e1> TYPE ty_s_data.");
buildExp(" STATICS sv_b2 TYPE i.");
buildExp(" DATA lv_d4 TYPE i.");
buildExp(" STATICS sv_f5 TYPE i.");
buildExp(" FIELD-SYMBOLS <lv_c7> TYPE ty_s_data.");
buildExp(" DATA lv_a8 TYPE i.");
buildExp("");
buildExp(" UNASSIGN <lv_e1>.");
buildExp(" CLEAR sv_b2.");
buildExp(" IF iv_case = lc_h3.");
buildExp(" CLEAR lv_d4.");
buildExp(" CLEAR sv_f5.");
buildExp(" ELSEIF iv_case = lc_g6.");
buildExp(" UNASSIGN <lv_c7>.");
buildExp(" CLEAR lv_a8.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testMoveTypesToMethodStart() {
// ensure that all TYPES declarations are placed at method start (but after the initial comment),
// and comments between multiple TYPES are kept
buildSrc(" \" initial comment on this method");
buildSrc(" FIELD-SYMBOLS <ls_any> TYPE ty_s_data.");
buildSrc(" TYPES: BEGIN OF ty_s_data,");
buildSrc(" comp TYPE i.");
buildSrc(" INCLUDE TYPE ty_s_include.");
buildSrc(" TYPES END OF ty_s_data.");
buildSrc(" STATICS sv_any TYPE i.");
buildSrc(" IF iv_case = 1.");
buildSrc(" TYPES ty_tt_data TYPE STANDARD TABLE OF ty_s_data WITH EMPTY KEY.");
buildSrc(" \" comment on ty_ts_data");
buildSrc(" TYPES ty_ts_data TYPE SORTED TABLE OF ty_s_data WITH NON-UNIQUE KEY comp.");
buildSrc(" \" comment on lv_any");
buildSrc(" DATA lv_any TYPE i VALUE 1.");
buildSrc("");
buildSrc(" sv_any += lv_any.");
buildSrc(" ENDIF.");
buildSrc(" rv_result = sv_any + <ls_any>-value.");
buildExp(" \" initial comment on this method");
buildExp("");
buildExp(" TYPES: BEGIN OF ty_s_data,");
buildExp(" comp TYPE i.");
buildExp(" INCLUDE TYPE ty_s_include.");
buildExp(" TYPES END OF ty_s_data.");
buildExp(" TYPES ty_tt_data TYPE STANDARD TABLE OF ty_s_data WITH EMPTY KEY.");
buildExp(" \" comment on ty_ts_data");
buildExp(" TYPES ty_ts_data TYPE SORTED TABLE OF ty_s_data WITH NON-UNIQUE KEY comp.");
buildExp("");
buildExp(" STATICS sv_any TYPE i.");
buildExp("");
buildExp(" \" comment on lv_any");
buildExp(" DATA lv_any TYPE i VALUE 1.");
buildExp("");
buildExp(" FIELD-SYMBOLS <ls_any> TYPE ty_s_data.");
buildExp("");
buildExp(" IF iv_case = 1.");
buildExp(" sv_any += lv_any.");
buildExp(" ENDIF.");
buildExp(" rv_result = sv_any + <ls_any>-value.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testFieldSymbolKeptAtMethodStart() {
// ensure that when ls_struc is moved the write position under the method start (which is then pointing to
// 'DATA ls_struc...') is correctly updated; otherwise, the FIELD-SYMBOL would erroneously be moved into the IF block
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
buildSrc(" DATA lts_data TYPE ty_tt_data.");
buildSrc(" DATA ls_struc TYPE ty_s_any.");
buildSrc("");
buildSrc(" FIELD-SYMBOLS <ls_data> LIKE LINE OF lts_data.");
buildSrc("");
buildSrc(" LOOP AT lts_data ASSIGNING <ls_data>.");
buildSrc(" ENDLOOP.");
buildSrc("");
buildSrc(" IF iv_case = 1.");
buildSrc(" CLEAR ls_struc.");
buildSrc(" ENDIF.");
buildExp(" DATA lts_data TYPE ty_tt_data.");
buildExp("");
buildExp(" FIELD-SYMBOLS <ls_data> LIKE LINE OF lts_data.");
buildExp("");
buildExp(" LOOP AT lts_data ASSIGNING <ls_data>.");
buildExp(" ENDLOOP.");
buildExp("");
buildExp(" IF iv_case = 1.");
buildExp(" DATA ls_struc TYPE ty_s_any.");
buildExp("");
buildExp(" CLEAR ls_struc.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testLineBreaksAfterParentKept() {
// ensure that existing line breaks at the beginning of an enclosing block are kept
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
buildSrc(" METHOD any_method.");
buildSrc("");
buildSrc("");
buildSrc(" DATA lv_any TYPE i.");
buildSrc(" DATA lv_other TYPE i.");
buildSrc("");
buildSrc(" CLEAR lv_other.");
buildSrc("");
buildSrc(" IF iv_case = 1");
buildSrc(" OR iv_case = 2.");
buildSrc("");
buildSrc("");
buildSrc(" CLEAR lv_any.");
buildSrc(" ENDIF.");
buildSrc(" ENDMETHOD.");
buildExp(" METHOD any_method.");
buildExp("");
buildExp("");
buildExp(" DATA lv_other TYPE i.");
buildExp("");
buildExp(" CLEAR lv_other.");
buildExp("");
buildExp(" IF iv_case = 1");
buildExp(" OR iv_case = 2.");
buildExp("");
buildExp("");
buildExp(" DATA lv_any TYPE i.");
buildExp("");
buildExp(" CLEAR lv_any.");
buildExp(" ENDIF.");
buildExp(" ENDMETHOD.");
testRule();
}
@Test
void testVariableUsedWithLikeIsKeptAtStart() {
// although it appears as if 'DATA lts_data' could be moved into the IF block, ensure that it is always kept it
// method start, because it is used in a 'LIKE' clause (which could be moved around, too)
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
buildSrc(" DATA lts_data TYPE ty_tt_data.");
buildSrc("");
buildSrc(" IF iv_case = 1.");
buildSrc(" FIELD-SYMBOLS <ls_data> LIKE LINE OF lts_data.");
buildSrc(" LOOP AT lts_data ASSIGNING <ls_data>.");
buildSrc(" ENDLOOP.");
buildSrc(" ENDIF.");
buildExp(" DATA lts_data TYPE ty_tt_data.");
buildExp("");
buildExp(" FIELD-SYMBOLS <ls_data> LIKE LINE OF lts_data.");
buildExp("");
buildExp(" IF iv_case = 1.");
buildExp(" LOOP AT lts_data ASSIGNING <ls_data>.");
buildExp(" ENDLOOP.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainKeptAtMethodStart() {
// ensure that a declaration chain is kept at method start, even if its first variable (or even all its variables)
// could move inside a block
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
buildSrc(" DATA: a TYPE i, \" comment");
buildSrc(" b TYPE i.");
buildSrc("");
buildSrc(" IF lv_case = 1.");
buildSrc(" a = 1.");
buildSrc(" b = a.");
buildSrc(" rv_result = b.");
buildSrc(" ENDIF.");
copyExpFromSrc();
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testWritePosAfterInitialMethodComment() {
// expect the initial comment to be considered as a comment for the whole method, meaning that declarations
// should be moved below it
buildSrc(" \" comment on whole method");
buildSrc(" CLEAR ev_any.");
buildSrc("");
buildSrc(" DATA b TYPE string VALUE `abc`.");
buildSrc(" DATA a TYPE string.");
buildSrc("");
buildSrc(" rv_result = a + b.");
buildExp(" \" comment on whole method");
buildExp("");
buildExp(" DATA a TYPE string.");
buildExp(" DATA b TYPE string VALUE `abc`.");
buildExp("");
buildExp(" CLEAR ev_any.");
buildExp("");
buildExp(" rv_result = a + b.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testWritePosAfterInitialMethodComments() {
// expect the initial comment to be considered as a comment for the whole method, but the comment just before
// the CLEAR command to be considered as a comment that belongs to the executable code; therefore, expect
// that declarations are inserted between those two types of comments
buildSrc(" \" comment on whole method");
buildSrc("");
buildSrc(" \" more comment on whole method");
buildSrc("");
buildSrc(" \" comment on CLEAR");
buildSrc(" CLEAR ev_any.");
buildSrc("");
buildSrc(" DATA b TYPE string VALUE `abc`.");
buildSrc(" DATA a TYPE string.");
buildSrc("");
buildSrc(" rv_result = a + b.");
buildExp(" \" comment on whole method");
buildExp("");
buildExp(" \" more comment on whole method");
buildExp("");
buildExp(" DATA a TYPE string.");
buildExp(" DATA b TYPE string VALUE `abc`.");
buildExp("");
buildExp(" \" comment on CLEAR");
buildExp(" CLEAR ev_any.");
buildExp("");
buildExp(" rv_result = a + b.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testWritePosRightAfterEnclosingCommand() {
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
buildSrc(" DATA b TYPE string VALUE `abc`.");
buildSrc(" DATA a TYPE string.");
buildSrc("");
buildSrc(" IF lv_calculate_result = abap_true.");
buildSrc(" \" explanation of IF block");
buildSrc(" a = 1.");
buildSrc(" b = a.");
buildSrc(" rv_result = b.");
buildSrc(" ENDIF.");
buildExp(" IF lv_calculate_result = abap_true.");
buildExp(" DATA a TYPE string.");
buildExp(" DATA b TYPE string VALUE `abc`.");
buildExp("");
buildExp(" \" explanation of IF block");
buildExp(" a = 1.");
buildExp(" b = a.");
buildExp(" rv_result = b.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testExtendSectionWithPragmaAfterPeriod() {
// ensure that pragmas are moved along with the declaration, even if they are in an incorrect position,
// after the period in the same line
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
buildSrc(" DATA b TYPE string VALUE `abc`. ##NO_TEXT");
buildSrc(" DATA a TYPE string. ##NEEDED");
buildSrc("");
buildSrc(" IF lv_calculate_result = abap_true.");
buildSrc(" \" explanation");
buildSrc(" a = 1.");
buildSrc(" b = a.");
buildSrc(" rv_result = b.");
buildSrc(" ENDIF.");
buildExp(" IF lv_calculate_result = abap_true.");
buildExp(" DATA a TYPE string. ##NEEDED");
buildExp(" DATA b TYPE string VALUE `abc`. ##NO_TEXT");
buildExp("");
buildExp(" \" explanation");
buildExp(" a = 1.");
buildExp(" b = a.");
buildExp(" rv_result = b.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testCommentsOnExecutableCommandsNotMoved() {
// ensure that comments that look like they describe following executable code are not moved,
// while comments that seem to describe the declarations are moved
// (for this, the rule analyzes whether there are empty lines or other comments before the next executable command)
buildSrc(" DATA lv_value TYPE i.");
buildSrc("");
buildSrc(" \" comment on first executable section");
buildSrc(" TYPES ty_any TYPE i.");
buildSrc(" DATA lv_data TYPE ty_any.");
buildSrc(" CLEAR lv_data.");
buildSrc(" do_something( lv_data ).");
buildSrc("");
buildSrc(" \" comment on other type");
buildSrc(" TYPES ty_other TYPE i.");
buildSrc(" \" comment on other data");
buildSrc(" DATA lv_other TYPE ty_other.");
buildSrc(" \" comment on second executable section");
buildSrc(" rv_result = lv_data + lv_value + lv_other.");
buildExp(" TYPES ty_any TYPE i.");
buildExp(" \" comment on other type");
buildExp(" TYPES ty_other TYPE i.");
buildExp("");
buildExp(" DATA lv_data TYPE ty_any.");
buildExp(" DATA lv_value TYPE i.");
buildExp(" \" comment on other data");
buildExp(" DATA lv_other TYPE ty_other.");
buildExp("");
buildExp(" \" comment on first executable section");
buildExp(" CLEAR lv_data.");
buildExp(" do_something( lv_data ).");
buildExp("");
buildExp(" \" comment on second executable section");
buildExp(" rv_result = lv_data + lv_value + lv_other.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testMovePragmaBeforeExistingDeclaration() {
rule.configDataOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
buildSrc(" DATA a TYPE string. ##NEEDED");
buildSrc("");
buildSrc(" IF lv_calculate_result = abap_true.");
buildSrc(" DATA b TYPE string VALUE `abc`. ##NO_TEXT");
buildSrc(" a = 1.");
buildSrc(" b = a.");
buildSrc(" rv_result = b.");
buildSrc(" ENDIF.");
buildExp(" IF lv_calculate_result = abap_true.");
buildExp(" DATA a TYPE string. ##NEEDED");
buildExp(" DATA b TYPE string VALUE `abc`. ##NO_TEXT");
buildExp("");
buildExp(" a = 1.");
buildExp(" b = a.");
buildExp(" rv_result = b.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainMovedForFirstVariable() {
// ensure that a declaration chain is moved depending on the usage position of its first variable ('b' in this case)
buildSrc(" DATA: b TYPE i,");
buildSrc(" d TYPE i.");
buildSrc(" DATA a TYPE i.");
buildSrc(" DATA c TYPE i.");
buildSrc("");
buildSrc(" a = 1.");
buildSrc(" b = 2.");
buildSrc(" c = 3.");
buildSrc(" d = 4.");
buildSrc(" rv_result = a + b + c + d.");
buildExp(" DATA a TYPE i.");
buildExp(" DATA: b TYPE i,");
buildExp(" d TYPE i.");
buildExp(" DATA c TYPE i.");
buildExp("");
buildExp(" a = 1.");
buildExp(" b = 2.");
buildExp(" c = 3.");
buildExp(" d = 4.");
buildExp(" rv_result = a + b + c + d.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testToDoCommentAttachedBeforeAndAfter() {
// ensure that a to-do comments that were generated by ABAP cleaner for a declaration
// is moved with the declaration, even if it is attached both to the declaration and the preceding parent Command
buildSrc(" LOOP AT it_data ASSIGNING FIELD-SYMBOL(<ls_data>).");
buildSrc(" \" TODO: variable is assigned but never used (ABAP cleaner)");
buildSrc(" DATA lv_value TYPE i.");
buildSrc(" \" TODO: constant is never used (ABAP cleaner)");
buildSrc(" CONSTANTS lc_count TYPE i VALUE 1.");
buildSrc(" CLEAR lv_value.");
buildSrc(" ENDLOOP.");
buildExp(" \" TODO: constant is never used (ABAP cleaner)");
buildExp(" CONSTANTS lc_count TYPE i VALUE 1.");
buildExp("");
buildExp(" \" TODO: variable is assigned but never used (ABAP cleaner)");
buildExp(" DATA lv_value TYPE i.");
buildExp("");
buildExp(" LOOP AT it_data ASSIGNING FIELD-SYMBOL(<ls_data>).");
buildExp(" CLEAR lv_value.");
buildExp(" ENDLOOP.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testIgnoreUsageInComment() {
// ensure that (as configured here), the usage of the field-symbol in the commented is NOT considered as its
// first usage, so the field-symbol can be moved inside the IF block
rule.configFieldSymbolsOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
rule.configConsiderComments.setValue(false);
buildSrc(" FIELD-SYMBOLS <ls_data_avg> TYPE ty_s_data.");
buildSrc("* UNASSIGN <ls_data_avg>.");
buildSrc("");
buildSrc(" IF iv_get_average = abap_true.");
buildSrc(" ls_data_avg-name = lc_name_average.");
buildSrc(" LOOP AT lts_data ASSIGNING <ls_data_avg>.");
buildSrc(" ls_data_avg-count += <ls_data_avg>-count.");
buildSrc(" ENDLOOP.");
buildSrc(" ENDIF.");
buildExp("* UNASSIGN <ls_data_avg>.");
buildExp("");
buildExp(" IF iv_get_average = abap_true.");
buildExp(" FIELD-SYMBOLS <ls_data_avg> TYPE ty_s_data.");
buildExp("");
buildExp(" ls_data_avg-name = lc_name_average.");
buildExp(" LOOP AT lts_data ASSIGNING <ls_data_avg>.");
buildExp(" ls_data_avg-count += <ls_data_avg>-count.");
buildExp(" ENDLOOP.");
buildExp(" ENDIF.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testConsiderUsageInComment() {
// ensure that (as configured here), the usage of the field-symbol in the commented IS considered as its
// first usage, so the field-symbol can NOT be moved inside the IF block
rule.configFieldSymbolsOrder.setEnumValue(LocalDeclarationOrder.ENCLOSING_BLOCK_CHANGE_ORDER);
rule.configConsiderComments.setValue(true);
buildSrc(" FIELD-SYMBOLS <ls_data_avg> TYPE ty_s_data.");
buildSrc("");
buildSrc("* UNASSIGN <ls_data_avg>.");
buildSrc("");
buildSrc(" IF iv_get_average = abap_true.");
buildSrc(" ls_data_avg-name = lc_name_average.");
buildSrc(" LOOP AT lts_data ASSIGNING <ls_data_avg>.");
buildSrc(" ls_data_avg-count += <ls_data_avg>-count.");
buildSrc(" ENDLOOP.");
buildSrc(" ENDIF.");
copyExpFromSrc();
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testMoveAttachedCommentsWithDeclarations() {
// ensure that the attached comments are moved along with the declarations
rule.configMoveComments.setValue(true);
buildSrc(" TYPES ty_s_data TYPE i.");
buildSrc(" \" structure for calculating the average count");
buildSrc(" DATA ls_data_avg TYPE ty_s_data.");
buildSrc("");
buildSrc(" \" structure for calculating the maximum count");
buildSrc(" DATA ls_data_max TYPE ty_s_data.");
buildSrc("");
buildSrc(" CLEAR ls_data_max.");
buildSrc(" CLEAR ls_data_avg.");
buildExp(" TYPES ty_s_data TYPE i.");
buildExp("");
buildExp(" \" structure for calculating the maximum count");
buildExp(" DATA ls_data_max TYPE ty_s_data.");
buildExp(" \" structure for calculating the average count");
buildExp(" DATA ls_data_avg TYPE ty_s_data.");
buildExp("");
buildExp(" CLEAR ls_data_max.");
buildExp(" CLEAR ls_data_avg.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testMoveCommentAttachedBeforeAndAfter() {
// ensure that the attached comments are moved, even though they are also attached above
rule.configMoveComments.setValue(true);
buildSrc(" TYPES ty TYPE i.");
buildSrc(" \" structure for calculating the average count");
buildSrc(" DATA ls_data_avg TYPE ty_s_data.");
buildSrc("");
buildSrc(" CLEAR lts_result.");
buildSrc(" \" structure for calculating the maximum count");
buildSrc(" DATA ls_data_max TYPE ty_s_data.");
buildSrc("");
buildSrc(" CLEAR ls_data_max.");
buildSrc(" CLEAR ls_data_avg.");
buildExp(" TYPES ty TYPE i.");
buildExp("");
buildExp(" \" structure for calculating the maximum count");
buildExp(" DATA ls_data_max TYPE ty_s_data.");
buildExp(" \" structure for calculating the average count");
buildExp(" DATA ls_data_avg TYPE ty_s_data.");
buildExp("");
buildExp(" CLEAR lts_result.");
buildExp("");
buildExp(" CLEAR ls_data_max.");
buildExp(" CLEAR ls_data_avg.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testKeepCommentsInPlace() {
// ensure that (as configured here), attached comments are NOT moved with the declarations
rule.configMoveComments.setValue(false);
buildSrc(" TYPES ty_s_data TYPE i.");
buildSrc(" \" structure for calculating the average count");
buildSrc(" DATA ls_data_avg TYPE ty_s_data.");
buildSrc("");
buildSrc(" \" structure for calculating the maximum count");
buildSrc(" DATA ls_data_max TYPE ty_s_data.");
buildSrc("");
buildSrc(" CLEAR ls_data_max.");
buildSrc(" CLEAR ls_data_avg.");
buildExp(" TYPES ty_s_data TYPE i.");
buildExp("");
buildExp(" DATA ls_data_max TYPE ty_s_data.");
buildExp(" DATA ls_data_avg TYPE ty_s_data.");
buildExp("");
buildExp(" \" structure for calculating the average count");
buildExp("");
buildExp(" \" structure for calculating the maximum count");
buildExp("");
buildExp(" CLEAR ls_data_max.");
buildExp(" CLEAR ls_data_avg.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testAbapDocCommentsMovedWithDeclarations() {
// ensure that despite configuration, ABAP Doc comments are ALWAYS moved with the declarations
rule.configMoveComments.setValue(false);
buildSrc(" TYPES ty_s_data TYPE i.");
buildSrc(" \"! structure for calculating the average count");
buildSrc(" DATA ls_data_avg TYPE ty_s_data.");
buildSrc("");
buildSrc(" \"! structure for calculating the maximum count");
buildSrc(" DATA ls_data_max TYPE ty_s_data.");
buildSrc("");
buildSrc(" CLEAR ls_data_max.");
buildSrc(" CLEAR ls_data_avg.");
buildExp(" TYPES ty_s_data TYPE i.");
buildExp("");
buildExp(" \"! structure for calculating the maximum count");
buildExp(" DATA ls_data_max TYPE ty_s_data.");
buildExp(" \"! structure for calculating the average count");
buildExp(" DATA ls_data_avg TYPE ty_s_data.");
buildExp("");
buildExp(" CLEAR ls_data_max.");
buildExp(" CLEAR ls_data_avg.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testSimpleChain() {
buildSrc(" DATA:");
buildSrc(" a2 TYPE string,");
buildSrc(" b3 TYPE i,");
buildSrc(" c1 TYPE ty_s_struc.");
buildSrc("");
buildSrc(" CLEAR: c1, a2, b3.");
buildExp(" DATA:");
buildExp(" c1 TYPE ty_s_struc,");
buildExp(" a2 TYPE string,");
buildExp(" b3 TYPE i.");
buildExp("");
buildExp(" CLEAR: c1, a2, b3.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testSimpleChainUnchanged() {
rule.configRearrangeChains.setValue(false);
buildSrc(" DATA:");
buildSrc(" a2 TYPE string,");
buildSrc(" b3 TYPE i,");
buildSrc(" c1 TYPE ty_s_struc.");
buildSrc("");
buildSrc(" CLEAR: c1, a2, b3.");
copyExpFromSrc();
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testSimpleChainOtherOrder() {
buildSrc(" DATA:");
buildSrc(" a3 TYPE string,");
buildSrc(" b1 TYPE i,");
buildSrc(" c2 TYPE ty_s_struc.");
buildSrc("");
buildSrc(" CLEAR: b1, c2, a3.");
buildExp(" DATA:");
buildExp(" b1 TYPE i,");
buildExp(" c2 TYPE ty_s_struc,");
buildExp(" a3 TYPE string.");
buildExp("");
buildExp(" CLEAR: b1, c2, a3.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainWithCommentsOnDeclarations() {
buildSrc(" DATA: a2 TYPE string, \" comment on a2");
buildSrc(" b3 TYPE i, \" comment on b3");
buildSrc("* two lines of comments");
buildSrc("* on c1");
buildSrc(" c1 TYPE ty_s_struc. \" another comment on c1");
buildSrc("");
buildSrc(" CLEAR: c1, a2, b3.");
buildExp(" DATA:");
buildExp("* two lines of comments");
buildExp("* on c1");
buildExp(" c1 TYPE ty_s_struc, \" another comment on c1");
buildExp(" a2 TYPE string, \" comment on a2");
buildExp(" b3 TYPE i. \" comment on b3");
buildExp("");
buildExp(" CLEAR: c1, a2, b3.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainWithCommentsOtherOrder() {
buildSrc(" DATA: a3 TYPE string, \" comment on a2");
buildSrc(" b1 TYPE i, \" comment on b3");
buildSrc("* two lines of comments");
buildSrc("* on c1");
buildSrc(" c2 TYPE ty_s_struc. \" another comment on c1");
buildSrc("");
buildSrc(" CLEAR: b1, c2, a3.");
buildExp(" DATA: b1 TYPE i, \" comment on b3");
buildExp("* two lines of comments");
buildExp("* on c1");
buildExp(" c2 TYPE ty_s_struc, \" another comment on c1");
buildExp(" a3 TYPE string. \" comment on a2");
buildExp("");
buildExp(" CLEAR: b1, c2, a3.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainWithCommentsThirdOrder() {
buildSrc(" DATA:");
buildSrc("* two lines of comments");
buildSrc("* on a3");
buildSrc(" a3 TYPE ty_s_struc, \" another comment on a3");
buildSrc(" b2 TYPE string, \" comment on b2");
buildSrc(" c1 TYPE i. \" comment on c1");
buildSrc("");
buildSrc(" CLEAR: c1, b2, a3.");
buildExp(" DATA:");
buildExp(" c1 TYPE i, \" comment on c1");
buildExp(" b2 TYPE string, \" comment on b2");
buildExp("* two lines of comments");
buildExp("* on a3");
buildExp(" a3 TYPE ty_s_struc. \" another comment on a3");
buildExp("");
buildExp(" CLEAR: c1, b2, a3.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainWithCommentOnWholeChain() {
buildSrc(" DATA: \" comment on the whole chain");
buildSrc(" \" comment on a2");
buildSrc(" a2 TYPE string, \" another comment on a2");
buildSrc(" b3 TYPE i, \" comment on b3");
buildSrc("* two lines of comments");
buildSrc("* on c1");
buildSrc(" c1 TYPE ty_s_struc. \" another comment on c1");
buildSrc("");
buildSrc(" CLEAR: c1, a2, b3.");
buildExp(" DATA: \" comment on the whole chain");
buildExp("* two lines of comments");
buildExp("* on c1");
buildExp(" c1 TYPE ty_s_struc, \" another comment on c1");
buildExp(" \" comment on a2");
buildExp(" a2 TYPE string, \" another comment on a2");
buildExp(" b3 TYPE i. \" comment on b3");
buildExp("");
buildExp(" CLEAR: c1, a2, b3.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainWithPragmasAndComments() {
// ensure that the pragmas stay with the declarations which they belong to,
// even if some of them are in an incorrect position
buildSrc(" DATA ##NEEDED: \" this pragma belongs to the whole chain!");
buildSrc(" a2 TYPE string VALUE `abc`, ##NO_TEXT \" wrong pragma position is tolerated");
buildSrc(" ##NEEDED b3 TYPE i, \" correct pragma position");
buildSrc(" c1 TYPE string VALUE `def`.");
buildSrc("");
buildSrc(" CLEAR: c1, a2, b3.");
buildExp(" DATA ##NEEDED: \" this pragma belongs to the whole chain!");
buildExp(" c1 TYPE string VALUE `def`,");
buildExp(" a2 TYPE string VALUE `abc`, ##NO_TEXT \" wrong pragma position is tolerated");
buildExp(" ##NEEDED b3 TYPE i. \" correct pragma position");
buildExp("");
buildExp(" CLEAR: c1, a2, b3.");
putAnyMethodAroundSrcAndExp();
testRule();
}
@Test
void testChainWithMultiLineElements() {