From 63820c3e49c7d4d5dfb3de183c6096cedb7d780b Mon Sep 17 00:00:00 2001 From: Mike Pokraka Date: Thu, 16 Sep 2021 15:09:42 +0000 Subject: [PATCH 1/7] Functional operand check --- src/checks/y_check_unit_test_assert.clas.abap | 19 +++- ...eck_unit_test_assert.clas.testclasses.abap | 101 ++++++++++++++++++ 2 files changed, 115 insertions(+), 5 deletions(-) diff --git a/src/checks/y_check_unit_test_assert.clas.abap b/src/checks/y_check_unit_test_assert.clas.abap index 96aa8951..4c0edb61 100644 --- a/src/checks/y_check_unit_test_assert.clas.abap +++ b/src/checks/y_check_unit_test_assert.clas.abap @@ -6,14 +6,17 @@ CLASS y_check_unit_test_assert DEFINITION PUBLIC INHERITING FROM y_check_base CR METHODS inspect_tokens REDEFINITION. PRIVATE SECTION. - METHODS get_parameter_reference IMPORTING statement TYPE sstmnt - parameter TYPE string + METHODS get_parameter_reference IMPORTING statement TYPE sstmnt + parameter TYPE string RETURNING VALUE(result) TYPE string - RAISING cx_sy_itab_line_not_found. + RAISING cx_sy_itab_line_not_found. - METHODS is_variable IMPORTING token TYPE stokesx + METHODS is_variable IMPORTING token TYPE stokesx RETURNING VALUE(result) TYPE abap_bool. + METHODS contains_functional_operand IMPORTING expression TYPE string + RETURNING VALUE(result) TYPE abap_bool. + ENDCLASS. @@ -61,7 +64,7 @@ CLASS y_check_unit_test_assert IMPLEMENTATION. RETURN. ENDIF. - IF act <> exp. + IF act <> exp OR contains_functional_operand( act ). RETURN. ENDIF. @@ -144,4 +147,10 @@ CLASS y_check_unit_test_assert IMPLEMENTATION. ENDMETHOD. + + METHOD contains_functional_operand. + result = xsdbool( matches( val = expression + regex = `[a-z_][a-z0-9_]*\(` ) ). + ENDMETHOD. + ENDCLASS. diff --git a/src/checks/y_check_unit_test_assert.clas.testclasses.abap b/src/checks/y_check_unit_test_assert.clas.testclasses.abap index b3f372b6..b424711f 100644 --- a/src/checks/y_check_unit_test_assert.clas.testclasses.abap +++ b/src/checks/y_check_unit_test_assert.clas.testclasses.abap @@ -588,3 +588,104 @@ CLASS ltc_assert_empty IMPLEMENTATION. ENDMETHOD. ENDCLASS. + + +CLASS ltc_functional_operand 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_functional_operand IMPLEMENTATION. + + METHOD get_cut. + result ?= NEW y_check_unit_test_assert( ). + 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 test FOR TESTING. ' ) + ( ' METHODS get_val RETURNING VALUE(result) type string' ) + ( ' ENDCLASS. ' ) + + ( ' CLASS y_example IMPLEMENTATION. ' ) + ( ' METHOD test. ' ) + ( ' " given ' ) + ( ' DATA(first) = 10. ' ) + ( ' DATA(second) = 10. ' ) + ( ' " when ' ) + ( ' DATA(sum) = first + second. ' ) + ( ' " then ' ) + ( ' cl_abap_unit_assert=>assert_equals( act = first && second+2(1)' ) + ( ' exp = first && second+2(1) ).' ) + ( ' ENDMETHOD. ' ) + ( ' METHOD get_val.' ) + ( ' result = `Foo`.' ) + ( ' 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 test FOR TESTING. ' ) + ( ' METHODS get_val RETURNING VALUE(result) type string' ) + ( ' ENDCLASS. ' ) + + ( ' CLASS y_example IMPLEMENTATION. ' ) + ( ' METHOD test. ' ) + ( ' " given ' ) + ( ' DATA(first) = 10. ' ) + ( ' DATA(second) = 10. ' ) + ( ' " when ' ) + ( ' DATA(sum) = first + second. ' ) + ( ' " then ' ) + ( ' cl_abap_unit_assert=>assert_equals( act = first && get_val( ) && second+2(1)' ) + ( ' exp = first && get_val( ) && second+2(1) ).' ) + ( ' ENDMETHOD. ' ) + ( ' METHOD get_val.' ) + ( ' result = `Foo`.' ) + ( ' 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 test FOR TESTING. ' ) + ( ' METHODS get_val RETURNING VALUE(result) type string' ) + ( ' ENDCLASS. ' ) + + ( ' CLASS y_example IMPLEMENTATION. ' ) + ( ' METHOD test. ' ) + ( ' " given ' ) + ( ' DATA(first) = 10. ' ) + ( ' DATA(second) = 10. ' ) + ( ' " when ' ) + ( ' DATA(sum) = first + second. ' ) + ( ' " then ' ) + ( ' cl_abap_unit_assert=>assert_equals( act = first && second+2(1)' ) + ( ' exp = first && second+2(1) ). "#EC UT_ASSERT' ) + ( ' ENDMETHOD. ' ) + ( ' METHOD get_val.' ) + ( ' result = `Foo`.' ) + ( ' ENDMETHOD. ' ) + ( ' ENDCLASS. ' ) + ). + ENDMETHOD. + +ENDCLASS. From 6ab49cb074a761f724d1d901d7b847a8beb5b993 Mon Sep 17 00:00:00 2001 From: Mike Pokraka Date: Thu, 16 Sep 2021 17:27:41 +0000 Subject: [PATCH 2/7] Fix unit test and nesting syntax --- src/checks/y_check_unit_test_assert.clas.abap | 8 ++++---- ...heck_unit_test_assert.clas.testclasses.abap | 18 ++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/checks/y_check_unit_test_assert.clas.abap b/src/checks/y_check_unit_test_assert.clas.abap index 4c0edb61..cb2d7dee 100644 --- a/src/checks/y_check_unit_test_assert.clas.abap +++ b/src/checks/y_check_unit_test_assert.clas.abap @@ -114,9 +114,9 @@ CLASS y_check_unit_test_assert IMPLEMENTATION. CONTINUE. ENDIF. - IF token-str CP '*(*'. + IF token-str CP '*( *'. depth = depth + 1. - ELSEIF token-str CP '*)*'. + ELSEIF token-str CP '* )*'. depth = depth - 1. ENDIF. @@ -149,8 +149,8 @@ CLASS y_check_unit_test_assert IMPLEMENTATION. METHOD contains_functional_operand. - result = xsdbool( matches( val = expression - regex = `[a-z_][a-z0-9_]*\(` ) ). + FIND REGEX `[A-Z_][A-Z0-9_]*\(` IN expression. + result = xsdbool( sy-subrc = 0 ). ENDMETHOD. ENDCLASS. diff --git a/src/checks/y_check_unit_test_assert.clas.testclasses.abap b/src/checks/y_check_unit_test_assert.clas.testclasses.abap index b424711f..92766319 100644 --- a/src/checks/y_check_unit_test_assert.clas.testclasses.abap +++ b/src/checks/y_check_unit_test_assert.clas.testclasses.abap @@ -611,17 +611,14 @@ CLASS ltc_functional_operand IMPLEMENTATION. ( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' ) ( ' PUBLIC SECTION. ' ) ( ' METHODS test FOR TESTING. ' ) + ( ' PRIVATE SECTION.' ) ( ' METHODS get_val RETURNING VALUE(result) type string' ) ( ' ENDCLASS. ' ) ( ' CLASS y_example IMPLEMENTATION. ' ) ( ' METHOD test. ' ) - ( ' " given ' ) - ( ' DATA(first) = 10. ' ) - ( ' DATA(second) = 10. ' ) - ( ' " when ' ) - ( ' DATA(sum) = first + second. ' ) - ( ' " then ' ) + ( ' DATA(first) = `abc`.' ) + ( ' DATA(second) = `def`.' ) ( ' cl_abap_unit_assert=>assert_equals( act = first && second+2(1)' ) ( ' exp = first && second+2(1) ).' ) ( ' ENDMETHOD. ' ) @@ -639,17 +636,14 @@ CLASS ltc_functional_operand IMPLEMENTATION. ( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' ) ( ' PUBLIC SECTION. ' ) ( ' METHODS test FOR TESTING. ' ) + ( ' PRIVATE SECTION.' ) ( ' METHODS get_val RETURNING VALUE(result) type string' ) ( ' ENDCLASS. ' ) ( ' CLASS y_example IMPLEMENTATION. ' ) ( ' METHOD test. ' ) - ( ' " given ' ) - ( ' DATA(first) = 10. ' ) - ( ' DATA(second) = 10. ' ) - ( ' " when ' ) - ( ' DATA(sum) = first + second. ' ) - ( ' " then ' ) + ( ' DATA(first) = `abc`.' ) + ( ' DATA(second) = `def`.' ) ( ' cl_abap_unit_assert=>assert_equals( act = first && get_val( ) && second+2(1)' ) ( ' exp = first && get_val( ) && second+2(1) ).' ) ( ' ENDMETHOD. ' ) From 7831383b7d256c81d3affb452fb172600ae41b16 Mon Sep 17 00:00:00 2001 From: Mike Pokraka Date: Thu, 16 Sep 2021 17:36:01 +0000 Subject: [PATCH 3/7] Lint --- src/checks/y_check_unit_test_assert.clas.abap | 1 - 1 file changed, 1 deletion(-) diff --git a/src/checks/y_check_unit_test_assert.clas.abap b/src/checks/y_check_unit_test_assert.clas.abap index cb2d7dee..35fc1f7a 100644 --- a/src/checks/y_check_unit_test_assert.clas.abap +++ b/src/checks/y_check_unit_test_assert.clas.abap @@ -147,7 +147,6 @@ CLASS y_check_unit_test_assert IMPLEMENTATION. ENDMETHOD. - METHOD contains_functional_operand. FIND REGEX `[A-Z_][A-Z0-9_]*\(` IN expression. result = xsdbool( sy-subrc = 0 ). From 4327ae29f7d8a4b67633a1cddf8a6ebe98fef03d Mon Sep 17 00:00:00 2001 From: Mike Pokraka Date: Thu, 16 Sep 2021 17:43:34 +0000 Subject: [PATCH 4/7] Update exemption unit test --- .../y_check_unit_test_assert.clas.testclasses.abap | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/checks/y_check_unit_test_assert.clas.testclasses.abap b/src/checks/y_check_unit_test_assert.clas.testclasses.abap index 92766319..060b20b4 100644 --- a/src/checks/y_check_unit_test_assert.clas.testclasses.abap +++ b/src/checks/y_check_unit_test_assert.clas.testclasses.abap @@ -661,17 +661,14 @@ CLASS ltc_functional_operand IMPLEMENTATION. ( ' CLASS y_example DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. ' ) ( ' PUBLIC SECTION. ' ) ( ' METHODS test FOR TESTING. ' ) + ( ' PRIVATE SECTION.' ) ( ' METHODS get_val RETURNING VALUE(result) type string' ) ( ' ENDCLASS. ' ) ( ' CLASS y_example IMPLEMENTATION. ' ) ( ' METHOD test. ' ) - ( ' " given ' ) - ( ' DATA(first) = 10. ' ) - ( ' DATA(second) = 10. ' ) - ( ' " when ' ) - ( ' DATA(sum) = first + second. ' ) - ( ' " then ' ) + ( ' DATA(first) = `abc`.' ) + ( ' DATA(second) = `def`.' ) ( ' cl_abap_unit_assert=>assert_equals( act = first && second+2(1)' ) ( ' exp = first && second+2(1) ). "#EC UT_ASSERT' ) ( ' ENDMETHOD. ' ) From 2d3a019edc293577c7b5335cc6c86536c7b4fa8c Mon Sep 17 00:00:00 2001 From: Mike Pokraka Date: Thu, 16 Sep 2021 19:52:54 +0000 Subject: [PATCH 5/7] Fix unit test --- src/checks/y_check_unit_test_assert.clas.testclasses.abap | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/checks/y_check_unit_test_assert.clas.testclasses.abap b/src/checks/y_check_unit_test_assert.clas.testclasses.abap index e8462b10..aeb68171 100644 --- a/src/checks/y_check_unit_test_assert.clas.testclasses.abap +++ b/src/checks/y_check_unit_test_assert.clas.testclasses.abap @@ -642,7 +642,7 @@ CLASS ltc_functional_operand IMPLEMENTATION. ( ' PUBLIC SECTION. ' ) ( ' METHODS test FOR TESTING. ' ) ( ' PRIVATE SECTION.' ) - ( ' METHODS get_val RETURNING VALUE(result) type string' ) + ( ' METHODS get_val RETURNING VALUE(result) type string.' ) ( ' ENDCLASS. ' ) ( ' CLASS y_example IMPLEMENTATION. ' ) @@ -667,7 +667,7 @@ CLASS ltc_functional_operand IMPLEMENTATION. ( ' PUBLIC SECTION. ' ) ( ' METHODS test FOR TESTING. ' ) ( ' PRIVATE SECTION.' ) - ( ' METHODS get_val RETURNING VALUE(result) type string' ) + ( ' METHODS get_val RETURNING VALUE(result) type string.' ) ( ' ENDCLASS. ' ) ( ' CLASS y_example IMPLEMENTATION. ' ) @@ -692,7 +692,7 @@ CLASS ltc_functional_operand IMPLEMENTATION. ( ' PUBLIC SECTION. ' ) ( ' METHODS test FOR TESTING. ' ) ( ' PRIVATE SECTION.' ) - ( ' METHODS get_val RETURNING VALUE(result) type string' ) + ( ' METHODS get_val RETURNING VALUE(result) type string.' ) ( ' ENDCLASS. ' ) ( ' CLASS y_example IMPLEMENTATION. ' ) @@ -709,4 +709,4 @@ CLASS ltc_functional_operand IMPLEMENTATION. ). ENDMETHOD. -ENDCLASS. \ No newline at end of file +ENDCLASS. From 4ce5fb3c3026a3b13a6111ed01103f3a126e0052 Mon Sep 17 00:00:00 2001 From: Mike Pokraka Date: Thu, 16 Sep 2021 21:02:44 +0100 Subject: [PATCH 6/7] Add functional operand fix #460 to changelog --- changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.txt b/changelog.txt index 06dc0557..e820d693 100644 --- a/changelog.txt +++ b/changelog.txt @@ -20,6 +20,7 @@ Whenever you upgrade code pal for ABAP, it is highly recommended to execute the + Prefer Pragmas to Pseudo Comments (#421) + COLLECT restriction (#441) * RAP needs CREATE OBJECT ... FOR TESTING (#444) +* Filter functional operands in "Unit Test Assert" check (#460) 2021-08-12 v.1.15.0 ------------------ From ffb1504222df1041edb201daa2e5eb01f67ec905 Mon Sep 17 00:00:00 2001 From: Lucas Borin <5233413+lucasborin@users.noreply.github.com> Date: Fri, 17 Sep 2021 14:06:49 +0000 Subject: [PATCH 7/7] removing static test, and adding more test cases --- ...eck_unit_test_assert.clas.testclasses.abap | 50 +++---------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/src/checks/y_check_unit_test_assert.clas.testclasses.abap b/src/checks/y_check_unit_test_assert.clas.testclasses.abap index aeb68171..66b824cf 100644 --- a/src/checks/y_check_unit_test_assert.clas.testclasses.abap +++ b/src/checks/y_check_unit_test_assert.clas.testclasses.abap @@ -491,50 +491,6 @@ 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. - - - CLASS ltc_assert_fail DEFINITION INHERITING FROM ltc_hardcoded_string FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. PROTECTED SECTION. METHODS get_code_without_issue REDEFINITION. @@ -676,6 +632,12 @@ CLASS ltc_functional_operand IMPLEMENTATION. ( ' DATA(second) = `def`.' ) ( ' cl_abap_unit_assert=>assert_equals( act = first && get_val( ) && second+2(1)' ) ( ' exp = first && get_val( ) && second+2(1) ).' ) + ( ' cl_abap_unit_assert=>assert_equals( act = first && get_val( ) && second+2(1)' ) + ( ' exp = first && second+2(1) ).' ) + ( ' cl_abap_unit_assert=>assert_equals( act = first && second+2(1)' ) + ( ' exp = first && get_val( ) && second+2(1) ).' ) + ( ' cl_abap_unit_assert=>assert_equals( act = get_val( )' ) + ( ' exp = get_val( ) ).' ) ( ' ENDMETHOD. ' ) ( ' METHOD get_val.' ) ( ' result = `Foo`.' )