All files / src/rules local_testclass_consistency.ts

89.7% Statements 61/68
89.47% Branches 17/19
100% Functions 5/5
89.7% Lines 61/68

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 681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8923x 8923x 8923x 8923x 8923x 26608x 26608x 26608x 26608x 26608x 26608x 26608x 8923x 8923x 8485x 8485x 8923x 8923x 202x 202x 8923x 8923x 216x 216x 216x       216x 216x 166x 166x 50x 216x 45x         45x 50x 50x 216x 216x 1x 1x 1x 1x 1x 1x 1x 50x 50x 50x 8923x 8923x
import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Class} from "../objects";
import {IObject} from "../objects/_iobject";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {Version} from "../version";
 
export class LocalTestclassConsistencyConf extends BasicRuleConfig {
}
 
export class LocalTestclassConsistency extends ABAPRule {
 
  private conf = new LocalTestclassConsistencyConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "local_testclass_consistency",
      title: "Local testclass consistency",
      shortDescription: `Checks that local test classes are placed in the test include, and class unit test flag is set`,
      tags: [RuleTag.Syntax],
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: LocalTestclassConsistencyConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile, obj: IObject) {
    const issues: Issue[] = [];
 
    if (this.reg.getConfig().getVersion() === Version.v700) {
      // 700 does not have testclass includes
      return [];
    }
 
    if (!(obj instanceof Class)) {
      return [];
    }
 
    for (const c of file.getInfo().listClassDefinitions()) {
      if (c.isLocal && c.isForTesting && !file.getFilename().includes(".testclasses.abap")) {
        const message = "Place local testclass \"" + c.name + "\" in the testclass include";
        const issue = Issue.atIdentifier(c.identifier, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    if (file.getFilename().includes(".testclasses.") === true
        && obj.getTestclassFile() !== undefined
        && obj.getXML()?.includes("<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>") === false) {
      const id = obj.getIdentifier();
      if (id) {
        const message = "Has testclass, but XML does not set <WITH_UNIT_TESTS>";
        const issue = Issue.atIdentifier(id, message, this.getMetadata().key, this.conf.severity);
        issues.push(issue);
      }
    }
 
    return issues;
  }
 
}