|
| 1 | +package com.github.xepozz.testo |
| 2 | + |
| 3 | +import com.intellij.psi.util.PsiTreeUtil |
| 4 | +import com.intellij.testFramework.TestDataPath |
| 5 | +import com.intellij.testFramework.fixtures.BasePlatformTestCase |
| 6 | +import com.jetbrains.php.lang.PhpFileType |
| 7 | +import com.jetbrains.php.lang.psi.PhpFile |
| 8 | +import com.jetbrains.php.lang.psi.elements.Function |
| 9 | +import com.jetbrains.php.lang.psi.elements.Method |
| 10 | +import com.jetbrains.php.lang.psi.elements.PhpClass |
| 11 | + |
| 12 | +@TestDataPath("\$CONTENT_ROOT/src/test/testData") |
| 13 | +class MixinPsiTest : BasePlatformTestCase() { |
| 14 | + |
| 15 | + override fun getTestDataPath() = "src/test/testData" |
| 16 | + |
| 17 | + // ---- isTestoClass ---- |
| 18 | + |
| 19 | + fun testIsTestoClass_classWithTestSuffix() { |
| 20 | + val psiFile = myFixture.configureByText( |
| 21 | + PhpFileType.INSTANCE, |
| 22 | + """<?php class UserTest { public function testSomething(): void {} }""" |
| 23 | + ) |
| 24 | + val phpClass = PsiTreeUtil.findChildOfType(psiFile, PhpClass::class.java)!! |
| 25 | + assertTrue("Class ending with 'Test' should be a Testo class", phpClass.isTestoClass()) |
| 26 | + } |
| 27 | + |
| 28 | + fun testIsTestoClass_classWithTestBaseSuffix() { |
| 29 | + val psiFile = myFixture.configureByText( |
| 30 | + PhpFileType.INSTANCE, |
| 31 | + """<?php class AbstractTestBase { public function testBase(): void {} }""" |
| 32 | + ) |
| 33 | + val phpClass = PsiTreeUtil.findChildOfType(psiFile, PhpClass::class.java)!! |
| 34 | + assertTrue("Class ending with 'TestBase' should be a Testo class", phpClass.isTestoClass()) |
| 35 | + } |
| 36 | + |
| 37 | + fun testIsTestoClass_regularClass() { |
| 38 | + val psiFile = myFixture.configureByText( |
| 39 | + PhpFileType.INSTANCE, |
| 40 | + """<?php class UserService { public function getUser(): void {} }""" |
| 41 | + ) |
| 42 | + val phpClass = PsiTreeUtil.findChildOfType(psiFile, PhpClass::class.java)!! |
| 43 | + assertFalse("Regular class should not be a Testo class", phpClass.isTestoClass()) |
| 44 | + } |
| 45 | + |
| 46 | + fun testIsTestoClass_classWithTestMethodButNoSuffix() { |
| 47 | + val psiFile = myFixture.configureByText( |
| 48 | + PhpFileType.INSTANCE, |
| 49 | + """<?php class MyFeature { public function testSomething(): void {} }""" |
| 50 | + ) |
| 51 | + val phpClass = PsiTreeUtil.findChildOfType(psiFile, PhpClass::class.java)!! |
| 52 | + assertTrue("Class with test methods should be a Testo class", phpClass.isTestoClass()) |
| 53 | + } |
| 54 | + |
| 55 | + // ---- isTestoMethod ---- |
| 56 | + |
| 57 | + fun testIsTestoMethod_publicMethodStartingWithTest() { |
| 58 | + val psiFile = myFixture.configureByText( |
| 59 | + PhpFileType.INSTANCE, |
| 60 | + """<?php class Foo { public function testSomething(): void {} }""" |
| 61 | + ) |
| 62 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 63 | + assertTrue("Public method starting with 'test' should be a Testo method", method.isTestoMethod()) |
| 64 | + } |
| 65 | + |
| 66 | + fun testIsTestoMethod_privateMethodStartingWithTest() { |
| 67 | + val psiFile = myFixture.configureByText( |
| 68 | + PhpFileType.INSTANCE, |
| 69 | + """<?php class Foo { private function testSomething(): void {} }""" |
| 70 | + ) |
| 71 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 72 | + assertFalse("Private method starting with 'test' should not be a Testo method", method.isTestoMethod()) |
| 73 | + } |
| 74 | + |
| 75 | + fun testIsTestoMethod_publicMethodNotStartingWithTest() { |
| 76 | + val psiFile = myFixture.configureByText( |
| 77 | + PhpFileType.INSTANCE, |
| 78 | + """<?php class Foo { public function helper(): void {} }""" |
| 79 | + ) |
| 80 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 81 | + assertFalse("Public method not starting with 'test' should not be a Testo method", method.isTestoMethod()) |
| 82 | + } |
| 83 | + |
| 84 | + fun testIsTestoMethod_protectedMethodStartingWithTest() { |
| 85 | + val psiFile = myFixture.configureByText( |
| 86 | + PhpFileType.INSTANCE, |
| 87 | + """<?php class Foo { protected function testProtected(): void {} }""" |
| 88 | + ) |
| 89 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 90 | + assertFalse("Protected method starting with 'test' should not be a Testo method", method.isTestoMethod()) |
| 91 | + } |
| 92 | + |
| 93 | + // ---- isTestoDataProviderLike ---- |
| 94 | + |
| 95 | + fun testIsTestoDataProviderLike_publicStaticMethod() { |
| 96 | + val psiFile = myFixture.configureByText( |
| 97 | + PhpFileType.INSTANCE, |
| 98 | + """<?php class Foo { public static function provideData(): iterable { yield [1]; } }""" |
| 99 | + ) |
| 100 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 101 | + assertTrue("Public static method should be data provider like", method.isTestoDataProviderLike()) |
| 102 | + } |
| 103 | + |
| 104 | + fun testIsTestoDataProviderLike_publicNonStaticMethod() { |
| 105 | + val psiFile = myFixture.configureByText( |
| 106 | + PhpFileType.INSTANCE, |
| 107 | + """<?php class Foo { public function provideData(): iterable { yield [1]; } }""" |
| 108 | + ) |
| 109 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 110 | + assertFalse("Public non-static method should not be data provider like", method.isTestoDataProviderLike()) |
| 111 | + } |
| 112 | + |
| 113 | + fun testIsTestoDataProviderLike_privateStaticMethod() { |
| 114 | + val psiFile = myFixture.configureByText( |
| 115 | + PhpFileType.INSTANCE, |
| 116 | + """<?php class Foo { private static function provideData(): iterable { yield [1]; } }""" |
| 117 | + ) |
| 118 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 119 | + assertFalse("Private static method should not be data provider like", method.isTestoDataProviderLike()) |
| 120 | + } |
| 121 | + |
| 122 | + fun testIsTestoDataProviderLike_function() { |
| 123 | + val psiFile = myFixture.configureByText( |
| 124 | + PhpFileType.INSTANCE, |
| 125 | + """<?php function provideData(): iterable { yield [1]; }""" |
| 126 | + ) |
| 127 | + val function = PsiTreeUtil.findChildrenOfType(psiFile, Function::class.java) |
| 128 | + .first { it !is Method } |
| 129 | + assertTrue("Standalone function should be data provider like", function.isTestoDataProviderLike()) |
| 130 | + } |
| 131 | + |
| 132 | + // ---- isTestoFile ---- |
| 133 | + |
| 134 | + fun testIsTestoFile_fileNameEndingWithTest() { |
| 135 | + val psiFile = myFixture.configureByText( |
| 136 | + "UserTest.php", |
| 137 | + """<?php class UserTest { public function testSomething(): void {} }""" |
| 138 | + ) |
| 139 | + assertTrue("File named *Test.php should be a Testo file", psiFile.isTestoFile()) |
| 140 | + } |
| 141 | + |
| 142 | + fun testIsTestoFile_regularFile() { |
| 143 | + val psiFile = myFixture.configureByText( |
| 144 | + "UserService.php", |
| 145 | + """<?php class UserService { public function getUser(): void {} }""" |
| 146 | + ) |
| 147 | + assertFalse("Regular PHP file should not be a Testo file", psiFile.isTestoFile()) |
| 148 | + } |
| 149 | + |
| 150 | + fun testIsTestoFile_fileWithTestClass() { |
| 151 | + val psiFile = myFixture.configureByText( |
| 152 | + "Features.php", |
| 153 | + """<?php class FeatureTest { public function testFeature(): void {} }""" |
| 154 | + ) |
| 155 | + assertTrue("File containing test class should be a Testo file", psiFile.isTestoFile()) |
| 156 | + } |
| 157 | + |
| 158 | + // ---- isTestoExecutable ---- |
| 159 | + |
| 160 | + fun testIsTestoExecutable_testMethod() { |
| 161 | + val psiFile = myFixture.configureByText( |
| 162 | + PhpFileType.INSTANCE, |
| 163 | + """<?php class Foo { public function testSomething(): void {} }""" |
| 164 | + ) |
| 165 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 166 | + assertTrue("Test method should be executable", method.isTestoExecutable()) |
| 167 | + } |
| 168 | + |
| 169 | + fun testIsTestoExecutable_nonTestMethod() { |
| 170 | + val psiFile = myFixture.configureByText( |
| 171 | + PhpFileType.INSTANCE, |
| 172 | + """<?php class Foo { public function helper(): void {} }""" |
| 173 | + ) |
| 174 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 175 | + assertFalse("Non-test method should not be executable", method.isTestoExecutable()) |
| 176 | + } |
| 177 | + |
| 178 | + // ---- isTestoClassFile / isTestoFunctionFile ---- |
| 179 | + |
| 180 | + fun testIsTestoClassFile() { |
| 181 | + val psiFile = myFixture.configureByText( |
| 182 | + PhpFileType.INSTANCE, |
| 183 | + """<?php class SomeTest { public function testA(): void {} }""" |
| 184 | + ) as PhpFile |
| 185 | + assertTrue("File with test class should be a Testo class file", psiFile.isTestoClassFile()) |
| 186 | + } |
| 187 | + |
| 188 | + fun testIsTestoClassFile_noTestClass() { |
| 189 | + val psiFile = myFixture.configureByText( |
| 190 | + PhpFileType.INSTANCE, |
| 191 | + """<?php class Helper { public function doStuff(): void {} }""" |
| 192 | + ) as PhpFile |
| 193 | + assertFalse("File without test class should not be a Testo class file", psiFile.isTestoClassFile()) |
| 194 | + } |
| 195 | + |
| 196 | + // ---- Multiple methods in one class ---- |
| 197 | + |
| 198 | + fun testMultipleMethods_mixedTestAndNonTest() { |
| 199 | + val psiFile = myFixture.configureByText( |
| 200 | + PhpFileType.INSTANCE, |
| 201 | + """<?php |
| 202 | + class UserTest { |
| 203 | + public function testCreate(): void {} |
| 204 | + public function testDelete(): void {} |
| 205 | + public function setUp(): void {} |
| 206 | + private function helper(): void {} |
| 207 | + }""" |
| 208 | + ) |
| 209 | + val methods = PsiTreeUtil.findChildrenOfType(psiFile, Method::class.java) |
| 210 | + val testMethods = methods.filter { it.isTestoMethod() } |
| 211 | + val nonTestMethods = methods.filter { !it.isTestoMethod() } |
| 212 | + |
| 213 | + assertEquals("Should find 2 test methods", 2, testMethods.size) |
| 214 | + assertEquals("Should find 2 non-test methods", 2, nonTestMethods.size) |
| 215 | + assertTrue(testMethods.any { it.name == "testCreate" }) |
| 216 | + assertTrue(testMethods.any { it.name == "testDelete" }) |
| 217 | + } |
| 218 | + |
| 219 | + // ---- Edge cases ---- |
| 220 | + |
| 221 | + fun testIsTestoClass_emptyClass() { |
| 222 | + val psiFile = myFixture.configureByText( |
| 223 | + PhpFileType.INSTANCE, |
| 224 | + """<?php class EmptyTest {}""" |
| 225 | + ) |
| 226 | + val phpClass = PsiTreeUtil.findChildOfType(psiFile, PhpClass::class.java)!! |
| 227 | + assertTrue("Empty class ending with 'Test' should still be a Testo class", phpClass.isTestoClass()) |
| 228 | + } |
| 229 | + |
| 230 | + fun testIsTestoMethod_methodNamedExactlyTest() { |
| 231 | + val psiFile = myFixture.configureByText( |
| 232 | + PhpFileType.INSTANCE, |
| 233 | + """<?php class Foo { public function test(): void {} }""" |
| 234 | + ) |
| 235 | + val method = PsiTreeUtil.findChildOfType(psiFile, Method::class.java)!! |
| 236 | + assertTrue("Method named exactly 'test' should be a Testo method", method.isTestoMethod()) |
| 237 | + } |
| 238 | +} |
0 commit comments