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

fix(test-runner): Fix scoping for higher order tests #26

Closed
wants to merge 5 commits into from
Closed
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
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);