|
| 1 | +/* |
| 2 | + * Source++, the continuous feedback platform for developers. |
| 3 | + * Copyright (C) 2022-2024 CodeBrig, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package spp.jetbrains.marker.jvm.service |
| 18 | + |
| 19 | +import com.google.common.base.CaseFormat |
| 20 | +import com.intellij.openapi.application.ApplicationManager |
| 21 | +import com.intellij.openapi.util.Computable |
| 22 | +import com.intellij.psi.PsiClass |
| 23 | +import com.intellij.psi.PsiElement |
| 24 | +import com.intellij.psi.PsiJavaFile |
| 25 | +import com.intellij.psi.PsiNamedElement |
| 26 | +import com.intellij.testFramework.TestDataPath |
| 27 | +import com.intellij.testFramework.fixtures.BasePlatformTestCase |
| 28 | +import kotlinx.coroutines.runBlocking |
| 29 | +import org.jetbrains.kotlin.psi.KtClass |
| 30 | +import org.jetbrains.kotlin.psi.KtFile |
| 31 | +import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType |
| 32 | +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile |
| 33 | +import spp.jetbrains.artifact.service.getFields |
| 34 | +import spp.jetbrains.marker.SourceMarker |
| 35 | +import spp.jetbrains.marker.jvm.JVMLanguageProvider |
| 36 | +import spp.jetbrains.marker.source.SourceFileMarker |
| 37 | + |
| 38 | +@TestDataPath("\$CONTENT_ROOT/testData/field/") |
| 39 | +class JVMFieldTest : BasePlatformTestCase() { |
| 40 | + |
| 41 | + override fun setUp() { |
| 42 | + super.setUp() |
| 43 | + ApplicationManager.getApplication().runReadAction(Computable { |
| 44 | + runBlocking { |
| 45 | + SourceMarker.getInstance(myFixture.project).clearAvailableSourceFileMarkers() |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + JVMLanguageProvider().setup(project) |
| 50 | + SourceFileMarker.SUPPORTED_FILE_TYPES.add(PsiJavaFile::class.java) |
| 51 | + SourceFileMarker.SUPPORTED_FILE_TYPES.add(KtFile::class.java) |
| 52 | + SourceFileMarker.SUPPORTED_FILE_TYPES.add(GroovyFile::class.java) |
| 53 | + } |
| 54 | + |
| 55 | + override fun getTestDataPath(): String { |
| 56 | + return "src/test/testData/field/" |
| 57 | + } |
| 58 | + |
| 59 | + fun testSingleField() { |
| 60 | + doTest<PsiClass>("java") |
| 61 | + doTest<KtClass>("kt") |
| 62 | + doTest<PsiClass>("groovy") |
| 63 | + } |
| 64 | + |
| 65 | + fun testFieldAndFunction() { |
| 66 | + doTest<PsiClass>("java") |
| 67 | + doTest<KtClass>("kt") |
| 68 | + doTest<PsiClass>("groovy") |
| 69 | + } |
| 70 | + |
| 71 | + private inline fun <reified T : PsiElement> doTest(extension: String) { |
| 72 | + val className = getTestName(false) |
| 73 | + val testDir = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN).convert(className) |
| 74 | + val psiFile = myFixture.configureByFile("$testDir/$className.$extension") |
| 75 | + val clazz = psiFile.findDescendantOfType<T> { true } |
| 76 | + assertNotNull(clazz) |
| 77 | + |
| 78 | + val fields = clazz!!.getFields() |
| 79 | + assertEquals(1, fields.size) |
| 80 | + assertEquals("foo", (fields[0] as PsiNamedElement).name) |
| 81 | + } |
| 82 | +} |
0 commit comments