Search in sources :

Example 1 with ComprehensionExpression

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);
}
Also used : ComprehensionFor(org.sonar.plugins.python.api.tree.ComprehensionFor) ComprehensionIf(org.sonar.plugins.python.api.tree.ComprehensionIf) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 2 with ComprehensionExpression

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);
}
Also used : Symbol(org.sonar.plugins.python.api.symbols.Symbol) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) Test(org.junit.Test)

Example 3 with ComprehensionExpression

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);
}
Also used : Symbol(org.sonar.plugins.python.api.symbols.Symbol) ExpressionStatement(org.sonar.plugins.python.api.tree.ExpressionStatement) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Example 4 with ComprehensionExpression

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);
    }
}
Also used : Symbol(org.sonar.plugins.python.api.symbols.Symbol) ExpressionStatement(org.sonar.plugins.python.api.tree.ExpressionStatement) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Example 5 with ComprehensionExpression

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);
}
Also used : Symbol(org.sonar.plugins.python.api.symbols.Symbol) ExpressionStatement(org.sonar.plugins.python.api.tree.ExpressionStatement) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Aggregations

ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)11 Test (org.junit.Test)10 RuleTest (org.sonar.python.parser.RuleTest)6 Symbol (org.sonar.plugins.python.api.symbols.Symbol)4 ComprehensionFor (org.sonar.plugins.python.api.tree.ComprehensionFor)4 FunctionDef (org.sonar.plugins.python.api.tree.FunctionDef)4 ExpressionStatement (org.sonar.plugins.python.api.tree.ExpressionStatement)3 Name (org.sonar.plugins.python.api.tree.Name)3 Token (org.sonar.plugins.python.api.tree.Token)3 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)2 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)2 Expression (org.sonar.plugins.python.api.tree.Expression)2 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)2 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)2 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)2 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)2 AstNode (com.sonar.sslr.api.AstNode)1 RecognitionException (com.sonar.sslr.api.RecognitionException)1 AwaitExpression (org.sonar.plugins.python.api.tree.AwaitExpression)1 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)1