From e7875f1d5e36039e44bb7a9d779d14a82f97510a Mon Sep 17 00:00:00 2001 From: breandan Date: Sun, 30 May 2021 01:31:37 -0400 Subject: [PATCH] add notebook integration tests as per Kotlin/kotlin-jupyter#270 --- core/build.gradle.kts | 7 ++ .../kotlingrad/notebook/NotebookTest.kt | 107 ++++++++++++++++++ kaliningraph | 2 +- 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 core/src/test/kotlin/edu/umontreal/kotlingrad/notebook/NotebookTest.kt diff --git a/core/build.gradle.kts b/core/build.gradle.kts index e569803c..b39a7c52 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -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" } diff --git a/core/src/test/kotlin/edu/umontreal/kotlingrad/notebook/NotebookTest.kt b/core/src/test/kotlin/edu/umontreal/kotlingrad/notebook/NotebookTest.kt new file mode 100644 index 00000000..404c7926 --- /dev/null +++ b/core/src/test/kotlin/edu/umontreal/kotlingrad/notebook/NotebookTest.kt @@ -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 exec(code: Code): T { + val res = exec(code) + res.shouldBeInstanceOf() + return res + } + + fun execHtml(code: Code): String { + val res = exec(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 + 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") + || (file.extension == "jar" && scriptArtifacts.any { + name.startsWith( + it + ) + }) + } + } + } +} \ No newline at end of file diff --git a/kaliningraph b/kaliningraph index 9b08eb91..4645931e 160000 --- a/kaliningraph +++ b/kaliningraph @@ -1 +1 @@ -Subproject commit 9b08eb91ab0cc035d2318db360c746885c3d7a19 +Subproject commit 4645931e058ff0faa34b453f9923eee1bad44e60