Added import-resolve integration tests

This commit is contained in:
Andrey Uskov
2019-11-14 21:06:54 +03:00
parent e9946b21b5
commit 9643d5d28e
16 changed files with 306 additions and 69 deletions
@@ -40,79 +40,85 @@ class GradleMultiplatformHighlightingTest : GradleImportingTestCase() {
val files = importProjectFromTestData()
val project = myTestFixture.project
checkFiles(files, project) { editor ->
daemonAnalyzerTestCase.checkHighlighting(project, editor)
checkFiles(files, project, GradleDaemonAnalyzerTestCase(testLineMarkers = true, checkWarnings = true, checkInfos = false)) { file ->
file.extension == "kt"
}
}
private val daemonAnalyzerTestCase = object : DaemonAnalyzerTestCase() {
override fun doTestLineMarkers() = true
fun checkHighlighting(project: Project, editor: Editor) {
myProject = project
runInEdtAndWait {
checkHighlighting(editor, /* checkWarnings = */ true, /* checkInfos = */ false)
}
}
}
private fun checkFiles(files: List<VirtualFile>, project: Project, check: (Editor) -> Unit) {
var atLeastOneFile = false
val kotlinFiles = files.filter { it.extension == "kt" }
val content = mutableMapOf<VirtualFile, String>()
kotlinFiles.forEach { file ->
val (_, textWithTags) = configureEditorByExistingFile(file, project)
atLeastOneFile = true
content[file] = textWithTags
}
Assert.assertTrue(atLeastOneFile)
kotlinFiles.forEach { file ->
val (editor, _) = configureEditorByExistingFile(file, project, content[file])
check(editor)
}
}
private fun configureEditorByExistingFile(
virtualFile: VirtualFile,
project: Project,
contentToSet: String? = null
): Pair<Editor, String> {
var result: Pair<Editor, String>? = null
runInEdtAndWait {
val editor = createEditor(virtualFile, project)
val document = editor.document
val editorInfo = EditorInfo(document.text)
val textWithTags = editorInfo.newFileText
ApplicationManager.getApplication().runWriteAction {
val newText = contentToSet ?: textWithTags.withoutTags()
if (document.text != newText) {
document.setText(newText)
}
editorInfo.applyToEditor(editor)
}
PsiDocumentManager.getInstance(project).commitAllDocuments()
result = editor to textWithTags
}
return result!!
}
private fun String.withoutTags(): String {
val regex = "</?(error|warning|lineMarker).*?>".toRegex()
return regex.replace(this, "")
}
private fun createEditor(file: VirtualFile, project: Project): Editor {
val instance = FileEditorManager.getInstance(this.myProject)
PsiDocumentManager.getInstance(project).commitAllDocuments()
val editor = instance.openTextEditor(OpenFileDescriptor(this.myProject, file, 0), false)
(editor as EditorImpl).setCaretActive()
PsiDocumentManager.getInstance(project).commitAllDocuments()
DaemonCodeAnalyzer.getInstance(project).restart()
return editor
}
override fun testDataDirName(): String {
return "newMultiplatformHighlighting"
}
}
class GradleDaemonAnalyzerTestCase(val testLineMarkers: Boolean, val checkWarnings: Boolean, val checkInfos: Boolean) :
DaemonAnalyzerTestCase() {
override fun doTestLineMarkers() = testLineMarkers
fun checkHighlighting(project: Project, editor: Editor) {
myProject = project
runInEdtAndWait {
checkHighlighting(editor, checkWarnings, checkInfos)
}
}
}
internal fun checkFiles(
files: List<VirtualFile>,
project: Project,
analyzer: GradleDaemonAnalyzerTestCase,
fileFilter: (VirtualFile) -> Boolean
) {
var atLeastOneFile = false
val kotlinFiles = files.filter(fileFilter)
val content = mutableMapOf<VirtualFile, String>()
kotlinFiles.forEach { file ->
val (_, textWithTags) = configureEditorByExistingFile(file, project)
atLeastOneFile = true
content[file] = textWithTags
}
Assert.assertTrue(atLeastOneFile)
kotlinFiles.forEach { file ->
val (editor, _) = configureEditorByExistingFile(file, project, content[file])
analyzer.checkHighlighting(project, editor)
}
}
internal fun textWithoutTags(text: String): String {
val regex = "</?(error|warning|lineMarker).*?>".toRegex()
return regex.replace(text, "")
}
private fun createEditor(file: VirtualFile, project: Project): Editor {
val instance = FileEditorManager.getInstance(project)
PsiDocumentManager.getInstance(project).commitAllDocuments()
val editor = instance.openTextEditor(OpenFileDescriptor(project, file, 0), false)
(editor as EditorImpl).setCaretActive()
PsiDocumentManager.getInstance(project).commitAllDocuments()
DaemonCodeAnalyzer.getInstance(project).restart()
return editor
}
internal fun configureEditorByExistingFile(
virtualFile: VirtualFile,
project: Project,
contentToSet: String? = null
): Pair<Editor, String> {
var result: Pair<Editor, String>? = null
runInEdtAndWait {
val editor = createEditor(virtualFile, project)
val document = editor.document
val editorInfo = EditorInfo(document.text)
val textWithTags = editorInfo.newFileText
ApplicationManager.getApplication().runWriteAction {
val newText = contentToSet ?: textWithoutTags(textWithTags)
if (document.text != newText) {
document.setText(newText)
}
editorInfo.applyToEditor(editor)
}
PsiDocumentManager.getInstance(project).commitAllDocuments()
result = editor to textWithTags
}
return result!!
}
@@ -0,0 +1,39 @@
/*
* 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.gradle
import org.jetbrains.kotlin.idea.codeInsight.gradle.MultiplePluginVersionGradleImportingTestCase
import org.junit.Test
import org.jetbrains.plugins.gradle.tooling.annotation.PluginTargetVersions
class ImportAndCheckHighlighting : MultiplePluginVersionGradleImportingTestCase() {
@Test
@PluginTargetVersions(gradleVersion = "4.0+", pluginVersion = "1.3.40+")
fun testMultiplatformLibrary() {
importAndCheckHighlighting()
}
@Test
@PluginTargetVersions(gradleVersion = "4.0+", pluginVersion = "1.3.40+")
fun testUnresolvedInMultiplatformLibrary() {
importAndCheckHighlighting(false, false)
}
private fun importAndCheckHighlighting(testLineMarkers: Boolean = true, checkWarnings: Boolean = true) {
val files = configureByFiles()
importProject()
val project = myTestFixture.project
checkFiles(files, project, GradleDaemonAnalyzerTestCase(testLineMarkers = testLineMarkers, checkWarnings = checkWarnings, checkInfos = false)) { file ->
file.extension == "kt" || file.extension == "java"
}
}
override fun testDataDirName(): String {
return "importAndCheckHighlighting"
}
}
@@ -0,0 +1,59 @@
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '{{kotlin_plugin_version}}'
}
repositories {
mavenCentral()
}
group 'com.example'
version '0.0.1'
apply plugin: 'maven-publish'
kotlin {
jvm()
js {
browser {
}
nodejs {
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation kotlin('stdlib-jdk8')
}
}
jvmTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
jsMain {
dependencies {
implementation kotlin('stdlib-js')
}
}
jsTest {
dependencies {
implementation kotlin('test-js')
}
}
mingwMain {
}
mingwTest {
}
}
}
@@ -0,0 +1 @@
kotlin.code.style=official
@@ -0,0 +1,3 @@
rootProject.name = 'mppLibrary'
enableFeaturePreview('GRADLE_METADATA')
@@ -0,0 +1,11 @@
package sample
expect class <lineMarker descr="Has actuals in JS, JVM">Sample</lineMarker>() {
fun <lineMarker>checkMe</lineMarker>(): Int
}
expect object <lineMarker descr="Has actuals in JS, JVM">Platform</lineMarker> {
val <lineMarker descr="Has actuals in JS, JVM">name</lineMarker>: String
}
fun hello(): String = "Hello from ${Platform.name}"
@@ -0,0 +1,11 @@
package sample
import kotlin.test.Test
import kotlin.test.assertTrue
class <lineMarker descr="Run Test">SampleTests</lineMarker> {
@Test
fun <lineMarker descr="Run Test">testMe</lineMarker>() {
assertTrue(Sample().checkMe() > 0)
}
}
@@ -0,0 +1,9 @@
package sample
actual class <lineMarker descr="Has declaration in common module">Sample</lineMarker> {
actual fun <lineMarker descr="Has declaration in common module">checkMe</lineMarker>() = 12
}
actual object <lineMarker descr="Has declaration in common module">Platform</lineMarker> {
actual val <lineMarker descr="Has declaration in common module">name</lineMarker>: String = "JS"
}
@@ -0,0 +1,9 @@
package sample
actual class <lineMarker descr="Has declaration in common module">Sample</lineMarker> {
actual fun <lineMarker descr="Has declaration in common module">checkMe</lineMarker>() = 42
}
actual object <lineMarker descr="Has declaration in common module">Platform</lineMarker> {
actual val <lineMarker descr="Has declaration in common module">name</lineMarker>: String = "JVM"
}
@@ -0,0 +1,11 @@
package sample
import kotlin.test.Test
import kotlin.test.assertTrue
class <lineMarker descr="Run Test">SampleTestsJVM</lineMarker> {
@Test
fun <lineMarker descr="Run Test">testHello</lineMarker>() {
assertTrue("JVM" in hello())
}
}
@@ -0,0 +1,59 @@
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '{{kotlin_plugin_version}}'
}
repositories {
mavenCentral()
}
group 'com.example'
version '0.0.1'
apply plugin: 'maven-publish'
kotlin {
jvm()
js {
browser {
}
nodejs {
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation kotlin('stdlib-jdk8')
}
}
jvmTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
jsMain {
dependencies {
implementation kotlin('stdlib-js')
}
}
jsTest {
dependencies {
implementation kotlin('test-js')
}
}
mingwMain {
}
mingwTest {
}
}
}
@@ -0,0 +1 @@
kotlin.code.style=official
@@ -0,0 +1,3 @@
rootProject.name = 'mppLibrary'
enableFeaturePreview('GRADLE_METADATA')
@@ -0,0 +1,4 @@
package sample
fun common(): Boolean {
return <error descr="[TYPE_MISMATCH] Type mismatch: inferred type is String but Boolean was expected">""</error>
}
@@ -0,0 +1,5 @@
package sample
fun js() {
println(common())
}
@@ -0,0 +1,6 @@
package sample
fun jvm() {
println(common())
println(<error descr="[UNRESOLVED_REFERENCE] Unresolved reference: js">js</error>())
}