Refactoring: always use compiler settings with de-configuration in tests

This commit is contained in:
Nikolay Krasko
2020-02-13 19:42:46 +03:00
committed by Nikolay Krasko
parent 018215f47d
commit 3a5f42cc5e
17 changed files with 373 additions and 397 deletions
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.idea.perf
import org.jetbrains.kotlin.idea.completion.test.handlers.CompletionHandlerTestBase
import com.intellij.application.options.CodeStyle
import com.intellij.codeInsight.completion.CompletionType
import com.intellij.openapi.application.runWriteAction
@@ -17,10 +16,10 @@ import org.jetbrains.kotlin.idea.completion.test.handlers.AbstractCompletionHand
import org.jetbrains.kotlin.idea.completion.test.handlers.AbstractCompletionHandlerTest.Companion.INVOCATION_COUNT_PREFIX
import org.jetbrains.kotlin.idea.completion.test.handlers.AbstractCompletionHandlerTest.Companion.LOOKUP_STRING_PREFIX
import org.jetbrains.kotlin.idea.completion.test.handlers.AbstractCompletionHandlerTest.Companion.TAIL_TEXT_PREFIX
import org.jetbrains.kotlin.idea.completion.test.handlers.CompletionHandlerTestBase
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.idea.test.withCustomCompilerOptions
import org.jetbrains.kotlin.idea.testFramework.commitAllDocuments
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.utils.addToStdlib.indexOfOrNull
@@ -62,44 +61,42 @@ abstract class AbstractPerformanceCompletionHandlerTests(
val tempSettings = CodeStyle.getSettings(project).clone()
CodeStyle.setTemporarySettings(project, tempSettings)
val fileText = FileUtil.loadFile(File(testPath))
val configured = configureCompilerOptions(fileText, project, module)
try {
assertTrue("\"<caret>\" is missing in file \"$testPath\"", fileText.contains("<caret>"))
val fileText = FileUtil.loadFile(File(testPath))
withCustomCompilerOptions(fileText, project, module) {
assertTrue("\"<caret>\" is missing in file \"$testPath\"", fileText.contains("<caret>"))
val invocationCount = InTextDirectivesUtils.getPrefixedInt(fileText, INVOCATION_COUNT_PREFIX) ?: 1
val lookupString = InTextDirectivesUtils.findStringWithPrefixes(fileText, LOOKUP_STRING_PREFIX)
val itemText = InTextDirectivesUtils.findStringWithPrefixes(fileText, ELEMENT_TEXT_PREFIX)
val tailText = InTextDirectivesUtils.findStringWithPrefixes(fileText, TAIL_TEXT_PREFIX)
val completionChars = completionChars(fileText)
val invocationCount = InTextDirectivesUtils.getPrefixedInt(fileText, INVOCATION_COUNT_PREFIX) ?: 1
val lookupString = InTextDirectivesUtils.findStringWithPrefixes(fileText, LOOKUP_STRING_PREFIX)
val itemText = InTextDirectivesUtils.findStringWithPrefixes(fileText, ELEMENT_TEXT_PREFIX)
val tailText = InTextDirectivesUtils.findStringWithPrefixes(fileText, TAIL_TEXT_PREFIX)
val completionChars = completionChars(fileText)
val completionType = ExpectedCompletionUtils.getCompletionType(fileText) ?: defaultCompletionType
val completionType = ExpectedCompletionUtils.getCompletionType(fileText) ?: defaultCompletionType
val kotlinStyleSettings = KotlinCodeStyleSettings.getInstance(project)
val commonStyleSettings = CodeStyle.getLanguageSettings(file)
for (line in InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileText, CODE_STYLE_SETTING_PREFIX)) {
val index = line.indexOfOrNull('=') ?: error("Invalid code style setting '$line': '=' expected")
val settingName = line.substring(0, index).trim()
val settingValue = line.substring(index + 1).trim()
val (field, settings) = try {
kotlinStyleSettings::class.java.getField(settingName) to kotlinStyleSettings
} catch (e: NoSuchFieldException) {
commonStyleSettings::class.java.getField(settingName) to commonStyleSettings
}
when (field.type.name) {
"boolean" -> field.setBoolean(settings, settingValue.toBoolean())
"int" -> field.setInt(settings, settingValue.toInt())
else -> error("Unsupported setting type: ${field.type}")
val kotlinStyleSettings = KotlinCodeStyleSettings.getInstance(project)
val commonStyleSettings = CodeStyle.getLanguageSettings(file)
for (line in InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileText, CODE_STYLE_SETTING_PREFIX)) {
val index = line.indexOfOrNull('=') ?: error("Invalid code style setting '$line': '=' expected")
val settingName = line.substring(0, index).trim()
val settingValue = line.substring(index + 1).trim()
val (field, settings) = try {
kotlinStyleSettings::class.java.getField(settingName) to kotlinStyleSettings
} catch (e: NoSuchFieldException) {
commonStyleSettings::class.java.getField(settingName) to commonStyleSettings
}
when (field.type.name) {
"boolean" -> field.setBoolean(settings, settingValue.toBoolean())
"int" -> field.setInt(settings, settingValue.toInt())
else -> error("Unsupported setting type: ${field.type}")
}
}
doPerfTestWithTextLoaded(
testPath, completionType, invocationCount, lookupString, itemText, tailText, completionChars,
)
}
doPerfTestWithTextLoaded(
testPath, completionType, invocationCount, lookupString, itemText, tailText, completionChars
)
} finally {
if (configured) {
rollbackCompilerOptions(project, module)
}
CodeStyle.dropTemporarySettings(project)
tearDownFixture()
}