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("}");
}
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);
}
Aggregations