Search in sources :

Example 1 with StringElement

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

the class PythonTreeMakerTest method string_interpolation_nested_expressions_in_format_specifier.

@Test
public void string_interpolation_nested_expressions_in_format_specifier() {
    setRootRule(PythonGrammar.ATOM);
    Expression exp = parse("f'{3.1416:{width}.{prec * 5}}'", treeMaker::expression);
    StringLiteral stringLiteral = (StringLiteral) exp;
    assertThat(stringLiteral.stringElements()).hasSize(1);
    StringElement elmt = stringLiteral.stringElements().get(0);
    assertThat(elmt.isInterpolated()).isTrue();
    assertThat(elmt.formattedExpressions()).hasSize(1);
    FormattedExpression formattedExpression = elmt.formattedExpressions().get(0);
    FormatSpecifier formatSpecifier = formattedExpression.formatSpecifier();
    assertThat(formatSpecifier).isNotNull();
    assertThat(formatSpecifier.getKind()).isEqualTo(Kind.FORMAT_SPECIFIER);
    assertThat(formatSpecifier.children()).hasSize(3);
    assertThat(formatSpecifier.formatExpressions()).hasSize(2);
    assertThat(formatSpecifier.formatExpressions().get(0).expression().is(Tree.Kind.NAME)).isTrue();
    assertThat(formatSpecifier.formatExpressions().get(1).expression().is(Kind.MULTIPLICATION)).isTrue();
}
Also used : FormatSpecifier(org.sonar.plugins.python.api.tree.FormatSpecifier) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) 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) StringElement(org.sonar.plugins.python.api.tree.StringElement) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 2 with StringElement

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

the class PythonTreeMakerTest method parseInterpolated.

private FormattedExpression parseInterpolated(String interpolatedExpr) {
    Expression exp = parse("f'" + interpolatedExpr + "'", treeMaker::expression);
    StringLiteral stringLiteral = (StringLiteral) exp;
    assertThat(stringLiteral.stringElements()).hasSize(1);
    StringElement elmt = stringLiteral.stringElements().get(0);
    assertThat(elmt.isInterpolated()).isTrue();
    assertThat(elmt.formattedExpressions()).extracting(FormattedExpression::expression).containsExactlyElementsOf(elmt.interpolatedExpressions());
    assertThat(elmt.formattedExpressions()).hasSize(1);
    return elmt.formattedExpressions().get(0);
}
Also used : StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) 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) StringElement(org.sonar.plugins.python.api.tree.StringElement)

Example 3 with StringElement

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

the class PythonTreeMakerTest method multiline_string_literal_expression.

@Test
public void multiline_string_literal_expression() {
    setRootRule(PythonGrammar.ATOM);
    Expression parse = parse("('Hello \\ ' #Noncompliant\n            'world')", treeMaker::expression);
    assertThat(parse.is(Tree.Kind.PARENTHESIZED)).isTrue();
    ParenthesizedExpression parenthesized = (ParenthesizedExpression) parse;
    assertThat(parenthesized.expression().is(Tree.Kind.STRING_LITERAL)).isTrue();
    StringLiteral pyStringLiteralTree = (StringLiteral) parenthesized.expression();
    assertThat(pyStringLiteralTree.children()).hasSize(2);
    assertThat(pyStringLiteralTree.stringElements().size()).isEqualTo(2);
    assertThat(pyStringLiteralTree.stringElements().get(0).value()).isEqualTo("\'Hello \\ '");
    StringElement firstElement = pyStringLiteralTree.stringElements().get(0);
    StringElement secondElement = pyStringLiteralTree.stringElements().get(1);
    assertThat(secondElement.value()).isEqualTo("'world'");
    assertThat(firstElement.trimmedQuotesValue()).isEqualTo("Hello \\ ");
    assertThat(secondElement.trimmedQuotesValue()).isEqualTo("world");
}
Also used : ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) 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) StringElement(org.sonar.plugins.python.api.tree.StringElement) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 4 with StringElement

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

the class PythonTreeMakerTest method assertStringLiteral.

private void assertStringLiteral(String fullValue, String trimmedQuoteValue, String prefix) {
    Expression parse = parse(fullValue, treeMaker::expression);
    assertThat(parse.is(Tree.Kind.STRING_LITERAL)).isTrue();
    StringLiteral stringLiteral = (StringLiteral) parse;
    assertThat(stringLiteral.stringElements()).hasSize(1);
    StringElement firstElement = stringLiteral.stringElements().get(0);
    assertThat(firstElement.value()).isEqualTo(fullValue);
    assertThat(firstElement.trimmedQuotesValue()).isEqualTo(trimmedQuoteValue);
    assertThat(firstElement.prefix()).isEqualTo(prefix);
}
Also used : StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) 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) StringElement(org.sonar.plugins.python.api.tree.StringElement)

Example 5 with StringElement

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

the class RegexParserTestUtils method makeSource.

public static RegexSource makeSource(String content) {
    FileInput inputFile = parse(String.format(PYTHON_CODE, content));
    StringElement pattern = getFirstDescendant(inputFile, tree -> tree.is(Tree.Kind.STRING_ELEMENT));
    return new PythonAnalyzerRegexSource(pattern);
}
Also used : StringElement(org.sonar.plugins.python.api.tree.StringElement) FileInput(org.sonar.plugins.python.api.tree.FileInput)

Aggregations

StringElement (org.sonar.plugins.python.api.tree.StringElement)15 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)8 Expression (org.sonar.plugins.python.api.tree.Expression)8 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)7 StringLiteral (org.sonar.plugins.python.api.tree.StringLiteral)7 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)7 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)6 AwaitExpression (org.sonar.plugins.python.api.tree.AwaitExpression)6 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)6 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)6 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)6 DictCompExpression (org.sonar.plugins.python.api.tree.DictCompExpression)6 EllipsisExpression (org.sonar.plugins.python.api.tree.EllipsisExpression)6 InExpression (org.sonar.plugins.python.api.tree.InExpression)6 IsExpression (org.sonar.plugins.python.api.tree.IsExpression)6 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)6 NoneExpression (org.sonar.plugins.python.api.tree.NoneExpression)6 ParenthesizedExpression (org.sonar.plugins.python.api.tree.ParenthesizedExpression)6 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)6 ReprExpression (org.sonar.plugins.python.api.tree.ReprExpression)6