Skip to content

Commit

Permalink
✅ Improve GramsValueFormatter + TwelveHourFormatter coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Feb 1, 2025
1 parent 98738ce commit 22e2326
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ val GramsValueFormatter = object : IValueFormatter {
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?
): String {
return "%.2f".format(entry?.y) + "g"
return "%.2f".format(entry?.y ?: 0.0) + "g"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.formatter.IAxisValueFormatter
import kotlin.math.roundToInt

@Suppress("MagicNumber")
val TwelveHourFormatter = object : IAxisValueFormatter {
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
if (value.roundToInt() == 12) return "12"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package br.com.colman.petals.statistics.graph.formatter

import com.github.mikephil.charting.data.Entry
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class GramsValueFormatterTest : FunSpec({
val formatter = GramsValueFormatter

test("formats positive value with two decimal places") {
val entry = Entry(0f, 123.456f)
formatter.getFormattedValue(999f, entry, 0, null) shouldBe "123.46g"
}

test("formats exact two decimal places correctly") {
val entry = Entry(0f, 7.89f)
formatter.getFormattedValue(0f, entry, 0, null) shouldBe "7.89g"
}

test("rounds up correctly") {
val entry = Entry(0f, 2.999f)
formatter.getFormattedValue(0f, entry, 0, null) shouldBe "3.00g"
}

test("formats zero value correctly") {
val entry = Entry(0f, 0f)
formatter.getFormattedValue(0f, entry, 0, null) shouldBe "0.00g"
}

test("formats negative value correctly") {
val entry = Entry(0f, -3.5f)
formatter.getFormattedValue(0f, entry, 0, null) shouldBe "-3.50g"
}

test("handles null entry by showing '0.00g'") {
formatter.getFormattedValue(0f, null, 0, null) shouldBe "0.00g"
}

test("ignores value parameter and uses entry.y") {
val entry = Entry(0f, 5f)
formatter.getFormattedValue(10f, entry, 0, null) shouldBe "5.00g"
}

test("formats large numbers correctly") {
val entry = Entry(0f, 999999.999f)
formatter.getFormattedValue(0f, entry, 0, null) shouldBe "1000000.00g"
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package br.com.colman.petals.statistics.graph.formatter

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class TwelveHourFormatterTest : FunSpec({
val formatter = TwelveHourFormatter

test("12.0 should return '12'") {
formatter.getFormattedValue(12.0f, null) shouldBe "12"
}

test("0.0 should return '0'") {
formatter.getFormattedValue(0.0f, null) shouldBe "0"
}

test("13.0 should return '1'") {
formatter.getFormattedValue(13.0f, null) shouldBe "1"
}

test("24.0 should return '0'") {
formatter.getFormattedValue(24.0f, null) shouldBe "0"
}

test("12.5 rounds to 13 and returns '1'") {
formatter.getFormattedValue(12.5f, null) shouldBe "1"
}

test("11.5 rounds to 12 and returns '12'") {
formatter.getFormattedValue(11.5f, null) shouldBe "12"
}

test("1.0 should return '1'") {
formatter.getFormattedValue(1.0f, null) shouldBe "1"
}

test("23.0 should return '11'") {
formatter.getFormattedValue(23.0f, null) shouldBe "11"
}

test("12.9 rounds to 13 and returns '1'") {
formatter.getFormattedValue(12.9f, null) shouldBe "1"
}

test("11.4 rounds to 11 and returns '11'") {
formatter.getFormattedValue(11.4f, null) shouldBe "11"
}

test("-1.0 should return '-1'") {
formatter.getFormattedValue(-1.0f, null) shouldBe "-1"
}

test("-13.0 should return '-1'") {
formatter.getFormattedValue(-13.0f, null) shouldBe "-1"
}
})

0 comments on commit 22e2326

Please sign in to comment.