Files
kotlin-fork/idea/tests/org/jetbrains/kotlin/AbstractImportsTest.kt
T

89 lines
4.0 KiB
Kotlin

/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin
import com.intellij.application.options.CodeStyle
import com.intellij.testFramework.LightProjectDescriptor
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
import org.jetbrains.kotlin.idea.core.formatter.KotlinPackageEntry
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
abstract class AbstractImportsTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
protected fun doTest(unused: String) {
val testPath = testPath()
CodeStyle.setTemporarySettings(project, CodeStyle.getSettings(project).clone())
val codeStyleSettings = KotlinCodeStyleSettings.getInstance(project)
try {
val fixture = myFixture
val dependencySuffixes = listOf(".dependency.kt", ".dependency.java", ".dependency1.kt", ".dependency2.kt")
for (suffix in dependencySuffixes) {
val dependencyPath = fileName().replace(".kt", suffix)
if (File(testDataPath, dependencyPath).exists()) {
fixture.configureByFile(dependencyPath)
}
}
fixture.configureByFile(fileName())
val file = fixture.file as KtFile
if (file.isScript()) {
ScriptConfigurationManager.updateScriptDependenciesSynchronously(file)
}
val fileText = file.text
codeStyleSettings.NAME_COUNT_TO_USE_STAR_IMPORT =
InTextDirectivesUtils.getPrefixedInt(fileText, "// NAME_COUNT_TO_USE_STAR_IMPORT:") ?: nameCountToUseStarImportDefault
codeStyleSettings.NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS =
InTextDirectivesUtils.getPrefixedInt(fileText, "// NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS:")
?: nameCountToUseStarImportForMembersDefault
codeStyleSettings.IMPORT_NESTED_CLASSES =
InTextDirectivesUtils.getPrefixedBoolean(fileText, "// IMPORT_NESTED_CLASSES:") ?: false
InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileText, "// PACKAGE_TO_USE_STAR_IMPORTS:").forEach {
codeStyleSettings.PACKAGES_TO_USE_STAR_IMPORTS.addEntry(KotlinPackageEntry(it.trim(), false))
}
InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileText, "// PACKAGES_TO_USE_STAR_IMPORTS:").forEach {
codeStyleSettings.PACKAGES_TO_USE_STAR_IMPORTS.addEntry(KotlinPackageEntry(it.trim(), true))
}
val log = project.executeWriteCommand<String?>("") { doTest(file) }
KotlinTestUtils.assertEqualsToFile(File("$testPath.after"), myFixture.file.text)
if (log != null) {
val logFile = File("$testPath.log")
if (log.isNotEmpty()) {
KotlinTestUtils.assertEqualsToFile(logFile, log)
} else {
TestCase.assertFalse(logFile.exists())
}
}
} finally {
CodeStyle.dropTemporarySettings(project)
}
}
// returns test log
protected abstract fun doTest(file: KtFile): String?
protected open val nameCountToUseStarImportDefault: Int
get() = 1
protected open val nameCountToUseStarImportForMembersDefault: Int
get() = 3
}