Skip to content

Commit

Permalink
implements #369
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasborin committed May 14, 2021
1 parent d4d37c2 commit f7a12e3
Show file tree
Hide file tree
Showing 3 changed files with 352 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/checks/y_check_cut_as_default.clas.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
CLASS y_check_cut_as_default DEFINITION PUBLIC INHERITING FROM y_check_base CREATE PUBLIC .
PUBLIC SECTION.
METHODS constructor.

PROTECTED SECTION.
METHODS inspect_tokens REDEFINITION.

PRIVATE SECTION.
METHODS is_for_testing IMPORTING method_name TYPE string
class_definition TYPE sstruc
RETURNING VALUE(result) TYPE abap_bool.

METHODS has_cut IMPORTING structure TYPE sstruc
RETURNING VALUE(result) TYPE abap_bool.

METHODS get_class_definition IMPORTING structure TYPE sstruc
RETURNING VALUE(result) TYPE sstruc
RAISING cx_sy_itab_line_not_found.

ENDCLASS.


CLASS y_check_cut_as_default IMPLEMENTATION.


METHOD constructor.
super->constructor( ).

settings-pseudo_comment = '"#EC CUT_AS_DEFAULT' ##NO_TEXT.
settings-disable_threshold_selection = abap_true.
settings-disable_on_testcode_selection = abap_true.
settings-disable_on_prodcode_selection = abap_true.
settings-apply_on_productive_code = abap_false.
settings-apply_on_test_code = abap_true.
settings-threshold = 0.
settings-documentation = |{ c_docs_path-checks }cut_as_default.md|.
relevant_statement_types = VALUE #( ( scan_struc_stmnt_type-method ) ).
relevant_structure_types = VALUE #( ).

set_check_message( 'Give the variable that represents the code under test the `cut` name' ).
ENDMETHOD.


METHOD inspect_tokens.
CHECK get_token_abs( statement-from ) = 'METHOD'.

DATA(class_definition) = get_class_definition( structure ).

DATA(for_testing) = is_for_testing( method_name = get_token_abs( statement-from + 1 )
class_definition = class_definition ).

IF for_testing = abap_false.
RETURN.
ENDIF.

IF has_cut( structure ) = abap_true
OR has_cut( class_definition ) = abap_true.
RETURN.
ENDIF.

DATA(check_configuration) = detect_check_configuration( statement ).

IF check_configuration IS INITIAL.
RETURN.
ENDIF.

raise_error( statement_level = statement-level
statement_index = index
statement_from = statement-from
error_priority = check_configuration-prio ).
ENDMETHOD.


METHOD is_for_testing.
LOOP AT ref_scan_manager->statements ASSIGNING FIELD-SYMBOL(<statement>)
FROM class_definition-stmnt_from TO class_definition-stmnt_to.

IF get_token_abs( <statement>-from ) <> 'METHODS'
AND get_token_abs( <statement>-from ) <> 'CLASS-METHODS'.
CONTINUE.
ENDIF.

DATA(statement_abs) = get_statement_abs( <statement> ).

IF statement_abs NP |*{ method_name }*|.
CONTINUE.
ENDIF.

result = xsdbool( statement_abs CP '*FOR TESTING*' ).
RETURN.

ENDLOOP.
ENDMETHOD.


METHOD has_cut.
LOOP AT ref_scan_manager->statements ASSIGNING FIELD-SYMBOL(<statement>)
FROM structure-stmnt_from TO structure-stmnt_to
WHERE type <> scan_stmnt_type-comment
AND type <> scan_stmnt_type-pragma.
DATA(statement_abs) = get_statement_abs( <statement> ).

result = xsdbool( statement_abs CP '* CUT *'
OR statement_abs CP '*_CUT *' ).

IF result = abap_true.
RETURN.
ENDIF.
ENDLOOP.
ENDMETHOD.


METHOD get_class_definition.
DATA(class_implementation) = ref_scan_manager->structures[ structure-back ].
result = ref_scan_manager->structures[ class_implementation-back ].
ENDMETHOD.


ENDCLASS.
216 changes: 216 additions & 0 deletions src/checks/y_check_cut_as_default.clas.testclasses.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
CLASS ltc_cut DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_cut REDEFINITION.
METHODS get_code_with_issue REDEFINITION.
METHODS get_code_without_issue REDEFINITION.
METHODS get_code_with_exemption REDEFINITION.
ENDCLASS.

CLASS ltc_cut IMPLEMENTATION.

METHOD get_cut.
result ?= NEW y_check_cut_as_default( ).
ENDMETHOD.

METHOD get_code_with_issue.
result = VALUE #(
( ' REPORT y_example. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' " given ' )
( ' DATA demo_failures TYPE REF TO y_demo_failures. ' )
( ' " when ' )
( ' demo_failures = NEW #( ). ' )
( ' " then ' )
( ' cl_abap_unit_assert=>assert_bound( demo_failures ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

METHOD get_code_without_issue.
result = VALUE #(
( ' REPORT y_example. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' " given ' )
( ' DATA cut TYPE REF TO y_demo_failures. ' )
( ' " when ' )
( ' cut = NEW #( ). ' )
( ' " then ' )
( ' cl_abap_unit_assert=>assert_bound( cut ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

METHOD get_code_with_exemption.
result = VALUE #(
( ' REPORT y_example. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. "#EC CUT_AS_DEFAULT ' )
( ' " given ' )
( ' DATA demo_failures TYPE REF TO y_demo_failures. ' )
( ' " when ' )
( ' demo_failures = NEW #( ). ' )
( ' " then ' )
( ' cl_abap_unit_assert=>assert_bound( demo_failures ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.



CLASS ltc_prefix DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_code_without_issue REDEFINITION.
ENDCLASS.

CLASS ltc_prefix IMPLEMENTATION.

METHOD get_code_without_issue.
result = VALUE #(
( ' REPORT y_example. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' " given ' )
( ' DATA lo_cut TYPE REF TO y_demo_failures. ' )
( ' " when ' )
( ' lo_cut = NEW #( ). ' )
( ' " then ' )
( ' cl_abap_unit_assert=>assert_bound( lo_cut ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.



CLASS ltc_not_for_testing DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_code_without_issue REDEFINITION.
ENDCLASS.

CLASS ltc_not_for_testing IMPLEMENTATION.

METHOD get_code_without_issue.
result = VALUE #(
( ' REPORT y_example. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.



CLASS ltc_attribute DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_code_without_issue REDEFINITION.
ENDCLASS.

CLASS ltc_attribute IMPLEMENTATION.

METHOD get_code_without_issue.
result = VALUE #(
( ' REPORT y_example. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' PRIVATE SECTION. ' )
( ' DATA lo_cut TYPE REF TO y_demo_failures. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' " when ' )
( ' lo_cut = NEW #( ). ' )
( ' " then ' )
( ' cl_abap_unit_assert=>assert_bound( lo_cut ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.



CLASS ltc_cut_out_of_testing_method DEFINITION INHERITING FROM ltc_cut FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_code_without_issue REDEFINITION.
ENDCLASS.

CLASS ltc_cut_out_of_testing_method IMPLEMENTATION.

METHOD get_code_without_issue.
result = VALUE #(
( ' REPORT y_example. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' PROTECTED SECTION. ' )
( ' METHODS when. ' )
( ' METHODS then. ' )
( ' PRIVATE SECTION. ' )
( ' DATA cut TYPE REF TO y_demo_failures. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' when( ).' )
( ' then( ).' )
( ' ENDMETHOD. ' )

( ' METHOD when. ' )
( ' cut = NEW #( ). ' )
( ' ENDMETHOD. ' )

( ' METHOD then. ' )
( ' cl_abap_unit_assert=>assert_bound( cut ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.
17 changes: 17 additions & 0 deletions src/checks/y_check_cut_as_default.clas.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOCLASS>
<CLSNAME>Y_CHECK_CUT_AS_DEFAULT</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>Name the Class Under Test to CUT</DESCRIPT>
<STATE>1</STATE>
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
</VSEOCLASS>
</asx:values>
</asx:abap>
</abapGit>

0 comments on commit f7a12e3

Please sign in to comment.