Skip to content

Commit f7a12e3

Browse files
committed
implements #369
1 parent d4d37c2 commit f7a12e3

3 files changed

+352
-0
lines changed
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
CLASS y_check_cut_as_default DEFINITION PUBLIC INHERITING FROM y_check_base CREATE PUBLIC .
2+
PUBLIC SECTION.
3+
METHODS constructor.
4+
5+
PROTECTED SECTION.
6+
METHODS inspect_tokens REDEFINITION.
7+
8+
PRIVATE SECTION.
9+
METHODS is_for_testing IMPORTING method_name TYPE string
10+
class_definition TYPE sstruc
11+
RETURNING VALUE(result) TYPE abap_bool.
12+
13+
METHODS has_cut IMPORTING structure TYPE sstruc
14+
RETURNING VALUE(result) TYPE abap_bool.
15+
16+
METHODS get_class_definition IMPORTING structure TYPE sstruc
17+
RETURNING VALUE(result) TYPE sstruc
18+
RAISING cx_sy_itab_line_not_found.
19+
20+
ENDCLASS.
21+
22+
23+
CLASS y_check_cut_as_default IMPLEMENTATION.
24+
25+
26+
METHOD constructor.
27+
super->constructor( ).
28+
29+
settings-pseudo_comment = '"#EC CUT_AS_DEFAULT' ##NO_TEXT.
30+
settings-disable_threshold_selection = abap_true.
31+
settings-disable_on_testcode_selection = abap_true.
32+
settings-disable_on_prodcode_selection = abap_true.
33+
settings-apply_on_productive_code = abap_false.
34+
settings-apply_on_test_code = abap_true.
35+
settings-threshold = 0.
36+
settings-documentation = |{ c_docs_path-checks }cut_as_default.md|.
37+
relevant_statement_types = VALUE #( ( scan_struc_stmnt_type-method ) ).
38+
relevant_structure_types = VALUE #( ).
39+
40+
set_check_message( 'Give the variable that represents the code under test the `cut` name' ).
41+
ENDMETHOD.
42+
43+
44+
METHOD inspect_tokens.
45+
CHECK get_token_abs( statement-from ) = 'METHOD'.
46+
47+
DATA(class_definition) = get_class_definition( structure ).
48+
49+
DATA(for_testing) = is_for_testing( method_name = get_token_abs( statement-from + 1 )
50+
class_definition = class_definition ).
51+
52+
IF for_testing = abap_false.
53+
RETURN.
54+
ENDIF.
55+
56+
IF has_cut( structure ) = abap_true
57+
OR has_cut( class_definition ) = abap_true.
58+
RETURN.
59+
ENDIF.
60+
61+
DATA(check_configuration) = detect_check_configuration( statement ).
62+
63+
IF check_configuration IS INITIAL.
64+
RETURN.
65+
ENDIF.
66+
67+
raise_error( statement_level = statement-level
68+
statement_index = index
69+
statement_from = statement-from
70+
error_priority = check_configuration-prio ).
71+
ENDMETHOD.
72+
73+
74+
METHOD is_for_testing.
75+
LOOP AT ref_scan_manager->statements ASSIGNING FIELD-SYMBOL(<statement>)
76+
FROM class_definition-stmnt_from TO class_definition-stmnt_to.
77+
78+
IF get_token_abs( <statement>-from ) <> 'METHODS'
79+
AND get_token_abs( <statement>-from ) <> 'CLASS-METHODS'.
80+
CONTINUE.
81+
ENDIF.
82+
83+
DATA(statement_abs) = get_statement_abs( <statement> ).
84+
85+
IF statement_abs NP |*{ method_name }*|.
86+
CONTINUE.
87+
ENDIF.
88+
89+
result = xsdbool( statement_abs CP '*FOR TESTING*' ).
90+
RETURN.
91+
92+
ENDLOOP.
93+
ENDMETHOD.
94+
95+
96+
METHOD has_cut.
97+
LOOP AT ref_scan_manager->statements ASSIGNING FIELD-SYMBOL(<statement>)
98+
FROM structure-stmnt_from TO structure-stmnt_to
99+
WHERE type <> scan_stmnt_type-comment
100+
AND type <> scan_stmnt_type-pragma.
101+
DATA(statement_abs) = get_statement_abs( <statement> ).
102+
103+
result = xsdbool( statement_abs CP '* CUT *'
104+
OR statement_abs CP '*_CUT *' ).
105+
106+
IF result = abap_true.
107+
RETURN.
108+
ENDIF.
109+
ENDLOOP.
110+
ENDMETHOD.
111+
112+
113+
METHOD get_class_definition.
114+
DATA(class_implementation) = ref_scan_manager->structures[ structure-back ].
115+
result = ref_scan_manager->structures[ class_implementation-back ].
116+
ENDMETHOD.
117+
118+
119+
ENDCLASS.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
CLASS ltc_cut DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
2+
PROTECTED SECTION.
3+
METHODS get_cut REDEFINITION.
4+
METHODS get_code_with_issue REDEFINITION.
5+
METHODS get_code_without_issue REDEFINITION.
6+
METHODS get_code_with_exemption REDEFINITION.
7+
ENDCLASS.
8+
9+
CLASS ltc_cut IMPLEMENTATION.
10+
11+
METHOD get_cut.
12+
result ?= NEW y_check_cut_as_default( ).
13+
ENDMETHOD.
14+
15+
METHOD get_code_with_issue.
16+
result = VALUE #(
17+
( ' REPORT y_example. ' )
18+
19+
( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
20+
( ' PUBLIC SECTION. ' )
21+
( ' METHODS example FOR TESTING. ' )
22+
( ' ENDCLASS. ' )
23+
24+
( ' CLASS y_example IMPLEMENTATION. ' )
25+
( ' METHOD example. ' )
26+
( ' " given ' )
27+
( ' DATA demo_failures TYPE REF TO y_demo_failures. ' )
28+
( ' " when ' )
29+
( ' demo_failures = NEW #( ). ' )
30+
( ' " then ' )
31+
( ' cl_abap_unit_assert=>assert_bound( demo_failures ). ' )
32+
( ' ENDMETHOD. ' )
33+
( ' ENDCLASS. ' )
34+
).
35+
ENDMETHOD.
36+
37+
METHOD get_code_without_issue.
38+
result = VALUE #(
39+
( ' REPORT y_example. ' )
40+
41+
( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
42+
( ' PUBLIC SECTION. ' )
43+
( ' METHODS example FOR TESTING. ' )
44+
( ' ENDCLASS. ' )
45+
46+
( ' CLASS y_example IMPLEMENTATION. ' )
47+
( ' METHOD example. ' )
48+
( ' " given ' )
49+
( ' DATA cut TYPE REF TO y_demo_failures. ' )
50+
( ' " when ' )
51+
( ' cut = NEW #( ). ' )
52+
( ' " then ' )
53+
( ' cl_abap_unit_assert=>assert_bound( cut ). ' )
54+
( ' ENDMETHOD. ' )
55+
( ' ENDCLASS. ' )
56+
).
57+
ENDMETHOD.
58+
59+
METHOD get_code_with_exemption.
60+
result = VALUE #(
61+
( ' REPORT y_example. ' )
62+
63+
( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
64+
( ' PUBLIC SECTION. ' )
65+
( ' METHODS example FOR TESTING. ' )
66+
( ' ENDCLASS. ' )
67+
68+
( ' CLASS y_example IMPLEMENTATION. ' )
69+
( ' METHOD example. "#EC CUT_AS_DEFAULT ' )
70+
( ' " given ' )
71+
( ' DATA demo_failures TYPE REF TO y_demo_failures. ' )
72+
( ' " when ' )
73+
( ' demo_failures = NEW #( ). ' )
74+
( ' " then ' )
75+
( ' cl_abap_unit_assert=>assert_bound( demo_failures ). ' )
76+
( ' ENDMETHOD. ' )
77+
( ' ENDCLASS. ' )
78+
).
79+
ENDMETHOD.
80+
81+
ENDCLASS.
82+
83+
84+
85+
CLASS ltc_prefix DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
86+
PROTECTED SECTION.
87+
METHODS get_code_without_issue REDEFINITION.
88+
ENDCLASS.
89+
90+
CLASS ltc_prefix IMPLEMENTATION.
91+
92+
METHOD get_code_without_issue.
93+
result = VALUE #(
94+
( ' REPORT y_example. ' )
95+
96+
( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
97+
( ' PUBLIC SECTION. ' )
98+
( ' METHODS example FOR TESTING. ' )
99+
( ' ENDCLASS. ' )
100+
101+
( ' CLASS y_example IMPLEMENTATION. ' )
102+
( ' METHOD example. ' )
103+
( ' " given ' )
104+
( ' DATA lo_cut TYPE REF TO y_demo_failures. ' )
105+
( ' " when ' )
106+
( ' lo_cut = NEW #( ). ' )
107+
( ' " then ' )
108+
( ' cl_abap_unit_assert=>assert_bound( lo_cut ). ' )
109+
( ' ENDMETHOD. ' )
110+
( ' ENDCLASS. ' )
111+
).
112+
ENDMETHOD.
113+
114+
ENDCLASS.
115+
116+
117+
118+
CLASS ltc_not_for_testing DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
119+
PROTECTED SECTION.
120+
METHODS get_code_without_issue REDEFINITION.
121+
ENDCLASS.
122+
123+
CLASS ltc_not_for_testing IMPLEMENTATION.
124+
125+
METHOD get_code_without_issue.
126+
result = VALUE #(
127+
( ' REPORT y_example. ' )
128+
129+
( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
130+
( ' PUBLIC SECTION. ' )
131+
( ' METHODS example. ' )
132+
( ' ENDCLASS. ' )
133+
134+
( ' CLASS y_example IMPLEMENTATION. ' )
135+
( ' METHOD example. ' )
136+
( ' ENDMETHOD. ' )
137+
( ' ENDCLASS. ' )
138+
).
139+
ENDMETHOD.
140+
141+
ENDCLASS.
142+
143+
144+
145+
CLASS ltc_attribute DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
146+
PROTECTED SECTION.
147+
METHODS get_code_without_issue REDEFINITION.
148+
ENDCLASS.
149+
150+
CLASS ltc_attribute IMPLEMENTATION.
151+
152+
METHOD get_code_without_issue.
153+
result = VALUE #(
154+
( ' REPORT y_example. ' )
155+
156+
( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
157+
( ' PUBLIC SECTION. ' )
158+
( ' METHODS example FOR TESTING. ' )
159+
( ' PRIVATE SECTION. ' )
160+
( ' DATA lo_cut TYPE REF TO y_demo_failures. ' )
161+
( ' ENDCLASS. ' )
162+
163+
( ' CLASS y_example IMPLEMENTATION. ' )
164+
( ' METHOD example. ' )
165+
( ' " when ' )
166+
( ' lo_cut = NEW #( ). ' )
167+
( ' " then ' )
168+
( ' cl_abap_unit_assert=>assert_bound( lo_cut ). ' )
169+
( ' ENDMETHOD. ' )
170+
( ' ENDCLASS. ' )
171+
).
172+
ENDMETHOD.
173+
174+
ENDCLASS.
175+
176+
177+
178+
CLASS ltc_cut_out_of_testing_method DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
179+
PROTECTED SECTION.
180+
METHODS get_code_without_issue REDEFINITION.
181+
ENDCLASS.
182+
183+
CLASS ltc_cut_out_of_testing_method IMPLEMENTATION.
184+
185+
METHOD get_code_without_issue.
186+
result = VALUE #(
187+
( ' REPORT y_example. ' )
188+
189+
( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
190+
( ' PUBLIC SECTION. ' )
191+
( ' METHODS example FOR TESTING. ' )
192+
( ' PROTECTED SECTION. ' )
193+
( ' METHODS when. ' )
194+
( ' METHODS then. ' )
195+
( ' PRIVATE SECTION. ' )
196+
( ' DATA cut TYPE REF TO y_demo_failures. ' )
197+
( ' ENDCLASS. ' )
198+
199+
( ' CLASS y_example IMPLEMENTATION. ' )
200+
( ' METHOD example. ' )
201+
( ' when( ).' )
202+
( ' then( ).' )
203+
( ' ENDMETHOD. ' )
204+
205+
( ' METHOD when. ' )
206+
( ' cut = NEW #( ). ' )
207+
( ' ENDMETHOD. ' )
208+
209+
( ' METHOD then. ' )
210+
( ' cl_abap_unit_assert=>assert_bound( cut ). ' )
211+
( ' ENDMETHOD. ' )
212+
( ' ENDCLASS. ' )
213+
).
214+
ENDMETHOD.
215+
216+
ENDCLASS.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<VSEOCLASS>
6+
<CLSNAME>Y_CHECK_CUT_AS_DEFAULT</CLSNAME>
7+
<LANGU>E</LANGU>
8+
<DESCRIPT>Name the Class Under Test to CUT</DESCRIPT>
9+
<STATE>1</STATE>
10+
<CLSCCINCL>X</CLSCCINCL>
11+
<FIXPT>X</FIXPT>
12+
<UNICODE>X</UNICODE>
13+
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
14+
</VSEOCLASS>
15+
</asx:values>
16+
</asx:abap>
17+
</abapGit>

0 commit comments

Comments
 (0)