Skip to content

Empty Catch: Test Double Framework #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Legend

2021-08-XX v.1.16.0
------------------
* Empty Catch: Test Double Framework (#483)
* Y_CHECK_FORM: Screen Events (#454)
* Magic Number: CASE SY-TABIX and CO NUMBERS (#480)
* Magic Number: Leading Zeros (#479)
Expand Down
46 changes: 29 additions & 17 deletions src/checks/y_check_empty_catches.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ CLASS y_check_empty_catches DEFINITION PUBLIC INHERITING FROM y_check_base CREAT
METHODS constructor .

PROTECTED SECTION.
METHODS inspect_tokens REDEFINITION .
METHODS inspect_tokens REDEFINITION.
METHODS get_token_abs REDEFINITION.

PRIVATE SECTION.
METHODS get_next_token_from_index IMPORTING index TYPE i
RETURNING VALUE(result) TYPE stokesx.
METHODS is_test_double_framework RETURNING VALUE(result) TYPE abap_bool.

ENDCLASS.



CLASS Y_CHECK_EMPTY_CATCHES IMPLEMENTATION.
CLASS y_check_empty_catches IMPLEMENTATION.


METHOD constructor.
Expand All @@ -28,21 +28,13 @@ CLASS Y_CHECK_EMPTY_CATCHES IMPLEMENTATION.
ENDMETHOD.


METHOD get_next_token_from_index.
LOOP AT ref_scan->tokens ASSIGNING FIELD-SYMBOL(<token>)
FROM index WHERE type = 'I'.
IF result IS INITIAL.
result = <token>.
EXIT.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD inspect_tokens.
CHECK keyword( ) = 'CATCH'.

CHECK get_token_abs( statement-to + 1 ) = if_kaizen_keywords_c=>gc_endtry
OR get_token_abs( statement-to + 1 ) = 'ENDCATCH'.

METHOD inspect_tokens.
CHECK get_next_token_from_index( statement-from )-str = 'CATCH'
AND ( get_next_token_from_index( statement-to + 1 )-str = 'ENDTRY'
OR get_next_token_from_index( statement-to + 1 )-str = 'ENDCATCH' ).
CHECK is_test_double_framework( ) = abap_false.

DATA(check_configuration) = detect_check_configuration( statement ).

Expand All @@ -51,4 +43,24 @@ CLASS Y_CHECK_EMPTY_CATCHES IMPLEMENTATION.
statement_from = statement-from
check_configuration = check_configuration ).
ENDMETHOD.


METHOD get_token_abs.
p_result = super->get_token_abs( p_n ).
if token_wa-type = scan_token_type-comment.
p_result = get_token_abs( p_n + 1 ).
endif.
ENDMETHOD.


METHOD is_test_double_framework.
DATA(catch_structure) = ref_scan->structures[ statement_wa-struc ].
DATA(before_try_structure) = ref_scan->structures[ catch_structure-back - 1 ].
DATA(search_from_token) = ref_scan->statements[ before_try_structure-stmnt_from ]-from.
DATA(search_to_token) = ref_scan->statements[ before_try_structure-stmnt_to ]-to.
DATA(range_tokens) = VALUE stokesx_tab( FOR token IN ref_scan->tokens FROM search_from_token TO search_to_token ( token ) ).
result = xsdbool( line_exists( range_tokens[ str = 'CL_ABAP_TESTDOUBLE=>CONFIGURE_CALL(' ] ) ).
ENDMETHOD.


ENDCLASS.
35 changes: 35 additions & 0 deletions src/checks/y_check_empty_catches.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ CLASS ltc_class_based IMPLEMENTATION.

ENDCLASS.



CLASS ltc_system_based DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_cut REDEFINITION.
Expand Down Expand Up @@ -144,3 +146,36 @@ CLASS ltc_system_based IMPLEMENTATION.
ENDMETHOD.

ENDCLASS.



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

CLASS ltc_test_double_framework IMPLEMENTATION.

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

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

( ' CLASS classname IMPLEMENTATION. ' )
( ' METHOD framwork. ' )
( ' DATA cut TYPE REF TO y_check_empty_catches. ' )
( ' cl_abap_testdouble=>configure_call( cut )->returning( 55 ). ' )
( ' TRY. ' )
( ' cut->get_attributes( ). ' )
( ' CATCH cx_sy_no_handler. ' )
( ' ENDTRY. ' )
( ' ENDMETHOD. ' )
( 'ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.