Search in sources :

Example 16 with Name

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

the class MatchStatementSymbolsTest method or_pattern.

@Test
public void or_pattern() {
    OrPattern orPattern = pattern("class A: ...", "class B: ...", "def foo(value):", "  match(value):", "    case A() | B(): ...");
    Symbol symbolA = ((Name) ((ClassPattern) orPattern.patterns().get(0)).targetClass()).symbol();
    Symbol symbolB = ((Name) ((ClassPattern) orPattern.patterns().get(1)).targetClass()).symbol();
    assertThat(symbolA.usages()).extracting(Usage::kind).containsExactly(CLASS_DECLARATION, OTHER);
    assertThat(symbolB.usages()).extracting(Usage::kind).containsExactly(CLASS_DECLARATION, OTHER);
}
Also used : Symbol(org.sonar.plugins.python.api.symbols.Symbol) OrPattern(org.sonar.plugins.python.api.tree.OrPattern) Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Example 17 with Name

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

the class MatchStatementSymbolsTest method class_pattern.

@Test
public void class_pattern() {
    ClassPattern classPattern = pattern("class A:", "  foo = 42", "def foo(value):", "  match(value):", "    case A(foo=x):", "      print(x)");
    Name name = (Name) classPattern.targetClass();
    assertThat(name.symbol().kind()).isEqualTo(Symbol.Kind.CLASS);
    KeywordPattern keywordPattern = ((KeywordPattern) classPattern.arguments().get(0));
    Symbol x = ((CapturePattern) keywordPattern.pattern()).name().symbol();
    assertThat(x.usages()).extracting(Usage::kind).containsExactly(PATTERN_DECLARATION, OTHER);
    classPattern = pattern("import mod", "def foo(value):", "  match(value):", "    case mod.A(foo=x):", "      print(x)");
    QualifiedExpression qualifiedExpression = (QualifiedExpression) classPattern.targetClass();
    assertThat(qualifiedExpression.symbol().kind()).isEqualTo(Symbol.Kind.OTHER);
}
Also used : QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassPattern(org.sonar.plugins.python.api.tree.ClassPattern) KeywordPattern(org.sonar.plugins.python.api.tree.KeywordPattern) Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Example 18 with Name

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

the class ReachingDefinitionsAnalysisTest method valuesAtLocation_branches.

@Test
public void valuesAtLocation_branches() {
    Name x = (Name) lastExpressionInFunction("if p:", "  x = 1", "else:", "  x = 2", "x");
    assertThat(analysis.valuesAtLocation(x)).extracting(ReachingDefinitionsAnalysisTest::getValueAsString).containsExactlyInAnyOrder("1", "2");
}
Also used : Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Example 19 with Name

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

the class ReachingDefinitionsAnalysisTest method try_stmt.

@Test
public void try_stmt() {
    Name x = (Name) lastExpressionInFunction("x = 1", "try:", "  x = 2", "except:", "  pass", "x");
    assertThat(analysis.valuesAtLocation(x)).isEmpty();
}
Also used : Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

Example 20 with Name

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

the class ReachingDefinitionsAnalysisTest method valuesAtLocation_single_assignment.

@Test
public void valuesAtLocation_single_assignment() {
    Name x = (Name) lastExpressionInFunction("x = 42; x");
    assertThat(analysis.valuesAtLocation(x)).extracting(ReachingDefinitionsAnalysisTest::getValueAsString).containsExactly("42");
}
Also used : Name(org.sonar.plugins.python.api.tree.Name) Test(org.junit.Test)

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