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