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