ControlFlowGraph { public CfgBlock start() { return start; } ControlFlowGraph(Set<CfgBlock> blocks, CfgBlock start, CfgBlock end); @CheckForNull static ControlFlowGraph build(FunctionDef functionDef, PythonFile file); @CheckForNull static ControlFlowGraph build(FileInput fileInput, PythonFile file); CfgBlock start(); CfgBlock end(); Set<CfgBlock> blocks(); @Override String toString();  }
@Test public void element_order() { ControlFlowGraph cfg = verifyCfg("b1(succ = [END], pred = []); pass"); assertThat(cfg.start().elements()).extracting(Tree::getKind).containsExactly(Kind.EXPRESSION_STMT, Kind.PASS_STMT); }
@Test public void return_statement() { verifyCfg( "b1(succ = [END], syntSucc = END, pred = [])", "return"); ControlFlowGraph cfg = verifyCfg( "b1(succ = [END], syntSucc = b2, pred = [])", "return", "b2(succ = [END], pred = [])"); assertThat(cfg.start().elements().get(0).firstToken().value()).isEqualTo("b1"); }
@Test public void while_statement() { ControlFlowGraph cfg = verifyCfg( "before(succ = [cond_block], elem = 1)", "while cond_block(succ = [while_body, END], elem = 1):", " while_body(succ = [cond_block], elem = 1)" ); CfgBranchingBlock condBlock = (CfgBranchingBlock) cfg.start().successors().iterator().next(); assertThat(condBlock.branchingTree().getKind()).isEqualTo(Kind.WHILE_STMT); }
@Test public void simple_try_except_value_error() { ControlFlowGraph cfg = cfg( "try:", " foo()", "except ValueError as error:", " print(error)" ); assertNoEmptyBlocksInCFG(cfg); CfgBlock start = cfg.start(); assertThat(start.elements()).extracting(Tree::getKind).containsExactly(Kind.EXPRESSION_STMT); assertThat(start.successors()).hasSize(2); CfgBlock exceptCondition = start.successors().stream().filter(succ -> !(succ instanceof PythonCfgEndBlock)).findFirst().get(); assertThat(exceptCondition.elements()).extracting(Tree::getKind).containsExactly(Kind.NAME, Kind.NAME); assertThat(exceptCondition.successors()).hasSize(2); }
@Test public void simple_try_except_all_exceptions() { ControlFlowGraph cfg = cfg( "try:", " foo()", "except:", " print('exception')" ); assertNoEmptyBlocksInCFG(cfg); CfgBlock start = cfg.start(); assertThat(start.elements()).extracting(Tree::getKind).containsExactly(Kind.EXPRESSION_STMT); assertThat(start.successors()).hasSize(2); CfgBlock exceptCondition = start.successors().stream().filter(succ -> !(succ instanceof PythonCfgEndBlock)).findFirst().get(); assertThat(exceptCondition.elements()).isEmpty(); assertThat(exceptCondition.successors()).hasSize(2); }