-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from pestphp/duplicate-test-name-inspection
feat(inspection): Add duplicate test name inspection
- Loading branch information
Showing
8 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/com/pestphp/pest/inspections/DuplicateTestNameInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.pestphp.pest.inspections | ||
|
||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.jetbrains.php.lang.inspections.PhpInspection | ||
import com.jetbrains.php.lang.psi.PhpFile | ||
import com.jetbrains.php.lang.psi.elements.FunctionReference | ||
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor | ||
import com.pestphp.pest.getPestTestName | ||
import com.pestphp.pest.getPestTests | ||
|
||
class DuplicateTestNameInspection : PhpInspection() { | ||
companion object { | ||
private const val DESCRIPTION = "Pest test names must be unique in the same file." | ||
} | ||
|
||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : PhpElementVisitor() { | ||
override fun visitPhpFile(file: PhpFile) { | ||
file.getPestTests() | ||
.groupBy { it.getPestTestName()!! } | ||
.filter { it.value.count() > 1 } | ||
.forEach { | ||
declareProblemType(holder, it.value) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Suppress("SpreadOperator") | ||
private fun declareProblemType(holder: ProblemsHolder, tests: List<FunctionReference>) { | ||
tests.forEach { | ||
holder.registerProblem( | ||
it, | ||
DESCRIPTION, | ||
ProblemHighlightType.GENERIC_ERROR, | ||
*LocalQuickFix.EMPTY_ARRAY | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/resources/inspectionDescriptions/DuplicateTestNameInspection.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html lang="en"> | ||
<body> | ||
Reports duplicate pest test names in the same file. | ||
</body> | ||
</html> |
28 changes: 28 additions & 0 deletions
28
src/test/kotlin/com/pestphp/pest/inspections/DuplicateTestNameInspectionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pestphp.pest.inspections | ||
|
||
import com.pestphp.pest.tests.PestLightCodeFixture | ||
|
||
|
||
class DuplicateTestNameInspectionTest : PestLightCodeFixture() { | ||
override fun getTestDataPath(): String? { | ||
return "src/test/resources/com/pestphp/pest/inspections" | ||
} | ||
|
||
override fun setUp() { | ||
super.setUp() | ||
|
||
myFixture.enableInspections(DuplicateTestNameInspection::class.java) | ||
} | ||
|
||
fun testHasDuplicateTest() { | ||
val file = myFixture.configureByFile("DuplicateTestName.php") | ||
|
||
myFixture.checkHighlighting() | ||
} | ||
|
||
fun testNoDuplicateTest() { | ||
val file = myFixture.configureByFile("NoDuplicateTestName.php") | ||
|
||
myFixture.checkHighlighting() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/resources/com/pestphp/pest/inspections/DuplicateTestName.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
<error descr="Pest test names must be unique in the same file.">test('basic', function () { | ||
$this->assertTrue(true); | ||
})</error>; | ||
|
||
<error descr="Pest test names must be unique in the same file.">test('basic', function () { | ||
$this->assertTrue(true); | ||
})</error>; |
9 changes: 9 additions & 0 deletions
9
src/test/resources/com/pestphp/pest/inspections/NoDuplicateTestName.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
test('One test', function () { | ||
$this->assertTrue(true); | ||
}); | ||
|
||
test('Another test', function () { | ||
$this->assertTrue(true); | ||
}); |