Search in sources :

Example 1 with DictCompExpression

use of org.sonar.plugins.python.api.tree.DictCompExpression in project sonar-python by SonarSource.

the class PythonTreeMakerTest method dict_comprehension.

@Test
public void dict_comprehension() {
    setRootRule(PythonGrammar.TEST);
    DictCompExpression comprehension = (DictCompExpression) parse("{x-1:y+1 for x,y in [(42,43)]}", treeMaker::expression);
    assertThat(comprehension.firstToken().value()).isEqualTo("{");
    assertThat(comprehension.lastToken().value()).isEqualTo("}");
    assertThat(comprehension.getKind()).isEqualTo(Tree.Kind.DICT_COMPREHENSION);
    assertThat(comprehension.colonToken().value()).isEqualTo(":");
    assertThat(comprehension.keyExpression().getKind()).isEqualTo(Tree.Kind.MINUS);
    assertThat(comprehension.valueExpression().getKind()).isEqualTo(Tree.Kind.PLUS);
    assertThat(comprehension.comprehensionFor().loopExpression().getKind()).isEqualTo(Tree.Kind.TUPLE);
    assertThat(comprehension.children()).hasSize(6);
    assertThat(comprehension.firstToken().value()).isEqualTo("{");
    assertThat(comprehension.lastToken().value()).isEqualTo("}");
}
Also used : DictCompExpression(org.sonar.plugins.python.api.tree.DictCompExpression) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 2 with DictCompExpression

use of org.sonar.plugins.python.api.tree.DictCompExpression in project sonar-python by SonarSource.

the class SymbolTableBuilderTest method comprehension_shadowing_names.

@Test
public void comprehension_shadowing_names() {
    FunctionDef functionTree = functionTreesByName.get("comprehension_reusing_name");
    Map<String, Symbol> symbolByName = getSymbolByName(functionTree);
    assertThat(symbolByName).hasSize(1);
    assertThat(symbolByName.get("a").usages()).hasSize(2);
    List<Name> names = getNameFromExpression(((DictCompExpression) ((ExpressionStatement) functionTree.body().statements().get(0)).expressions().get(0)).comprehensionFor().loopExpression());
    assertThat(names).hasSize(1);
    assertThat(names.get(0).symbol().usages()).hasSize(2);
}
Also used : Symbol(org.sonar.plugins.python.api.symbols.Symbol) DictCompExpression(org.sonar.plugins.python.api.tree.DictCompExpression) ExpressionStatement(org.sonar.plugins.python.api.tree.ExpressionStatement) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 DictCompExpression (org.sonar.plugins.python.api.tree.DictCompExpression)2 Symbol (org.sonar.plugins.python.api.symbols.Symbol)1 ExpressionStatement (org.sonar.plugins.python.api.tree.ExpressionStatement)1 FunctionDef (org.sonar.plugins.python.api.tree.FunctionDef)1 Name (org.sonar.plugins.python.api.tree.Name)1 RuleTest (org.sonar.python.parser.RuleTest)1