-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add notebook integration tests as per Kotlin/kotlin-jupyter#270
- Loading branch information
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
core/src/test/kotlin/edu/umontreal/kotlingrad/notebook/NotebookTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
|| (file.extension == "jar" && scriptArtifacts.any { | ||
name.startsWith( | ||
it | ||
) | ||
}) | ||
} | ||
} | ||
} | ||
} |
Submodule kaliningraph
updated
from 9b08eb to 464593
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.