Skip to content

Commit

Permalink
introduced new API functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Wirtz committed Aug 22, 2018
1 parent c80a96f commit fdc7bb4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import org.gradle.api.publish.maven.MavenPom
val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion

project.group = "de.swirtz"
project.version = "0.0.2"
project.version = "0.0.3"
val artifactID = "ktsRunner"

plugins {
kotlin("jvm") version "1.2.41"
kotlin("jvm") version "1.2.61"
`maven-publish`
id("com.jfrog.bintray") version "1.8.0"
id("com.github.johnrengelman.shadow") version "2.0.2"
Expand Down
25 changes: 9 additions & 16 deletions src/main/kotlin/de/swirtz/ktsobjectloader/KtsObjectLoader.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.swirtz.ktsobjectloader

import java.io.InputStream
import java.io.Reader
import javax.script.ScriptEngineManager

Expand All @@ -9,23 +10,15 @@ class KtsObjectLoader {
val engine = ScriptEngineManager().getEngineByExtension("kts")
}

inline fun <reified T> load(script: String): T {
val loaded: Any = engine.eval(script)
return if (loaded is T) {
loaded
} else {
throw IllegalStateException("Could not load script from .kts")
}
}
inline fun <reified T> load(script: String): T = engine.eval(script).takeIf { it is T } as T
?: throw IllegalStateException("Could not load script from .kts")

inline fun <reified T> load(reader: Reader): T {
val loaded: Any = engine.eval(reader)
return if (loaded is T) {
loaded
} else {
throw IllegalStateException("Could not load script from .kts")
}
}
inline fun <reified T> load(reader: Reader): T = engine.eval(reader).takeIf { it is T } as T
?: throw IllegalStateException("Could not load script from .kts")

inline fun <reified T> load(inputStream: InputStream): T = engine.eval(inputStream.reader()).takeIf { it is T } as T
?: throw IllegalStateException("Could not load script from .kts")

inline fun <reified T> loadAll(vararg inputStream: InputStream): List<T> = inputStream.map(::load)
}

18 changes: 15 additions & 3 deletions src/test/kotlin/de/swirtz/ktsobjectloader/KtsObjectLoaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.fail


class KtsObjectLoaderTest {

@Test
Expand All @@ -34,7 +33,7 @@ class KtsObjectLoaderTest {

@Test
fun `simple evaluations should work`() {
with(KtsObjectLoader.engine as KotlinJsr223JvmLocalScriptEngine) {
with(KtsObjectLoader.engine as KotlinJsr223JvmLocalScriptEngine) {
val res1 = eval("val x = 3")
assertNull(res1, "No returned value expected")
val res2 = eval("x + 2")
Expand All @@ -52,7 +51,6 @@ class KtsObjectLoaderTest {
assertEquals(15, KtsObjectLoader().load(scriptContent))
}


@Test
fun `class loaded from script`() {
val scriptContent = Files.readAllBytes(Paths.get("src/test/resources/testscript.kts"))?.let {
Expand All @@ -69,4 +67,18 @@ class KtsObjectLoaderTest {
assertEquals(ClassFromScript::class, KtsObjectLoader().load<ClassFromScript>(scriptContent)::class)
}

@Test
fun `class loaded from script via InputStream`() {
val scriptContent = Files.newInputStream(Paths.get("src/test/resources/testscript.kts"))
assertEquals(ClassFromScript::class, KtsObjectLoader().load<ClassFromScript>(scriptContent)::class)
}

@Test
fun `multiple classes loaded from script via InputStream`() {
val scriptContent = Files.newInputStream(Paths.get("src/test/resources/testscript.kts"))
val scriptContent2 = Files.newInputStream(Paths.get("src/test/resources/testscript2.kts"))
KtsObjectLoader().loadAll<ClassFromScript>(scriptContent, scriptContent2).forEach {
assertEquals(ClassFromScript::class, it::class)
}
}
}
3 changes: 3 additions & 0 deletions src/test/resources/testscript2.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import de.swirtz.ktsobjectloader.ClassFromScript

ClassFromScript("2) I was created in kts; äö")

0 comments on commit fdc7bb4

Please sign in to comment.