Don't forget to rollback configured compiler options in IDE tests

This commit is contained in:
Mikhail Zarechenskiy
2019-05-06 04:33:46 +03:00
parent 4ed5c5363f
commit e8e8f6f336
4 changed files with 89 additions and 68 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.idea.completion.test.configureWithExtraFile
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.configureCompilerOptions
import org.jetbrains.kotlin.idea.test.rollbackCompilerOptions
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.utils.addToStdlib.indexOfOrNull
import java.io.File
@@ -30,11 +31,11 @@ abstract class AbstractCompletionHandlerTest(private val defaultCompletionType:
val tempSettings = CodeStyle.getSettings(project).clone()
CodeStyle.setTemporarySettings(project, tempSettings)
val fileText = FileUtil.loadFile(File(testPath))
val configured = configureCompilerOptions(fileText, project, module)
try {
val fileText = FileUtil.loadFile(File(testPath))
assertTrue("\"<caret>\" is missing in file \"$testPath\"", fileText.contains("<caret>"));
configureCompilerOptions(fileText, project, module)
val invocationCount = InTextDirectivesUtils.getPrefixedInt(fileText, INVOCATION_COUNT_PREFIX) ?: 1
val lookupString = InTextDirectivesUtils.findStringWithPrefixes(fileText, LOOKUP_STRING_PREFIX)
@@ -71,6 +72,9 @@ abstract class AbstractCompletionHandlerTest(private val defaultCompletionType:
doTestWithTextLoaded(completionType, invocationCount, lookupString, itemText, tailText, completionChar, File(testPath).name + ".after")
} finally {
if (configured) {
rollbackCompilerOptions(project, module)
}
CodeStyle.dropTemporarySettings(project)
tearDownFixture()
}
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.idea.completion.test.configureWithExtraFile
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.configureCompilerOptions
import org.jetbrains.kotlin.idea.test.rollbackCompilerOptions
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.junit.Assert
@@ -24,13 +25,19 @@ abstract class AbstractCompletionWeigherTest(val completionType: CompletionType,
val text = myFixture.editor.document.text
configureCompilerOptions(text, project, module)
val configured = configureCompilerOptions(text, project, module)
val items = InTextDirectivesUtils.findArrayWithPrefixes(text, "// ORDER:")
Assert.assertTrue("""Some items should be defined with "// ORDER:" directive""", !items.isEmpty())
myFixture.complete(completionType, InTextDirectivesUtils.getPrefixedInt(text, "// INVOCATION_COUNT:") ?: 1)
myFixture.assertPreferredCompletionItems(InTextDirectivesUtils.getPrefixedInt(text, "// SELECTED:") ?: 0, *items)
try {
myFixture.complete(completionType, InTextDirectivesUtils.getPrefixedInt(text, "// INVOCATION_COUNT:") ?: 1)
myFixture.assertPreferredCompletionItems(InTextDirectivesUtils.getPrefixedInt(text, "// SELECTED:") ?: 0, *items)
} finally {
if (configured) {
rollbackCompilerOptions(project, module)
}
}
}
}
@@ -15,10 +15,7 @@ import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.util.PathUtil
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.test.SdkAndMockLibraryProjectDescriptor
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.idea.test.ProjectDescriptorWithStdlibSources
import org.jetbrains.kotlin.idea.test.configureCompilerOptions
import org.jetbrains.kotlin.idea.test.*
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.allChildren
@@ -53,43 +50,49 @@ abstract class AbstractParameterInfoTest : LightCodeInsightFixtureTestCase() {
val file = myFixture.file as KtFile
configureCompilerOptions(file.text, project, myFixture.module)
val configured = configureCompilerOptions(file.text, project, myFixture.module)
val lastChild = file.allChildren.filter { it !is PsiWhiteSpace }.last()
val expectedResultText = when (lastChild.node.elementType) {
KtTokens.BLOCK_COMMENT -> lastChild.text.substring(2, lastChild.text.length - 2).trim()
KtTokens.EOL_COMMENT -> lastChild.text.substring(2).trim()
else -> error("Unexpected last file child")
try {
val lastChild = file.allChildren.filter { it !is PsiWhiteSpace }.last()
val expectedResultText = when (lastChild.node.elementType) {
KtTokens.BLOCK_COMMENT -> lastChild.text.substring(2, lastChild.text.length - 2).trim()
KtTokens.EOL_COMMENT -> lastChild.text.substring(2).trim()
else -> error("Unexpected last file child")
}
val context = ShowParameterInfoContext(editor, project, file, editor.caretModel.offset, -1, true)
val handlers = ShowParameterInfoHandler.getHandlers(project, KotlinLanguage.INSTANCE)!!
val handler = handlers.firstOrNull { it.findElementForParameterInfo(context) != null }
?: error("Could not find parameter info handler")
val mockCreateParameterInfoContext = MockCreateParameterInfoContext(file, myFixture)
val parameterOwner = handler.findElementForParameterInfo(mockCreateParameterInfoContext) as PsiElement
val textToType = InTextDirectivesUtils.findStringWithPrefixes(file.text, "// TYPE:")
if (textToType != null) {
myFixture.type(textToType)
PsiDocumentManager.getInstance(project).commitAllDocuments()
}
//to update current parameter index
val updateContext = MockUpdateParameterInfoContext(file, myFixture)
val elementForUpdating = handler.findElementForUpdatingParameterInfo(updateContext)
if (elementForUpdating != null) {
handler.updateParameterInfo(elementForUpdating, updateContext)
}
val parameterInfoUIContext = MockParameterInfoUIContext(parameterOwner, updateContext.currentParameter)
mockCreateParameterInfoContext.itemsToShow?.forEach {
handler.updateUI(it, parameterInfoUIContext)
}
Assert.assertEquals(expectedResultText, parameterInfoUIContext.resultText)
} finally {
if (configured) {
rollbackCompilerOptions(project, myFixture.module)
}
}
val context = ShowParameterInfoContext(editor, project, file, editor.caretModel.offset, -1, true)
val handlers = ShowParameterInfoHandler.getHandlers(project, KotlinLanguage.INSTANCE)!!
val handler = handlers.firstOrNull { it.findElementForParameterInfo(context) != null }
?: error("Could not find parameter info handler")
val mockCreateParameterInfoContext = MockCreateParameterInfoContext(file, myFixture)
val parameterOwner = handler.findElementForParameterInfo(mockCreateParameterInfoContext) as PsiElement
val textToType = InTextDirectivesUtils.findStringWithPrefixes(file.text, "// TYPE:")
if (textToType != null) {
myFixture.type(textToType)
PsiDocumentManager.getInstance(project).commitAllDocuments()
}
//to update current parameter index
val updateContext = MockUpdateParameterInfoContext(file, myFixture)
val elementForUpdating = handler.findElementForUpdatingParameterInfo(updateContext)
if (elementForUpdating != null) {
handler.updateParameterInfo(elementForUpdating, updateContext)
}
val parameterInfoUIContext = MockParameterInfoUIContext(parameterOwner, updateContext.currentParameter)
mockCreateParameterInfoContext.itemsToShow?.forEach {
handler.updateUI(it, parameterInfoUIContext)
}
Assert.assertEquals(expectedResultText, parameterInfoUIContext.resultText)
}
}
@@ -19,6 +19,7 @@ import junit.framework.TestCase
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.configureCompilerOptions
import org.jetbrains.kotlin.idea.test.rollbackCompilerOptions
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.KotlinTestUtils
@@ -40,37 +41,43 @@ abstract class AbstractInlineTest : KotlinLightCodeInsightFixtureTestCase() {
val extraFilesToPsi = extraFiles.associateBy { fixture.configureByFile(path.replace(mainFileName, it.name)) }
val file = myFixture.configureByFile(path)
configureCompilerOptions(file.text, project, module)
val configured = configureCompilerOptions(file.text, project, module)
val afterFileExists = afterFile.exists()
try {
val afterFileExists = afterFile.exists()
val targetElement = TargetElementUtil.findTargetElement(myFixture.editor, ELEMENT_NAME_ACCEPTED or REFERENCED_ELEMENT_ACCEPTED)!!
val handler = Extensions.getExtensions(InlineActionHandler.EP_NAME).firstOrNull { it.canInlineElement(targetElement) }
val expectedErrors = InTextDirectivesUtils.findLinesWithPrefixesRemoved(myFixture.file.text, "// ERROR: ")
if (handler != null) {
try {
runWriteAction { handler.inlineElement(myFixture.project, myFixture.editor, targetElement) }
val targetElement = TargetElementUtil.findTargetElement(myFixture.editor, ELEMENT_NAME_ACCEPTED or REFERENCED_ELEMENT_ACCEPTED)!!
val handler = Extensions.getExtensions(InlineActionHandler.EP_NAME).firstOrNull { it.canInlineElement(targetElement) }
val expectedErrors = InTextDirectivesUtils.findLinesWithPrefixesRemoved(myFixture.file.text, "// ERROR: ")
if (handler != null) {
try {
runWriteAction { handler.inlineElement(myFixture.project, myFixture.editor, targetElement) }
UsefulTestCase.assertEmpty(expectedErrors)
KotlinTestUtils.assertEqualsToFile(afterFile, file.text)
for ((extraPsiFile, extraFile) in extraFilesToPsi) {
KotlinTestUtils.assertEqualsToFile(File("${extraFile.path}.after"), extraPsiFile.text)
UsefulTestCase.assertEmpty(expectedErrors)
KotlinTestUtils.assertEqualsToFile(afterFile, file.text)
for ((extraPsiFile, extraFile) in extraFilesToPsi) {
KotlinTestUtils.assertEqualsToFile(File("${extraFile.path}.after"), extraPsiFile.text)
}
}
catch (e: CommonRefactoringUtil.RefactoringErrorHintException) {
TestCase.assertFalse("Refactoring not available: ${e.message}", afterFileExists)
TestCase.assertEquals("Expected errors", 1, expectedErrors.size)
TestCase.assertEquals("Error message", expectedErrors[0].replace("\\n", "\n"), e.message)
}
catch (e: BaseRefactoringProcessor.ConflictsInTestsException) {
TestCase.assertFalse("Conflicts: ${e.message}", afterFileExists)
TestCase.assertEquals("Expected errors", 1, expectedErrors.size)
TestCase.assertEquals("Error message", expectedErrors[0].replace("\\n", "\n"), e.message)
}
}
catch (e: CommonRefactoringUtil.RefactoringErrorHintException) {
TestCase.assertFalse("Refactoring not available: ${e.message}", afterFileExists)
TestCase.assertEquals("Expected errors", 1, expectedErrors.size)
TestCase.assertEquals("Error message", expectedErrors[0].replace("\\n", "\n"), e.message)
else {
TestCase.assertFalse("No refactoring handler available", afterFileExists)
}
catch (e: BaseRefactoringProcessor.ConflictsInTestsException) {
TestCase.assertFalse("Conflicts: ${e.message}", afterFileExists)
TestCase.assertEquals("Expected errors", 1, expectedErrors.size)
TestCase.assertEquals("Error message", expectedErrors[0].replace("\\n", "\n"), e.message)
} finally {
if (configured) {
rollbackCompilerOptions(project, module)
}
}
else {
TestCase.assertFalse("No refactoring handler available", afterFileExists)
}
}
override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE