|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2024 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.semantic.v2; |
| 21 | + |
| 22 | +import org.assertj.core.api.Assertions; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.sonar.python.semantic.ProjectLevelSymbolTable; |
| 25 | +import org.sonar.python.types.v2.ClassType; |
| 26 | +import org.sonar.python.types.v2.ModuleType; |
| 27 | +import org.sonar.python.types.v2.PythonType; |
| 28 | +import org.sonar.python.types.v2.TriBool; |
| 29 | + |
| 30 | +class ProjectLevelTypeTableTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void getBuiltinTypeTest() { |
| 34 | + var symbolTable = ProjectLevelSymbolTable.empty(); |
| 35 | + var table = new ProjectLevelTypeTable(symbolTable, new TypeShed(symbolTable)); |
| 36 | + |
| 37 | + var listClassType = table.getType("list"); |
| 38 | + Assertions.assertThat(listClassType).isNotNull().isInstanceOf(ClassType.class); |
| 39 | + |
| 40 | + var builtinsModuleType = table.getType(); |
| 41 | + Assertions.assertThat(builtinsModuleType).isNotNull().isInstanceOf(ModuleType.class); |
| 42 | + Assertions.assertThat(builtinsModuleType.resolveMember("list")).isPresent().containsSame(listClassType); |
| 43 | + |
| 44 | + Assertions.assertThat(table.getType("list.something")).isSameAs(PythonType.UNKNOWN); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void getTypeshedTypeTest() { |
| 49 | + var symbolTable = ProjectLevelSymbolTable.empty(); |
| 50 | + var table = new ProjectLevelTypeTable(symbolTable, new TypeShed(symbolTable)); |
| 51 | + |
| 52 | + var generatorClassType = table.getType("typing.Generator"); |
| 53 | + Assertions.assertThat(generatorClassType).isNotNull().isInstanceOf(ClassType.class); |
| 54 | + |
| 55 | + var typingModuleType = table.getType("typing"); |
| 56 | + Assertions.assertThat(typingModuleType).isNotNull().isInstanceOf(ModuleType.class); |
| 57 | + Assertions.assertThat(typingModuleType.resolveMember("Generator")).isPresent().containsSame(generatorClassType); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void updateTypeTableDuringGetTypeTest() { |
| 62 | + var symbolTable = ProjectLevelSymbolTable.empty(); |
| 63 | + var table = new ProjectLevelTypeTable(symbolTable, new TypeShed(symbolTable)); |
| 64 | + |
| 65 | + var rootModuleType = table.getType(); |
| 66 | + Assertions.assertThat(rootModuleType.hasMember("typing")).isNotEqualTo(TriBool.TRUE); |
| 67 | + |
| 68 | + var generatorClassType = table.getType("typing.Generator"); |
| 69 | + Assertions.assertThat(generatorClassType).isNotNull().isInstanceOf(ClassType.class); |
| 70 | + |
| 71 | + var typingModuleType = table.getType("typing"); |
| 72 | + Assertions.assertThat(typingModuleType).isNotNull().isInstanceOf(ModuleType.class); |
| 73 | + Assertions.assertThat(typingModuleType.resolveMember("Generator")).isPresent().containsSame(generatorClassType); |
| 74 | + |
| 75 | + Assertions.assertThat(rootModuleType.hasMember("typing")).isEqualTo(TriBool.TRUE); |
| 76 | + Assertions.assertThat(rootModuleType.resolveMember("typing")).containsSame(typingModuleType); |
| 77 | + } |
| 78 | +} |
0 commit comments