Search in sources :

Example 1 with ComprehensionIf

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

the class PythonTreeMakerTest method list_comprehension_with_if.

@Test
public void list_comprehension_with_if() {
    setRootRule(PythonGrammar.TEST);
    ComprehensionExpression comprehension = (ComprehensionExpression) parse("[x+1 for x in [42, 43] if x%2==0]", treeMaker::expression);
    assertThat(comprehension.getKind()).isEqualTo(Tree.Kind.LIST_COMPREHENSION);
    ComprehensionFor forClause = comprehension.comprehensionFor();
    assertThat(forClause.nestedClause().getKind()).isEqualTo(Tree.Kind.COMP_IF);
    ComprehensionIf ifClause = (ComprehensionIf) forClause.nestedClause();
    assertThat(ifClause.ifToken().value()).isEqualTo("if");
    assertThat(ifClause.condition().getKind()).isEqualTo(Tree.Kind.COMPARISON);
    assertThat(ifClause.nestedClause()).isNull();
    assertThat(ifClause.children()).hasSize(2);
}
Also used : ComprehensionFor(org.sonar.plugins.python.api.tree.ComprehensionFor) ComprehensionIf(org.sonar.plugins.python.api.tree.ComprehensionIf) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Aggregations

Test (org.junit.Test)1 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)1 ComprehensionFor (org.sonar.plugins.python.api.tree.ComprehensionFor)1 ComprehensionIf (org.sonar.plugins.python.api.tree.ComprehensionIf)1 RuleTest (org.sonar.python.parser.RuleTest)1