Skip to content

Commit

Permalink
✨file.writeTable(headers, rows)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfayard committed Aug 31, 2022
1 parent d376d4f commit e5cdf7f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 72 deletions.
Expand Up @@ -3,10 +3,12 @@ package com.sksamuel.kotest.data
import io.kotest.assertions.throwables.shouldThrowMessage
import io.kotest.core.spec.style.FunSpec
import io.kotest.data.headers
import io.kotest.data.mapRows
import io.kotest.data.row
import io.kotest.data.table
import io.kotest.data.toTable
import io.kotest.data.writeToFile
import io.kotest.data.writeTable
import io.kotest.engine.spec.tempfile
import io.kotest.matchers.shouldBe
import java.io.File

Expand Down Expand Up @@ -41,22 +43,25 @@ class StringTableTest : FunSpec({
)
}

test("create table from list") {
data class Language(val code: String, val english: String, val name: String)
val languagesTable = table(
headers("code", "name", "english"),
row("fr", "Français", "French"),
row("es", "Español", "Spanish"),
)

val languages = listOf(
Language("fr", "French", "Français"),
Language("es", "Spanish", "Español"),
)
data class Language(val code: String, val english: String, val name: String)

val languages = listOf(
Language("fr", "French", "Français"),
Language("es", "Spanish", "Español"),
)

test("create table from list") {
val table = table(
headers("code", "name", "english"),
languages.map { row(it.code, it.name, it.english) }
)
table shouldBe table(
headers("code", "name", "english"),
row("fr", "Français", "French"),
row("es", "Español", "Spanish"),
)
table shouldBe languagesTable
}
}

Expand Down Expand Up @@ -134,13 +139,31 @@ class StringTableTest : FunSpec({
}
}

test("table.writeToFile() - happy path") {
val file = resourcesDir.resolve("writeToFile.table")
expectedTable.writeToFile(file)
file.readText() shouldBe """
context("file.writeTable(headers, rows)") {
data class UserInfo(val username: String, val fullName: String)

val expectedFileContent = """
id | username | fullName
4 | jmfayard | Jean-Michel Fayard
6 | louis | Louis Caugnault
""".trim()

val table = table(
headers("id", "UserInfo"),
row(4, UserInfo("jmfayard", "Jean-Michel Fayard")),
row(6, UserInfo("louis", "Louis Caugnault"))
)

test("happy path") {
val file = tempfile()
val rows = table.mapRows { (id, userInfo) ->
row(id.toString(), userInfo.username, userInfo.fullName)
}
val fileContent = file.writeTable(headers("id", "username", "fullName"), rows)
file.readText() shouldBe expectedFileContent
fileContent shouldBe expectedFileContent
}
}
})

}
)

This file was deleted.

Expand Up @@ -23,90 +23,94 @@ fun <A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T> row(a: A, b: B,
fun <A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U> row(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R, s: S, t: T, u: U) = Row21(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)
fun <A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V> row(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o: O, p: P, q: Q, r: R, s: S, t: T, u: U, v: V) = Row22(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)

data class Row1<out A>(val a: A) {
fun values() = listOf(a)
interface Row {
fun values(): List<Any?>
}

data class Row2<out A, out B>(val a: A, val b: B) {
fun values() = listOf(a, b)
data class Row1<out A>(val a: A): Row {
override fun values() = listOf<Any?>(a)
}

data class Row3<out A, out B, out C>(val a: A, val b: B, val c: C) {
fun values() = listOf(a, b, c)
data class Row2<out A, out B>(val a: A, val b: B): Row {
override fun values() = listOf(a, b)
}

data class Row4<out A, out B, out C, out D>(val a: A, val b: B, val c: C, val d: D) {
fun values() = listOf(a, b, c, d)
data class Row3<out A, out B, out C>(val a: A, val b: B, val c: C): Row {
override fun values() = listOf(a, b, c)
}

data class Row5<out A, out B, out C, out D, out E>(val a: A, val b: B, val c: C, val d: D, val e: E) {
fun values() = listOf(a, b, c, d, e)
data class Row4<out A, out B, out C, out D>(val a: A, val b: B, val c: C, val d: D): Row {
override fun values(): List<Any?> = listOf(a, b, c, d)
}

data class Row6<out A, out B, out C, out D, out E, out F>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F) {
fun values() = listOf(a, b, c, d, e, f)
data class Row5<out A, out B, out C, out D, out E>(val a: A, val b: B, val c: C, val d: D, val e: E): Row {
override fun values() = listOf(a, b, c, d, e)
}

data class Row7<out A, out B, out C, out D, out E, out F, out G>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G) {
fun values() = listOf(a, b, c, d, e, f, g)
data class Row6<out A, out B, out C, out D, out E, out F>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F): Row {
override fun values() = listOf(a, b, c, d, e, f)
}

data class Row8<out A, out B, out C, out D, out E, out F, out G, out H>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H) {
fun values() = listOf(a, b, c, d, e, f, g, h)
data class Row7<out A, out B, out C, out D, out E, out F, out G>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G): Row {
override fun values() = listOf(a, b, c, d, e, f, g)
}

data class Row9<out A, out B, out C, out D, out E, out F, out G, out H, out I>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I) {
fun values() = listOf(a, b, c, d, e, f, g, h, i)
data class Row8<out A, out B, out C, out D, out E, out F, out G, out H>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h)
}

data class Row10<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j)
data class Row9<out A, out B, out C, out D, out E, out F, out G, out H, out I>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i)
}

data class Row11<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k)
data class Row10<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j)
}

data class Row12<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l)
data class Row11<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k)
}

data class Row13<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m)
data class Row12<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l)
}

data class Row14<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
data class Row13<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m)
}

data class Row15<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
data class Row14<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
}

data class Row16<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
data class Row15<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
}

data class Row17<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q)
data class Row16<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
}

data class Row18<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)
data class Row17<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q)
}

data class Row19<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)
data class Row18<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)
}

data class Row20<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S, out T>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S, val t: T) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)
data class Row19<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)
}

data class Row21<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S, out T, out U>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S, val t: T, val u: U) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)
data class Row20<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S, out T>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S, val t: T): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)
}

data class Row22<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S, out T, out U, out V>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S, val t: T, val u: U, val v: V) {
fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)
data class Row21<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S, out T, out U>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S, val t: T, val u: U): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)
}

data class Row22<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S, out T, out U, out V>(val a: A, val b: B, val c: C, val d: D, val e: E, val f: F, val g: G, val h: H, val i: I, val j: J, val k: K, val l: L, val m: M, val n: N, val o: O, val p: P, val q: Q, val r: R, val s: S, val t: T, val u: U, val v: V): Row {
override fun values() = listOf(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)
}
Expand Up @@ -2,11 +2,17 @@ package io.kotest.data

fun <Key, Value> Map<Key, Value>.toTable(
headers: Headers2 = headers("key", "value"),
) = Table2(headers, entries.map { row(it.key, it.value) })
) = table(headers, entries.map { row(it.key, it.value) })

// only 3 arguments for now
fun <A> table(headers: Headers1, rows: List<Row1<A>>) = Table1(headers, rows)
fun <A, B> table(headers: Headers2, rows: List<Row2<A, B>>) = Table2(headers, rows)
fun <A, B, C> table(headers: Headers3, rows: List<Row3<A, B, C>>) = Table3(headers, rows)
// TODO more

fun <A, T: Row> Table1<A>.mapRows(fn: (Row1<A>) -> T): List<T> = rows.map(fn)
fun <A, B, T: Row> Table2<A, B>.mapRows(fn: (Row2<A, B>) -> T): List<T> = rows.map(fn)
fun <A, B, C, T: Row> Table3<A, B, C>.mapRows(fn: (Row3<A, B, C>) -> T): List<T> = rows.map(fn)
// TODO more

fun <A> table(headers: Headers1, vararg rows: Row1<A>) = Table1(headers, rows.asList())
fun <A, B> table(headers: Headers2, vararg rows: Row2<A, B>) = Table2(headers, rows.asList())
Expand Down
Expand Up @@ -15,11 +15,13 @@ fun <A, B, C> table(
return Table3(headers, rows)
}

fun <A, B, C> Table3<A, B, C>.writeToFile(file: File) {
val cells = rows.map { row -> row.values().map { it.toString() } }
writeToFile(file, headers.values(), cells)
fun File.writeTable(headers: Headers3, rows: List<Row3<String, String, String>>): String {
return writeTable(headers.values(), rows.map { it.strings() } )
}

private fun Row.strings(): List<String> = values().map { it.toString() }


internal fun File.readStringTable(headers: List<String>): StringTable {
if (exists().not()) throw AssertionError("Can't read table file")
if (extension != "table") throw AssertionError("Table file must have a .table extension")
Expand All @@ -29,15 +31,17 @@ internal fun File.readStringTable(headers: List<String>): StringTable {
return StringTable(headers, lines, skipFirstLine = true)
}

fun writeToFile(file: File, headers: List<String>, cells: List<List<String>>) {
fun File.writeTable(headers: List<String>, cells: List<List<String>>): String {
val separator = " | "
val formattedHeader = headers.joinToString(separator)
val formattedContent = cells.joinToString("\n") { row ->
row.joinToString(separator)
}

file.writeText("""
val fileContent = """
$formattedHeader
$formattedContent
""".trimIndent())
""".trimIndent()
writeText(fileContent)
return fileContent
}

0 comments on commit e5cdf7f

Please sign in to comment.