Skip to content

Commit

Permalink
fix(test-runner): Fix scoping for higher order tests (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivernybroe committed Jul 15, 2020
2 parents 526d90a + 2b7308a commit 8506878
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 124 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Line markers now works, for the whole file and the single test. ([#17](https://github.com/pestphp/pest-intellij/pull/17), [#24](https://github.com/pestphp/pest-intellij/pull/24))
- Add running support with debugger ([#19](https://github.com/pestphp/pest-intellij/pull/19))
- `$this->field` type support for fields declared in `beforeEach` and `beforeAll` functions ([#22](https://github.com/pestphp/pest-intellij/pull/22))
- Run tests scoped based on the cursor ([#24](https://github.com/pestphp/pest-intellij/pull/24), [#26](https://github.com/pestphp/pest-intellij/pull/26))

## [v0.1.1]
### Added
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = com.pestphp
pluginName = Pest
pluginVersion = 0.2.0
pluginVersion = 0.2.1-alpha.1
pluginSinceBuild = 201
pluginUntilBuild = null

Expand Down
32 changes: 0 additions & 32 deletions src/main/java/com/pestphp/pest/PestTestRunLineMarkerProvider.java

This file was deleted.

83 changes: 0 additions & 83 deletions src/main/java/com/pestphp/pest/PestUtil.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/kotlin/com/pestphp/pest/PestTestRunLineMarkerProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.pestphp.pest

import com.intellij.execution.lineMarker.RunLineMarkerContributor
import com.intellij.psi.PsiElement
import com.jetbrains.php.lang.lexer.PhpTokenTypes
import com.jetbrains.php.lang.psi.PhpPsiUtil
import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.pestphp.pest.PestUtil.isPestTestFunction

class PestTestRunLineMarkerProvider : RunLineMarkerContributor() {
override fun getInfo(leaf: PsiElement): Info? {
if (!PhpPsiUtil.isOfType(leaf, PhpTokenTypes.IDENTIFIER)) {
return null
}

// return RunLineMarkerContributor.withExecutorActions(getTestStateIcon(getLocationHint(testName), leaf.getProject(), false));
return when {
leaf.parent !is FunctionReference -> null
isPestTestFunction(leaf.parent as FunctionReference) -> withExecutorActions(PestIcons.RUN_SINGLE_TEST)
else -> null
}

}
}
105 changes: 105 additions & 0 deletions src/main/kotlin/com/pestphp/pest/PestUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.pestphp.pest

import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.ObjectUtils
import com.jetbrains.php.lang.psi.PhpFile
import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.jetbrains.php.lang.psi.elements.MethodReference
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression
import com.jetbrains.php.testFramework.PhpTestFrameworkConfiguration
import com.jetbrains.php.testFramework.PhpTestFrameworkSettingsManager
import org.jetbrains.annotations.Nls

object PestUtil {
private const val NOTIFICATION_GROUP = "Pest"

@JvmStatic
fun isPestTestFile(element: PsiElement?): Boolean {
if (element == null) {
return false
}
if (element !is PhpFile) {
return false
}
val functions = PsiTreeUtil.findChildrenOfType(element, FunctionReference::class.java)
return functions.stream()
.anyMatch { obj: FunctionReference? -> isPestTestFunction(obj) }
}

@JvmStatic
fun isPestTestFunction(element: PsiElement?): Boolean {
return when (element) {
null -> false
is MethodReference -> isPestTestFunction(element)
is FunctionReference -> isPestTestFunction(element)
else -> false
}
}

@JvmStatic
fun isPestTestFunction(reference: FunctionReference): Boolean {
return reference.canonicalText in setOf("it", "test")
}

@JvmStatic
fun isPestTestFunction(methodReference: MethodReference): Boolean {
val reference = ObjectUtils.tryCast(methodReference.classReference, FunctionReference::class.java)
return reference != null && isPestTestFunction(reference)
}

@JvmStatic
fun getTestName(element: FunctionReference): String? {
return when (val parameter = element.getParameter(0)) {
is StringLiteralExpression -> parameter.contents
else -> null
}
}

@JvmStatic
fun getTestName(element: PsiElement?): String? {
return when (element) {
is MethodReference -> (element.classReference as? FunctionReference)?.let(this::getTestName)
is FunctionReference -> getTestName(element)
else -> null
}
}

@JvmStatic
fun isEnabled(project: Project): Boolean {
return PhpTestFrameworkSettingsManager
.getInstance(project)
.getConfigurations(PestFrameworkType.getInstance())
.stream()
.anyMatch { config: PhpTestFrameworkConfiguration -> StringUtil.isNotEmpty(config.executablePath) }
}

@JvmStatic
fun doNotify(
title: String,
content: @Nls(capitalization = Nls.Capitalization.Sentence) String,
type: NotificationType,
project: Project?
) {
val notification = Notification(NOTIFICATION_GROUP, title, content, type)
doNotify(notification, project)
}

@JvmStatic
fun doNotify(notification: Notification?, project: Project?) {
if (project != null && !project.isDisposed && !project.isDefault) {
project.messageBus.syncPublisher(Notifications.TOPIC).notify(notification!!)
} else {
val app = ApplicationManager.getApplication()
if (!app.isDisposed) {
app.messageBus.syncPublisher(Notifications.TOPIC).notify(notification!!)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.util.Function
import com.jetbrains.php.lang.psi.elements.FunctionReference
import com.jetbrains.php.testFramework.run.PhpTestConfigurationProducer
import com.pestphp.pest.PestUtil

Expand All @@ -32,14 +31,10 @@ class PestRunConfigurationProducer : PhpTestConfigurationProducer<PestRunConfigu

companion object {
private val METHOD = Condition<PsiElement> { element: PsiElement? ->
(element is FunctionReference
&& PestUtil.isPestTestFunction(element))
return@Condition PestUtil.isPestTestFunction(element)
}
private val METHOD_NAMER = Function<PsiElement, String?> { element: PsiElement? ->
if (element is FunctionReference) {
return@Function PestUtil.getTestName(element)
}
null
return@Function PestUtil.getTestName(element)
}
private val FILE_TO_SCOPE = Function<PsiFile, PsiElement?> { file: PsiFile? ->
if (PestUtil.isPestTestFile(file)) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/pestphp/pest/tests/PestUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PestUtilTest: PestLightCodeFixture() {
val file = myFixture.configureByFile("SimpleTest.php")

val functions = PsiTreeUtil.findChildrenOfType(file, FunctionReference::class.java)
.stream().filter(PestUtil::isPestTestFunction).collect(Collectors.toList())
.stream().filter { element -> PestUtil.isPestTestFunction(element)}.collect(Collectors.toList())

assertEquals(1, functions.count())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

it('lives')->assertTrue(true);

0 comments on commit 8506878

Please sign in to comment.