Skip to content
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
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Upgrade Note
------------------
Whenever you upgrade code pal for ABAP, it is highly recommended to execute the Y_CI_CHECK_REGISTRATION report to activate/reactivate the Checks (SE38 transaction) and regenerate the respective Code Inspector Variant (SCI transaction)

2021-05-03 v.1.14.1
------------------
+ Assert Classes (#378)
* Unit Test Assertion (#383)

2021-04-23 v.1.14.0
------------------
! Notification as Default Severity (#368)
Expand Down
100 changes: 72 additions & 28 deletions src/checks/y_check_unit_test_assert.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ CLASS y_check_unit_test_assert DEFINITION PUBLIC INHERITING FROM y_check_base CR
METHODS inspect_tokens REDEFINITION.

PRIVATE SECTION.
METHODS get_act_and_exp IMPORTING statement TYPE sstmnt
EXPORTING act TYPE stokesx
exp TYPE stokesx.
METHODS get_parameter_reference IMPORTING statement TYPE sstmnt
parameter TYPE string
RETURNING VALUE(result) TYPE string
RAISING cx_sy_itab_line_not_found.

METHODS is_variable IMPORTING token TYPE stokesx
RETURNING VALUE(result) TYPE abap_bool.
Expand Down Expand Up @@ -37,20 +38,23 @@ CLASS y_check_unit_test_assert IMPLEMENTATION.


METHOD inspect_tokens.
CHECK get_token_abs( statement-from ) CP 'CL_ABAP_UNIT_ASSERT=>ASSERT*'
OR get_token_abs( statement-from ) CP 'CL_AUNIT_ASSERT=>ASSERT*'.
DATA(token) = ref_scan_manager->tokens[ statement-from ].

get_act_and_exp( EXPORTING statement = statement
IMPORTING act = DATA(act)
exp = DATA(exp) ).

IF act IS INITIAL
OR exp IS INITIAL.
IF token-str NP '*ASSERT*'
OR token-type = scan_token_type-comment.
RETURN.
ENDIF.

IF act-str <> exp-str
AND ( is_variable( act ) = abap_true OR is_variable( exp ) = abap_true ).
TRY.
DATA(act) = get_parameter_reference( statement = statement
parameter = 'ACT' ).
DATA(exp) = get_parameter_reference( statement = statement
parameter = 'EXP' ).
CATCH cx_sy_itab_line_not_found.
RETURN.
ENDTRY.

IF act <> exp.
RETURN.
ENDIF.

Expand All @@ -67,25 +71,65 @@ CLASS y_check_unit_test_assert IMPLEMENTATION.
ENDMETHOD.


METHOD get_act_and_exp.
LOOP AT ref_scan_manager->tokens ASSIGNING FIELD-SYMBOL(<token>)
FROM statement-from TO statement-to.
DATA(tabix) = sy-tabix.
CASE <token>-str.
WHEN 'ACT'.
act = ref_scan_manager->tokens[ tabix + 2 ].
WHEN 'EXP'.
exp = ref_scan_manager->tokens[ tabix + 2 ].
WHEN OTHERS.
CONTINUE.
ENDCASE.
ENDLOOP.
METHOD get_parameter_reference.
DATA(in) = abap_false.
DATA(depth) = 0.
DATA(position) = statement-from.

DO.
IF position = statement-to.
RETURN.
ENDIF.

DATA(token) = ref_scan_manager->tokens[ position ].

IF token-type = scan_token_type-comment.
position = position + 1.
CONTINUE.
ENDIF.

IF token-str = parameter.
in = abap_true.
depth = 0.
position = position + 2.
CONTINUE.
ENDIF.

IF in = abap_false.
position = position + 1.
CONTINUE.
ENDIF.

IF token-str CP '*(*'.
depth = depth + 1.
ELSEIF token-str CP '*)*'.
depth = depth - 1.
ENDIF.

IF depth = 0
AND line_exists( ref_scan_manager->tokens[ position + 1 ] ).
DATA(next) = ref_scan_manager->tokens[ position + 1 ].
IF next-str = '='.
in = abap_false.
RETURN.
ENDIF.
ENDIF.

IF is_variable( token ) = abap_false.
token-str = '*'.
ENDIF.

result = COND #( WHEN result IS INITIAL THEN token-str
ELSE |{ result } { token-str }| ).

position = position + 1.
ENDDO.
ENDMETHOD.


METHOD is_variable.
result = COND #( WHEN token-type = scan_token_type-literal THEN abap_false
WHEN token-type = scan_token_type-identifier THEN xsdbool( token-str CN '0123456789' ) ).
CHECK token-type = scan_token_type-identifier.
result = xsdbool( token-str CN '0123456789' ).
ENDMETHOD.


Expand Down
227 changes: 227 additions & 0 deletions src/checks/y_check_unit_test_assert.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,230 @@ CLASS ltc_protected_data IMPLEMENTATION.
ENDMETHOD.

ENDCLASS.



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

CLASS ltc_comments 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. ' )
( ' DATA system TYPE string. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( |* cl_aunit_assert=>assert_differs( act = system | )
( |* exp = system ). | )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.



CLASS ltc_non_standard_assert DEFINITION INHERITING FROM ltc_same_variable 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_non_standard_assert IMPLEMENTATION.

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

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

( ' CLASS y_assert DEFINITION. ' )
( ' PUBLIC SECTION. ' )
( ' CLASS-METHODS assert IMPORTING VALUE(exp) TYPE any ' )
( ' VALUE(act) TYPE any ' )
( ' RETURNING VALUE(result) TYPE abap_bool. ' )
( ' ENDCLASS. ' )

( ' CLASS y_assert IMPLEMENTATION. ' )
( ' METHOD assert. ' )
( ' result = cl_abap_unit_assert=>assert_equals( exp = exp ' )
( ' act = act ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' PROTECTED SECTION. ' )
( ' DATA system TYPE string. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' y_assert=>assert( act = system ' )
( ' exp = system ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

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

( ' CLASS y_assert DEFINITION. ' )
( ' PUBLIC SECTION. ' )
( ' CLASS-METHODS assert IMPORTING VALUE(exp) TYPE any ' )
( ' VALUE(act) TYPE any ' )
( ' RETURNING VALUE(result) TYPE abap_bool. ' )
( ' ENDCLASS. ' )

( ' CLASS y_assert IMPLEMENTATION. ' )
( ' METHOD assert. ' )
( ' result = cl_abap_unit_assert=>assert_equals( exp = exp ' )
( ' act = act ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' PROTECTED SECTION. ' )
( ' DATA system TYPE string. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' y_assert=>assert( act = sy-sysid ' )
( ' exp = system ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

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

( ' CLASS y_assert DEFINITION. ' )
( ' PUBLIC SECTION. ' )
( ' CLASS-METHODS assert IMPORTING VALUE(exp) TYPE any ' )
( ' VALUE(act) TYPE any ' )
( ' RETURNING VALUE(result) TYPE abap_bool. ' )
( ' ENDCLASS. ' )

( ' CLASS y_assert IMPLEMENTATION. ' )
( ' METHOD assert. ' )
( ' result = cl_abap_unit_assert=>assert_equals( exp = exp ' )
( ' act = act ). ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS example FOR TESTING. ' )
( ' PROTECTED SECTION. ' )
( ' DATA system TYPE string. ' )
( ' ENDCLASS. ' )

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( ' y_assert=>assert( act = system ' )
( ' exp = system ). "#EC UT_ASSERT ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.


ENDCLASS.



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

CLASS ltc_lines 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. ' )
( ' DATA atc_tadir TYPE TABLE OF tadir. ' )
( ' DATA exp_tadir TYPE TABLE OF tadir. ' )
( | cl_aunit_assert=>assert_equals( act = lines( atc_tadir ) | )
( | exp = lines( exp_tadir ) ). | )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.



CLASS ltc_call_static DEFINITION INHERITING FROM ltc_hardcoded_string FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_code_with_issue REDEFINITION.
ENDCLASS.

CLASS ltc_call_static IMPLEMENTATION.

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

( ' CLASS y_fake DEFINITION. ' )
( ' PUBLIC SECTION. ' )
( ' CLASS-METHODS get_fullname IMPORTING name TYPE string ' )
( ' surname TYPE string ' )
( ' RETURNING VALUE(result) TYPE string. ' )
( ' ENDCLASS. ' )

( ' CLASS y_fake IMPLEMENTATION. ' )
( ' METHOD get_fullname. ' )
( ' result = |{ name } { surname }|. ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )


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

( ' CLASS y_example IMPLEMENTATION. ' )
( ' METHOD example. ' )
( | cl_aunit_assert=>assert_equals( act = y_fake=>get_fullname( name = 'code pal' surname = 'for ABAP' ) | )
( | exp = y_fake=>get_fullname( name = 'code pal' | )
( | surname = 'for ABAP' ) ). | )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
).
ENDMETHOD.

ENDCLASS.
2 changes: 1 addition & 1 deletion src/y_code_pal_version.intf.abap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
INTERFACE y_code_pal_version PUBLIC.
CONSTANTS abap TYPE string VALUE '1.14.0' ##NO_TEXT.
CONSTANTS abap TYPE string VALUE '1.14.1' ##NO_TEXT.
ENDINTERFACE.