Search in sources :

Example 26 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class ClassSymbolTest method unpacking_expression_as_parent.

@Test
public void unpacking_expression_as_parent() {
    FileInput fileInput = parse("foo = (Something, SomethingElse)", "class C(*foo): ", "  pass");
    ClassDef classDef = (ClassDef) fileInput.statements().statements().get(1);
    Symbol symbol = classDef.name().symbol();
    assertThat(symbol instanceof ClassSymbol).isTrue();
    assertThat(symbol.kind().equals(Symbol.Kind.CLASS)).isTrue();
    ClassSymbol classSymbol = (ClassSymbol) symbol;
    assertThat(classSymbol.superClasses()).hasSize(0);
    assertThat(classSymbol.hasUnresolvedTypeHierarchy()).isTrue();
}
Also used : ClassDef(org.sonar.plugins.python.api.tree.ClassDef) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FileInput(org.sonar.plugins.python.api.tree.FileInput) Test(org.junit.Test)

Example 27 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class ClassSymbolTest method parent_has_multiple_bindings.

@Test
public void parent_has_multiple_bindings() {
    FileInput fileInput = parse("class C: ", "  pass", "C = \"hello\"", "class B(C): ", "  pass");
    ClassDef classDef = (ClassDef) fileInput.statements().statements().get(2);
    Symbol symbol = classDef.name().symbol();
    assertThat(symbol instanceof ClassSymbol).isTrue();
    assertThat(symbol.kind().equals(Symbol.Kind.CLASS)).isTrue();
    ClassSymbol classSymbol = (ClassSymbol) symbol;
    assertThat(classSymbol.hasUnresolvedTypeHierarchy()).isTrue();
}
Also used : ClassDef(org.sonar.plugins.python.api.tree.ClassDef) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FileInput(org.sonar.plugins.python.api.tree.FileInput) Test(org.junit.Test)

Example 28 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class ClassSymbolTest method class_members_empty.

@Test
public void class_members_empty() {
    ClassSymbol symbol = lastClassSymbol("class C: ", "  pass");
    assertThat(symbol.declaredMembers()).isEmpty();
}
Also used : ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Test(org.junit.Test)

Example 29 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class ClassSymbolTest method parent_is_not_a_class.

@Test
public void parent_is_not_a_class() {
    FileInput fileInput = parse("def foo():", "  pass", "A = foo()", "class C(A): ", "  pass");
    ClassDef classDef = (ClassDef) fileInput.statements().statements().get(2);
    Symbol symbol = classDef.name().symbol();
    assertThat(symbol instanceof ClassSymbol).isTrue();
    assertThat(symbol.kind().equals(Symbol.Kind.CLASS)).isTrue();
    ClassSymbol classSymbol = (ClassSymbol) symbol;
    assertThat(classSymbol.superClasses()).hasSize(1);
    assertThat(classSymbol.hasUnresolvedTypeHierarchy()).isTrue();
}
Also used : ClassDef(org.sonar.plugins.python.api.tree.ClassDef) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FileInput(org.sonar.plugins.python.api.tree.FileInput) Test(org.junit.Test)

Example 30 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class ClassSymbolTest method builtin_parent_with_unknown.

@Test
public void builtin_parent_with_unknown() {
    FileInput fileInput = parse("class C: ", "  pass", "class B(C, BaseException, unknown): ", "  pass");
    ClassDef classDef = (ClassDef) fileInput.statements().statements().get(1);
    Symbol symbol = classDef.name().symbol();
    assertThat(symbol instanceof ClassSymbol).isTrue();
    assertThat(symbol.kind().equals(Symbol.Kind.CLASS)).isTrue();
    ClassSymbol classSymbol = (ClassSymbol) symbol;
    assertThat(classSymbol.superClasses()).hasSize(2);
    assertThat(classSymbol.hasUnresolvedTypeHierarchy()).isTrue();
}
Also used : ClassDef(org.sonar.plugins.python.api.tree.ClassDef) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FileInput(org.sonar.plugins.python.api.tree.FileInput) Test(org.junit.Test)

Aggregations

ClassSymbol (org.sonar.plugins.python.api.symbols.ClassSymbol)87 Test (org.junit.Test)64 Symbol (org.sonar.plugins.python.api.symbols.Symbol)55 AmbiguousSymbol (org.sonar.plugins.python.api.symbols.AmbiguousSymbol)48 FileInput (org.sonar.plugins.python.api.tree.FileInput)36 FunctionSymbol (org.sonar.plugins.python.api.symbols.FunctionSymbol)29 ClassDef (org.sonar.plugins.python.api.tree.ClassDef)29 ClassSymbolImpl (org.sonar.python.semantic.ClassSymbolImpl)13 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 Set (java.util.Set)11 List (java.util.List)10 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)10 SymbolImpl (org.sonar.python.semantic.SymbolImpl)9 Collections (java.util.Collections)7 HasSymbol (org.sonar.plugins.python.api.tree.HasSymbol)7 InferredType (org.sonar.plugins.python.api.types.InferredType)7 Arrays (java.util.Arrays)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6