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
@@ -11,8 +11,7 @@ import com.intellij.openapi.util.io.FileUtil
import org.jetbrains.kotlin.idea.caches.project.LibraryModificationTracker
import org.jetbrains.kotlin.idea.test.CompilerTestDirectives
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
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.platform.TargetPlatform
import java.io.File
@@ -30,27 +29,24 @@ abstract class KotlinFixtureCompletionBaseTestCase : KotlinLightCodeInsightFixtu
setUpFixture(testPath)
val fileText = FileUtil.loadFile(File(testPath), true)
val configured = configureCompilerOptions(fileText, project, module)
try {
withCustomCompilerOptions(fileText, project, module) {
assertTrue("\"<caret>\" is missing in file \"$testPath\"", fileText.contains("<caret>"))
assertTrue("\"<caret>\" is missing in file \"$testPath\"", fileText.contains("<caret>"))
if (ExpectedCompletionUtils.shouldRunHighlightingBeforeCompletion(fileText)) {
myFixture.doHighlighting()
}
if (ExpectedCompletionUtils.shouldRunHighlightingBeforeCompletion(fileText)) {
myFixture.doHighlighting()
testCompletion(
fileText,
getPlatform(),
{ completionType, count -> complete(completionType, count) },
defaultCompletionType(),
defaultInvocationCount(),
additionalValidDirectives = CompilerTestDirectives.ALL_COMPILER_TEST_DIRECTIVES
)
}
testCompletion(
fileText,
getPlatform(),
{ completionType, count -> complete(completionType, count) },
defaultCompletionType(),
defaultInvocationCount(),
additionalValidDirectives = CompilerTestDirectives.ALL_COMPILER_TEST_DIRECTIVES
)
} finally {
if (configured) {
rollbackCompilerOptions(project, module)
}
tearDownFixture()
}
}
@@ -12,8 +12,7 @@ import org.jetbrains.kotlin.idea.completion.test.ExpectedCompletionUtils
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.idea.test.withCustomCompilerOptions
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.utils.addToStdlib.indexOfOrNull
import java.io.File
@@ -36,51 +35,48 @@ 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 {
assertTrue("\"<caret>\" is missing in file \"$testPath\"", fileText.contains("<caret>"))
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}")
}
}
doTestWithTextLoaded(
myFixture,
completionType,
invocationCount,
lookupString,
itemText,
tailText,
completionChars,
File(testPath).name + ".after",
)
}
doTestWithTextLoaded(
myFixture,
completionType,
invocationCount,
lookupString,
itemText,
tailText,
completionChars,
File(testPath).name + ".after"
)
} finally {
if (configured) {
rollbackCompilerOptions(project, module)
}
CodeStyle.dropTemporarySettings(project)
tearDownFixture()
}
@@ -10,8 +10,7 @@ import org.jetbrains.kotlin.idea.completion.test.RELATIVE_COMPLETION_TEST_DATA_B
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.idea.test.withCustomCompilerOptions
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.junit.Assert
@@ -26,18 +25,12 @@ abstract class AbstractCompletionWeigherTest(val completionType: CompletionType,
val text = myFixture.editor.document.text
val configured = configureCompilerOptions(text, project, module)
val items = InTextDirectivesUtils.findArrayWithPrefixes(text, "// ORDER:")
Assert.assertTrue("""Some items should be defined with "// ORDER:" directive""", items.isNotEmpty())
try {
withCustomCompilerOptions(text, project, module) {
myFixture.complete(completionType, InTextDirectivesUtils.getPrefixedInt(text, "// INVOCATION_COUNT:") ?: 1)
myFixture.assertPreferredCompletionItems(InTextDirectivesUtils.getPrefixedInt(text, "// SELECTED:") ?: 0, *items)
} finally {
if (configured) {
rollbackCompilerOptions(project, module)
}
}
}
}