Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Kotest tests #243

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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"
Copy link
Owner

Choose a reason for hiding this comment

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

Why you did not remove Junit then?

Copy link
Owner

Choose a reason for hiding this comment

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

Looks like Junit still remains @ILikeYourHat what was the idea?

Copy link
Author

Choose a reason for hiding this comment

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

Migrating to Kotest is a big change, and my idea was to migrate step by step, PR by PR, to make CR easier and avoid possible conflicts. However I can migrate all tests at once in this PR, if you prefer that way. It is up to you :)

Copy link
Owner

Choose a reason for hiding this comment

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

If we do a migration - it is difficult to keep both Unit and Kotest :(
Better to migrate everything, sorry 🙏

Copy link
Author

Choose a reason for hiding this comment

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

@akuleshov7 it took a while, but here you have it: everything migrated to Kotest :)

Please note that the actual runner for jvm is still JUnit5. Kotest also have an jvm test runner, but the test format is very different there: https://kotest.io/docs/framework/writing-tests.html. I know, the issue mentions also the change of the runner, but in my opinion:

  • the main benefit are assertions, and those are migrated
  • things like parametrized test, user friendly names of tests etc. can be achieved also in JUnit5 runner
  • this PR is big enough, and I don't have enough energy for increasing the scope

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)
Copy link
Owner

Choose a reason for hiding this comment

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

why this was removed?

Copy link
Owner

Choose a reason for hiding this comment

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

I do not see this class anymore in the tests

Copy link
Author

@ILikeYourHat ILikeYourHat Nov 4, 2023

Choose a reason for hiding this comment

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

It was removed because it was actually not used.

In this test you are providing a toml example which as a whole is parseable to TwoTomlTables. However, the thing that is being tested here is partial decoding. So you don't need to use the whole model, only the model of the part you are decoding. In other words, the TwoTomlTables model was used only to get the table2 value, which seems like a boilerplate to me


@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