Skip to content

Commit 0b57c21

Browse files
Unit Test Assert Check (#347)
* new check * fixing class description * adding demo * comparing token type * Update check_documentation.md * Update check_documentation.md * Create unit_test_assert.md * Update changelog.txt * Update unit_test_assert.md Co-authored-by: estevao-schultz-neto-SAP <[email protected]>
1 parent bdebcfc commit 0b57c21

7 files changed

+494
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Whenever you upgrade code pal for ABAP, it is highly recommended to execute the
1414

1515
2021-04-** v.1.14.0
1616
------------------
17+
+ Unit Test Without/With Invalid Assert (#288)
1718
+ Prefer NEW to CREATE OBJECT (#283)
1819
* Standard functions in Prefer IS NOT to NOT IS (#338)
1920
* In the profile feature, you can add all the missing checks (#346)

docs/check_documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@
5151
- [Self-Reference](checks/self-reference.md)
5252
- [TEST-SEAM Statement Usage](checks/test-seam-usage.md)
5353
- [Unit-Test Coverages (Statement, Branch and Procedure)](checks/unit-test-coverages.md)
54+
- [Unit-Test Assert Validator](checks/unit_test_assert.md)

docs/checks/unit_test_assert.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[code pal for ABAP](../../README.md) > [Documentation](../check_documentation.md) > [Unit-Test Assert Validator](unit_test_assert.md)
2+
3+
## Unit-Test Assert Validator
4+
5+
### What is the Intent of the Check?
6+
7+
This check verifies invalid assertions in unit tests.
8+
It supports the `CL_ABAP_UNIT_ASSERT=>ASSERT*` and `CL_AUNIT_ASSERT=>ASSERT*`.
9+
10+
### How does the check work?
11+
12+
It checks for actual (`act`) or expected (`exp`) invalid value(s), for instance:
13+
- When both are using the same variable for the Assertion (which will always return TRUE);
14+
- When both are hardcoded.
15+
16+
### How to solve the issue?
17+
18+
Fix the actual (`act`) or expected (`exp`) value(s) in the unit test assertion in order to achieve a meaningful and real Assertion.
19+
20+
### What to do in case of exception?
21+
22+
In exceptional cases (if any), you can suppress this finding by using the pseudo comment `"#EC UT_ASSERT` which has to be placed after the assertion statement:
23+
24+
```abap
25+
cl_abap_unit_assert=>assert_equals( act = sum
26+
exp = sum ). "#EC UT_ASSERT
27+
```
28+
29+
### Example
30+
31+
Before the check:
32+
33+
```abap
34+
METHOD sum.
35+
" given
36+
DATA(first) = 10.
37+
DATA(second) = 10.
38+
" when
39+
DATA(sum) = first + second.
40+
" then
41+
cl_abap_unit_assert=>assert_equals( act = sum
42+
exp = sum ).
43+
ENDMETHOD.
44+
```
45+
46+
After the check:
47+
48+
```abap
49+
METHOD sum.
50+
" given
51+
DATA(first) = 10.
52+
DATA(second) = 10.
53+
" when
54+
DATA(sum) = first + second.
55+
" then
56+
cl_abap_unit_assert=>assert_equals( act = sum
57+
exp = 20 ).
58+
ENDMETHOD.
59+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
CLASS y_check_unit_test_assert 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 get_act_and_exp IMPORTING statement TYPE sstmnt
10+
EXPORTING act TYPE stokesx
11+
exp TYPE stokesx.
12+
13+
METHODS is_variable IMPORTING token TYPE stokesx
14+
RETURNING VALUE(result) TYPE abap_bool.
15+
16+
ENDCLASS.
17+
18+
19+
CLASS y_check_unit_test_assert IMPLEMENTATION.
20+
21+
22+
METHOD constructor.
23+
super->constructor( ).
24+
25+
settings-pseudo_comment = '"#EC UT_ASSERT' ##NO_TEXT.
26+
settings-disable_threshold_selection = abap_true.
27+
settings-apply_on_productive_code = abap_false.
28+
settings-apply_on_test_code = abap_true.
29+
settings-threshold = 0.
30+
settings-documentation = |{ c_docs_path-checks }unit_test_assert.md|.
31+
32+
relevant_statement_types = VALUE #( ( scan_struc_stmnt_type-method ) ).
33+
relevant_structure_types = VALUE #( ).
34+
35+
set_check_message( 'Invalid Unit Test Assertion!' ).
36+
ENDMETHOD.
37+
38+
39+
METHOD inspect_tokens.
40+
CHECK get_token_abs( statement-from ) CP 'CL_ABAP_UNIT_ASSERT=>ASSERT*'
41+
OR get_token_abs( statement-from ) CP 'CL_AUNIT_ASSERT=>ASSERT*'.
42+
43+
get_act_and_exp( EXPORTING statement = statement
44+
IMPORTING act = DATA(act)
45+
exp = DATA(exp) ).
46+
47+
IF act IS INITIAL
48+
OR exp IS INITIAL.
49+
RETURN.
50+
ENDIF.
51+
52+
IF act-str <> exp-str.
53+
IF is_variable( act ) = abap_true
54+
OR is_variable( exp ) = abap_true.
55+
RETURN.
56+
ENDIF.
57+
ENDIF.
58+
59+
DATA(check_configuration) = detect_check_configuration( statement ).
60+
61+
IF check_configuration IS INITIAL.
62+
RETURN.
63+
ENDIF.
64+
65+
raise_error( statement_level = statement-level
66+
statement_index = index
67+
statement_from = statement-from
68+
error_priority = check_configuration-prio ).
69+
ENDMETHOD.
70+
71+
72+
METHOD get_act_and_exp.
73+
LOOP AT ref_scan_manager->tokens ASSIGNING FIELD-SYMBOL(<token>)
74+
FROM statement-from TO statement-to.
75+
DATA(tabix) = sy-tabix.
76+
CASE <token>-str.
77+
WHEN 'ACT'.
78+
act = ref_scan_manager->tokens[ tabix + 2 ].
79+
WHEN 'EXP'.
80+
exp = ref_scan_manager->tokens[ tabix + 2 ].
81+
WHEN OTHERS.
82+
CONTINUE.
83+
ENDCASE.
84+
ENDLOOP.
85+
ENDMETHOD.
86+
87+
88+
METHOD is_variable.
89+
result = COND #( WHEN token-type = scan_token_type-literal THEN abap_false
90+
WHEN token-type = scan_token_type-identifier THEN xsdbool( token-str CN '0123456789' ) ).
91+
ENDMETHOD.
92+
93+
94+
ENDCLASS.

0 commit comments

Comments
 (0)