From 6f6bbb61aefb8eb0db3630c95f23d0dbe09a8941 Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Thu, 5 Dec 2024 20:00:45 +0100 Subject: [PATCH] Add MoneyTest.kt --- src/commonTest/kotlin/MoneyTest.kt | 87 ++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/commonTest/kotlin/MoneyTest.kt diff --git a/src/commonTest/kotlin/MoneyTest.kt b/src/commonTest/kotlin/MoneyTest.kt new file mode 100644 index 0000000..c33eec3 --- /dev/null +++ b/src/commonTest/kotlin/MoneyTest.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2024 Kazimierz Pogoda / Xemantic + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.xemantic.ai.money + +import com.xemantic.ai.money.test.shouldBe +import kotlin.test.Test + +class MoneyTest { + + @Test + fun `Should create Money instance from String`() { + Money("42.50").toString() shouldBe "42.5" + } + + @Test + fun `Should create Money instance with long fractional part`() { + Money("42.123456789").toString() shouldBe "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") + } + + @Test + fun `Should add Moneys`() { + Money("10.25") + Money("5.75") shouldBe Money("16.00") + } + + @Test + fun `Should subtract Moneys`() { + Money("20.00") - Money("7.50") shouldBe Money("12.50") + } + + @Test + fun `Should multiply Moneys`() { + Money("5.01") * Money("3.00") shouldBe Money("15.03") + } + + @Test + fun `Should multiply Money by Int Ratio scalar`() { + Money("5.01") * Money.Ratio("3") shouldBe Money("15.03") + } + + @Test + fun `Should multiply Money by proportion Ratio`() { + Money("2.0") * Money.Ratio("1.25") shouldBe Money("2.5") + } + + @Test + fun `Should create Money instance for token price`() { + Money("3.0") * Money.Ratio("0.000001") shouldBe Money("0.000003") + } + + @Test + fun `Should compare different Money amounts`() { + // given + val money1 = Money("10.00") + val money2 = Money("10.00") + 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 + } + +}