mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
InjectionTestFixture extracted from AbstractLanguageInjectionTestCase
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.testFramework.fixtures
|
||||
|
||||
import com.intellij.lang.injection.InjectedLanguageManager
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.fileEditor.TextEditor
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiLanguageInjectionHost
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import junit.framework.TestCase
|
||||
import java.util.*
|
||||
import kotlin.test.fail
|
||||
|
||||
class InjectionTestFixture(private val javaFixture: CodeInsightTestFixture) {
|
||||
|
||||
val injectedLanguageManager: InjectedLanguageManager
|
||||
get() = InjectedLanguageManager.getInstance(javaFixture.project)
|
||||
|
||||
fun assertInjectedLangAtCaret(lang: String?) {
|
||||
val injectedElement = injectedLanguageManager.findInjectedElementAt(topLevelFile, topLevelCaretPosition)
|
||||
if (lang != null) {
|
||||
TestCase.assertNotNull("injection of '$lang' expected", injectedElement)
|
||||
TestCase.assertEquals(lang, injectedElement!!.language.id)
|
||||
}
|
||||
else {
|
||||
TestCase.assertNull(injectedElement)
|
||||
}
|
||||
}
|
||||
|
||||
fun getAllInjections(): List<Pair<PsiElement, PsiFile>> {
|
||||
val injected = mutableListOf<Pair<PsiElement, PsiFile>>()
|
||||
val hosts = PsiTreeUtil.collectElementsOfType(topLevelFile, PsiLanguageInjectionHost::class.java)
|
||||
for (host in hosts) {
|
||||
injectedLanguageManager.enumerate(host,
|
||||
PsiLanguageInjectionHost.InjectedPsiVisitor { injectedPsi, places ->
|
||||
injected.add(host to injectedPsi)
|
||||
})
|
||||
}
|
||||
return injected
|
||||
}
|
||||
|
||||
fun assertInjected(vararg expectedInjections: InjectionAssertionData) {
|
||||
|
||||
val expected = expectedInjections.toCollection(LinkedList())
|
||||
val foundInjections = getAllInjections().toCollection(LinkedList())
|
||||
|
||||
while (expected.isNotEmpty()) {
|
||||
val (text, injectedLanguage) = expected.pop()
|
||||
val found = (foundInjections.find { (psi, file) -> psi.text == text && file.language.id == injectedLanguage }
|
||||
?: fail(
|
||||
"no injection '$text' -> '$injectedLanguage' were found, remains: ${foundInjections.joinToString { (psi, file) -> "'${psi.text}' -> '${file.language}'" }} "))
|
||||
foundInjections.remove(found)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
val topLevelFile: PsiFile get() = javaFixture.file.let { injectedLanguageManager.getTopLevelFile(it) }
|
||||
|
||||
val topLevelCaretPosition get() = topLevelEditor.caretModel.offset
|
||||
|
||||
val topLevelEditor
|
||||
get() = (FileEditorManager.getInstance(
|
||||
javaFixture.project).getSelectedEditor(topLevelFile.virtualFile) as TextEditor).editor
|
||||
|
||||
}
|
||||
|
||||
data class InjectionAssertionData(val text: String, val injectedLanguage: String? = null) {
|
||||
fun hasLanguage(lang: String): InjectionAssertionData = this.copy(injectedLanguage = lang)
|
||||
}
|
||||
|
||||
fun injectionForHost(text: String) = InjectionAssertionData(text)
|
||||
@@ -1,39 +1,29 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package org.intellij.plugins.intelliLang
|
||||
|
||||
import com.intellij.lang.injection.InjectedLanguageManager
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager
|
||||
import com.intellij.openapi.fileEditor.TextEditor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiLanguageInjectionHost
|
||||
import com.intellij.psi.SmartPsiElementPointer
|
||||
import com.intellij.testFramework.fixtures.InjectionTestFixture
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
|
||||
import com.intellij.util.Processor
|
||||
import junit.framework.TestCase
|
||||
import org.intellij.plugins.intelliLang.inject.InjectLanguageAction
|
||||
|
||||
abstract class AbstractLanguageInjectionTestCase : LightCodeInsightFixtureTestCase() {
|
||||
|
||||
private val injectionTestFixture by lazy { InjectionTestFixture(myFixture) }
|
||||
|
||||
fun assertInjectedLangAtCaret(lang: String?) {
|
||||
val injectedElement = injectedLanguageManager.findInjectedElementAt(topLevelFile, topLevelCaretPosition)
|
||||
if (lang != null) {
|
||||
TestCase.assertNotNull("injection of '$lang' expected", injectedElement)
|
||||
TestCase.assertEquals(lang, injectedElement!!.language.id)
|
||||
}
|
||||
else {
|
||||
TestCase.assertNull(injectedElement)
|
||||
}
|
||||
injectionTestFixture.assertInjectedLangAtCaret(lang)
|
||||
}
|
||||
|
||||
val topLevelEditor get() = (FileEditorManager.getInstance(project).getSelectedEditor(topLevelFile.virtualFile) as TextEditor).editor
|
||||
val topLevelEditor get() = injectionTestFixture.topLevelEditor
|
||||
|
||||
val topLevelCaretPosition get() = topLevelEditor.caretModel.offset
|
||||
val topLevelCaretPosition get() = injectionTestFixture.topLevelCaretPosition
|
||||
|
||||
val injectedLanguageManager: InjectedLanguageManager get() = InjectedLanguageManager.getInstance(project)
|
||||
|
||||
val topLevelFile: PsiFile get() = file.let { injectedLanguageManager.getTopLevelFile(it) }
|
||||
val topLevelFile: PsiFile get() = injectionTestFixture.topLevelFile
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user