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