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

Merge Feature/29 currency unit tests #32

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 18 additions & 2 deletions core/entities/src/commonMain/kotlin/business/Currency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ interface Currency: Comparable<Currency> {
decimalPlaces = 2,
centsPerUnit = 1,
)

/*
TODO: Add currencies as different areas are supported by the application
1. currency conversion service will be needed; can't use comparable directly
2. currency exchange rates will be needed
*/
}
}
}
Expand All @@ -86,8 +92,17 @@ internal class SimpleCurrency(
override val form: Currency.Form,
): Currency {
override fun compareTo(other: Currency): Int {
val otherValue = other.amount * form.centsPerUnit / other.form.centsPerUnit
return amount.compareTo(otherValue)
when {
form.centsPerUnit > other.form.centsPerUnit -> {
val thisValue = amount * form.centsPerUnit / other.form.centsPerUnit
return thisValue.compareTo(other.amount)
}
form.centsPerUnit < other.form.centsPerUnit -> {
val otherValue = other.amount * other.form.centsPerUnit / form.centsPerUnit
return amount.compareTo(otherValue)
}
else -> return amount.compareTo(other.amount)
}
}

override fun toString(): String {
Expand All @@ -98,4 +113,5 @@ fun Currency(amount: Long, form: Currency.Form): Currency {
return SimpleCurrency(amount, form)
}

fun Long.dollars() = Currency(this, Currency.Form.usDollars)
fun Long.dollarCents() = Currency(this, Currency.Form.usDollarCents)
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024 Pointyware. Use of this software is governed by the GPL-3.0 license.
*/

package org.pointyware.xyz.core.entities.business

import kotlin.test.Test
import kotlin.test.assertEquals

/**
*
*/
class CurrencyUnitTest {

data class FormattingCase(
val amount: Long,
val form: Currency.Form,
val expected: String
)

@Test
fun testSimpleForm() {
listOf(
FormattingCase(
100,
Currency.Form.usDollars,
"$100"
),
FormattingCase(
56,
Currency.Form.usDollars,
"$56"
),
).forEach { (amount, form, expected) ->
val currency = Currency(amount, form)

val string = currency.format()

assertEquals(expected, string)
}
}

data class ComparisonCase(
val amount1: Long,
val form1: Currency.Form,
val amount2: Long,
val form2: Currency.Form,
val expected: Int
)

@Test
fun `comparison should `() {
listOf(
ComparisonCase(
1,
Currency.Form.usDollars,
100,
Currency.Form.usDollarCents,
0
),
ComparisonCase(
56,
Currency.Form.usDollars,
5610,
Currency.Form.usDollarCents,
-1
),
ComparisonCase(
56,
Currency.Form.usDollarCents,
54,
Currency.Form.usDollarCents,
1
),
).forEach { (amount1, form1, amount2, form2, expected) ->

val left = Currency(amount1, form1)
val right = Currency(amount2, form2)
val comparison = left.compareTo(right)

assertEquals(expected, comparison)
}
}

@Test
fun `dollars should return a currency with the correct form`() {
val amount = 100L
val currency = amount.dollars()

assertEquals(amount, currency.amount)
assertEquals(Currency.Form.usDollars, currency.form)
}

@Test
fun `cents should return a currency with the correct form`() {
val amount = 100L
val currency = amount.dollarCents()

assertEquals(amount, currency.amount)
assertEquals(Currency.Form.usDollarCents, currency.form)
}
}