use of org.sonar.plugins.python.api.tree.ImportName in project sonar-python by SonarSource.
the class PythonTreeMakerTest method importStatement.
@Test
public void importStatement() {
setRootRule(PythonGrammar.IMPORT_STMT);
AstNode astNode = p.parse("import foo");
StatementWithSeparator statementWithSeparator = new StatementWithSeparator(astNode, null);
ImportName importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
assertThat(importStatement.firstToken().value()).isEqualTo("import");
assertThat(importStatement.lastToken().value()).isEqualTo("foo");
assertThat(importStatement).isNotNull();
assertThat(importStatement.importKeyword().value()).isEqualTo("import");
assertThat(importStatement.modules()).hasSize(1);
AliasedName importedName1 = importStatement.modules().get(0);
assertThat(importedName1.dottedName().names()).hasSize(1);
assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
assertThat(importStatement.children()).hasSize(2);
astNode = p.parse("import foo as f");
statementWithSeparator = new StatementWithSeparator(astNode, null);
importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
assertThat(importStatement).isNotNull();
assertThat(importStatement.firstToken().value()).isEqualTo("import");
assertThat(importStatement.lastToken().value()).isEqualTo("f");
assertThat(importStatement.importKeyword().value()).isEqualTo("import");
assertThat(importStatement.modules()).hasSize(1);
importedName1 = importStatement.modules().get(0);
assertThat(importedName1.firstToken().value()).isEqualTo("foo");
assertThat(importedName1.lastToken().value()).isEqualTo("f");
assertThat(importedName1.dottedName().names()).hasSize(1);
assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
assertThat(importedName1.asKeyword().value()).isEqualTo("as");
assertThat(importedName1.alias().name()).isEqualTo("f");
assertThat(importStatement.children()).hasSize(2);
astNode = p.parse("import foo.bar");
statementWithSeparator = new StatementWithSeparator(astNode, null);
importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
assertThat(importStatement).isNotNull();
assertThat(importStatement.importKeyword().value()).isEqualTo("import");
assertThat(importStatement.modules()).hasSize(1);
importedName1 = importStatement.modules().get(0);
assertThat(importedName1.dottedName().names()).hasSize(2);
assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
assertThat(importedName1.dottedName().names().get(1).name()).isEqualTo("bar");
assertThat(importStatement.children()).hasSize(2);
astNode = p.parse("import foo, bar");
statementWithSeparator = new StatementWithSeparator(astNode, null);
importStatement = (ImportName) treeMaker.importStatement(statementWithSeparator);
assertThat(importStatement).isNotNull();
assertThat(importStatement.importKeyword().value()).isEqualTo("import");
assertThat(importStatement.modules()).hasSize(2);
importedName1 = importStatement.modules().get(0);
assertThat(importedName1.dottedName().names()).hasSize(1);
assertThat(importedName1.dottedName().names().get(0).name()).isEqualTo("foo");
AliasedName importedName2 = importStatement.modules().get(1);
assertThat(importedName2.dottedName().names()).hasSize(1);
assertThat(importedName2.dottedName().names().get(0).name()).isEqualTo("bar");
assertThat(importStatement.children()).hasSize(3);
}
Aggregations