Fix SamWithReceiver tests for scripts, add tests...

that use new script definition and ability to load
annotations from script definition
This commit is contained in:
Ilya Chernikov
2020-06-02 12:43:25 +02:00
parent 8fb41e4562
commit 74c697af92
6 changed files with 121 additions and 7 deletions
@@ -17,7 +17,8 @@ dependencies {
testCompile(project(":compiler:cli"))
testCompile(projectTests(":compiler:tests-common"))
testCompile(commonDep("junit:junit"))
testCompileOnly(project(":kotlin-compiler"))
testCompile(project(":kotlin-scripting-jvm-host-unshaded"))
testRuntimeOnly(intellijCoreDep()) { includeJars("intellij-core") }
Platform[192].orHigher {
@@ -0,0 +1,73 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.samWithReceiver
import com.intellij.mock.MockProject
import org.jetbrains.kotlin.checkers.AbstractDiagnosticsTest
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerFromEnvironment
import org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition
import org.jetbrains.kotlin.scripting.resolve.KtFileScriptSource
import org.junit.Assert
import java.io.File
import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptCompilationConfiguration
import kotlin.script.experimental.api.compilerOptions
import kotlin.script.experimental.api.fileExtension
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
import kotlin.script.experimental.jvmhost.createJvmScriptDefinitionFromTemplate
abstract class AbstractSamWithReceiverScriptNewDefTest : AbstractDiagnosticsTest() {
override fun setupEnvironment(environment: KotlinCoreEnvironment) {
val def = ScriptDefinition.FromTemplate(
defaultJvmScriptingHostConfiguration,
ScriptForSamWithReceiversNewDef::class
)
environment.configuration.add(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, def)
// need to make a single script compilation to process definition options before registering SamWithReceiver component
val scriptCompiler = ScriptJvmCompilerFromEnvironment(environment)
val res = scriptCompiler.compile("42".toScriptSource("\$init"), ScriptForSamWithReceiversNewDefCompilationConfiguration)
Assert.assertTrue(res is ResultWithDiagnostics.Success<*>)
SamWithReceiverComponentRegistrar().registerProjectComponents(environment.project as MockProject, environment.configuration)
}
override fun analyzeAndCheck(testDataFile: File, files: List<TestFile>) {
val definition = createJvmScriptDefinitionFromTemplate<ScriptForSamWithReceiversNewDef>()
val scriptCompiler = ScriptJvmCompilerFromEnvironment(environment)
val (scripts, regular) = files.partition {
it.ktFile?.virtualFile?.extension == definition.compilationConfiguration[ScriptCompilationConfiguration.fileExtension]
}
super.analyzeAndCheck(testDataFile, files)
for (file in scripts) {
val res = scriptCompiler.compile(KtFileScriptSource(file.ktFile!!), ScriptForSamWithReceiversNewDefCompilationConfiguration)
val x = res
}
}
}
@KotlinScript(compilationConfiguration = ScriptForSamWithReceiversNewDefCompilationConfiguration::class)
abstract class ScriptForSamWithReceiversNewDef
object ScriptForSamWithReceiversNewDefCompilationConfiguration : ScriptCompilationConfiguration(
{
compilerOptions("-P", "plugin:org.jetbrains.kotlin.samWithReceiver:annotation=SamWithReceiver1")
}
)
@@ -29,12 +29,13 @@ import kotlin.script.templates.ScriptTemplateDefinition
abstract class AbstractSamWithReceiverScriptTest : AbstractDiagnosticsTest() {
override fun setupEnvironment(environment: KotlinCoreEnvironment) {
StorageComponentContainerContributor.registerExtension(environment.project, CliSamWithReceiverComponentContributor(emptyList()))
val def = ScriptDefinition.FromLegacy(
defaultJvmScriptingHostConfiguration,
KotlinScriptDefinitionFromAnnotatedTemplate(ScriptForSamWithReceivers::class, emptyMap())
)
environment.configuration.add(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, def)
val anns = def.annotationsForSamWithReceivers
StorageComponentContainerContributor.registerExtension(environment.project, CliSamWithReceiverComponentContributor(anns))
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2020 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.samWithReceiver;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class SamWithReceiverScriptNewDefTestGenerated extends AbstractSamWithReceiverScriptNewDefTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInScript() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script"), Pattern.compile("^(.+)\\.kts$"), null, true);
}
@TestMetadata("samConversionSimple.kts")
public void testSamConversionSimple() throws Exception {
runTest("plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts");
}
}
@@ -1,4 +1,4 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE,-UNUSED_ANONYMOUS_PARAMETER
// FILE: SamWithReceiver1.java
import java.lang.annotation.Retention;
@@ -11,15 +11,19 @@ public @interface SamWithReceiver1 {
// FILE: Sam.java
@SamWithReceiver1
public interface Sam {
void run(String a);
void run(String a, String b);
}
// FILE: Exec.java
public class Exec {
void exec(Sam sam) {}
void exec(Sam sam) { sam.run("a", "b") }
}
// FILE: test.kts
val e = Exec()
e.exec { System.out.println(this) }
e.exec <!TYPE_MISMATCH!>{ <!EXPECTED_PARAMETERS_NUMBER_MISMATCH!>a, <!CANNOT_INFER_PARAMETER_TYPE!>b<!><!> -> System.out.println(a) }<!>
e.exec { b ->
val a: String = this
System.out.println(a)
}
@@ -11,7 +11,7 @@ public open class Exec {
@SamWithReceiver1 public interface Sam {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public abstract fun run(/*0*/ a: kotlin.String!): kotlin.Unit
public abstract fun run(/*0*/ a: kotlin.String!, /*1*/ b: kotlin.String!): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}