Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test detection in code mining is not reliable #213 #219

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 17 additions & 39 deletions org.moreunit.plugin/src/org/moreunit/codemining/JumpCodeMining.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import java.util.function.Consumer;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -58,22 +56,25 @@ private static int getLineNumber(IJavaElement element, IDocument document) throw
@Override
protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor)
{
String testOrTested = isTest() ? "tested" : "test";
return CompletableFuture.runAsync(() -> {
IMember member = (IMember) element;
TypeFacade typeFacade = TypeFacade.createFacade(member.getCompilationUnit());
String testOrTested = typeFacade instanceof TestCaseTypeFacade ? "tested" : "test";
if(element instanceof IType)
{
MethodSearchMode searchMode = Preferences.getInstance().getMethodSearchMode(element.getJavaProject());

TypeFacade typeFacade = TypeFacade.createFacade(((IMember) element).getCompilationUnit());

CorrespondingMemberRequest request = newCorrespondingMemberRequest() //
.withExpectedResultType(MemberType.TYPE_OR_METHOD) //
.withCurrentMethod(element instanceof IMethod method ? method : null) //
.methodSearchMode(searchMode) //
.build();

IMember memberToJump = typeFacade.getOneCorrespondingMember(request);
if(memberToJump != null)
boolean jumpable = false;
if(typeFacade instanceof ClassTypeFacade)
{
ClassTypeFacade classTypeFacade = (ClassTypeFacade) typeFacade;
jumpable = classTypeFacade.hasTestCase();
}
else if(typeFacade instanceof TestCaseTypeFacade)
{
TestCaseTypeFacade testCaseTypeFacade = (TestCaseTypeFacade) typeFacade;
IType correspondingClassUnderTest = testCaseTypeFacade.getCorrespondingClassUnderTest();
jumpable = correspondingClassUnderTest != null;
}
if(jumpable)
{
setLabel("Jump to " + testOrTested + " class");
}
Expand All @@ -85,17 +86,11 @@ protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor
else if(element instanceof IMethod)
{
IMethod method = (IMethod) element;
TestAnnotationMode testAnnotationMode = Preferences.forProject(method.getJavaProject()).getTestAnnotationMode();
if(testAnnotationMode == TestAnnotationMode.OFF)
{
return;
}
TypeFacade typeFacade = TypeFacade.createFacade(((IMember) element).getCompilationUnit());
boolean jumpable = false;
if(typeFacade instanceof ClassTypeFacade)
{
ClassTypeFacade classTypeFacade = (ClassTypeFacade) typeFacade;
jumpable = ! (classTypeFacade.getCorrespondingTestMethods(method, testAnnotationMode.getMethodSearchMode()).isEmpty());
jumpable = ! (classTypeFacade.getCorrespondingTestMethods(method, TestAnnotationMode.BY_CALL_AND_BY_NAME.getMethodSearchMode()).isEmpty());
}
else if(typeFacade instanceof TestCaseTypeFacade)
{
Expand All @@ -118,23 +113,6 @@ else if(typeFacade instanceof TestCaseTypeFacade)
});
}

private boolean isTest()
{
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if(packageFragmentRoot != null)
{
try
{
IClasspathEntry resolvedClasspathEntry = packageFragmentRoot.getResolvedClasspathEntry();
return resolvedClasspathEntry != null && resolvedClasspathEntry.isTest();
}
catch (JavaModelException e)
{
}
}
return false;
}

@Override
public Consumer<MouseEvent> getAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface PreferenceConstants
String DEFAULT_TEST_CLASS_NAME_TEMPLATE = TestFileNamePattern.SRC_FILE_VARIABLE + "(Test|IT)";
String DEFAULT_TEST_METHOD_DEFAULT_CONTENT = "throw new RuntimeException(\"not yet implemented\");";
String DEFAULT_TEST_ANNOTATION_MODE = "OFF";
boolean DEFAULT_ENABLE_MOREUNIT_CODEMINING = true;

String TEXT_GENERAL_SETTINGS = "General settings for your unit tests (they can then be refined for each project):";
String TEXT_TEST_SUPERCLASS = "Test superclass:";
Expand All @@ -63,7 +64,7 @@ public interface PreferenceConstants
String TEXT_EXTENDED_TEST_METHOD_SEARCH = "Enable test method search \"by call\"";
String TEXT_ENABLE_TEST_METHOD_SEARCH_BY_NAME = "Enable test method search \"by name\"";
String TEXT_GENERATE_COMMENTS_FOR_TEST_METHOD = "Generate comments for test methods";
String TEXT_ENABLE_MOREUNIT_CODEMINING = "Enable MoreUnit Code Mining (Beta)";
String TEXT_ENABLE_MOREUNIT_CODEMINING = "Enable MoreUnit Code Mining";
String TEXT_ANNOTATION_MODE = "Annotate tested methods";
String TEST_ANNOTATION_MODE_DISABLED = "Disabled";
String TEST_ANNOTATION_MODE_BY_NAME = "Search by method name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected static final IPreferenceStore initStore(IPreferenceStore store)
store.setDefault(PreferenceConstants.TEST_CLASS_NAME_TEMPLATE, PreferenceConstants.DEFAULT_TEST_CLASS_NAME_TEMPLATE);
store.setDefault(PreferenceConstants.TEST_METHOD_DEFAULT_CONTENT, PreferenceConstants.DEFAULT_TEST_METHOD_DEFAULT_CONTENT);
store.setDefault(PreferenceConstants.TEST_ANNOTATION_MODE, PreferenceConstants.DEFAULT_TEST_ANNOTATION_MODE);
store.setDefault(PreferenceConstants.ENABLE_MOREUNIT_CODE_MINING, PreferenceConstants.DEFAULT_ENABLE_MOREUNIT_CODEMINING);
return store;
}

Expand Down
Loading