fix: fix unit tests and add tomorrow date format logic
Some checks failed
Android Unit Tests / tests (push) Failing after 5m53s

This commit is contained in:
Leonardo Murça 2025-07-19 22:54:16 -03:00
parent f10e0b60c7
commit 53987bd2e4
3 changed files with 32 additions and 18 deletions

View file

@ -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)
}

View file

@ -7,6 +7,7 @@
<string name="live">Agora</string>
<string name="match_time_tbd">A definir</string>
<string name="match_time_today">Hoje, %1$s</string>
<string name="match_time_tomorrow">Amanhã, %1$s</string>
<string name="match_time_weekday">%1$s, %2$s</string>
<string name="try_again">Tentar novamente</string>
<string name="back">Voltar</string>

View file

@ -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)