Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Notify and reason to the user that UtBot can't be run on the edited c…
…lass
  • Loading branch information
EgorkaKulikov committed Oct 5, 2022
commit d53a4e9f0c3d916af64ce88aa26804649e2415ba
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ object UnsupportedJdkNotifier : ErrorNotifier() {
"JDK versions older than 8 are not supported. This project's JDK version is $info"
}

object InvalidClassNotifier : WarningNotifier() {
override val displayId: String = "Invalid class"
override fun content(project: Project?, module: Module?, info: String): String =
"Generate tests with UtBot for the $info is not supported"
}

object MissingLibrariesNotifier : WarningNotifier() {
override val displayId: String = "Missing libraries"
override fun content(project: Project?, module: Module?, info: String): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ import org.utbot.intellij.plugin.util.isVisible
import java.util.*
import org.jetbrains.kotlin.j2k.getContainingClass
import org.jetbrains.kotlin.utils.addIfNotNull
import org.utbot.intellij.plugin.models.packageName
import org.utbot.intellij.plugin.ui.InvalidClassNotifier
import org.utbot.intellij.plugin.util.isAbstract

class GenerateTestsAction : AnAction(), UpdateInBackground {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val (srcClasses, focusedMethods, extractMembersFromSrcClasses) = getPsiTargets(e) ?: return
val (srcClasses, focusedMethods, extractMembersFromSrcClasses) = getPsiTargets(e, true) ?: return
UtTestsDialogProcessor.createDialogAndGenerateTests(project, srcClasses, extractMembersFromSrcClasses, focusedMethods)
}

Expand All @@ -41,7 +44,7 @@ class GenerateTestsAction : AnAction(), UpdateInBackground {
e.presentation.isEnabled = getPsiTargets(e) != null
}

private fun getPsiTargets(e: AnActionEvent): Triple<Set<PsiClass>, Set<MemberInfo>, Boolean>? {
private fun getPsiTargets(e: AnActionEvent, withWarnings: Boolean = false): Triple<Set<PsiClass>, Set<MemberInfo>, Boolean>? {
val project = e.project ?: return null
val editor = e.getData(CommonDataKeys.EDITOR)
if (editor != null) {
Expand All @@ -53,7 +56,7 @@ class GenerateTestsAction : AnAction(), UpdateInBackground {

if (psiElementHandler.isCreateTestActionAvailable(element)) {
val srcClass = psiElementHandler.containingClass(element) ?: return null
if (srcClass.isInterface || !srcClass.isVisible) return null
if (srcClass.isInvalid(withWarnings)) return null
val srcSourceRoot = srcClass.getSourceRoot() ?: return null
val srcMembers = srcClass.extractFirstLevelMembers(false)
val focusedMethod = focusedMethodOrNull(element, srcMembers, psiElementHandler)
Expand Down Expand Up @@ -113,7 +116,8 @@ class GenerateTestsAction : AnAction(), UpdateInBackground {
}
}
}
srcClasses.removeIf { it.isInterface }

srcClasses.removeIf { it.isInvalid(withWarnings) }
if (srcClasses.size > 1) {
extractMembersFromSrcClasses = false
}
Expand All @@ -136,6 +140,28 @@ class GenerateTestsAction : AnAction(), UpdateInBackground {
return null
}

private fun PsiClass.isInvalid(withWarnings: Boolean): Boolean {
val isAbstractOrInterface = this.isInterface || this.isAbstract
if (isAbstractOrInterface) {
if (withWarnings) InvalidClassNotifier.notify("abstract class or interface")
return true
}

val isInvisible = !this.isVisible
if (isInvisible) {
if (withWarnings) InvalidClassNotifier.notify("private or protected class")
return true
}

val packageIsIncorrect = this.packageName.startsWith("java")
if (packageIsIncorrect) {
if (withWarnings) InvalidClassNotifier.notify("class located in java.* package")
return true
}

return false
}

private fun PsiElement?.getSourceRoot() : VirtualFile? {
val project = this?.project?: return null
val virtualFile = this.containingFile?.originalFile?.virtualFile?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import org.jetbrains.kotlin.asJava.elements.isSetter
import org.utbot.common.filterWhen
import org.utbot.framework.UtSettings

private val PsiMember.isAbstract: Boolean
val PsiMember.isAbstract: Boolean
get() = modifierList?.hasModifierProperty(PsiModifier.ABSTRACT)?: false


private val PsiMember.isKotlinGetterOrSetter: Boolean
get() {
if (this !is KtLightMethod)
Expand Down