Search in sources :

Example 1 with Name

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);
}
Also used : CompoundAssignmentStatement(org.sonar.plugins.python.api.tree.CompoundAssignmentStatement) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) ReprExpression(org.sonar.plugins.python.api.tree.ReprExpression) IsExpression(org.sonar.plugins.python.api.tree.IsExpression) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) EllipsisExpression(org.sonar.plugins.python.api.tree.EllipsisExpression) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) UnpackingExpression(org.sonar.plugins.python.api.tree.UnpackingExpression) NoneExpression(org.sonar.plugins.python.api.tree.NoneExpression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) DictCompExpression(org.sonar.plugins.python.api.tree.DictCompExpression) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) Expression(org.sonar.plugins.python.api.tree.Expression) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) InExpression(org.sonar.plugins.python.api.tree.InExpression) AwaitExpression(org.sonar.plugins.python.api.tree.AwaitExpression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) ExpressionList(org.sonar.plugins.python.api.tree.ExpressionList) AstNode(com.sonar.sslr.api.AstNode) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) ImportName(org.sonar.plugins.python.api.tree.ImportName) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 2 with 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);
    }
}
Also used : ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) IfStatement(org.sonar.plugins.python.api.tree.IfStatement) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) ReprExpression(org.sonar.plugins.python.api.tree.ReprExpression) IsExpression(org.sonar.plugins.python.api.tree.IsExpression) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) EllipsisExpression(org.sonar.plugins.python.api.tree.EllipsisExpression) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) UnpackingExpression(org.sonar.plugins.python.api.tree.UnpackingExpression) NoneExpression(org.sonar.plugins.python.api.tree.NoneExpression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) DictCompExpression(org.sonar.plugins.python.api.tree.DictCompExpression) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) Expression(org.sonar.plugins.python.api.tree.Expression) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) InExpression(org.sonar.plugins.python.api.tree.InExpression) AwaitExpression(org.sonar.plugins.python.api.tree.AwaitExpression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) Token(org.sonar.plugins.python.api.tree.Token) RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) AstNode(com.sonar.sslr.api.AstNode) RecognitionException(com.sonar.sslr.api.RecognitionException) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) ImportName(org.sonar.plugins.python.api.tree.ImportName) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 3 with Name

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);
}
Also used : AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) ExpressionList(org.sonar.plugins.python.api.tree.ExpressionList) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) ImportName(org.sonar.plugins.python.api.tree.ImportName) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 4 with Name

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");
}
Also used : AstNode(com.sonar.sslr.api.AstNode) Arrays(java.util.Arrays) CompoundAssignmentStatement(org.sonar.plugins.python.api.tree.CompoundAssignmentStatement) ReprExpression(org.sonar.plugins.python.api.tree.ReprExpression) NonlocalStatement(org.sonar.plugins.python.api.tree.NonlocalStatement) IfStatement(org.sonar.plugins.python.api.tree.IfStatement) IsExpression(org.sonar.plugins.python.api.tree.IsExpression) ExceptClause(org.sonar.plugins.python.api.tree.ExceptClause) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Kind(org.sonar.plugins.python.api.tree.Tree.Kind) AssertStatement(org.sonar.plugins.python.api.tree.AssertStatement) Decorator(org.sonar.plugins.python.api.tree.Decorator) ImportFrom(org.sonar.plugins.python.api.tree.ImportFrom) Name(org.sonar.plugins.python.api.tree.Name) ReturnStatement(org.sonar.plugins.python.api.tree.ReturnStatement) Map(java.util.Map) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) MatchStatement(org.sonar.plugins.python.api.tree.MatchStatement) Statement(org.sonar.plugins.python.api.tree.Statement) ExecStatement(org.sonar.plugins.python.api.tree.ExecStatement) TupleParameter(org.sonar.plugins.python.api.tree.TupleParameter) ContinueStatement(org.sonar.plugins.python.api.tree.ContinueStatement) SliceItem(org.sonar.plugins.python.api.tree.SliceItem) PythonTokenType(org.sonar.python.api.PythonTokenType) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) ListLiteral(org.sonar.plugins.python.api.tree.ListLiteral) StandardCharsets(java.nio.charset.StandardCharsets) DelStatement(org.sonar.plugins.python.api.tree.DelStatement) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) ExpressionList(org.sonar.plugins.python.api.tree.ExpressionList) Assertions.fail(org.assertj.core.api.Assertions.fail) EllipsisExpression(org.sonar.plugins.python.api.tree.EllipsisExpression) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) Trivia(org.sonar.plugins.python.api.tree.Trivia) ForStatement(org.sonar.plugins.python.api.tree.ForStatement) RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) PythonGrammar(org.sonar.python.api.PythonGrammar) YieldStatement(org.sonar.plugins.python.api.tree.YieldStatement) WithStatement(org.sonar.plugins.python.api.tree.WithStatement) ArgList(org.sonar.plugins.python.api.tree.ArgList) UnpackingExpression(org.sonar.plugins.python.api.tree.UnpackingExpression) NoneExpression(org.sonar.plugins.python.api.tree.NoneExpression) GlobalStatement(org.sonar.plugins.python.api.tree.GlobalStatement) PythonPunctuator(org.sonar.python.api.PythonPunctuator) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) FormatSpecifier(org.sonar.plugins.python.api.tree.FormatSpecifier) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) KeyValuePair(org.sonar.plugins.python.api.tree.KeyValuePair) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) ImportName(org.sonar.plugins.python.api.tree.ImportName) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) TryStatement(org.sonar.plugins.python.api.tree.TryStatement) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) Tree(org.sonar.plugins.python.api.tree.Tree) TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) DictionaryLiteral(org.sonar.plugins.python.api.tree.DictionaryLiteral) ImportStatement(org.sonar.plugins.python.api.tree.ImportStatement) DictCompExpression(org.sonar.plugins.python.api.tree.DictCompExpression) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) AnnotatedAssignment(org.sonar.plugins.python.api.tree.AnnotatedAssignment) ElseClause(org.sonar.plugins.python.api.tree.ElseClause) SetLiteral(org.sonar.plugins.python.api.tree.SetLiteral) Expression(org.sonar.plugins.python.api.tree.Expression) List(java.util.List) FileInput(org.sonar.plugins.python.api.tree.FileInput) Tuple(org.sonar.plugins.python.api.tree.Tuple) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) PassStatement(org.sonar.plugins.python.api.tree.PassStatement) NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) InExpression(org.sonar.plugins.python.api.tree.InExpression) HashMap(java.util.HashMap) ExpressionStatement(org.sonar.plugins.python.api.tree.ExpressionStatement) ComprehensionFor(org.sonar.plugins.python.api.tree.ComprehensionFor) ClassDef(org.sonar.plugins.python.api.tree.ClassDef) PrintStatement(org.sonar.plugins.python.api.tree.PrintStatement) RuleTest(org.sonar.python.parser.RuleTest) RecognitionException(com.sonar.sslr.api.RecognitionException) AwaitExpression(org.sonar.plugins.python.api.tree.AwaitExpression) Parameter(org.sonar.plugins.python.api.tree.Parameter) WithItem(org.sonar.plugins.python.api.tree.WithItem) RaiseStatement(org.sonar.plugins.python.api.tree.RaiseStatement) ComprehensionIf(org.sonar.plugins.python.api.tree.ComprehensionIf) StringElement(org.sonar.plugins.python.api.tree.StringElement) GenericTokenType(com.sonar.sslr.api.GenericTokenType) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) StatementList(org.sonar.plugins.python.api.tree.StatementList) Token(org.sonar.plugins.python.api.tree.Token) WhileStatement(org.sonar.plugins.python.api.tree.WhileStatement) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) BreakStatement(org.sonar.plugins.python.api.tree.BreakStatement) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) ImportName(org.sonar.plugins.python.api.tree.ImportName) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 5 with Name

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();
}
Also used : SliceItem(org.sonar.plugins.python.api.tree.SliceItem) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) ImportName(org.sonar.plugins.python.api.tree.ImportName) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Aggregations

Name (org.sonar.plugins.python.api.tree.Name)104 Test (org.junit.Test)44 Expression (org.sonar.plugins.python.api.tree.Expression)37 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)35 Symbol (org.sonar.plugins.python.api.symbols.Symbol)33 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)32 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)27 FunctionDef (org.sonar.plugins.python.api.tree.FunctionDef)24 Tree (org.sonar.plugins.python.api.tree.Tree)24 ImportName (org.sonar.plugins.python.api.tree.ImportName)22 RegularArgument (org.sonar.plugins.python.api.tree.RegularArgument)22 Token (org.sonar.plugins.python.api.tree.Token)20 List (java.util.List)18 AssignmentStatement (org.sonar.plugins.python.api.tree.AssignmentStatement)16 FileInput (org.sonar.plugins.python.api.tree.FileInput)15 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)14 RuleTest (org.sonar.python.parser.RuleTest)14 Optional (java.util.Optional)13 Set (java.util.Set)13 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)13