Skip to content

Commit

Permalink
Added support for Kotest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ILikeYourHat committed Oct 18, 2023
1 parent e4bb37a commit b849347
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 183 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.akuleshov7.buildutils.*
plugins {
kotlin("multiplatform") apply false
kotlin("plugin.serialization") version Versions.KOTLIN apply false
id("io.kotest.multiplatform") version Versions.KOTEST apply false
id("com.akuleshov7.buildutils.publishing-configuration")
}

Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/com/akuleshov7/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ object Versions {
const val JUNIT = "5.7.1"
const val OKIO = "3.1.0"
const val SERIALIZATION = "1.5.0"
const val KOTEST = "5.7.2"
}
3 changes: 3 additions & 0 deletions ktoml-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("io.kotest.multiplatform")
id("com.akuleshov7.buildutils.publishing-configuration")
}

Expand Down Expand Up @@ -50,6 +51,8 @@ kotlin {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("io.kotest:kotest-framework-engine:${Versions.KOTEST}")
implementation("io.kotest:kotest-assertions-core:${Versions.KOTEST}")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.akuleshov7.ktoml.decoders

import com.akuleshov7.ktoml.Toml
import com.akuleshov7.ktoml.TomlConfig
import com.akuleshov7.ktoml.TomlInputConfig
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@Serializable
data class Key(val value: Long)
Expand Down Expand Up @@ -43,8 +42,8 @@ class NullableTablesTest {
""".trimIndent()
)

assertNotNull(toml1)
assertEquals(1L, toml1.key?.value)
toml1.shouldNotBeNull()
toml1.key?.value shouldBe 1L

val toml2 = mapper.decodeFromString<Config2>(
"""
Expand All @@ -53,8 +52,8 @@ class NullableTablesTest {
""".trimIndent()
)

assertNotNull(toml2)
assertEquals(1L, toml2.key?.value)
toml2.shouldNotBeNull()
toml2.key?.value shouldBe 1L

val toml3 = mapper.decodeFromString<Config3>(
"""
Expand All @@ -63,8 +62,8 @@ class NullableTablesTest {
""".trimIndent()
)

assertNotNull(toml3)
assertEquals(1L, toml3.key.value)
toml3.shouldNotBeNull()
toml3.key.value shouldBe 1L

val toml4 = mapper.decodeFromString<Config4>(
"""
Expand All @@ -73,8 +72,8 @@ class NullableTablesTest {
""".trimIndent()
)

assertNotNull(toml4)
assertEquals(1L, toml4.key.value)
toml4.shouldNotBeNull()
toml4.key.value shouldBe 1L
}
}

Expand All @@ -88,12 +87,12 @@ class EmptyTomlTest {
""".trimIndent()
)

assertEquals(Config(), res)
res shouldBe Config()

res = Toml.decodeFromString(
"".trimIndent()
)

assertEquals(Config(), res)
res shouldBe Config()
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
package com.akuleshov7.ktoml.decoders

import com.akuleshov7.ktoml.Toml
import io.kotest.matchers.shouldBe
import kotlinx.serialization.ExperimentalSerializationApi
import kotlin.test.Test
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer
import kotlin.test.assertEquals

@ExperimentalSerializationApi
class PartialDecoderTest {
@Serializable
data class TwoTomlTables(val table1: Table1, val table2: Table2)

@Serializable
data class Table1(val a: Long, val b: Long)

@Serializable
data class Table2(val c: Long, val e: Long, val d: Long)

@Test
fun testPartialDecoding() {
val test = TwoTomlTables(Table1(1, 2), Table2(1, 2, 3))
assertEquals(
test.table1,
Toml.partiallyDecodeFromString(
serializer(),
"[table1] \n a = 1 \n b = 2 \n [table2] \n c = 1 \n e = 2 \n d = 3",
"table1"
)
val parsedResult = Toml.partiallyDecodeFromString<Table1>(
serializer(),
"[table1] \n a = 1 \n b = 2 \n [table2] \n c = 1 \n e = 2 \n d = 3",
"table1"
)

parsedResult shouldBe Table1(1, 2)
}
}
2 changes: 2 additions & 0 deletions ktoml-file/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("io.kotest.multiplatform")
id("com.akuleshov7.buildutils.publishing-configuration")
}

Expand Down Expand Up @@ -46,6 +47,7 @@ kotlin {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("io.kotest:kotest-framework-engine:${Versions.KOTEST}")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.akuleshov7.ktoml.file

import io.kotest.matchers.shouldBe
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer
import kotlin.test.Test
import kotlin.test.assertEquals

class Regression189 {
@Serializable
Expand All @@ -21,13 +21,12 @@ class Regression189 {
fun regressionCastTest() {
val file = "src/commonTest/resources/regression_189.toml"
val parsedResult = TomlFileReader.decodeFromFile<ServerSettings>(serializer(), file)
assertEquals(ServerSettings(
parsedResult shouldBe ServerSettings(
"test5",
"http://localhost:8080",
"50694ed7-a93f-4713-9e55-4d512ce2e4db",
"a8DkRGThvz13cmVubFdgX0CsoLfAtXcBvyxiKCPY34FEt3UDmPBkMKFRk4iKRuRp",
"20NSBgKB2B9C2u2toAuiPqlaZgfEWJ4m50562YK9w575SNt31CWrjcpwqeiDCYhZ"),
parsedResult
"20NSBgKB2B9C2u2toAuiPqlaZgfEWJ4m50562YK9w575SNt31CWrjcpwqeiDCYhZ"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import com.akuleshov7.ktoml.parsers.TomlParser
import com.akuleshov7.ktoml.source.useLines
import com.akuleshov7.ktoml.tree.nodes.TableType
import com.akuleshov7.ktoml.tree.nodes.TomlTable
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.shouldBe
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class TomlFileParserTest {
@Serializable
Expand Down Expand Up @@ -40,7 +40,10 @@ class TomlFileParserTest {

@Test
fun readParseAndDecodeFile() {
val expected = TestClass(
val parsedResult =
TomlFileReader().decodeFromFile<TestClass>(serializer(), "src/commonTest/resources/simple_example.toml")

parsedResult shouldBe TestClass(
"TOML \"Example\"",
Owner(
"Tom Preston-Werner",
Expand All @@ -51,13 +54,6 @@ class TomlFileParserTest {
"192.168.1.1"
)
)
assertEquals(
expected,
TomlFileReader().decodeFromFile(
serializer(),
"src/commonTest/resources/simple_example.toml"
)
)
}

// ================
Expand All @@ -84,17 +80,18 @@ class TomlFileParserTest {
fun testTableDiscovery() {
val file = "src/commonTest/resources/complex_toml_tables.toml"
// ==== reading from file
val test = MyTableTest(A(Ab(InnerTest("Undefined")), InnerTest("Undefined")), D(InnerTest("Undefined")))
assertEquals(test, TomlFileReader.decodeFromFile(serializer(), file))
val parsedMyTableTest = TomlFileReader.decodeFromFile<MyTableTest>(serializer(), file)
parsedMyTableTest shouldBe MyTableTest(
A(Ab(InnerTest("Undefined")), InnerTest("Undefined")),
D(InnerTest("Undefined"))
)

// ==== checking how table discovery works
val parsedResult = getFileSource(file).useLines {
TomlParser(TomlInputConfig()).parseStringsToTomlTree(it, TomlInputConfig())
}

assertEquals(
listOf("a", "a.b.c", "a.d", "d", "d.a"),
parsedResult.getRealTomlTables().map { it.fullTableKey.toString() })
val tableNames = parsedResult.getRealTomlTables().map { it.fullTableKey.toString() }
tableNames shouldBe listOf("a", "a.b.c", "a.d", "d", "d.a")
}

@Serializable
Expand All @@ -105,15 +102,15 @@ class TomlFileParserTest {
fun regressionCastTest() {
val file = "src/commonTest/resources/class_cast_regression.toml"
val parsedResult = TomlFileReader.decodeFromFile<RegressionTest>(serializer(), file)
assertEquals(RegressionTest(null, 1, 2, null), parsedResult)
parsedResult shouldBe RegressionTest(null, 1, 2, null)
}

@ExperimentalSerializationApi
@Test
fun regressionPartialTest() {
val file = "src/commonTest/resources/class_cast_regression.toml"
val parsedResult = TomlFileReader.decodeFromFile<RegressionTest>(serializer(), file)
assertEquals(RegressionTest(null, 1, 2, null), parsedResult)
parsedResult shouldBe RegressionTest(null, 1, 2, null)
}


Expand Down Expand Up @@ -146,8 +143,21 @@ class TomlFileParserTest {
@Test
fun regressionInvalidIndex() {
val file = "src/commonTest/resources/partial_parser_regression.toml"
assertEquals(
GeneralConfig(
val parsedGeneralConfig = TomlFileReader.partiallyDecodeFromFile<GeneralConfig>(serializer(), file, "general")
parsedGeneralConfig shouldBe GeneralConfig(
execCmd = "echo hello world",
tags = listOf("Tag", "Other tag"),
description = "My description",
suiteName = "// DocsCheck",
excludedTests = null,
includedTests = null,
ignoreSaveComments = null
)

val parsedTestRegression = TomlFileReader.decodeFromFile<TestRegression>(serializer(), file)
parsedTestRegression shouldBe TestRegression(
list1 = listOf(1.0, 2.0),
general = GeneralConfig(
execCmd = "echo hello world",
tags = listOf("Tag", "Other tag"),
description = "My description",
Expand All @@ -156,69 +166,41 @@ class TomlFileParserTest {
includedTests = null,
ignoreSaveComments = null
),
TomlFileReader.partiallyDecodeFromFile(serializer(), file, "general")
)
assertEquals(
TestRegression(
list1 = listOf(1.0, 2.0),
general = GeneralConfig(
execCmd = "echo hello world",
tags = listOf("Tag", "Other tag"),
description = "My description",
suiteName = "// DocsCheck",
excludedTests = null,
includedTests = null,
ignoreSaveComments = null
),
list2 = listOf(1, 3, 5),
warn = WarnConfig(list = listOf("12a", "12f")),
list3 = listOf("mystr", "2", "3")
),
TomlFileReader.decodeFromFile(serializer(), file)
list2 = listOf(1, 3, 5),
warn = WarnConfig(list = listOf("12a", "12f")),
list3 = listOf("mystr", "2", "3")
)
}

@Serializable
data class Table1(val a: Long, val b: Long)

@Serializable
data class Table2(val c: Long, val e: Long, val d: Long)

@Serializable
data class TwoTomlTables(val table1: Table1, val table2: Table2)

@Test
fun testPartialFileDecoding() {
val file = "src/commonTest/resources/partial_decoder.toml"
val test = TwoTomlTables(Table1(1, 2), Table2(1, 2, 3))
assertEquals(
test.table1,
TomlFileReader.partiallyDecodeFromFile(
serializer(), file, "table1"
)
)
val parsedResult = TomlFileReader.partiallyDecodeFromFile<Table1>(serializer(), file, "table1")
parsedResult shouldBe Table1(1, 2)
}

@Test
fun readTopLevelTables() {
val file = "src/commonTest/resources/simple_example.toml"
assertEquals(
listOf("owner", "database"),
getFileSource(file).useLines { lines ->
TomlParser(TomlInputConfig())
.parseStringsToTomlTree(lines, TomlInputConfig())
.children
.filterIsInstance<TomlTable>()
.filter { it.type == TableType.PRIMITIVE && !it.isSynthetic }
.map { it.fullTableKey.toString() }
}
)
val tableNames = getFileSource(file).useLines { lines ->
TomlParser(TomlInputConfig())
.parseStringsToTomlTree(lines, TomlInputConfig())
.children
.filterIsInstance<TomlTable>()
.filter { it.type == TableType.PRIMITIVE && !it.isSynthetic }
.map { it.fullTableKey.toString() }
}

tableNames shouldBe listOf("owner", "database")
}

@Test
fun invalidFile() {
val file = "src/commonTest/resources/simple_example.wrongext"
assertFailsWith<IllegalStateException> {
shouldThrow<IllegalStateException> {
getFileSource(file)
}
}
Expand Down
Loading

0 comments on commit b849347

Please sign in to comment.