use of org.sonar.plugins.python.api.tree.ComprehensionExpression in project sonar-python by SonarSource.
the class PythonTreeMakerTest method list_comprehension_with_if.
@Test
public void list_comprehension_with_if() {
setRootRule(PythonGrammar.TEST);
ComprehensionExpression comprehension = (ComprehensionExpression) parse("[x+1 for x in [42, 43] if x%2==0]", treeMaker::expression);
assertThat(comprehension.getKind()).isEqualTo(Tree.Kind.LIST_COMPREHENSION);
ComprehensionFor forClause = comprehension.comprehensionFor();
assertThat(forClause.nestedClause().getKind()).isEqualTo(Tree.Kind.COMP_IF);
ComprehensionIf ifClause = (ComprehensionIf) forClause.nestedClause();
assertThat(ifClause.ifToken().value()).isEqualTo("if");
assertThat(ifClause.condition().getKind()).isEqualTo(Tree.Kind.COMPARISON);
assertThat(ifClause.nestedClause()).isNull();
assertThat(ifClause.children()).hasSize(2);
}
use of org.sonar.plugins.python.api.tree.ComprehensionExpression in project sonar-python by SonarSource.
the class SymbolTableBuilderTest method comprehension_vars.
@Test
public void comprehension_vars() {
FunctionDef functionTree = functionTreesByName.get("comprehension_vars");
ComprehensionExpression comprehensionExpression = ((ComprehensionExpression) ((ExpressionStatement) functionTree.body().statements().get(0)).expressions().get(0));
assertThat(comprehensionExpression.localVariables()).hasSize(1);
Map<String, Symbol> symbolByName = comprehensionExpression.localVariables().stream().collect(Collectors.toMap(Symbol::name, Functions.identity()));
Symbol a = symbolByName.get("a");
assertThat(a.usages()).extracting(Usage::kind).containsExactlyInAnyOrder(Usage.Kind.COMP_DECLARATION);
}
use of org.sonar.plugins.python.api.tree.ComprehensionExpression in project sonar-python by SonarSource.
the class SymbolTableBuilderTest method comprehension_scope.
@Test
public void comprehension_scope() {
FunctionDef functionTree = functionTreesByName.get("scope_of_comprehension");
Map<String, Symbol> symbolByName = getSymbolByName(functionTree);
assertThat(symbolByName).containsOnlyKeys("x");
assertThat(symbolByName.get("x").usages()).extracting(u -> u.tree().firstToken().line()).containsExactly(108, 110).doesNotContain(109);
Name name = (Name) ((ComprehensionExpression) ((ExpressionStatement) functionTree.body().statements().get(0)).expressions().get(0)).comprehensionFor().loopExpression();
assertThat(name.symbol().usages()).extracting(u -> u.tree().firstToken().line()).doesNotContain(108, 110).containsExactly(109, 109);
}
use of org.sonar.plugins.python.api.tree.ComprehensionExpression in project sonar-python by SonarSource.
the class SymbolTableBuilderTest method tuples_in_comp.
@Test
public void tuples_in_comp() {
FunctionDef functionTree = functionTreesByName.get("symbols_in_comp");
Map<String, Symbol> symbolByName = getSymbolByName(functionTree);
assertThat(symbolByName).isEmpty();
List<Name> names = getNameFromExpression(((ComprehensionExpression) ((ExpressionStatement) functionTree.body().statements().get(0)).expressions().get(0)).comprehensionFor().loopExpression());
assertThat(names).hasSize(3).extracting(Name::name).containsOnly("x", "y", "z");
for (Symbol symbol : symbolByName.values()) {
assertThat(symbol.usages()).hasSize(2);
}
}
use of org.sonar.plugins.python.api.tree.ComprehensionExpression in project sonar-python by SonarSource.
the class SymbolTableBuilderTest method comprehension.
@Test
public void comprehension() {
FunctionDef functionTree = functionTreesByName.get("function_with_comprehension");
Map<String, Symbol> symbolByName = getSymbolByName(functionTree);
assertThat(symbolByName).isEmpty();
List<Name> names = getNameFromExpression(((ComprehensionExpression) ((ExpressionStatement) functionTree.body().statements().get(0)).expressions().get(0)).comprehensionFor().loopExpression());
assertThat(names).hasSize(1).extracting(Name::name).containsOnly("a");
Symbol a = names.get(0).symbol();
assertThat(a.usages()).extracting(Usage::kind).containsOnly(Usage.Kind.COMP_DECLARATION);
}
Aggregations