use of org.sonar.plugins.python.api.tree.Name in project sonar-python by SonarSource.
the class PythonTreeMakerTest method assignement_statement.
@Test
public void assignement_statement() {
setRootRule(PythonGrammar.EXPRESSION_STMT);
AstNode astNode = p.parse("x = y");
StatementWithSeparator statementWithSeparator = new StatementWithSeparator(astNode, null);
AssignmentStatement pyAssignmentStatement = treeMaker.assignment(statementWithSeparator);
assertThat(pyAssignmentStatement.firstToken().value()).isEqualTo("x");
assertThat(pyAssignmentStatement.lastToken().value()).isEqualTo("y");
Name assigned = (Name) pyAssignmentStatement.assignedValue();
Name lhs = (Name) pyAssignmentStatement.lhsExpressions().get(0).expressions().get(0);
assertThat(assigned.name()).isEqualTo("y");
assertThat(lhs.name()).isEqualTo("x");
assertThat(pyAssignmentStatement.children()).hasSize(3);
astNode = p.parse("x = y = z");
statementWithSeparator = new StatementWithSeparator(astNode, null);
pyAssignmentStatement = treeMaker.assignment(statementWithSeparator);
assertThat(pyAssignmentStatement.equalTokens()).hasSize(2);
assertThat(pyAssignmentStatement.children()).hasSize(5);
assigned = (Name) pyAssignmentStatement.assignedValue();
lhs = (Name) pyAssignmentStatement.lhsExpressions().get(0).expressions().get(0);
Name lhs2 = (Name) pyAssignmentStatement.lhsExpressions().get(1).expressions().get(0);
assertThat(assigned.name()).isEqualTo("z");
assertThat(lhs.name()).isEqualTo("x");
assertThat(lhs2.name()).isEqualTo("y");
astNode = p.parse("a,b = x");
statementWithSeparator = new StatementWithSeparator(astNode, null);
pyAssignmentStatement = treeMaker.assignment(statementWithSeparator);
assertThat(pyAssignmentStatement.children()).hasSize(3);
assigned = (Name) pyAssignmentStatement.assignedValue();
ExpressionList expressionList = pyAssignmentStatement.lhsExpressions().get(0);
assertThat(expressionList.children()).hasSize(3);
assertThat(expressionList.children().get(1)).isSameAs(expressionList.commas().get(0));
List<Expression> expressions = expressionList.expressions();
assertThat(assigned.name()).isEqualTo("x");
assertThat(expressions.get(0).getKind()).isEqualTo(Tree.Kind.NAME);
assertThat(expressions.get(1).getKind()).isEqualTo(Tree.Kind.NAME);
astNode = p.parse("a,b, = x");
statementWithSeparator = new StatementWithSeparator(astNode, null);
pyAssignmentStatement = treeMaker.assignment(statementWithSeparator);
assertThat(pyAssignmentStatement.children()).hasSize(3);
assigned = (Name) pyAssignmentStatement.assignedValue();
expressionList = pyAssignmentStatement.lhsExpressions().get(0);
assertThat(expressionList.children()).hasSize(4);
assertThat(expressionList.children().get(1)).isSameAs(expressionList.commas().get(0));
assertThat(expressionList.children().get(3)).isSameAs(expressionList.commas().get(1));
expressions = expressionList.expressions();
assertThat(assigned.name()).isEqualTo("x");
assertThat(expressions.get(0).getKind()).isEqualTo(Tree.Kind.NAME);
assertThat(expressions.get(1).getKind()).isEqualTo(Tree.Kind.NAME);
astNode = p.parse("x = a,b");
statementWithSeparator = new StatementWithSeparator(astNode, null);
pyAssignmentStatement = treeMaker.assignment(statementWithSeparator);
assertThat(pyAssignmentStatement.children()).hasSize(3);
expressions = pyAssignmentStatement.lhsExpressions().get(0).expressions();
assertThat(expressions.get(0).getKind()).isEqualTo(Tree.Kind.NAME);
assertThat(pyAssignmentStatement.assignedValue().getKind()).isEqualTo(Tree.Kind.TUPLE);
astNode = p.parse("x = yield 1");
statementWithSeparator = new StatementWithSeparator(astNode, null);
pyAssignmentStatement = treeMaker.assignment(statementWithSeparator);
assertThat(pyAssignmentStatement.children()).hasSize(3);
expressions = pyAssignmentStatement.lhsExpressions().get(0).expressions();
assertThat(expressions.get(0).getKind()).isEqualTo(Tree.Kind.NAME);
assertThat(pyAssignmentStatement.assignedValue().getKind()).isEqualTo(Tree.Kind.YIELD_EXPR);
// FIXME: lhs expression list shouldn't allow yield expressions. We need to change the grammar
astNode = p.parse("x = yield 1 = y");
statementWithSeparator = new StatementWithSeparator(astNode, null);
pyAssignmentStatement = treeMaker.assignment(statementWithSeparator);
assertThat(pyAssignmentStatement.children()).hasSize(5);
List<ExpressionList> lhsExpressions = pyAssignmentStatement.lhsExpressions();
assertThat(lhsExpressions.get(1).expressions().get(0).getKind()).isEqualTo(Tree.Kind.YIELD_EXPR);
assertThat(pyAssignmentStatement.assignedValue().getKind()).isEqualTo(Tree.Kind.NAME);
}
use of org.sonar.plugins.python.api.tree.Name in project sonar-python by SonarSource.
the class PythonTreeMakerTest method assignement_expression.
@Test
public void assignement_expression() {
setRootRule(PythonGrammar.NAMED_EXPR_TEST);
AstNode astNode = p.parse("b := 12");
Expression expression = treeMaker.expression(astNode);
assertThat(expression.is(Kind.ASSIGNMENT_EXPRESSION)).isTrue();
AssignmentExpression assignmentExpression = (AssignmentExpression) expression;
Name name = assignmentExpression.lhsName();
Token walrus = assignmentExpression.operator();
Expression walrusExpression = assignmentExpression.expression();
assertThat(name.name()).isEqualTo("b");
assertThat(walrus.value()).isEqualTo(":=");
assertThat(walrusExpression.is(Kind.NUMERIC_LITERAL)).isTrue();
assertThat(((NumericLiteral) walrusExpression).valueAsString()).isEqualTo("12");
assertThat(assignmentExpression.children()).containsExactly(name, walrus, walrusExpression);
setRootRule(PythonGrammar.IF_STMT);
astNode = p.parse("if a or (b := foo()):\n" + " print(b)");
IfStatement ifStatement = treeMaker.ifStatement(astNode);
BinaryExpression condition = (BinaryExpression) ifStatement.condition();
ParenthesizedExpression parenthesized = ((ParenthesizedExpression) condition.rightOperand());
assignmentExpression = (AssignmentExpression) parenthesized.expression();
name = assignmentExpression.lhsName();
walrus = assignmentExpression.operator();
walrusExpression = assignmentExpression.expression();
assertThat(name.name()).isEqualTo("b");
assertThat(walrus.value()).isEqualTo(":=");
assertThat(walrusExpression.is(Kind.CALL_EXPR)).isTrue();
Expression callee = ((CallExpression) walrusExpression).callee();
assertThat(callee.is(Kind.NAME)).isTrue();
assertThat(((Name) callee).name()).isEqualTo("foo");
assertThat(assignmentExpression.children()).containsExactly(name, walrus, walrusExpression);
setRootRule(PythonGrammar.EXPR);
astNode = p.parse("foo(a:=42)");
expression = treeMaker.expression(astNode);
assertThat(expression.is(Kind.CALL_EXPR)).isTrue();
Expression argumentExpression = ((RegularArgument) ((CallExpression) expression).arguments().get(0)).expression();
assertThat(argumentExpression.is(Kind.ASSIGNMENT_EXPRESSION)).isTrue();
assignmentExpression = (AssignmentExpression) argumentExpression;
assertThat(assignmentExpression.lhsName().name()).isEqualTo("a");
assertThat(assignmentExpression.expression().is(Kind.NUMERIC_LITERAL)).isTrue();
setRootRule(PythonGrammar.NAMED_EXPR_TEST);
try {
astNode = p.parse("a.b := 12");
treeMaker.expression(astNode);
fail("Expected RecognitionException");
} catch (RecognitionException e) {
assertThat(e.getLine()).isEqualTo(1);
}
}
use of org.sonar.plugins.python.api.tree.Name in project sonar-python by SonarSource.
the class PythonTreeMakerTest method subscription_expressions.
@Test
public void subscription_expressions() {
setRootRule(PythonGrammar.TEST);
SubscriptionExpression expr = (SubscriptionExpression) parse("x[a]", treeMaker::expression);
assertThat(expr.getKind()).isEqualTo(Tree.Kind.SUBSCRIPTION);
assertThat(((Name) expr.object()).name()).isEqualTo("x");
assertThat(((Name) expr.subscripts().expressions().get(0)).name()).isEqualTo("a");
assertThat(expr.leftBracket().value()).isEqualTo("[");
assertThat(expr.rightBracket().value()).isEqualTo("]");
assertThat(expr.children()).hasSize(4);
SubscriptionExpression multipleSubscripts = (SubscriptionExpression) parse("x[a, 42]", treeMaker::expression);
ExpressionList subscripts = multipleSubscripts.subscripts();
assertThat(subscripts.children()).hasSize(3);
assertThat(subscripts.children().get(1)).isSameAs(subscripts.commas().get(0));
assertThat(subscripts.expressions()).extracting(Tree::getKind).containsExactly(Tree.Kind.NAME, Tree.Kind.NUMERIC_LITERAL);
SubscriptionExpression subscriptionExpression = (SubscriptionExpression) parse("a[b:=1]", treeMaker::expression);
assertThat(subscriptionExpression.subscripts().expressions()).hasSize(1);
AssignmentExpression assignmentExpression = ((AssignmentExpression) subscriptionExpression.subscripts().expressions().get(0));
assertThat(assignmentExpression.lhsName().name()).isEqualTo("b");
assertThat(((NumericLiteral) assignmentExpression.expression()).valueAsLong()).isEqualTo(1);
}
use of org.sonar.plugins.python.api.tree.Name in project sonar-python by SonarSource.
the class PythonTreeMakerTest method combinations_with_call_expressions.
@Test
public void combinations_with_call_expressions() {
setRootRule(PythonGrammar.TEST);
CallExpression nestingCall = (CallExpression) parse("foo('a').bar(42)", treeMaker::expression);
assertThat(nestingCall.argumentList().arguments()).extracting(t -> ((RegularArgument) t).expression().getKind()).containsExactly(Tree.Kind.NUMERIC_LITERAL);
QualifiedExpression callee = (QualifiedExpression) nestingCall.callee();
assertThat(callee.name().name()).isEqualTo("bar");
assertThat(callee.qualifier().firstToken().value()).isEqualTo("foo");
assertThat(callee.qualifier().lastToken().value()).isEqualTo(")");
assertThat(callee.qualifier().getKind()).isEqualTo(Tree.Kind.CALL_EXPR);
nestingCall = (CallExpression) parse("foo('a').bar()", treeMaker::expression);
assertThat(nestingCall.argumentList()).isNull();
CallExpression callOnSubscription = (CallExpression) parse("a[42](arg)", treeMaker::expression);
SubscriptionExpression subscription = (SubscriptionExpression) callOnSubscription.callee();
assertThat(((Name) subscription.object()).name()).isEqualTo("a");
assertThat(subscription.subscripts().expressions()).extracting(Tree::getKind).containsExactly(Tree.Kind.NUMERIC_LITERAL);
assertThat(((Name) ((RegularArgument) callOnSubscription.argumentList().arguments().get(0)).expression()).name()).isEqualTo("arg");
}
use of org.sonar.plugins.python.api.tree.Name in project sonar-python by SonarSource.
the class PythonTreeMakerTest method slice.
@Test
public void slice() {
setRootRule(PythonGrammar.SUBSCRIPT);
SliceItem slice = parse("a:b:c", treeMaker::sliceItem);
assertThat(((Name) slice.lowerBound()).name()).isEqualTo("a");
assertThat(((Name) slice.upperBound()).name()).isEqualTo("b");
assertThat(((Name) slice.stride()).name()).isEqualTo("c");
assertThat(slice.boundSeparator().value()).isEqualTo(":");
assertThat(slice.strideSeparator().value()).isEqualTo(":");
assertThat(slice.children()).hasSize(5);
SliceItem trivial = parse(":", treeMaker::sliceItem);
assertThat(trivial.lowerBound()).isNull();
assertThat(trivial.upperBound()).isNull();
assertThat(trivial.stride()).isNull();
assertThat(trivial.strideSeparator()).isNull();
SliceItem lowerBoundOnly = parse("a:", treeMaker::sliceItem);
assertThat(((Name) lowerBoundOnly.lowerBound()).name()).isEqualTo("a");
assertThat(lowerBoundOnly.upperBound()).isNull();
assertThat(lowerBoundOnly.stride()).isNull();
assertThat(lowerBoundOnly.strideSeparator()).isNull();
SliceItem upperBoundOnly = parse(":a", treeMaker::sliceItem);
assertThat(upperBoundOnly.lowerBound()).isNull();
assertThat(((Name) upperBoundOnly.upperBound()).name()).isEqualTo("a");
assertThat(upperBoundOnly.stride()).isNull();
assertThat(upperBoundOnly.strideSeparator()).isNull();
SliceItem strideOnly = parse("::a", treeMaker::sliceItem);
assertThat(strideOnly.lowerBound()).isNull();
assertThat(strideOnly.upperBound()).isNull();
assertThat(((Name) strideOnly.stride()).name()).isEqualTo("a");
assertThat(strideOnly.strideSeparator()).isNotNull();
SliceItem strideContainingOnlyColon = parse("::", treeMaker::sliceItem);
assertThat(strideContainingOnlyColon.lowerBound()).isNull();
assertThat(strideContainingOnlyColon.upperBound()).isNull();
assertThat(strideContainingOnlyColon.strideSeparator()).isNotNull();
}
Aggregations