Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ artifacts {
builtBy(tasks.jar)
}
}

dependencies {
testImplementation(project(":common-test-utils"))
}
3 changes: 3 additions & 0 deletions common-test-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## :common-test-utils

This internal module contains shared test utilities.
17 changes: 17 additions & 0 deletions common-test-utils/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
with(conventions.plugins.dfbuild) {
alias(kotlinJvm11)
}
}

group = "org.jetbrains.kotlinx"

dependencies {
api(projects.core)

api(libs.kotestAssertions) {
exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
}
implementation(libs.kotlin.stdlib)
implementation(libs.kotlin.reflect)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jetbrains.kotlinx.dataframe

import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.print
import org.jetbrains.kotlinx.dataframe.api.schema
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
import org.jetbrains.kotlinx.dataframe.io.renderToString
import kotlin.reflect.typeOf

public fun <T : DataFrame<*>> T.toDebugString(rowsLimit: Int = 20): String =
"""
${renderToString(borders = true, title = true, columnTypes = true, valueLimit = -1, rowsLimit = rowsLimit)}

${schema()}
""".trimIndent()

/**
* Prints dataframe to console with borders, title, column types and schema
*/
public fun <T : DataFrame<*>> T.alsoDebug(println: String? = null, rowsLimit: Int = 20): T =
apply {
println?.let { println(it) }
print(borders = true, title = true, columnTypes = true, valueLimit = -1, rowsLimit = rowsLimit)
schema().print()
}
Comment on lines +23 to +28

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point


public fun DataFrame<*>.shouldHaveColumnGroup(name: String, block: (ColumnGroup<*>) -> Unit = { }): ColumnGroup<*> =
getColumnOrNull(name).shouldBeInstanceOf<ColumnGroup<*>>(block)

public fun DataFrame<*>.shouldHaveFrameColumn(name: String, block: (FrameColumn<*>) -> Unit = { }): FrameColumn<*> =
getColumnOrNull(name).shouldBeInstanceOf<FrameColumn<*>>(block)

public inline fun <reified T> DataFrame<*>.shouldHaveColumn(
name: String,
block: (DataColumn<T>) -> Unit = { },
): DataColumn<T> {
val shouldBeInstanceOf = getColumnOrNull(name).shouldBeInstanceOf<DataColumn<*>>()
shouldBeInstanceOf.type() shouldBe typeOf<T>()
val cast = shouldBeInstanceOf.cast<T>()
block(cast)
return cast
}
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
testImplementation(libs.kotestAssertions) {
exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
}

// kotest shouldBe forcefully escapes newlines when content is identical except additional newlines
// which makes diff very hard to perceive. CodeGenerationTests.kt suffers from it a lot. using assertEquals there for working diff.
testImplementation(kotlin("test"))
Expand Down
43 changes: 0 additions & 43 deletions core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,53 +1,10 @@
package org.jetbrains.kotlinx.dataframe

import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.print
import org.jetbrains.kotlinx.dataframe.api.schema
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
import org.jetbrains.kotlinx.dataframe.io.renderToString
import org.jetbrains.kotlinx.dataframe.types.UtilTests
import java.net.URL
import kotlin.reflect.typeOf

fun testResource(resourcePath: String): URL = UtilTests::class.java.classLoader.getResource(resourcePath)!!

fun testCsv(csvName: String) = testResource("$csvName.csv")

fun testJson(jsonName: String) = testResource("$jsonName.json")

fun <T : DataFrame<*>> T.toDebugString(rowsLimit: Int = 20) =
"""
${renderToString(borders = true, title = true, columnTypes = true, valueLimit = -1, rowsLimit = rowsLimit)}

${schema()}
""".trimIndent()

/**
* Prints dataframe to console with borders, title, column types and schema
*/
fun <T : DataFrame<*>> T.alsoDebug(println: String? = null, rowsLimit: Int = 20): T =
apply {
println?.let { println(it) }
print(borders = true, title = true, columnTypes = true, valueLimit = -1, rowsLimit = rowsLimit)
schema().print()
}

fun DataFrame<*>.shouldHaveColumnGroup(name: String, block: (ColumnGroup<*>) -> Unit = { }): ColumnGroup<*> =
getColumnOrNull(name).shouldBeInstanceOf<ColumnGroup<*>>(block)

fun DataFrame<*>.shouldHaveFrameColumn(name: String, block: (FrameColumn<*>) -> Unit = { }): FrameColumn<*> =
getColumnOrNull(name).shouldBeInstanceOf<FrameColumn<*>>(block)

inline fun <reified T> DataFrame<*>.shouldHaveColumn(
name: String,
block: (DataColumn<T>) -> Unit = { },
): DataColumn<T> {
val shouldBeInstanceOf = getColumnOrNull(name).shouldBeInstanceOf<DataColumn<*>>()
shouldBeInstanceOf.type() shouldBe typeOf<T>()
val cast = shouldBeInstanceOf.cast<T>()
block(cast)
return cast
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.intellij.lang.annotations.Language
import org.jetbrains.kotlinx.dataframe.AnyFrame
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.DataRow
import org.jetbrains.kotlinx.dataframe.alsoDebug
import org.jetbrains.kotlinx.dataframe.api.FormattedFrame
import org.jetbrains.kotlinx.dataframe.api.JsonPath
import org.jetbrains.kotlinx.dataframe.api.allNulls
Expand All @@ -34,7 +35,6 @@ import org.jetbrains.kotlinx.dataframe.api.format
import org.jetbrains.kotlinx.dataframe.api.getColumnGroup
import org.jetbrains.kotlinx.dataframe.api.getColumns
import org.jetbrains.kotlinx.dataframe.api.getFrameColumn
import org.jetbrains.kotlinx.dataframe.api.print
import org.jetbrains.kotlinx.dataframe.api.schema
import org.jetbrains.kotlinx.dataframe.api.toFloat
import org.jetbrains.kotlinx.dataframe.api.toMap
Expand Down Expand Up @@ -1221,16 +1221,6 @@ fun parseJsonStr(jsonStr: String): JsonObject = Json.parseToJsonElement(jsonStr)

fun testJson(jsonName: String) = testResource("$jsonName.json")

/**
* Prints dataframe to console with borders, title, column types and schema
*/
fun <T : DataFrame<*>> T.alsoDebug(println: String? = null, rowsLimit: Int = 20): T =
apply {
println?.let { println(it) }
print(borders = true, title = true, columnTypes = true, valueLimit = -1, rowsLimit = rowsLimit)
schema().print()
}

internal val nothingType: KType = typeOf<List<Nothing>>().arguments.first().type!!
internal val nullableNothingType: KType = typeOf<List<Nothing?>>().arguments.first().type!!

Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ include("dataframe-geo-jupyter")
include("dataframe-openapi-generator")
include("core")
include("dataframe-compiler-plugin-core")
include("common-test-utils")
Loading