Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42203x 42203x 42203x 42203x 42203x 42203x 42203x 42203x 1x 1x 1x 21103x 21103x 21103x 21103x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 63077x 21103x 21103x 20205x 20205x 21103x 21103x 376x 376x 21103x 21103x 434x 434x 434x 15x 15x 419x 419x 434x 373x 373x 46x 434x 47x 47x 47x 4x 4x 43x 47x 417x 6x 6x 417x 36x 36x 20x 20x 36x 417x 43x 46x 46x 46x 21103x 21103x 21103x 419x 419x 444x 221x 46x 46x 221x 444x 419x 419x 21103x 21103x 21103x 36x 36x 1x 1x 35x 36x 18x 18x 18x 3x 3x 18x 11x 11x 36x 13x 13x 13x 13x 6x 13x 7x 3x 3x 13x 12x 12x 12x 21103x 21103x 21103x 36x 36x 36x 36x 36x 36x 36x 33x 33x 33x 33x 33x 33x 36x 3x 3x 3x 3x 3x 3x 36x 36x 36x 36x 36x 1x 1x 35x 35x 35x 36x 36x 36x 36x 36x 36x 36x 11x 11x 11x 36x 45x 45x 45x 45x 45x 45x 35x 35x 35x 21103x | import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import * as Structures from "../abap/3_structures/structures";
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {IRuleMetadata} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
import {ExpressionNode, StatementNode} from "../abap/nodes";
import {Severity} from "../severity";
import {hasCall, normalizedTokens} from "./_compare";
export class UnitTestAssertionsConf extends BasicRuleConfig {
public severity?: Severity = Severity.Warning;
/** Report assertions that cannot fail: assert_equals or assert_equals_float with the same operand for act and exp,
* assert_true with abap_true or 'X', assert_false with abap_false, space or ' ' */
public cannotFail: boolean = true;
/** Report the ASSERT statement in FOR TESTING classes */
public assertStatement: boolean = true;
}
const ASSERT_CLASSES = ["CL_ABAP_UNIT_ASSERT", "CL_AUNIT_ASSERT"];
export class UnitTestAssertions extends ABAPRule {
private conf = new UnitTestAssertionsConf();
public getMetadata(): IRuleMetadata {
return {
key: "unit_test_assertions",
title: "Unit test assertions",
shortDescription: `Reports unit test assertions that cannot fail, and the ASSERT statement in FOR TESTING classes`,
extendedInformation: `Only code in FOR TESTING classes is checked. Each check has its own option.
cannotFail: an assertion of cl_abap_unit_assert or cl_aunit_assert that passes whatever the code under test does.
assert_equals and assert_equals_float with the same operand for act and exp, assert_true with abap_true or 'X',
assert_false with abap_false, space or ' '. Not reported: operands that contain a method call or NEW, as two calls
can return different values; two different literals or constants; assertions that always fail.
assertStatement: the ASSERT statement. A failing ASSERT ends in the runtime error ASSERTION_FAILED, so the test
run shows a short dump instead of a failed test with act, exp and a message. Use cl_abap_unit_assert instead.
Not reported: ASSERT in classes that are not FOR TESTING.`,
tags: [],
badExample: `CLASS ltcl_test DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA mt_rows TYPE string_table.
METHODS test FOR TESTING.
ENDCLASS.
CLASS ltcl_test IMPLEMENTATION.
METHOD test.
DATA(lv_count) = lines( mt_rows ).
cl_abap_unit_assert=>assert_equals( act = lv_count exp = lv_count ).
cl_abap_unit_assert=>assert_true( abap_true ).
ASSERT lv_count = 2.
ENDMETHOD.
ENDCLASS.`,
goodExample: `CLASS ltcl_test DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA mt_rows TYPE string_table.
METHODS test FOR TESTING.
ENDCLASS.
CLASS ltcl_test IMPLEMENTATION.
METHOD test.
DATA(lv_count) = lines( mt_rows ).
cl_abap_unit_assert=>assert_equals( act = lv_count exp = 2 ).
cl_abap_unit_assert=>assert_true( xsdbool( lv_count > 0 ) ).
ENDMETHOD.
ENDCLASS.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: UnitTestAssertionsConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile, obj: ABAPObject): Issue[] {
const issues: Issue[] = [];
const structure = file.getStructure();
if (structure === undefined) {
return [];
}
const testClasses = this.testClassNames(obj);
if (testClasses.size === 0) {
return [];
}
for (const implementation of structure.findAllStructures(Structures.ClassImplementation)) {
const name = implementation.findFirstStatement(Statements.ClassImplementation)
?.findFirstExpression(Expressions.ClassName)?.concatTokens().toUpperCase();
if (name === undefined || testClasses.has(name) === false) {
continue;
}
for (const statement of implementation.findAllStatementNodes()) {
if (this.conf.assertStatement === true && statement.get() instanceof Statements.Assert) {
const message = "Use cl_abap_unit_assert instead of ASSERT in a test class";
issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
} else if (this.conf.cannotFail === true && statement.get() instanceof Statements.Call) {
const message = this.cannotFail(statement);
if (message !== undefined) {
issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
}
}
}
}
return issues;
}
/** names of the FOR TESTING classes defined in any file of the object, upper case */
private testClassNames(obj: ABAPObject): Set<string> {
const ret = new Set<string>();
for (const f of obj.getABAPFiles()) {
for (const def of f.getInfo().listClassDefinitions()) {
if (def.isForTesting === true) {
ret.add(def.name.toUpperCase());
}
}
}
return ret;
}
/** returns the message if the call is an assertion that cannot fail */
private cannotFail(statement: StatementNode): string | undefined {
const call = this.assertCall(statement);
if (call === undefined) {
return undefined;
}
if (call.method === "ASSERT_EQUALS" || call.method === "ASSERT_EQUALS_FLOAT") {
const act = call.parameters.get("ACT");
const exp = call.parameters.get("EXP");
if (act === undefined || exp === undefined || hasCall(act) || hasCall(exp)) {
return undefined;
}
if (normalizedTokens(act) === normalizedTokens(exp)) {
return "Assertion cannot fail, act and exp are the same";
}
} else if (call.method === "ASSERT_TRUE" || call.method === "ASSERT_FALSE") {
const act = call.parameters.get("ACT");
if (act === undefined) {
return undefined;
}
const value = normalizedTokens(act);
if (call.method === "ASSERT_TRUE" && (value === "ABAP_TRUE" || value === "'X'")) {
return "Assertion cannot fail, act is abap_true";
} else if (call.method === "ASSERT_FALSE"
&& (value === "ABAP_FALSE" || value === "SPACE" || value === "''" || value === "' '")) {
return "Assertion cannot fail, act is abap_false";
}
}
return undefined;
}
/** class method call of cl_abap_unit_assert or cl_aunit_assert: method name and the sources by parameter name, upper case */
private assertCall(statement: StatementNode): {method: string, parameters: Map<string, ExpressionNode>} | undefined {
let className: ExpressionNode | undefined;
let methodName: ExpressionNode | undefined;
let parameters: ExpressionNode | undefined;
const chain = statement.findDirectExpression(Expressions.MethodCallChain);
const source = statement.findDirectExpression(Expressions.MethodSource);
if (chain !== undefined) {
const children = chain.getChildren();
const methodCall = chain.findDirectExpression(Expressions.MethodCall);
if (children.length !== 3 || methodCall === undefined) {
return undefined;
}
className = chain.findDirectExpression(Expressions.ClassName);
methodName = methodCall.findDirectExpression(Expressions.MethodName);
parameters = methodCall.findDirectExpression(Expressions.MethodCallParam);
} else if (source !== undefined) {
const children = source.getChildren();
if (children.length !== 3) {
return undefined;
}
className = source.findDirectExpression(Expressions.ClassName);
methodName = source.findDirectExpression(Expressions.AttributeName);
parameters = statement.findDirectExpression(Expressions.MethodCallBody);
}
if (className === undefined
|| methodName === undefined
|| parameters === undefined
|| ASSERT_CLASSES.includes(className.concatTokens().toUpperCase()) === false) {
return undefined;
}
// CALL METHOD x( ... ) and the functional call have a MethodCallParam, CALL METHOD x EXPORTING ... has not
const param = parameters.get() instanceof Expressions.MethodCallParam
? parameters : parameters.findDirectExpression(Expressions.MethodCallParam);
const list = param?.findDirectExpression(Expressions.ParameterListS)
|| (param || parameters).findDirectExpression(Expressions.MethodParameters)?.findDirectExpression(Expressions.ParameterListS);
const map = new Map<string, ExpressionNode>();
const single = param?.findDirectExpression(Expressions.Source);
if (single !== undefined) {
// only the preferred parameter is given, for the assertions checked here it is ACT
map.set("ACT", single);
}
for (const parameter of list?.findDirectExpressions(Expressions.ParameterS) || []) {
const name = parameter.findDirectExpression(Expressions.ParameterName)?.concatTokens().toUpperCase();
const value = parameter.findDirectExpression(Expressions.Source);
if (name !== undefined && value !== undefined) {
map.set(name, value);
}
}
return {method: methodName.concatTokens().toUpperCase(), parameters: map};
}
}
|