我使用模板https://github.com/JetBrains/intellij-platform-plugin-template創建了一個 IntelliJ 插件。該模板附帶一個在 XML 檔案上運行的測驗。我想為 Kotlin 檔案創建一個類似的測驗。這是模板測驗檔案加上我添加的測驗(test2):
package org.jetbrains.plugins.template
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.psi.xml.XmlFile
import com.intellij.testFramework.TestDataPath
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.util.PsiErrorElementUtil
@TestDataPath("\$CONTENT_ROOT/src/test/testData")
class MyPluginTest : BasePlatformTestCase() {
fun testXMLFile() {
val psiFile = myFixture.configureByText(XmlFileType.INSTANCE, "<foo>bar</foo>")
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
assertFalse(PsiErrorElementUtil.hasErrors(project, xmlFile.virtualFile))
assertNotNull(xmlFile.rootTag)
xmlFile.rootTag?.let {
assertEquals("foo", it.name)
assertEquals("bar", it.value.text)
}
}
override fun getTestDataPath() = "src/test/testData/rename"
fun testRename() {
myFixture.testRename("foo.xml", "foo_after.xml", "a2")
}
// Here's my test
fun test2() {
val fileText: String = """
package com.loganmay.test
data class MyClass(val myString: String)
""".trimIndent()
val psiFile = myFixture.configureByText("a.kt", fileText)
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
}
}
在不更改 build.gradle 檔案的情況下,該測驗會失敗并顯示:
Expected instance of: com.intellij.psi.xml.XmlFile actual: com.intellij.psi.impl.source.PsiPlainTextFileImpl
我希望它將文本決議為 aPsiFile也是KtFile. 從各種來源,我被引導相信夾具將其決議為純文本檔案,因為測驗專案無法訪問 Kotlin 編譯器。所以,我補充說:
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
到 build.gradle。然后,當我運行測驗時,configureByText拋出一個帶有很大痕跡的例外,其根例外是:
Caused by: java.lang.Throwable: 'filetype.archive.display.name' is not found in java.util.PropertyResourceBundle@4ecbb519(messages.CoreBundle)
... 53 more
org.jetbrains.plugins.template.MyPluginTest > test2 FAILED
com.intellij.diagnostic.PluginException at ComponentManagerImpl.kt:511
Caused by: java.util.MissingResourceException at Registry.java:164
有沒有人知道問題是什么或知道如何解決它?
筆記:
- 我還嘗試匯入 kotlin 編譯器和 cast
psiFile as KtFile,這產生了相同的錯誤,這是我從這里得到的一個想法 - 這個專案有一個像這樣的測驗可能正在作業
- 這篇文章和這篇文章推薦添加 kotlin gradle 插件,我做了
- 這個問題看起來很相似
uj5u.com熱心網友回復:
Yann Cebron在jetbrains 幫助論壇上回復了 Java 的答案,該答案也適用于 Kotlin。
解決方案是向 IntelliJ gradle 插件添加依賴項。該模板在 build.gradle 中帶有這些行:
intellij {
pluginName.set(properties("pluginName"))
version.set(properties("platformVersion"))
type.set(properties("platformType"))
// Plugin Dependencies. Uses `platformPlugins` property from the gradle.properties file.
plugins.set(properties("platformPlugins").split(',').map(String::trim).filter(String::isNotEmpty))
}
所以,不需要在那里做任何事情。在我的 gradle.properties 中,我添加了
platformPlugins = com.intellij.java, org.jetbrains.kotlin
在我的 plugin.xml 中,我添加了:
<depends>com.intellij.modules.java</depends>
<depends>org.jetbrains.kotlin</depends>
我能夠洗掉
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
來自我上面提到的 build.gradle。
現在,該測驗適用于 Java 和 Kotlin 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/509876.html
上一篇:app\src\main\kotlin\KotlinApp.kt:(48,34):'Any'型別的運算式'f[2]'作為函式呼叫。未找到函式“invok
