Injection test refactoring

This commit is contained in:
Nikolay Krasko
2016-04-19 21:17:45 +03:00
parent 42084b0529
commit 6bea3923f5
3 changed files with 139 additions and 71 deletions
@@ -0,0 +1,88 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.psi
import com.intellij.injected.editor.EditorWindow
import com.intellij.psi.injection.Injectable
import com.intellij.testFramework.LightProjectDescriptor
import junit.framework.TestCase
import org.intellij.plugins.intelliLang.Configuration
import org.intellij.plugins.intelliLang.inject.InjectLanguageAction
import org.intellij.plugins.intelliLang.inject.UnInjectLanguageAction
import org.intellij.plugins.intelliLang.references.FileReferenceInjector
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCaseBase
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
abstract class AbstractInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor {
val testName = getTestName(true)
return when {
testName.endsWith("WithAnnotation") -> KotlinLightProjectDescriptor.INSTANCE
testName.endsWith("WithRuntime") -> KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
else -> KotlinLightCodeInsightFixtureTestCaseBase.JAVA_LATEST
}
}
protected fun testInjectionPresent(text: String, languageId: String? = null, unInjectShouldBePresent: Boolean = true) {
myFixture.configureByText("${getTestName(true)}.kt", text.trimMargin())
TestCase.assertFalse("Injection action is available. There's probably no injection at caret place",
InjectLanguageAction().isAvailable(project, myFixture.editor, myFixture.file))
if (languageId != null) {
val injectedFile = (editor as? EditorWindow)?.injectedFile
KotlinLightCodeInsightFixtureTestCaseBase.assertEquals("Wrong injection language", languageId, injectedFile?.language?.id)
}
if (unInjectShouldBePresent) {
TestCase.assertTrue("UnInjection action is not available. There's no injection at caret place or some other troubles.",
UnInjectLanguageAction().isAvailable(project, myFixture.editor, myFixture.file))
}
}
protected fun doRemoveInjectionTest(before: String, after: String) {
myFixture.setCaresAboutInjection(false)
myFixture.configureByText("${getTestName(true)}.kt", before.trimMargin())
TestCase.assertTrue(UnInjectLanguageAction().isAvailable(project, myFixture.editor, myFixture.file))
UnInjectLanguageAction.invokeImpl(project, myFixture.editor, myFixture.file)
myFixture.checkResult(after.trimMargin())
}
protected fun doFileReferenceInjectTest(before: String, after: String) {
doTest(FileReferenceInjector(), before, after)
}
protected fun doTest(injectable: Injectable, before: String, after: String) {
val configuration = Configuration.getProjectInstance(project).advancedConfiguration
val allowed = configuration.isSourceModificationAllowed
configuration.isSourceModificationAllowed = true
try {
myFixture.configureByText("${getTestName(true)}.kt", before.trimMargin())
InjectLanguageAction.invokeImpl(project, myFixture.editor, myFixture.file, injectable)
myFixture.checkResult(after.trimMargin())
}
finally {
configuration.isSourceModificationAllowed = allowed
}
}
}
@@ -16,11 +16,8 @@
package org.jetbrains.kotlin.psi
import com.intellij.injected.editor.EditorWindow
import com.intellij.lang.html.HTMLLanguage
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference
import com.intellij.psi.injection.Injectable
import com.intellij.testFramework.LightProjectDescriptor
import junit.framework.TestCase
import org.intellij.lang.regexp.RegExpLanguage
import org.intellij.plugins.intelliLang.Configuration
@@ -29,11 +26,8 @@ import org.intellij.plugins.intelliLang.inject.UnInjectLanguageAction
import org.intellij.plugins.intelliLang.inject.config.BaseInjection
import org.intellij.plugins.intelliLang.inject.config.InjectionPlace
import org.intellij.plugins.intelliLang.references.FileReferenceInjector
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
class KotlinInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
class KotlinInjectionTest : AbstractInjectionTest() {
fun testInjectUnInjectOnSimpleString() {
myFixture.configureByText("test.kt",
"""val test = "<caret>simple" """)
@@ -58,14 +52,6 @@ class KotlinInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
unInjectShouldBePresent = false
)
fun testInjectionOnKotlinPredefinedMethodWithRuntime() = testInjectionPresent(
"""
|val test1 = kotlin.text.Regex("<caret>some")
""",
RegExpLanguage.INSTANCE.id,
unInjectShouldBePresent = false
)
fun testInjectionOnJavaCustomInjectionWithAnnotation() {
val customInjection = BaseInjection("java")
customInjection.injectedLanguageId = HTMLLanguage.INSTANCE.id
@@ -292,60 +278,4 @@ class KotlinInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
|}
"""
)
override fun getProjectDescriptor(): LightProjectDescriptor {
val testName = getTestName(true)
return when {
testName.endsWith("WithAnnotation") -> KotlinLightProjectDescriptor.INSTANCE
testName.endsWith("WithRuntime") -> KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
else -> JAVA_LATEST
}
}
private fun testInjectionPresent(text: String, languageId: String? = null, unInjectShouldBePresent: Boolean = true) {
myFixture.configureByText("${getTestName(true)}.kt", text.trimMargin())
TestCase.assertFalse("Injection action is available. There's probably no injection at caret place",
InjectLanguageAction().isAvailable(project, myFixture.editor, myFixture.file))
if (languageId != null) {
val injectedFile = (editor as? EditorWindow)?.injectedFile
assertEquals("Wrong injection language", languageId, injectedFile?.language?.id)
}
if (unInjectShouldBePresent) {
TestCase.assertTrue("UnInjection action is not available. There's no injection at caret place or some other troubles.",
UnInjectLanguageAction().isAvailable(project, myFixture.editor, myFixture.file))
}
}
private fun doRemoveInjectionTest(before: String, after: String) {
myFixture.setCaresAboutInjection(false)
myFixture.configureByText("${getTestName(true)}.kt", before.trimMargin())
TestCase.assertTrue(UnInjectLanguageAction().isAvailable(project, myFixture.editor, myFixture.file))
UnInjectLanguageAction.invokeImpl(project, myFixture.editor, myFixture.file)
myFixture.checkResult(after.trimMargin())
}
private fun doFileReferenceInjectTest(before: String, after: String) {
doTest(FileReferenceInjector(), before, after)
}
private fun doTest(injectable: Injectable, before: String, after: String) {
val configuration = Configuration.getProjectInstance(project).advancedConfiguration
val allowed = configuration.isSourceModificationAllowed
configuration.isSourceModificationAllowed = true
try {
myFixture.configureByText("${getTestName(true)}.kt", before.trimMargin())
InjectLanguageAction.invokeImpl(project, myFixture.editor, myFixture.file, injectable)
myFixture.checkResult(after.trimMargin())
}
finally {
configuration.isSourceModificationAllowed = allowed
}
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.psi
import org.intellij.lang.regexp.RegExpLanguage
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
class KotlinStdlibInjectionTest : AbstractInjectionTest() {
fun testOnRegex0() = testInjection(
"""
|val test1 = kotlin.text.Regex("<caret>some")
""",
RegExpLanguage.INSTANCE.id
)
fun testOnRegex1() = testInjection(
"""
|val test1 = kotlin.text.Regex("<caret>some", RegexOption.COMMENTS)
""",
RegExpLanguage.INSTANCE.id
)
fun testOnRegex2() = testInjection(
"""
|val test1 = kotlin.text.Regex("<caret>some", setOf(RegexOption.COMMENTS))
""",
RegExpLanguage.INSTANCE.id
)
private fun testInjection(text: String, languageId: String) {
testInjectionPresent(text, languageId, false)
}
override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
}