Skip to content
New issue

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

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

Already on GitHub? # to your account

Run the spek under cursor when "Run context configuration" occurs #41

Merged
merged 1 commit into from
Jun 17, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import com.intellij.openapi.util.Ref
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.jps.model.java.JavaSourceRootType
import org.jetbrains.kotlin.asJava.toLightClass
import org.jetbrains.kotlin.idea.core.getPackage
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.spek.tooling.Target

/**
Expand Down Expand Up @@ -88,6 +91,14 @@ class SpekRunConfigurationProducer: JavaRunConfigurationProducerBase<SpekRunConf
}
}
}

if (!configurationSet && SpekUtils.isInKotlinFile(element)) {
val target = targetForNearestSurroundingSpekBlock(element)
if (target != null) {
configuration.target = target
configurationSet = true
}
}
}

if (configurationSet) {
Expand Down Expand Up @@ -155,6 +166,10 @@ class SpekRunConfigurationProducer: JavaRunConfigurationProducerBase<SpekRunConf

}
}

if (target == null && SpekUtils.isInKotlinFile(element)) {
target = targetForNearestSurroundingSpekBlock(element)
}
}

return if (target == null) {
Expand All @@ -164,4 +179,27 @@ class SpekRunConfigurationProducer: JavaRunConfigurationProducerBase<SpekRunConf
&& configuration.configurationModule.module == context.module
}
}

private fun targetForNearestSurroundingSpekBlock(element: PsiElement): Target?
{
var nearestCallExpression = PsiTreeUtil.getParentOfType(element, KtCallExpression::class.java)

while (nearestCallExpression != null) {
val calleeExpression = nearestCallExpression.calleeExpression!! as KtNameReferenceExpression
val resolved = calleeExpression.mainReference.resolve()

if (resolved is KtNamedFunction && SpekUtils.isGroupOrTest(resolved)) {
val spec = SpekUtils.getContainingSpecClass(nearestCallExpression)
if (spec != null) {
return Target.Spec(
spec.qualifiedName!!,
SpekUtils.extractPath(nearestCallExpression)
)
}
}
nearestCallExpression = PsiTreeUtil.getParentOfType(nearestCallExpression, KtCallExpression::class.java)
}

return null
}
}
11 changes: 11 additions & 0 deletions plugin/src/main/kotlin/org/jetbrains/spek/idea/SpekUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.spek.idea

import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtil
import org.jetbrains.kotlin.asJava.classes.KtLightClass
import org.jetbrains.kotlin.asJava.toLightClass
Expand All @@ -13,6 +14,7 @@ import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
Expand Down Expand Up @@ -80,6 +82,15 @@ object SpekUtils {
return cls.findAnnotation(fqName) != null
}

fun isInKotlinFile(element: PsiElement): Boolean {
val file = PsiTreeUtil.getParentOfType(element, KtFile::class.java)
return file != null
}

fun isGroupOrTest(function: KtNamedFunction): Boolean {
return isGroup(function) || isTest(function)
}

fun isGroup(function: KtNamedFunction): Boolean {
return GROUP_FN.contains(function.name)
}
Expand Down
26 changes: 26 additions & 0 deletions tooling/src/main/kotlin/org/jetbrains/spek/tooling/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,30 @@ class Path(@JsonProperty("type") val type: PathType,

fun deserialize(path: String): Path = mapper.readValue(path)
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

if (other?.javaClass != javaClass) {
return false
}

other as Path

if (this.type != other.type) {
return false
}

if (this.description != other.description) {
return false
}

if (this.next != other.next) {
return false
}

return true
}
}