Implement main-kts project and test

This commit is contained in:
Ilya Chernikov
2018-08-31 13:33:07 +03:00
parent 79bad24941
commit fa98b0afac
10 changed files with 289 additions and 0 deletions
@@ -0,0 +1,20 @@
description = "Kotlin \"main\" script definition tests"
plugins {
kotlin("jvm")
id("jps-compatible")
}
dependencies {
testCompile(project(":kotlin-main-kts"))
testCompile(project(":kotlin-scripting-jvm-host"))
testCompile(commonDep("junit"))
testRuntime("org.apache.ivy:ivy:2.4.0")
}
sourceSets {
"main" { }
"test" { projectDefault() }
}
@@ -0,0 +1,63 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.mainKts.test
import org.jetbrains.kotlin.mainKts.MainKtsScript
import org.junit.Assert
import org.junit.Test
import java.io.File
import kotlin.script.experimental.api.EvaluationResult
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
import kotlin.script.experimental.api.constructorArgs
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
val scriptDefinition = createJvmCompilationConfigurationFromTemplate<MainKtsScript>()
val evaluationEnv = ScriptEvaluationConfiguration {
constructorArgs(emptyArray<String>())
}
return BasicJvmScriptingHost().eval(scriptFile.toScriptSource(), scriptDefinition, evaluationEnv)
}
class MainKtsTest {
@Test
fun testResolveJunit() {
val res = evalFile(File("testData/hello-resolve-junit.main.kts"))
Assert.assertTrue(
"test failed:\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Success
)
}
@Test
fun testUnresolvedJunit() {
val res = evalFile(File("testData/hello-unresolved-junit.main.kts"))
Assert.assertTrue(
"test failed - expecting a failure with the message \"Unresolved reference: junit\" but received " +
(if (res is ResultWithDiagnostics.Failure) "failure" else "success") +
":\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Failure && res.reports.any { it.message.contains("Unresolved reference: junit") })
}
@Test
fun testResolveError() {
val res = evalFile(File("testData/hello-resolve-error.main.kts"))
Assert.assertTrue(
"test failed - expecting a failure with the message \"Unknown set of arguments to maven resolver: abracadabra\" but received " +
(if (res is ResultWithDiagnostics.Failure) "failure" else "success") +
":\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Failure && res.reports.any { it.message.contains("Unknown set of arguments to maven resolver: abracadabra") })
}
}
@@ -0,0 +1,5 @@
@file:DependsOn("abracadabra")
println("Hello, World!")
@@ -0,0 +1,7 @@
@file:DependsOn("junit:junit:4.11")
org.junit.Assert.assertTrue(true)
println("Hello, World!")
@@ -0,0 +1,5 @@
org.junit.Assert.assertTrue(true)
println("Hello, World!")