From 74c697af9265d5db728d378544df1d8085a0e989 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 2 Jun 2020 12:43:25 +0200 Subject: [PATCH] Fix SamWithReceiver tests for scripts, add tests... that use new script definition and ability to load annotations from script definition --- .../sam-with-receiver-cli/build.gradle.kts | 3 +- ...AbstractSamWithReceiverScriptNewDefTest.kt | 73 +++++++++++++++++++ .../AbstractSamWithReceiverScriptTest.kt | 3 +- ...WithReceiverScriptNewDefTestGenerated.java | 35 +++++++++ .../testData/script/samConversionSimple.kts | 12 ++- .../testData/script/samConversionSimple.txt | 2 +- 6 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptNewDefTest.kt create mode 100644 plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/SamWithReceiverScriptNewDefTestGenerated.java diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/build.gradle.kts b/plugins/sam-with-receiver/sam-with-receiver-cli/build.gradle.kts index 103df84659a..28b42b5b0bc 100644 --- a/plugins/sam-with-receiver/sam-with-receiver-cli/build.gradle.kts +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/build.gradle.kts @@ -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 { diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptNewDefTest.kt b/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptNewDefTest.kt new file mode 100644 index 00000000000..557290cd6ec --- /dev/null +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptNewDefTest.kt @@ -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) { + val definition = createJvmScriptDefinitionFromTemplate() + 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") + } +) diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptTest.kt b/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptTest.kt index 918cb20788e..8dadbeade18 100644 --- a/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptTest.kt +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/AbstractSamWithReceiverScriptTest.kt @@ -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)) } } diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/SamWithReceiverScriptNewDefTestGenerated.java b/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/SamWithReceiverScriptNewDefTestGenerated.java new file mode 100644 index 00000000000..3a6602d98d0 --- /dev/null +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/test/org/jetbrains/kotlin/samWithReceiver/SamWithReceiverScriptNewDefTestGenerated.java @@ -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"); + } +} diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts b/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts index 5cae9fe5dd5..b64b43f2c58 100644 --- a/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.kts +++ b/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 { a, b -> System.out.println(a) } +e.exec { b -> + val a: String = this + System.out.println(a) +} diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.txt b/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.txt index b980ba83850..96f6aec890d 100644 --- a/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.txt +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/testData/script/samConversionSimple.txt @@ -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 }