Skip to content

Commit

Permalink
[23] Code completion for module imports
Browse files Browse the repository at this point in the history
integrate new test class in the suite, but isolate from similar tests

fixes eclipse-jdt#2823
  • Loading branch information
stephan-herrmann committed Aug 16, 2024
1 parent 77d287e commit fbc097c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

Expand All @@ -35,20 +36,23 @@ public class CompletionTests23 extends AbstractJavaModelCompletionTests {

private static int DEFAULT_RELEVANCE = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_RESTRICTED;

private IJavaProject completion23Project;

public CompletionTests23(String name) {
super(name);
}

@Override
public void setUpSuite() throws Exception {
if (COMPLETION_PROJECT == null) {
COMPLETION_PROJECT = setUpJavaProject("Completion23", "23", false);
} else {
setUpProjectCompliance(COMPLETION_PROJECT, "23", false);
}
COMPLETION_PROJECT.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
this.completion23Project = setUpJavaProject("Completion23", "23", false);
this.completion23Project.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
super.setUpSuite();
}
@Override
public void tearDownSuite() throws Exception {
deleteProject(this.completion23Project);
super.tearDownSuite();
}
public static Test suite() {
return buildModelTestSuite(CompletionTests23.class);
}
Expand Down Expand Up @@ -80,7 +84,7 @@ public class X {}
}
public void testKeyword_neg() throws JavaModelException {
// keyword not enabled, other proposals don't match
COMPLETION_PROJECT.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.DISABLED);
this.completion23Project.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.DISABLED);
try {
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy("/Completion23/src/p/X.java", """
Expand All @@ -98,7 +102,7 @@ public class X {}
"",
requestor.getResults());
} finally {
COMPLETION_PROJECT.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
this.completion23Project.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
}
}

Expand Down Expand Up @@ -130,7 +134,7 @@ public void test001_neg() throws JavaModelException {
// only java.base available (from JCL_23_LIB)
// preview JEP 476 not enabled
// other proposals don't match prefix
COMPLETION_PROJECT.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.DISABLED);
this.completion23Project.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.DISABLED);
try {
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy(
Expand All @@ -151,7 +155,7 @@ public class X {}
"",
requestor.getResults());
} finally {
COMPLETION_PROJECT.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
this.completion23Project.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
}
}

Expand Down Expand Up @@ -182,9 +186,9 @@ public void test003() throws JavaModelException {
// no prefix
// 2 modules available: mod.one & java.base
// unnamed module reads them all
IPath jarPath = COMPLETION_PROJECT.getPath().append("mod.one.jar");
IPath jarPath = this.completion23Project.getPath().append("mod.one.jar");
try {
addClasspathEntry(COMPLETION_PROJECT, newModularLibraryEntry(jarPath, null, null));
addClasspathEntry(this.completion23Project, newModularLibraryEntry(jarPath, null, null));
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy("/Completion23/src/p/X.java", """
package p;
Expand All @@ -202,19 +206,19 @@ public class X {}
"[MODULE_REF]{mod.one, mod.one, null, null, null, " + DEFAULT_RELEVANCE + "}",
requestor.getResults());
} finally {
removeClasspathEntry(COMPLETION_PROJECT, jarPath);
removeClasspathEntry(this.completion23Project, jarPath);
}
}

public void test004() throws JavaModelException {
// with prefix
// 3 modules on the module path: mod.two, mod.one & java.base
// prefix selects 2 out of 3
IPath jarOnePath = COMPLETION_PROJECT.getPath().append("mod.one.jar");
IPath jarTwoPath = COMPLETION_PROJECT.getPath().append("mod.two.jar");
IPath jarOnePath = this.completion23Project.getPath().append("mod.one.jar");
IPath jarTwoPath = this.completion23Project.getPath().append("mod.two.jar");
try {
addClasspathEntry(COMPLETION_PROJECT, newModularLibraryEntry(jarOnePath, null, null));
addClasspathEntry(COMPLETION_PROJECT, newModularLibraryEntry(jarTwoPath, null, null));
addClasspathEntry(this.completion23Project, newModularLibraryEntry(jarOnePath, null, null));
addClasspathEntry(this.completion23Project, newModularLibraryEntry(jarTwoPath, null, null));
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy("/Completion23/src/p/X.java", """
package p;
Expand All @@ -232,21 +236,21 @@ public class X {}
"[MODULE_REF]{mod.two, mod.two, null, null, null, " + DEFAULT_RELEVANCE + "}",
requestor.getResults());
} finally {
removeClasspathEntry(COMPLETION_PROJECT, jarOnePath);
removeClasspathEntry(COMPLETION_PROJECT, jarTwoPath);
removeClasspathEntry(this.completion23Project, jarOnePath);
removeClasspathEntry(this.completion23Project, jarTwoPath);
}
}

public void test005() throws CoreException {
// with prefix
// 4 modules available: mod.test (self), mod.two (required), mod.one (transitively required) & java.base (from JCL_23_LIB)
// prefix selects 3 out of 4
IPath jarOnePath = COMPLETION_PROJECT.getPath().append("mod.one.jar");
IPath jarTwoPath = COMPLETION_PROJECT.getPath().append("mod.two.jar");
IPath jarOnePath = this.completion23Project.getPath().append("mod.one.jar");
IPath jarTwoPath = this.completion23Project.getPath().append("mod.two.jar");
IFile moduleFile = null;
try {
addClasspathEntry(COMPLETION_PROJECT, newModularLibraryEntry(jarOnePath, null, null));
addClasspathEntry(COMPLETION_PROJECT, newModularLibraryEntry(jarTwoPath, null, null));
addClasspathEntry(this.completion23Project, newModularLibraryEntry(jarOnePath, null, null));
addClasspathEntry(this.completion23Project, newModularLibraryEntry(jarTwoPath, null, null));
moduleFile = createFile("Completion23/src/module-info.java",
"""
module mod.test {
Expand All @@ -271,8 +275,8 @@ public class X {}
"[MODULE_REF]{mod.two, mod.two, null, null, null, " + DEFAULT_RELEVANCE + "}",
requestor.getResults());
} finally {
removeClasspathEntry(COMPLETION_PROJECT, jarOnePath);
removeClasspathEntry(COMPLETION_PROJECT, jarTwoPath);
removeClasspathEntry(this.completion23Project, jarOnePath);
removeClasspathEntry(this.completion23Project, jarTwoPath);
if (moduleFile != null)
deleteResource(moduleFile);
}
Expand All @@ -282,11 +286,11 @@ public void test006() throws CoreException {
// 4 modules present: mod.test(self), mod.one, mod.two & java.base available
// + prefix rules out java.base
// + mod.two is proposed with lower relevance, because it is not read by the current module
IPath jarOnePath = COMPLETION_PROJECT.getPath().append("mod.one.jar");
IPath jarTwoPath = COMPLETION_PROJECT.getPath().append("mod.two.jar");
IPath jarOnePath = this.completion23Project.getPath().append("mod.one.jar");
IPath jarTwoPath = this.completion23Project.getPath().append("mod.two.jar");
try {
addClasspathEntry(COMPLETION_PROJECT, newModularLibraryEntry(jarOnePath, null, null));
addClasspathEntry(COMPLETION_PROJECT, newModularLibraryEntry(jarTwoPath, null, null)); // not read my the current module
addClasspathEntry(this.completion23Project, newModularLibraryEntry(jarOnePath, null, null));
addClasspathEntry(this.completion23Project, newModularLibraryEntry(jarTwoPath, null, null)); // not read my the current module
createFile("Completion23/src/module-info.java",
"""
module mod.test {
Expand All @@ -311,8 +315,8 @@ public class X {}
"[MODULE_REF]{mod.test, mod.test, null, null, null, " + DEFAULT_RELEVANCE + "}",
requestor.getResults());
} finally {
removeClasspathEntry(COMPLETION_PROJECT, jarOnePath);
removeClasspathEntry(COMPLETION_PROJECT, jarTwoPath);
removeClasspathEntry(this.completion23Project, jarOnePath);
removeClasspathEntry(this.completion23Project, jarTwoPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class RunCompletionModelTests extends junit.framework.TestCase {
COMPLETION_SUITES.add(CompletionTests16_1.class);
COMPLETION_SUITES.add(CompletionTests16_2.class);
COMPLETION_SUITES.add(CompletionTests17.class);
COMPLETION_SUITES.add(CompletionTests23.class);
COMPLETION_SUITES.add(CompletionTestsForRecordPattern.class);
COMPLETION_SUITES.add(CompletionContextTests.class);
COMPLETION_SUITES.add(CompletionContextTests_1_5.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<!--
<classpathentry kind="lib" path="test285379.jar"/>
-->
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# force existence of the src folder

0 comments on commit fbc097c

Please sign in to comment.