Search in sources :

Example 1 with BasicBlock

use of org.sonar.ucfg.BasicBlock in project sonar-java by SonarSource.

the class UCFGJavaVisitorTest method assertCodeToUCfg.

private UCFG assertCodeToUCfg(String source, UCFG expectedUCFG, boolean testLocations) {
    CompilationUnitTree cut = getCompilationUnitTreeWithSemantics(source);
    UCFGJavaVisitor UCFGJavaVisitor = new UCFGJavaVisitor(tmp.getRoot());
    UCFGJavaVisitor.fileKey = FILE_KEY;
    UCFGJavaVisitor.visitCompilationUnit(cut);
    UCFG actualUCFG = null;
    try {
        File java_ucfg_dir = new File(new File(tmp.getRoot(), "ucfg"), "java");
        File ucfg = new File(java_ucfg_dir, "ucfg_0.proto");
        actualUCFG = UCFGtoProtobuf.fromProtobufFile(ucfg);
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertThat(actualUCFG.methodId()).isEqualTo(expectedUCFG.methodId());
    assertThat(actualUCFG.basicBlocks()).isEqualTo(expectedUCFG.basicBlocks());
    assertThat(actualUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream())).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream()).collect(Collectors.toList()));
    assertThat(actualUCFG.basicBlocks().values().stream().map(BasicBlock::terminator)).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().map(BasicBlock::terminator).collect(Collectors.toList()));
    assertThat(actualUCFG.entryBlocks()).isEqualTo(expectedUCFG.entryBlocks());
    assertThat(toLocationStream(actualUCFG).noneMatch(l -> l == UCFGBuilder.LOC)).isTrue();
    if (testLocations) {
        Stream<LocationInFile> locStream = toLocationStream(actualUCFG);
        assertThat(locStream).containsExactlyElementsOf(toLocationStream(expectedUCFG).collect(Collectors.toList()));
    }
    return actualUCFG;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) UCFG(org.sonar.ucfg.UCFG) BasicBlock(org.sonar.ucfg.BasicBlock) UCFGBuilder.newBasicBlock(org.sonar.ucfg.UCFGBuilder.newBasicBlock) LocationInFile(org.sonar.ucfg.LocationInFile) IOException(java.io.IOException) File(java.io.File) LocationInFile(org.sonar.ucfg.LocationInFile)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)1 BasicBlock (org.sonar.ucfg.BasicBlock)1 LocationInFile (org.sonar.ucfg.LocationInFile)1 UCFG (org.sonar.ucfg.UCFG)1 UCFGBuilder.newBasicBlock (org.sonar.ucfg.UCFGBuilder.newBasicBlock)1