From aecb52e806190db3c8e48d7aaeb537003905d33f Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Fri, 13 Dec 2024 22:47:50 +0100 Subject: [PATCH] Tests updated to xemantic-kotlin-test (#2) --- build.gradle.kts | 4 +- gradle/libs.versions.toml | 4 +- src/commonTest/kotlin/MoneyRatioTest.kt | 20 ++++----- src/commonTest/kotlin/MoneyTest.kt | 40 ++++++++--------- .../MoneyRatioSerializationTest.kt | 29 ++++++++---- .../serialization/MoneySerializationTest.kt | 30 +++++++++---- src/commonTest/kotlin/test/TestSupport.kt | 44 ------------------- 7 files changed, 79 insertions(+), 92 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4a645b5..e8795ac 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -133,6 +133,7 @@ kotlin { dependencies { implementation(libs.kotlin.test) implementation(libs.kotlinx.serialization.json) + implementation(libs.xemantic.kotlin.test) } } @@ -180,7 +181,8 @@ tasks.withType { powerAssert { functions = listOf( - "com.xemantic.ai.money.test.shouldBe" + "com.xemantic.kotlin.test.assert", + "com.xemantic.kotlin.test.have" ) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1c5f39b..884b7a0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,8 @@ javaTarget = "17" kotlin = "2.1.0" kotlinxSerialization = "1.7.3" -xemanticAiToolSchema = "0.1.1" +xemanticAiToolSchema = "0.1.4" +xemanticKotlinTest = "1.0" bignum = "0.3.10" @@ -21,6 +22,7 @@ kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serializa kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerialization" } xemantic-ai-tool-schema = { module = "com.xemantic.ai:xemantic-ai-tool-schema", version.ref = "xemanticAiToolSchema" } +xemantic-kotlin-test = { module="com.xemantic.kotlin:xemantic-kotlin-test", version.ref="xemanticKotlinTest" } bignum = { module = "com.ionspin.kotlin:bignum", version.ref = "bignum" } diff --git a/src/commonTest/kotlin/MoneyRatioTest.kt b/src/commonTest/kotlin/MoneyRatioTest.kt index 1fdf00b..5c0270c 100644 --- a/src/commonTest/kotlin/MoneyRatioTest.kt +++ b/src/commonTest/kotlin/MoneyRatioTest.kt @@ -16,7 +16,7 @@ package com.xemantic.ai.money -import com.xemantic.ai.money.test.shouldBe +import com.xemantic.kotlin.test.assert import kotlin.test.Test import kotlin.test.assertFailsWith @@ -24,7 +24,7 @@ class MoneyRatioTest { @Test fun `Should create Money Ratio instance from String`() { - Money.Ratio("1.25").toString() shouldBe "1.25" + assert(Money.Ratio("1.25").toString() == "1.25") } @Test @@ -36,24 +36,24 @@ class MoneyRatioTest { @Test fun `Should create Money Ratio instance with long fractional part`() { - Money.Ratio("0.0000001").toString() shouldBe "0.0000001" + assert(Money.Ratio("0.0000001").toString() == "0.0000001") } @Test fun `Should compare Money Ratio instances`() { - Money.Ratio.ONE shouldBe Money.Ratio("1") - Money.Ratio("0") shouldBe Money.Ratio("0.0") - Money.Ratio("0.1") shouldBe Money.Ratio("0.100000") + assert(Money.Ratio.ONE == Money.Ratio("1")) + assert(Money.Ratio("0") == Money.Ratio("0.0")) + assert(Money.Ratio("0.1") == Money.Ratio("0.100000")) } @Test fun `Should multiple Money Ratio and Money`() { - Money.Ratio("0.000001") * Money("3.0") shouldBe Money("0.000003") + assert(Money.Ratio("0.000001") * Money("3.0") == Money("0.000003")) } @Test fun `Should convert valid string to Money Ratio`() { - "0.000001".moneyRatio shouldBe Money.Ratio("0.000001") + assert("0.000001".moneyRatio == Money.Ratio("0.000001")) } @Test @@ -65,8 +65,8 @@ class MoneyRatioTest { @Test fun `Should convert Int to Money Ratio`() { - 1.toMoneyRatio() shouldBe Money.Ratio("1") - 42.toMoneyRatio() shouldBe Money.Ratio("42") + assert(1.toMoneyRatio() == Money.Ratio("1")) + assert(42.toMoneyRatio() == Money.Ratio("42")) } } diff --git a/src/commonTest/kotlin/MoneyTest.kt b/src/commonTest/kotlin/MoneyTest.kt index fc86003..9f6ee15 100644 --- a/src/commonTest/kotlin/MoneyTest.kt +++ b/src/commonTest/kotlin/MoneyTest.kt @@ -16,7 +16,7 @@ package com.xemantic.ai.money -import com.xemantic.ai.money.test.shouldBe +import com.xemantic.kotlin.test.assert import kotlin.test.Test import kotlin.test.assertFailsWith @@ -24,7 +24,7 @@ class MoneyTest { @Test fun `Should create Money instance from String`() { - Money("42.50").toString() shouldBe "42.5" + assert(Money("42.50").toString() == "42.5") } @Test @@ -36,45 +36,45 @@ class MoneyTest { @Test fun `Should create Money instance with long fractional part`() { - Money("42.123456789").toString() shouldBe "42.123456789" + assert(Money("42.123456789").toString() == "42.123456789") } @Test fun `Should compare Money instances`() { - Money.ZERO shouldBe Money("0") - Money.ONE shouldBe Money("1") - Money.ONE shouldBe Money("1.0") - Money("0.1") shouldBe Money("0.100") + assert(Money.ZERO == Money("0")) + assert(Money.ONE == Money("1")) + assert(Money.ONE == Money("1.0")) + assert(Money("0.1") == Money("0.100")) } @Test fun `Should add Moneys`() { - Money("10.25") + Money("5.75") shouldBe Money("16.00") + assert(Money("10.25") + Money("5.75") == Money("16.00")) } @Test fun `Should subtract Moneys`() { - Money("20.00") - Money("7.50") shouldBe Money("12.50") + assert(Money("20.00") - Money("7.50") == Money("12.50")) } @Test fun `Should multiply Moneys`() { - Money("5.01") * Money("3.00") shouldBe Money("15.03") + assert(Money("5.01") * Money("3.00") == Money("15.03")) } @Test fun `Should multiply Money by Int Ratio scalar`() { - Money("5.01") * Money.Ratio("3") shouldBe Money("15.03") + assert(Money("5.01") * Money.Ratio("3") == Money("15.03")) } @Test fun `Should multiply Money by proportion Ratio`() { - Money("2.0") * Money.Ratio("1.25") shouldBe Money("2.5") + assert(Money("2.0") * Money.Ratio("1.25") == Money("2.5")) } @Test fun `Should create Money instance for token price`() { - Money("3.0") * Money.Ratio("0.000001") shouldBe Money("0.000003") + assert(Money("3.0") * Money.Ratio("0.000001") == Money("0.000003")) } @Test @@ -85,21 +85,21 @@ class MoneyTest { val money3 = Money("20.00") // then - (money1 == money2) shouldBe true - (money1 < money3) shouldBe true - (money3 > money1) shouldBe true - (money1 == money3) shouldBe false - (money3 < money1) shouldBe false + assert(money1 == money2) + assert(money1 < money3) + assert(money3 > money1) + assert(money1 < money3) + assert(money3 > money2) } @Test fun `Should multiply Int and Money`() { - 2 * Money("2.25") shouldBe Money("4.5") + assert(2 * Money("2.25") == Money("4.5")) } @Test fun `Should multiply Money and Int`() { - Money("2.25") * 2 shouldBe Money("4.5") + assert(Money("2.25") * 2 == Money("4.5")) } } diff --git a/src/commonTest/kotlin/serialization/MoneyRatioSerializationTest.kt b/src/commonTest/kotlin/serialization/MoneyRatioSerializationTest.kt index 9558170..8cb0792 100644 --- a/src/commonTest/kotlin/serialization/MoneyRatioSerializationTest.kt +++ b/src/commonTest/kotlin/serialization/MoneyRatioSerializationTest.kt @@ -18,10 +18,12 @@ package com.xemantic.ai.money.serialization import com.xemantic.ai.money.Money import com.xemantic.ai.money.Ratio -import com.xemantic.ai.money.test.shouldBe import com.xemantic.ai.money.test.testJson import com.xemantic.ai.tool.schema.meta.Description import com.xemantic.ai.tool.schema.meta.Pattern +import com.xemantic.kotlin.test.assert +import com.xemantic.kotlin.test.be +import com.xemantic.kotlin.test.should import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.encodeToString import kotlin.test.Test @@ -30,23 +32,34 @@ class MoneyRatioSerializationTest { @Test fun `Should serialize Money Ratio to JSON`() { - val ratio = Money.Ratio("0.000001") - testJson.encodeToString(ratio) shouldBe """"0.000001"""" + assert( + testJson.encodeToString( + Money.Ratio("0.000001") + ) == """"0.000001"""" + ) } @Test fun `Should deserialize Money Ratio from JSON`() { - val json = """"0.000001"""" - val ratio = testJson.decodeFromString(json) - ratio shouldBe Money.Ratio("0.000001") + assert( + testJson.decodeFromString( + """"0.000001"""" + ) == Money.Ratio("0.000001") + ) } @Test fun `Should preserve tool schema annotations of Money Ratio`() { @OptIn(ExperimentalSerializationApi::class) val meta = Money.Ratio.serializer().descriptor.annotations - (meta[0] as Description).value shouldBe "Represents a ratio used to multiply Money" - (meta[1] as Pattern).regex shouldBe $$"""^-?\d*\.?\d+$""" + meta[0] should { + be() + assert(value == "Represents a ratio used to multiply Money") + } + meta[1] should { + be() + assert(regex == $$"""^-?\d*\.?\d+$""") + } } } diff --git a/src/commonTest/kotlin/serialization/MoneySerializationTest.kt b/src/commonTest/kotlin/serialization/MoneySerializationTest.kt index 21de026..4d3cdad 100644 --- a/src/commonTest/kotlin/serialization/MoneySerializationTest.kt +++ b/src/commonTest/kotlin/serialization/MoneySerializationTest.kt @@ -17,10 +17,13 @@ package com.xemantic.ai.money.serialization import com.xemantic.ai.money.Money -import com.xemantic.ai.money.test.shouldBe import com.xemantic.ai.money.test.testJson import com.xemantic.ai.tool.schema.meta.Description import com.xemantic.ai.tool.schema.meta.Pattern +import com.xemantic.kotlin.test.assert +import com.xemantic.kotlin.test.be +import com.xemantic.kotlin.test.have +import com.xemantic.kotlin.test.should import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.encodeToString import kotlin.test.Test @@ -29,23 +32,34 @@ class MoneySerializationTest { @Test fun `Should serialize Money to JSON`() { - val money = Money("42.50") - testJson.encodeToString(money) shouldBe """"42.5"""" + assert( + testJson.encodeToString( + Money("42.50") + ) == """"42.5"""" + ) } @Test fun `Should deserialize Money from JSON`() { - val json = """"42.5"""" - val money = testJson.decodeFromString(json) - money shouldBe Money("42.5") + assert( + testJson.decodeFromString( + """"42.5"""" + ) == Money("42.5") + ) } @Test fun `Should preserve tool schema annotations of Money`() { @OptIn(ExperimentalSerializationApi::class) val meta = Money.serializer().descriptor.annotations - (meta[0] as Description).value shouldBe "Represents a monetary amount with arbitrary precision and no currency information" - (meta[1] as Pattern).regex shouldBe $$"""^-?\d*\.?\d+$""" + meta[0] should { + be() + have(value == "Represents a monetary amount with arbitrary precision and no currency information") + } + meta[1] should { + be() + have(regex == $$"""^-?\d*\.?\d+$""") + } } } diff --git a/src/commonTest/kotlin/test/TestSupport.kt b/src/commonTest/kotlin/test/TestSupport.kt index db9016a..9e932f7 100644 --- a/src/commonTest/kotlin/test/TestSupport.kt +++ b/src/commonTest/kotlin/test/TestSupport.kt @@ -18,7 +18,6 @@ package com.xemantic.ai.money.test import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json -import kotlin.test.asserter /** * A pretty printing [Json] for tests. @@ -28,46 +27,3 @@ val testJson = Json { @OptIn(ExperimentalSerializationApi::class) prettyPrintIndent = " " } - -/** - * Provides an infix function for equality assertions in tests. - * - * This function is inspired by the Kotest library's assertions but is implemented - * to support power-assert conventions. It allows for a more readable and expressive - * way of writing assertions in Kotlin tests. - * - * Note: Together with the `shouldBe` variant receiving a message this function fulfills - * the `power-assert` convention. - * - * @param expected The expected value to compare against. - * @throws AssertionError if the actual value is not equal to the expected value. - */ -infix fun V.shouldBe( - expected: V -) { - shouldBe( - expected = expected, - message = null - ) -} - -/** - * Asserts that the actual value is equal to the expected value. - * - * This function provides a more flexible way to perform equality assertions - * by allowing an optional custom error message. - * - * @param expected The expected value to compare against. - * @param message An optional custom message to be used in case of assertion failure. - * @throws AssertionError if the actual value is not equal to the expected value. - */ -fun V.shouldBe( - expected: V, - message: String? = null -) { - asserter.assertEquals( - message = message, - actual = this, - expected = expected - ) -}