Search in sources :

Example 11 with ClassSymbol

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

the class InferredTypesTest method assertAliasedTypeAnnotation.

private void assertAliasedTypeAnnotation(String type, String... code) {
    TypeAnnotation typeAnnotation = typeAnnotation(code);
    ClassSymbol typeClass = typeShedClass(type);
    assertThat(fromTypeshedTypeAnnotation(typeAnnotation)).isEqualTo(runtimeType(typeClass));
    assertThat(typeSymbols(fromTypeAnnotation(typeAnnotation))).containsExactly(typeClass);
}
Also used : InferredTypes.fromTypeshedTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeshedTypeAnnotation) InferredTypes.fromTypeAnnotation(org.sonar.python.types.InferredTypes.fromTypeAnnotation) TypeAnnotation(org.sonar.plugins.python.api.tree.TypeAnnotation) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol)

Example 12 with ClassSymbol

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

the class InferredTypesTest method test_typeSymbol.

@Test
public void test_typeSymbol() {
    ClassSymbol str = typeShedClass("str");
    assertThat(InferredTypes.typeSymbols(STR)).containsExactly(str);
    ClassSymbol a = new ClassSymbolImpl("A", "mod.A");
    assertThat(InferredTypes.typeSymbols(new RuntimeType(a))).containsExactly(a);
    assertThat(InferredTypes.typeSymbols(or(STR, INT))).containsExactlyInAnyOrder(str, typeShedClass("int"));
    assertThat(InferredTypes.typeSymbols(InferredTypes.anyType())).isEmpty();
    assertThat(InferredTypes.typeSymbols(declaredType(str))).containsExactly(str);
    assertThat(InferredTypes.typeSymbols(declaredType(new SymbolImpl("foo", "foo.bar")))).isEmpty();
}
Also used : SymbolImpl(org.sonar.python.semantic.SymbolImpl) AmbiguousSymbolImpl(org.sonar.python.semantic.AmbiguousSymbolImpl) ClassSymbolImpl(org.sonar.python.semantic.ClassSymbolImpl) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) ClassSymbolImpl(org.sonar.python.semantic.ClassSymbolImpl) Test(org.junit.Test)

Example 13 with ClassSymbol

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

the class RuntimeTypeTest method test_isCompatibleWith.

@Test
public void test_isCompatibleWith() {
    ClassSymbolImpl x1 = new ClassSymbolImpl("x1", "x1");
    ClassSymbolImpl x2 = new ClassSymbolImpl("x2", "x2");
    x2.addSuperClass(x1);
    assertThat(new RuntimeType(x2).isCompatibleWith(new RuntimeType(x1))).isTrue();
    assertThat(new RuntimeType(x1).isCompatibleWith(new RuntimeType(x1))).isTrue();
    assertThat(new RuntimeType(x1).isCompatibleWith(new RuntimeType(x2))).isTrue();
    assertThat(new RuntimeType(x2).isCompatibleWith(new DeclaredType(x1))).isTrue();
    assertThat(new RuntimeType(x1).isCompatibleWith(new DeclaredType(x1))).isTrue();
    assertThat(new RuntimeType(x1).isCompatibleWith(new DeclaredType(x2))).isTrue();
    assertThat(new RuntimeType(x1).isCompatibleWith(new DeclaredType(new SymbolImpl("foo", "foo")))).isTrue();
    ClassSymbolImpl a = new ClassSymbolImpl("a", null);
    ClassSymbolImpl b = new ClassSymbolImpl("b", "b");
    assertThat(new RuntimeType(a).isCompatibleWith(new RuntimeType(b))).isTrue();
    assertThat(new RuntimeType(b).isCompatibleWith(new RuntimeType(a))).isTrue();
    ClassSymbolImpl y = new ClassSymbolImpl("y", "y");
    ClassSymbolImpl z = new ClassSymbolImpl("z", "z");
    y.addSuperClass(new SymbolImpl("unknown", null));
    assertThat(new RuntimeType(y).isCompatibleWith(new RuntimeType(z))).isTrue();
    FileInput fileInput = PythonTestUtils.parse(new SymbolTableBuilder("animals", PythonTestUtils.pythonFile("foo")), "class duck:", "  def swim(): ...", "  def quack(): ...", "class goose:", "  def swim(): ...");
    ClassDef duckClass = PythonTestUtils.getFirstDescendant(fileInput, tree -> tree.is(Tree.Kind.CLASSDEF));
    ClassDef gooseClass = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Tree.Kind.CLASSDEF));
    ClassSymbol duck = TreeUtils.getClassSymbolFromDef(duckClass);
    ClassSymbol goose = TreeUtils.getClassSymbolFromDef(gooseClass);
    assertThat(new RuntimeType(duck).isCompatibleWith(new RuntimeType(goose))).isTrue();
    assertThat(new RuntimeType(goose).isCompatibleWith(new RuntimeType(duck))).isFalse();
}
Also used : SymbolImpl(org.sonar.python.semantic.SymbolImpl) ClassSymbolImpl(org.sonar.python.semantic.ClassSymbolImpl) ClassDef(org.sonar.plugins.python.api.tree.ClassDef) ClassSymbolImpl(org.sonar.python.semantic.ClassSymbolImpl) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) SymbolTableBuilder(org.sonar.python.semantic.SymbolTableBuilder) FileInput(org.sonar.plugins.python.api.tree.FileInput) Test(org.junit.Test)

Example 14 with ClassSymbol

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

the class TypeInferenceTest method call_expression.

@Test
public void call_expression() {
    assertThat(lastExpression("f()").type()).isEqualTo(anyType());
    assertThat(lastExpression("def f(): pass", "f()").type()).isEqualTo(anyType());
    CallExpression expression = (CallExpression) lastExpression("class A: pass", "A()");
    assertThat(expression.calleeSymbol().fullyQualifiedName()).isEqualTo("mod1.A");
    assertThat(expression.type()).isEqualTo(runtimeType((ClassSymbol) expression.calleeSymbol()));
}
Also used : ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Test(org.junit.Test)

Example 15 with ClassSymbol

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

the class ProjectLevelSymbolTableTest method builtin_symbol_in_super_class.

@Test
public void builtin_symbol_in_super_class() {
    ClassSymbolImpl classASymbol = new ClassSymbolImpl("A", "mod1.A");
    classASymbol.addSuperClass(new SymbolImpl("BaseException", "BaseException"));
    FileInput tree = parse(new SymbolTableBuilder("my_package", pythonFile("my_module.py"), from(Collections.singletonMap("mod1", Collections.singleton(classASymbol)))), "from mod1 import A");
    Symbol importedASymbol = tree.globalVariables().iterator().next();
    assertThat(importedASymbol.kind()).isEqualTo(Symbol.Kind.CLASS);
    ClassSymbol classA = (ClassSymbol) importedASymbol;
    assertThat(classA.hasUnresolvedTypeHierarchy()).isFalse();
    assertThat(classA.superClasses()).hasSize(1);
    assertThat(classA.superClasses().get(0).kind()).isEqualTo(Symbol.Kind.CLASS);
}
Also used : AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) 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)88 Test (org.junit.Test)64 Symbol (org.sonar.plugins.python.api.symbols.Symbol)56 AmbiguousSymbol (org.sonar.plugins.python.api.symbols.AmbiguousSymbol)49 FileInput (org.sonar.plugins.python.api.tree.FileInput)36 FunctionSymbol (org.sonar.plugins.python.api.symbols.FunctionSymbol)30 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 HasSymbol (org.sonar.plugins.python.api.tree.HasSymbol)8 Collections (java.util.Collections)7 InferredType (org.sonar.plugins.python.api.types.InferredType)7 Arrays (java.util.Arrays)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6