Migrate unit tests to Kotest

This commit is contained in:
Niels van Velzen 2022-04-30 14:02:00 +02:00 committed by Max Rumpf
parent d6ec54cea4
commit a687223c51
12 changed files with 251 additions and 266 deletions

View File

@ -3,6 +3,7 @@ plugins {
alias(libs.plugins.detekt)
alias(libs.plugins.dokka)
alias(libs.plugins.binarycompatibilityvalidator)
alias(libs.plugins.kotest)
}
// Versioning
@ -46,6 +47,7 @@ subprojects {
// Enable required plugins
apply<io.gitlab.arturbosch.detekt.DetektPlugin>()
apply<org.jetbrains.dokka.gradle.DokkaPlugin>()
apply<io.kotest.framework.multiplatform.gradle.KotestMultiplatformCompilerGradlePlugin>()
// Detekt linting
detekt {
@ -58,4 +60,8 @@ subprojects {
sarif.enabled = true
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
}

View File

@ -8,8 +8,9 @@ detekt = "1.20.0"
dokka = "1.6.21"
kasechange = "1.3.0"
koin = "3.1.6"
kotlin-logging = "2.1.21"
kotest = "5.2.3"
kotlin = "1.6.21"
kotlin-logging = "2.1.21"
kotlinpoet = "1.11.0"
kotlinx-coroutines = "1.6.1"
kotlinx-serialization = "1.3.2"
@ -22,6 +23,7 @@ swagger-parser = "2.0.32"
binarycompatibilityvalidator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binarycompatibilityvalidator" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
kotest = { id = "io.kotest.multiplatform", version.ref = "kotest" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
nexuspublish = { id = "io.github.gradle-nexus.publish-plugin", version.ref = "nexuspublish" }
@ -32,11 +34,11 @@ androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-cor
clikt = { module = "com.github.ajalt.clikt:clikt", version.ref = "clikt" }
kasechange = { module = "net.pearx.kasechange:kasechange", version.ref = "kasechange" }
koin = { module = "io.insert-koin:koin-core", version.ref = "koin" }
kotest-framework-engine = { module = "io.kotest:kotest-framework-engine", version.ref = "kotest" }
kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-logging = { module = "io.github.microutils:kotlin-logging", version.ref = "kotlin-logging" }
kotlin-test-annotations-common = { module = "org.jetbrains.kotlin:kotlin-test-annotations-common", version.ref = "kotlin" }
kotlin-test-common = { module = "org.jetbrains.kotlin:kotlin-test-common", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
kotlinpoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinpoet" }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }

View File

@ -3,6 +3,7 @@ package org.jellyfin.sdk.api.client.util
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import org.jellyfin.sdk.api.client.exception.MissingPathVariableException
import org.jellyfin.sdk.model.api.ItemFields
class UrlBuilderTests : FunSpec({
@ -41,7 +42,7 @@ class UrlBuilderTests : FunSpec({
"three" to "3"
)
shouldThrow<IllegalStateException> { UrlBuilder.buildPath(path, parameters) }
shouldThrow<MissingPathVariableException> { UrlBuilder.buildPath(path, parameters) }
}
test("buildPath replaces integers") {

View File

@ -1,62 +1,58 @@
package org.jellyfin.sdk.discovery
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.shouldBe
import org.jellyfin.sdk.createJellyfin
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertTrue
class DiscoveryServiceTests {
private fun getInstance() = DiscoveryService(createJellyfin {})
class DiscoveryServiceTests : FunSpec({
fun getInstance() = DiscoveryService(createJellyfin {})
@Test
fun `getAddressCandidates prefers https`() {
test("getAddressCandidates prefers https") {
val instance = getInstance()
assertTrue(instance.getAddressCandidates("demo.jellyfin.org:433/stable/").first().startsWith("https://"))
assertTrue(instance.getAddressCandidates("http://demo.jellyfin.org:433/stable/").first().startsWith("https://"))
instance.getAddressCandidates("demo.jellyfin.org:433/stable/").first().startsWith("https://") shouldBe true
instance.getAddressCandidates("http://demo.jellyfin.org:433/stable/").first()
.startsWith("https://") shouldBe true
}
@Test
fun `getAddressCandidates adds Jellyfin ports`() {
test("getAddressCandidates adds Jellyfin ports") {
val instance = getInstance()
assertContains(instance.getAddressCandidates("localhost"), "http://localhost:8096")
assertContains(instance.getAddressCandidates("localhost"), "https://localhost:8920")
instance.getAddressCandidates("localhost") shouldContain "http://localhost:8096"
instance.getAddressCandidates("localhost") shouldContain "https://localhost:8920"
}
@Test
fun `getAddressCandidates accepts hostnames`() {
test("getAddressCandidates accepts hostnames") {
val instance = getInstance()
assertContains(instance.getAddressCandidates("localhost"), "http://localhost")
assertContains(instance.getAddressCandidates("jellyfin.local"), "http://jellyfin.local")
assertContains(instance.getAddressCandidates("demo.jellyfin.org"), "http://demo.jellyfin.org")
assertContains(instance.getAddressCandidates("jellyfin.local:8096"), "http://jellyfin.local:8096")
instance.getAddressCandidates("localhost") shouldContain "http://localhost"
instance.getAddressCandidates("jellyfin.local") shouldContain "http://jellyfin.local"
instance.getAddressCandidates("demo.jellyfin.org") shouldContain "http://demo.jellyfin.org"
instance.getAddressCandidates("jellyfin.local:8096") shouldContain "http://jellyfin.local:8096"
}
@Test
fun `getAddressCandidates accepts ipv4 addresses`() {
test("getAddressCandidates accepts ipv4 addresses") {
val instance = getInstance()
assertContains(instance.getAddressCandidates("127.0.0.1"), "http://127.0.0.1")
assertContains(instance.getAddressCandidates("192.168.0.1"), "http://192.168.0.1")
assertContains(instance.getAddressCandidates("127.0.0.1:8096"), "http://127.0.0.1:8096")
instance.getAddressCandidates("127.0.0.1") shouldContain "http://127.0.0.1"
instance.getAddressCandidates("192.168.0.1") shouldContain "http://192.168.0.1"
instance.getAddressCandidates("127.0.0.1:8096") shouldContain "http://127.0.0.1:8096"
}
@Test
fun `getAddressCandidates accepts ipv6 addresses`() {
test("getAddressCandidates accepts ipv6 addresses") {
val instance = getInstance()
assertContains(instance.getAddressCandidates("[::1]"), "http://[::1]")
assertContains(instance.getAddressCandidates("[0:0:0:0:0:0:0:1]"), "http://[0:0:0:0:0:0:0:1]")
assertContains(instance.getAddressCandidates("[::1]:8096"), "http://[::1]:8096")
assertContains(instance.getAddressCandidates("[0:0:0:0:0:0:0:1]:8096"), "http://[0:0:0:0:0:0:0:1]:8096")
instance.getAddressCandidates("[::1]") shouldContain "http://[::1]"
instance.getAddressCandidates("[0:0:0:0:0:0:0:1]") shouldContain "http://[0:0:0:0:0:0:0:1]"
instance.getAddressCandidates("[::1]:8096") shouldContain "http://[::1]:8096"
instance.getAddressCandidates("[0:0:0:0:0:0:0:1]:8096") shouldContain "http://[0:0:0:0:0:0:0:1]:8096"
}
@Test
fun `getAddressCandidates fails on bad input`() {
test("getAddressCandidates fails on bad input") {
val instance = getInstance()
assertTrue(instance.getAddressCandidates("::").isEmpty())
instance.getAddressCandidates("::").shouldBeEmpty()
}
}
})

View File

@ -1,47 +1,43 @@
package org.jellyfin.sdk.model.discovery
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.comparables.shouldBeEqualComparingTo
import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.comparables.shouldBeLessThan
import io.kotest.matchers.shouldBe
import org.jellyfin.sdk.model.ServerVersion
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
class ServerVersionTests {
@Test
fun `Parses correct version strings`() {
assertEquals(ServerVersion.fromString("10.6.4"), ServerVersion(10, 6, 4, null))
assertEquals(ServerVersion.fromString("10.7.0"), ServerVersion(10, 7, 0, null))
assertEquals(ServerVersion.fromString("1.2.3"), ServerVersion(1, 2, 3, null))
assertEquals(ServerVersion.fromString("111.222.333"), ServerVersion(111, 222, 333, null))
assertEquals(ServerVersion.fromString("10.7.0"), ServerVersion(10, 7, 0, null))
assertEquals(ServerVersion.fromString("10.7.0.0"), ServerVersion(10, 7, 0, 0))
assertEquals(ServerVersion.fromString("10.7.0.12345"), ServerVersion(10, 7, 0, 12345))
class ServerVersionTests : FunSpec({
test("Parses correct version strings") {
ServerVersion.fromString("10.6.4") shouldBe ServerVersion(10, 6, 4, null)
ServerVersion.fromString("10.7.0") shouldBe ServerVersion(10, 7, 0, null)
ServerVersion.fromString("1.2.3") shouldBe ServerVersion(1, 2, 3, null)
ServerVersion.fromString("111.222.333") shouldBe ServerVersion(111, 222, 333, null)
ServerVersion.fromString("10.7.0") shouldBe ServerVersion(10, 7, 0, null)
ServerVersion.fromString("10.7.0.0") shouldBe ServerVersion(10, 7, 0, 0)
ServerVersion.fromString("10.7.0.12345") shouldBe ServerVersion(10, 7, 0, 12345)
}
@Test
fun `Returns null for incorrect version strings`() {
assertNull(ServerVersion.fromString("10.6.4-2"))
assertNull(ServerVersion.fromString("10.6.4.2.0"))
assertNull(ServerVersion.fromString("10.7"))
assertNull(ServerVersion.fromString("10"))
assertNull(ServerVersion.fromString("test"))
assertNull(ServerVersion.fromString("11.0.0-rc.1"))
test("Returns null for incorrect version strings") {
ServerVersion.fromString("10.6.4-2") shouldBe null
ServerVersion.fromString("10.6.4.2.0") shouldBe null
ServerVersion.fromString("10.7") shouldBe null
ServerVersion.fromString("10") shouldBe null
ServerVersion.fromString("test") shouldBe null
ServerVersion.fromString("11.0.0-rc.1") shouldBe null
}
@Test
fun `Compares to other versions`() {
assertTrue { ServerVersion(10, 6, 0) == ServerVersion(10, 6, 0) }
test("Compares to other versions") {
ServerVersion(10, 6, 0) shouldBeEqualComparingTo ServerVersion(10, 6, 0)
assertTrue { ServerVersion(10, 6, 0) < ServerVersion(10, 6, 1) }
assertTrue { ServerVersion(10, 6, 0) < ServerVersion(10, 7, 0) }
assertTrue { ServerVersion(10, 6, 0) < ServerVersion(11, 6, 0) }
ServerVersion(10, 6, 0) shouldBeLessThan ServerVersion(10, 6, 1)
ServerVersion(10, 6, 0) shouldBeLessThan ServerVersion(10, 7, 0)
ServerVersion(10, 6, 0) shouldBeLessThan ServerVersion(11, 6, 0)
assertTrue { ServerVersion(1, 2, 3) > ServerVersion(0, 0, 0) }
ServerVersion(1, 2, 3) shouldBeGreaterThan ServerVersion(0, 0, 0)
ServerVersion(1, 7, 0, 1) shouldBeGreaterThan ServerVersion(1, 7, 0)
assertTrue { ServerVersion(1, 7, 0, 1) > ServerVersion(1, 7, 0) }
assertFalse { ServerVersion.fromString("10.8.0")!! < ServerVersion(10, 8, 0, 0) }
assertFalse { ServerVersion.fromString("10.8.0")!! < ServerVersion.fromString("10.8.0")!! }
run { ServerVersion.fromString("10.8.0")!! < ServerVersion(10, 8, 0, 0) } shouldBe false
run { ServerVersion.fromString("10.8.0")!! < ServerVersion.fromString("10.8.0")!! } shouldBe false
}
}
})

View File

@ -1,63 +1,57 @@
package org.jellyfin.sdk.model.extensions
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import org.jellyfin.sdk.model.api.NameIdPair
import org.jellyfin.sdk.model.api.NameValuePair
import org.jellyfin.sdk.model.api.XmlAttribute
import kotlin.test.Test
import kotlin.test.assertEquals
class PairExtensionTests {
@Test
fun `NameIdPair to Pair`() {
assertEquals(Pair("id", "name"), NameIdPair(id = "id", name = "name").toPair())
assertEquals(Pair("id", null), NameIdPair(id = "id", name = null).toPair())
assertEquals(Pair(null, "name"), NameIdPair(id = null, name = "name").toPair())
assertEquals(Pair(null, null), NameIdPair(id = null, name = null).toPair())
assertEquals(Pair("id", "name"), NameIdPair("name", "id").toPair())
class PairExtensionTests : FunSpec({
test("NameIdPair to Pair") {
NameIdPair(id = "id", name = "name").toPair() shouldBe Pair("id", "name")
NameIdPair(id = "id", name = null).toPair() shouldBe Pair("id", null)
NameIdPair(id = null, name = "name").toPair() shouldBe Pair(null, "name")
NameIdPair(id = null, name = null).toPair() shouldBe Pair(null, null)
NameIdPair("name", "id").toPair() shouldBe Pair("id", "name")
}
@Test
fun `Pair to NameIdPair`() {
assertEquals(NameIdPair(id = "id", name = "name"), Pair("id", "name").toNameIdPair())
assertEquals(NameIdPair(id = "id", name = null), Pair("id", null).toNameIdPair())
assertEquals(NameIdPair(id = null, name = "name"), Pair(null, "name").toNameIdPair())
assertEquals(NameIdPair(id = null, name = null), Pair(null, null).toNameIdPair())
assertEquals(NameIdPair("name", "id"), Pair("id", "name").toNameIdPair())
test("Pair to NameIdPair") {
Pair("id", null).toNameIdPair() shouldBe NameIdPair(id = "id", name = null)
Pair("id", "name").toNameIdPair() shouldBe NameIdPair(id = "id", name = "name")
Pair(null, "name").toNameIdPair() shouldBe NameIdPair(id = null, name = "name")
Pair(null, null).toNameIdPair() shouldBe NameIdPair(id = null, name = null)
Pair("id", "name").toNameIdPair() shouldBe NameIdPair("name", "id")
}
@Test
fun `NameValuePair to Pair`() {
assertEquals(Pair("name", "value"), NameValuePair(name = "name", value = "value").toPair())
assertEquals(Pair("name", null), NameValuePair(name = "name", value = null).toPair())
assertEquals(Pair(null, "value"), NameValuePair(name = null, value = "value").toPair())
assertEquals(Pair(null, null), NameValuePair(name = null, value = null).toPair())
assertEquals(Pair("name", "value"), NameValuePair("name", "value").toPair())
test("NameValuePair to Pair") {
NameValuePair(name = "name", value = "value").toPair() shouldBe Pair("name", "value")
NameValuePair(name = "name", value = null).toPair() shouldBe Pair("name", null)
NameValuePair(name = null, value = "value").toPair() shouldBe Pair(null, "value")
NameValuePair(name = null, value = null).toPair() shouldBe Pair(null, null)
NameValuePair("name", "value").toPair() shouldBe Pair("name", "value")
}
@Test
fun `Pair to NameValuePair`() {
assertEquals(NameValuePair(name = "name", value = "value"), Pair("name", "value").toNameValuePair())
assertEquals(NameValuePair(name = "name", value = null), Pair("name", null).toNameValuePair())
assertEquals(NameValuePair(name = null, value = "value"), Pair(null, "value").toNameValuePair())
assertEquals(NameValuePair(name = null, value = null), Pair(null, null).toNameValuePair())
assertEquals(NameValuePair("name", "value"), Pair("name", "value").toNameValuePair())
test("Pair to NameValuePair") {
Pair("name", "value").toNameValuePair() shouldBe NameValuePair(name = "name", value = "value")
Pair("name", null).toNameValuePair() shouldBe NameValuePair(name = "name", value = null)
Pair(null, "value").toNameValuePair() shouldBe NameValuePair(name = null, value = "value")
Pair(null, null).toNameValuePair() shouldBe NameValuePair(name = null, value = null)
Pair("name", "value").toNameValuePair() shouldBe NameValuePair("name", "value")
}
@Test
fun `XmlAttribute to Pair`() {
assertEquals(Pair("name", "value"), XmlAttribute(name = "name", value = "value").toPair())
assertEquals(Pair("name", null), XmlAttribute(name = "name", value = null).toPair())
assertEquals(Pair(null, "value"), XmlAttribute(name = null, value = "value").toPair())
assertEquals(Pair(null, null), XmlAttribute(name = null, value = null).toPair())
assertEquals(Pair("name", "value"), XmlAttribute("name", "value").toPair())
test("XmlAttribute to Pair") {
XmlAttribute(name = "name", value = "value").toPair() shouldBe Pair("name", "value")
XmlAttribute(name = "name", value = null).toPair() shouldBe Pair("name", null)
XmlAttribute(name = null, value = "value").toPair() shouldBe Pair(null, "value")
XmlAttribute(name = null, value = null).toPair() shouldBe Pair(null, null)
XmlAttribute("name", "value").toPair() shouldBe Pair("name", "value")
}
@Test
fun `Pair to XmlAttribute`() {
assertEquals(XmlAttribute(name = "name", value = "value"), Pair("name", "value").toXmlAttribute())
assertEquals(XmlAttribute(name = "name", value = null), Pair("name", null).toXmlAttribute())
assertEquals(XmlAttribute(name = null, value = "value"), Pair(null, "value").toXmlAttribute())
assertEquals(XmlAttribute(name = null, value = null), Pair(null, null).toXmlAttribute())
assertEquals(XmlAttribute("name", "value"), Pair("name", "value").toXmlAttribute())
test("Pair to XmlAttribute") {
Pair("name", "value").toXmlAttribute() shouldBe XmlAttribute(name = "name", value = "value")
Pair("name", null).toXmlAttribute() shouldBe XmlAttribute(name = "name", value = null)
Pair(null, "value").toXmlAttribute() shouldBe XmlAttribute(name = null, value = "value")
Pair(null, null).toXmlAttribute() shouldBe XmlAttribute(name = null, value = null)
Pair("name", "value").toXmlAttribute() shouldBe XmlAttribute("name", "value")
}
}
})

View File

@ -1,71 +1,66 @@
package org.jellyfin.sdk.model.extensions
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import org.jellyfin.sdk.model.api.GeneralCommandType
import org.jellyfin.sdk.model.socket.GeneralCommandMessage
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
class GeneralCommandMessageExtensionTests {
private val dataArguments = mapOf(
class GeneralCommandMessageExtensionTests : FunSpec({
val dataArguments = mapOf(
"Header" to "This is the header",
"Text" to "This is the text",
"TimeoutMs" to "4000"
)
private val message = GeneralCommandMessage(
val message = GeneralCommandMessage(
messageId = UUID.randomUUID(),
command = GeneralCommandType.DISPLAY_MESSAGE,
userId = UUID.randomUUID(),
arguments = dataArguments
)
@Test
fun `GeneralCommandMessage allows array access for arguments`() {
test("GeneralCommandMessage allows array access for arguments") {
val header = message["Header"]
val text = message["Text"]
val timeoutMs = message["TimeoutMs"]
assertEquals("This is the header", header)
assertEquals("This is the text", text)
assertEquals("4000", timeoutMs)
header shouldBe "This is the header"
text shouldBe "This is the text"
timeoutMs shouldBe "4000"
}
@Test
fun `GeneralCommandMessage allows multi-array access for arguments`() {
test("GeneralCommandMessage allows multi-array access for arguments") {
val (header, text, timeoutMs) = message["Header", "Text", "TimeoutMs"]
assertEquals("This is the header", header)
assertEquals("This is the text", text)
assertEquals("4000", timeoutMs)
header shouldBe "This is the header"
text shouldBe "This is the text"
timeoutMs shouldBe "4000"
}
@Test
fun `GeneralCommandMessage allows property delegation read access for arguments`() {
test("GeneralCommandMessage allows property delegation read access for arguments") {
val header by message
val text by message
val timeoutMs by message
assertEquals("This is the header", header)
assertEquals("This is the text", text)
assertEquals("4000", timeoutMs)
header shouldBe "This is the header"
text shouldBe "This is the text"
timeoutMs shouldBe "4000"
}
@Test
fun `GeneralCommandMessage allows checking for values`() {
assertEquals(true, "Header" in message)
assertEquals(true, "Text" in message)
assertEquals(true, "TimeoutMs" in message)
assertEquals(false, "Unknown" in message)
test("GeneralCommandMessage allows checking for values") {
("Header" in message) shouldBe true
("Text" in message) shouldBe true
("TimeoutMs" in message) shouldBe true
("Unknown" in message) shouldBe false
}
@Test
fun `GeneralCommandMessage extensions are case insensitive`() {
assertEquals(true, "header" in message)
assertEquals(true, "HEADER" in message)
assertEquals(true, "Header" in message)
test("GeneralCommandMessage extensions are case insensitive") {
("header" in message) shouldBe true
("HEADER" in message) shouldBe true
("Header" in message) shouldBe true
assertEquals("This is the header", message["header"])
assertEquals("This is the header", message["HEADER"])
assertEquals("This is the header", message["Header"])
message["header"] shouldBe "This is the header"
message["HEADER"] shouldBe "This is the header"
message["Header"] shouldBe "This is the header"
}
}
})

View File

@ -1,26 +1,24 @@
package org.jellyfin.sdk.model.extensions
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import org.jellyfin.sdk.model.api.NameGuidPair
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
class PairExtensionTestsJvm {
@Test
fun `NameGuidPair to Pair`() {
class PairExtensionTestsJvm : FunSpec({
test("NameGuidPair to Pair") {
val uuid = UUID.randomUUID()
assertEquals(Pair(uuid, "name"), NameGuidPair(id = uuid, name = "name").toPair())
assertEquals(Pair(uuid, null), NameGuidPair(id = uuid, name = null).toPair())
assertEquals(Pair(uuid, "name"), NameGuidPair("name", uuid).toPair())
NameGuidPair(id = uuid, name = "name").toPair() shouldBe Pair(uuid, "name")
NameGuidPair(id = uuid, name = null).toPair() shouldBe Pair(uuid, null)
NameGuidPair("name", uuid).toPair() shouldBe Pair(uuid, "name")
}
@Test
fun `Pair to NameGuidPair`() {
test("Pair to NameGuidPair") {
val uuid = UUID.randomUUID()
assertEquals(NameGuidPair(id = uuid, name = "name"), Pair(uuid, "name").toNameGuidPair())
assertEquals(NameGuidPair(id = uuid, name = null), Pair(uuid, null).toNameGuidPair())
assertEquals(NameGuidPair("name", uuid), Pair(uuid, "name").toNameGuidPair())
Pair(uuid, "name").toNameGuidPair() shouldBe NameGuidPair(id = uuid, name = "name")
Pair(uuid, null).toNameGuidPair() shouldBe NameGuidPair(id = uuid, name = null)
Pair(uuid, "name").toNameGuidPair() shouldBe NameGuidPair("name", uuid)
}
}
})

View File

@ -1,64 +1,61 @@
package org.jellyfin.sdk.model.serializer
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.json.Json
import java.time.LocalDateTime
import java.time.ZoneId
import kotlin.test.Test
import kotlin.test.assertEquals
class DateTimeSerializerTests {
@Test
fun `Encodes dates and times (UTC)`() {
class DateTimeSerializerTests : FunSpec({
test("Encodes dates and times (UTC)") {
val instance = DateTimeSerializer(ZoneId.of("UTC"))
assertEquals(
""""2021-06-30T01:33:07Z"""",
Json.encodeToString(instance, LocalDateTime.of(2021, 6, 30, 1, 33, 7))
)
Json.encodeToString(
instance,
LocalDateTime.of(2021, 6, 30, 1, 33, 7)
) shouldBe """"2021-06-30T01:33:07Z""""
}
@Test
fun `Encodes dates and times (Offset)`() {
test("Encodes dates and times (Offset)") {
val instance = DateTimeSerializer(ZoneId.of("UTC+01:00"))
assertEquals(
""""2021-06-30T01:33:07+01:00"""",
Json.encodeToString(instance, LocalDateTime.of(2021, 6, 30, 1, 33, 7))
)
Json.encodeToString(
instance,
LocalDateTime.of(2021, 6, 30, 1, 33, 7)
) shouldBe """"2021-06-30T01:33:07+01:00""""
}
@Test
fun `Parses minimum value`() {
test("Parses minimum value") {
val instance = DateTimeSerializer(ZoneId.of("UTC"))
assertEquals(LocalDateTime.MIN, Json.decodeFromString(instance, """"0001-01-01T00:00:00""""))
Json.decodeFromString(instance, """"0001-01-01T00:00:00"""") shouldBe LocalDateTime.MIN
}
@Test
fun `Parses dates and times (UTC)`() {
test("Parses dates and times (UTC)") {
val instance = DateTimeSerializer(ZoneId.of("UTC"))
assertEquals(
LocalDateTime.of(2021, 6, 30, 1, 33, 7),
Json.decodeFromString(instance, """"2021-06-30T01:33:07Z"""")
)
assertEquals(
LocalDateTime.of(2021, 6, 30, 1, 33, 7, 420000000),
Json.decodeFromString(instance, """"2021-06-30T01:33:07.420Z"""")
)
Json.decodeFromString(
instance,
""""2021-06-30T01:33:07Z""""
) shouldBe LocalDateTime.of(2021, 6, 30, 1, 33, 7)
Json.decodeFromString(
instance,
""""2021-06-30T01:33:07.420Z""""
) shouldBe LocalDateTime.of(2021, 6, 30, 1, 33, 7, 420000000)
}
@Test
fun `Parses dates and times (Offset)`() {
test("Parses dates and times (Offset)") {
val instance = DateTimeSerializer(ZoneId.of("UTC+01:00"))
assertEquals(
LocalDateTime.of(2021, 6, 30, 1, 33, 7),
Json.decodeFromString(instance, """"2021-06-30T01:33:07+01:00"""")
)
assertEquals(
LocalDateTime.of(2021, 6, 30, 1, 33, 7, 420000000),
Json.decodeFromString(instance, """"2021-06-30T01:33:07.420+01:00"""")
)
Json.decodeFromString(
instance,
""""2021-06-30T01:33:07+01:00""""
) shouldBe LocalDateTime.of(2021, 6, 30, 1, 33, 7)
Json.decodeFromString(
instance,
""""2021-06-30T01:33:07.420+01:00""""
) shouldBe LocalDateTime.of(2021, 6, 30, 1, 33, 7, 420000000)
}
}
})

View File

@ -1,44 +1,47 @@
package org.jellyfin.sdk.model.serializer
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.json.Json
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
class UUIDSerializerTests {
@Test
fun `Parses correctly formatted UUIDs`() {
class UUIDSerializerTests : FunSpec({
test("Parses correctly formatted UUIDs") {
val instance = UUIDSerializer()
assertEquals(
UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a"),
Json.decodeFromString(instance, "\"713dc3fe-952b-438f-a70e-d35e4ef0525a\"")
)
assertEquals(
UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a"),
Json.decodeFromString(instance, "\"713dc3fe-952b-438f-a70e-d35e4ef0525a\"")
)
Json.decodeFromString(
instance,
"\"713dc3fe-952b-438f-a70e-d35e4ef0525a\""
) shouldBe UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a")
Json.decodeFromString(
instance,
"\"713dc3fe-952b-438f-a70e-d35e4ef0525a\""
) shouldBe UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a")
}
@Test
fun `Parses UUIDs formatted without dashes`() {
test("Parses UUIDs formatted without dashes") {
val instance = UUIDSerializer()
assertEquals(
UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a"),
Json.decodeFromString(instance, "\"713dc3fe952b438fa70ed35e4ef0525a\"")
)
assertEquals(
UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a"),
Json.decodeFromString(instance, "\"713dc3fe952b438fa70ed35e4ef0525a\"")
)
assertEquals(
UUID.fromString("be275f0b-db14-71d8-6d2d-be5b8bb6918e"),
Json.decodeFromString(instance, "\"be275f0bdb1471d86d2dbe5b8bb6918e\"")
)
assertEquals(
UUID.fromString("70a37b76-f996-24a5-7725-cbf8345e2b62"),
Json.decodeFromString(instance, "\"70a37b76f99624a57725cbf8345e2b62\"")
)
Json.decodeFromString(
instance,
"\"713dc3fe952b438fa70ed35e4ef0525a\""
) shouldBe UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a")
Json.decodeFromString(
instance,
"\"713dc3fe952b438fa70ed35e4ef0525a\""
) shouldBe UUID.fromString("713dc3fe-952b-438f-a70e-d35e4ef0525a")
Json.decodeFromString(
instance,
"\"be275f0bdb1471d86d2dbe5b8bb6918e\""
) shouldBe UUID.fromString("be275f0b-db14-71d8-6d2d-be5b8bb6918e")
Json.decodeFromString(
instance,
"\"70a37b76f99624a57725cbf8345e2b62\""
) shouldBe UUID.fromString("70a37b76-f996-24a5-7725-cbf8345e2b62")
}
}
})

View File

@ -1,37 +1,34 @@
package org.jellyfin.openapi.builder.api
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldEndWith
class ApiNameBuilderTests {
@Test
fun `Names end with Api`() {
class ApiNameBuilderTests : FunSpec({
test("Names end with Api") {
val instance = ApiNameBuilder()
assertTrue { instance.build("test").endsWith("Api") }
assertTrue { instance.build("api").endsWith("Api") }
assertTrue { instance.build("testApi").endsWith("Api") }
assertTrue { instance.build("fourSeemsLikeEnoughAsserts").endsWith("Api") }
instance.build("test") shouldEndWith "Api"
instance.build("api") shouldEndWith "Api"
instance.build("testApi") shouldEndWith "Api"
instance.build("fourSeemsLikeEnoughAsserts") shouldEndWith "Api"
}
@Test
fun `camelCase becomes PascalCase`() {
test("camelCase becomes PascalCase") {
val instance = ApiNameBuilder()
assertEquals("MiscApi", instance.build("misc"))
assertEquals("LibraryApi", instance.build("library"))
assertEquals("MediaInfoApi", instance.build("mediaInfo"))
assertEquals("UserApi", instance.build("user"))
instance.build("misc") shouldBe "MiscApi"
instance.build("library") shouldBe "LibraryApi"
instance.build("mediaInfo") shouldBe "MediaInfoApi"
instance.build("user") shouldBe "UserApi"
}
@Test
fun `PascalCase stays PascalCase`() {
test("PascalCase stays PascalCase") {
val instance = ApiNameBuilder()
assertEquals("MiscApi", instance.build("Misc"))
assertEquals("LibraryApi", instance.build("Library"))
assertEquals("MediaInfoApi", instance.build("MediaInfo"))
assertEquals("UserApi", instance.build("User"))
instance.build("Misc") shouldBe "MiscApi"
instance.build("Library") shouldBe "LibraryApi"
instance.build("MediaInfo") shouldBe "MediaInfoApi"
instance.build("User") shouldBe "UserApi"
}
}
})

View File

@ -18,15 +18,15 @@ kotlin {
dependencies {
api(libs.kotlinx.coroutines)
api(libs.kotlinx.coroutines.test)
api(libs.kotlin.test.common)
api(libs.kotlin.test.annotations.common)
api(libs.kotest.framework.engine)
api(libs.kotest.assertions)
}
}
val jvmMain by getting {
dependencies {
implementation(libs.slf4j.simple)
api(libs.kotlin.test.junit)
api(libs.kotest.runner.junit5)
}
}
}