From 53987bd2e46c4e03b07ca4db1e36e28eead59888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Sat, 19 Jul 2025 22:54:16 -0300 Subject: [PATCH] fix: fix unit tests and add tomorrow date format logic --- .../leomurca/csgomatches/utils/Extensions.kt | 28 +++++++++---------- app/src/main/res/values/strings.xml | 1 + .../csgomatches/utils/ExtensionsTest.kt | 21 +++++++++++--- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/xyz/leomurca/csgomatches/utils/Extensions.kt b/app/src/main/java/xyz/leomurca/csgomatches/utils/Extensions.kt index 53af59f..1d54fde 100644 --- a/app/src/main/java/xyz/leomurca/csgomatches/utils/Extensions.kt +++ b/app/src/main/java/xyz/leomurca/csgomatches/utils/Extensions.kt @@ -13,6 +13,7 @@ import java.util.Locale * * The formatting rules are as follows: * - If the date is today, returns: `"Hoje, HH:mm"` + * - If the date is tomorrow, returns: `"Amanhã, HH:mm"` * - If the date is within the current week, returns: `"EEE, HH:mm"` (e.g., `"Ter, 22:00"`) * - Otherwise, returns: `"dd.MM HH:mm"` (e.g., `"22.04 15:00"`) * @@ -21,19 +22,19 @@ import java.util.Locale * @receiver the [ZonedDateTime] instance to format. If null, an empty string is returned. * @return a formatted string representing the match time, or `"A definir"` if the input is null. */ -fun ZonedDateTime?.toFormattedMatchTime(context: Context): String { +fun ZonedDateTime?.toFormattedMatchTime( + context: Context, + today: LocalDate = LocalDate.now() +): String { if (this == null) return context.getString(R.string.match_time_tbd) val targetDate = toLocalDate() val timeFormatter = DateTimeFormatter.ofPattern("HH:mm") return when { - targetDate.isToday() -> { - val time = format(timeFormatter) - context.getString(R.string.match_time_today, time) - } - - targetDate.isInCurrentWeek() -> { + targetDate.isToday(today) -> context.getString(R.string.match_time_today, format(timeFormatter)) + targetDate.isTomorrow(today) -> context.getString(R.string.match_time_tomorrow, format(timeFormatter)) + targetDate.isInCurrentWeek(today) -> { val dayOfWeekFormatter = DateTimeFormatter.ofPattern("EEE", Locale("pt", "BR")) val day = format(dayOfWeekFormatter).replaceFirstChar { it.titlecase(Locale("pt", "BR")) @@ -49,11 +50,10 @@ fun ZonedDateTime?.toFormattedMatchTime(context: Context): String { } } -private fun LocalDate.isToday() = isEqual(ZonedDateTime.now().toLocalDate()) - -private fun LocalDate.isInCurrentWeek(): Boolean { - val today = LocalDate.now() - val startOfWeek = today.with(DayOfWeek.MONDAY) - val endOfWeek = today.with(DayOfWeek.SUNDAY) - return !this.isBefore(startOfWeek) && !this.isAfter(endOfWeek) +private fun LocalDate.isToday(reference: LocalDate): Boolean = isEqual(reference) +private fun LocalDate.isTomorrow(reference: LocalDate): Boolean = isEqual(reference.plusDays(1)) +private fun LocalDate.isInCurrentWeek(reference: LocalDate): Boolean { + val startOfWeek = reference.with(DayOfWeek.MONDAY) + val endOfWeek = reference.with(DayOfWeek.SUNDAY) + return !isBefore(startOfWeek) && !isAfter(endOfWeek) } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 515cc2b..1f8a6ea 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ Agora A definir Hoje, %1$s + Amanhã, %1$s %1$s, %2$s Tentar novamente Voltar diff --git a/app/src/test/java/xyz/leomurca/csgomatches/utils/ExtensionsTest.kt b/app/src/test/java/xyz/leomurca/csgomatches/utils/ExtensionsTest.kt index 2ae33f7..83d64aa 100644 --- a/app/src/test/java/xyz/leomurca/csgomatches/utils/ExtensionsTest.kt +++ b/app/src/test/java/xyz/leomurca/csgomatches/utils/ExtensionsTest.kt @@ -23,12 +23,25 @@ class ExtensionsTest { every { context.getString(R.string.match_time_today, any()) } returns "Hoje, 18:30" // Act - val result = date.toFormattedMatchTime(context) + val result = date.toFormattedMatchTime(context, today = now().toLocalDate()) // Assert assertEquals("Hoje, 18:30", result) } + @Test + fun `toFormattedMatchTime - returns Amanha when date is tomorrow`() { + // Arrange + val date = now().withHour(18).withMinute(30).plusDays(1) + every { context.getString(R.string.match_time_tomorrow, any()) } returns "Amanhã, 18:30" + + // Act + val result = date.toFormattedMatchTime(context, today = now().toLocalDate()) + + // Assert + assertEquals("Amanhã, 18:30", result) + } + @Test fun `toFormattedMatchTime - returns weekday in Portuguese when date is this week`() { // Arrange @@ -36,7 +49,7 @@ class ExtensionsTest { every { context.getString(R.string.match_time_weekday, any(), any()) } returns "Ter, 22:00" // Act - val result = tuesday.toFormattedMatchTime(context) + val result = tuesday.toFormattedMatchTime(context, today = now().toLocalDate()) // Assert assertEquals("Ter, 22:00", result) @@ -48,7 +61,7 @@ class ExtensionsTest { val future = now().plusWeeks(2).withHour(15).withMinute(0) // Act - val result = future.toFormattedMatchTime(context) + val result = future.toFormattedMatchTime(context, today = now().toLocalDate()) // Assert val expected = future.format(DateTimeFormatter.ofPattern("dd.MM HH:mm")) @@ -62,7 +75,7 @@ class ExtensionsTest { every { context.getString(R.string.match_time_tbd) } returns "A definir" // Act - val result = date.toFormattedMatchTime(context) + val result = date.toFormattedMatchTime(context, today = now().toLocalDate()) // Assert assertEquals("A definir", result)