Skip to content

Commit

Permalink
add notebook integration tests as per Kotlin/kotlin-jupyter#270
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed May 30, 2021
1 parent cef2632 commit e7875f1
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
7 changes: 7 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ dependencies {

// Mathematical libraries
implementation("com.github.JetBrains-Research:viktor:1.1.0")

// Notebook integration test
testImplementation(kotlin("reflect"))
testImplementation("junit", "junit", "4.13.2")
testImplementation(kotlin("scripting-jvm"))
testImplementation("org.jetbrains.kotlinx:kotlin-jupyter-kernel:0.10.0-45")

testImplementation("org.nd4j:nd4j-native-platform:1.0.0-beta7")

// val tfVersion by extra { "-SNAPSHOT" }
Expand Down
107 changes: 107 additions & 0 deletions core/src/test/kotlin/edu/umontreal/kotlingrad/notebook/NotebookTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package edu.umontreal.kotlingrad.notebook

import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.types.shouldBeInstanceOf
import jupyter.kotlin.DependsOn
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlinx.jupyter.*
import org.jetbrains.kotlinx.jupyter.api.*
import org.jetbrains.kotlinx.jupyter.libraries.EmptyResolutionInfoProvider
import org.junit.Before
import org.junit.jupiter.api.Test
import kotlin.reflect.KClass
import kotlin.reflect.full.memberProperties
import kotlin.script.experimental.jvm.util.classpathFromClassloader


class RenderingTests: AbstractReplTest() {
@Test
fun `circuit is rendered to html`() {
@Language("kts")
val html = execHtml(
"""
@file:Repository("https://jitpack.io")
@file:DependsOn("com.github.breandan:kotlingrad:0.4.5")
import edu.umontreal.kotlingrad.api.*
val x by SVar(DReal)
val y by SVar(DReal)
val z by SVar(DReal)
val t = (1 + x * 2 - 3 + y + z / y).d(y).d(x) + z / y * 3 - 2; t
""".trimIndent()
)
html shouldContain "polygon"
}
}

// https://github.com/Kotlin/kotlin-jupyter/issues/270
abstract class AbstractReplTest {
private val repl: ReplForJupyter = ReplForJupyterImpl(
EmptyResolutionInfoProvider, classpath, isEmbedded = true
)

@Before
fun initRepl() {
// Jupyter integration is loaded after some code was executed, so we do it here
// We also define here a class to retrieve values without rendering
exec("class $WRAP(val $WRAP_VAL: Any?)")
}

fun exec(code: Code): Any? {
return repl.eval(code).resultValue
}

@JvmName("execTyped")
inline fun <reified T: Any> exec(code: Code): T {
val res = exec(code)
res.shouldBeInstanceOf<T>()
return res
}

fun execHtml(code: Code): String {
val res = exec<MimeTypedResult>(code)
val html = res["text/html"]
html.shouldNotBeNull()
return html
}

fun execWrapped(code: Code): Any? {
val wrapped = exec(code)!!

@Suppress("UNCHECKED_CAST")
val clazz = wrapped::class as KClass<Any>
val prop = clazz.memberProperties.single { it.name == WRAP_VAL }
return prop.get(wrapped)
}

companion object {
@JvmStatic
protected val WRAP = "W"

private const val WRAP_VAL = "v"

private val classpath = run {
val scriptArtifacts = setOf(
"kotlin-jupyter-lib",
"kotlin-jupyter-api",
"kotlin-jupyter-shared-compiler",
"kotlin-stdlib",
"kotlin-reflect",
"kotlin-script-runtime",
)
classpathFromClassloader(DependsOn::class.java.classLoader).orEmpty()
.filter { file ->
val name = file.name
(name == "main" && file.parentFile.name == "kotlin")

This comment has been minimized.

Copy link
@ileasile

ileasile May 30, 2021

Contributor

The problem is here, I believe. Check that classes of your library pass through the filter here in all environments. The standard case is covered here (classes directory, main sourceset), but it may differ in your particular case.

|| (file.extension == "jar" && scriptArtifacts.any {
name.startsWith(
it
)
})
}
}
}
}
2 changes: 1 addition & 1 deletion kaliningraph

0 comments on commit e7875f1

Please sign in to comment.