Skip to content

Commit

Permalink
Tests updated to xemantic-kotlin-test (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil authored Dec 13, 2024
1 parent 111c7f9 commit aecb52e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 92 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ kotlin {
dependencies {
implementation(libs.kotlin.test)
implementation(libs.kotlinx.serialization.json)
implementation(libs.xemantic.kotlin.test)
}
}

Expand Down Expand Up @@ -180,7 +181,8 @@ tasks.withType<Test> {

powerAssert {
functions = listOf(
"com.xemantic.ai.money.test.shouldBe"
"com.xemantic.kotlin.test.assert",
"com.xemantic.kotlin.test.have"
)
}

Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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" }

Expand Down
20 changes: 10 additions & 10 deletions src/commonTest/kotlin/MoneyRatioTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

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

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
Expand All @@ -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
Expand All @@ -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"))
}

}
40 changes: 20 additions & 20 deletions src/commonTest/kotlin/MoneyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

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

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
Expand All @@ -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
Expand All @@ -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"))
}

}
29 changes: 21 additions & 8 deletions src/commonTest/kotlin/serialization/MoneyRatioSerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Money.Ratio>(json)
ratio shouldBe Money.Ratio("0.000001")
assert(
testJson.decodeFromString<Money.Ratio>(
""""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<Description>()
assert(value == "Represents a ratio used to multiply Money")
}
meta[1] should {
be<Pattern>()
assert(regex == $$"""^-?\d*\.?\d+$""")
}
}

}
30 changes: 22 additions & 8 deletions src/commonTest/kotlin/serialization/MoneySerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Money>(json)
money shouldBe Money("42.5")
assert(
testJson.decodeFromString<Money>(
""""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<Description>()
have(value == "Represents a monetary amount with arbitrary precision and no currency information")
}
meta[1] should {
be<Pattern>()
have(regex == $$"""^-?\d*\.?\d+$""")
}
}

}
44 changes: 0 additions & 44 deletions src/commonTest/kotlin/test/TestSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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> 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> V.shouldBe(
expected: V,
message: String? = null
) {
asserter.assertEquals(
message = message,
actual = this,
expected = expected
)
}

0 comments on commit aecb52e

Please sign in to comment.