-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARPY-2262 Introduce the TypesTable interface
- Loading branch information
1 parent
2babf83
commit 65d7e58
Showing
11 changed files
with
192 additions
and
24 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
python-frontend/src/main/java/org/sonar/python/semantic/v2/BasicTypeTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.python.semantic.v2; | ||
|
||
import java.util.List; | ||
import org.sonar.python.types.v2.PythonType; | ||
import org.sonar.python.types.v2.UnknownType; | ||
|
||
public class BasicTypeTable implements TypeTable { | ||
@Override | ||
public PythonType getBuiltinsModule() { | ||
return new UnknownType.UnresolvedImportType(""); | ||
} | ||
|
||
@Override | ||
public PythonType getType(String typeFqn) { | ||
return new UnknownType.UnresolvedImportType(typeFqn); | ||
} | ||
|
||
@Override | ||
public PythonType getType(String... typeFqnParts) { | ||
return new UnknownType.UnresolvedImportType(String.join(".", typeFqnParts)); | ||
} | ||
|
||
@Override | ||
public PythonType getType(List<String> typeFqnParts) { | ||
return new UnknownType.UnresolvedImportType(String.join(".", typeFqnParts)); | ||
} | ||
|
||
@Override | ||
public PythonType getModuleType(List<String> typeFqnParts) { | ||
return new UnknownType.UnresolvedImportType(String.join(".", typeFqnParts)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
python-frontend/src/main/java/org/sonar/python/semantic/v2/TypeTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.python.semantic.v2; | ||
|
||
import java.util.List; | ||
import org.sonar.python.types.v2.PythonType; | ||
|
||
public interface TypeTable { | ||
PythonType getBuiltinsModule(); | ||
|
||
PythonType getType(String typeFqn); | ||
|
||
PythonType getType(String... typeFqnParts); | ||
|
||
PythonType getType(List<String> typeFqnParts); | ||
|
||
PythonType getModuleType(List<String> typeFqnParts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
python-frontend/src/test/java/org/sonar/python/semantic/v2/BasicTypeTableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.python.semantic.v2; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import org.sonar.python.types.v2.PythonType; | ||
import org.sonar.python.types.v2.UnknownType; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class BasicTypeTableTest { | ||
|
||
private final BasicTypeTable basicTypeTable = new BasicTypeTable(); | ||
|
||
@Test | ||
void testGetBuiltinsModule() { | ||
PythonType builtinsModuleType = basicTypeTable.getBuiltinsModule(); | ||
assertThat(builtinsModuleType).isInstanceOf(UnknownType.UnresolvedImportType.class); | ||
assertResolvedMember(builtinsModuleType, "foo", "foo"); | ||
} | ||
|
||
@Test | ||
void testGetTypeFromTypeFqn() { | ||
PythonType fooType = basicTypeTable.getType("foo"); | ||
assertThat(fooType).isInstanceOf(UnknownType.UnresolvedImportType.class); | ||
assertResolvedMember(fooType, "bar", "foo.bar"); | ||
} | ||
|
||
@Test | ||
void testGetTypeFromTypeFqnParts() { | ||
PythonType fooType = basicTypeTable.getType("foo", "bar"); | ||
assertThat(fooType).isInstanceOf(UnknownType.UnresolvedImportType.class); | ||
assertResolvedMember(fooType, "qux", "foo.bar.qux"); | ||
} | ||
|
||
@Test | ||
void testGetTypeFromTypeFqnList() { | ||
PythonType fooType = basicTypeTable.getType(List.of("foo", "bar")); | ||
assertThat(fooType).isInstanceOf(UnknownType.UnresolvedImportType.class); | ||
assertResolvedMember(fooType, "qux", "foo.bar.qux"); | ||
} | ||
|
||
@Test | ||
void testGetModuleType() { | ||
PythonType fooType = basicTypeTable.getModuleType(List.of("foo", "bar")); | ||
assertThat(fooType).isInstanceOf(UnknownType.UnresolvedImportType.class); | ||
assertResolvedMember(fooType, "qux", "foo.bar.qux"); | ||
} | ||
|
||
private static void assertResolvedMember(PythonType builtinsModuleType, String memberName, String resolvedMemberName) { | ||
Optional<PythonType> resolvedMemberType = builtinsModuleType.resolveMember(memberName); | ||
assertThat(resolvedMemberType).isPresent(); | ||
assertThat(resolvedMemberType.get()).isInstanceOf(UnknownType.UnresolvedImportType.class); | ||
assertThat(((UnknownType.UnresolvedImportType) resolvedMemberType.get()).importPath()).isEqualTo(resolvedMemberName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters