fix: fix unit tests and add tomorrow date format logic
Some checks failed
Android Unit Tests / tests (push) Failing after 5m53s
Some checks failed
Android Unit Tests / tests (push) Failing after 5m53s
This commit is contained in:
parent
f10e0b60c7
commit
53987bd2e4
3 changed files with 32 additions and 18 deletions
|
@ -13,6 +13,7 @@ import java.util.Locale
|
||||||
*
|
*
|
||||||
* The formatting rules are as follows:
|
* The formatting rules are as follows:
|
||||||
* - If the date is today, returns: `"Hoje, HH:mm"`
|
* - 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"`)
|
* - 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"`)
|
* - 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.
|
* @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.
|
* @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)
|
if (this == null) return context.getString(R.string.match_time_tbd)
|
||||||
|
|
||||||
val targetDate = toLocalDate()
|
val targetDate = toLocalDate()
|
||||||
val timeFormatter = DateTimeFormatter.ofPattern("HH:mm")
|
val timeFormatter = DateTimeFormatter.ofPattern("HH:mm")
|
||||||
|
|
||||||
return when {
|
return when {
|
||||||
targetDate.isToday() -> {
|
targetDate.isToday(today) -> context.getString(R.string.match_time_today, format(timeFormatter))
|
||||||
val time = format(timeFormatter)
|
targetDate.isTomorrow(today) -> context.getString(R.string.match_time_tomorrow, format(timeFormatter))
|
||||||
context.getString(R.string.match_time_today, time)
|
targetDate.isInCurrentWeek(today) -> {
|
||||||
}
|
|
||||||
|
|
||||||
targetDate.isInCurrentWeek() -> {
|
|
||||||
val dayOfWeekFormatter = DateTimeFormatter.ofPattern("EEE", Locale("pt", "BR"))
|
val dayOfWeekFormatter = DateTimeFormatter.ofPattern("EEE", Locale("pt", "BR"))
|
||||||
val day = format(dayOfWeekFormatter).replaceFirstChar {
|
val day = format(dayOfWeekFormatter).replaceFirstChar {
|
||||||
it.titlecase(Locale("pt", "BR"))
|
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.isToday(reference: LocalDate): Boolean = isEqual(reference)
|
||||||
|
private fun LocalDate.isTomorrow(reference: LocalDate): Boolean = isEqual(reference.plusDays(1))
|
||||||
private fun LocalDate.isInCurrentWeek(): Boolean {
|
private fun LocalDate.isInCurrentWeek(reference: LocalDate): Boolean {
|
||||||
val today = LocalDate.now()
|
val startOfWeek = reference.with(DayOfWeek.MONDAY)
|
||||||
val startOfWeek = today.with(DayOfWeek.MONDAY)
|
val endOfWeek = reference.with(DayOfWeek.SUNDAY)
|
||||||
val endOfWeek = today.with(DayOfWeek.SUNDAY)
|
return !isBefore(startOfWeek) && !isAfter(endOfWeek)
|
||||||
return !this.isBefore(startOfWeek) && !this.isAfter(endOfWeek)
|
|
||||||
}
|
}
|
|
@ -7,6 +7,7 @@
|
||||||
<string name="live">Agora</string>
|
<string name="live">Agora</string>
|
||||||
<string name="match_time_tbd">A definir</string>
|
<string name="match_time_tbd">A definir</string>
|
||||||
<string name="match_time_today">Hoje, %1$s</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="match_time_weekday">%1$s, %2$s</string>
|
||||||
<string name="try_again">Tentar novamente</string>
|
<string name="try_again">Tentar novamente</string>
|
||||||
<string name="back">Voltar</string>
|
<string name="back">Voltar</string>
|
||||||
|
|
|
@ -23,12 +23,25 @@ class ExtensionsTest {
|
||||||
every { context.getString(R.string.match_time_today, any()) } returns "Hoje, 18:30"
|
every { context.getString(R.string.match_time_today, any()) } returns "Hoje, 18:30"
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
val result = date.toFormattedMatchTime(context)
|
val result = date.toFormattedMatchTime(context, today = now().toLocalDate())
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assertEquals("Hoje, 18:30", result)
|
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
|
@Test
|
||||||
fun `toFormattedMatchTime - returns weekday in Portuguese when date is this week`() {
|
fun `toFormattedMatchTime - returns weekday in Portuguese when date is this week`() {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
@ -36,7 +49,7 @@ class ExtensionsTest {
|
||||||
every { context.getString(R.string.match_time_weekday, any(), any()) } returns "Ter, 22:00"
|
every { context.getString(R.string.match_time_weekday, any(), any()) } returns "Ter, 22:00"
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
val result = tuesday.toFormattedMatchTime(context)
|
val result = tuesday.toFormattedMatchTime(context, today = now().toLocalDate())
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assertEquals("Ter, 22:00", result)
|
assertEquals("Ter, 22:00", result)
|
||||||
|
@ -48,7 +61,7 @@ class ExtensionsTest {
|
||||||
val future = now().plusWeeks(2).withHour(15).withMinute(0)
|
val future = now().plusWeeks(2).withHour(15).withMinute(0)
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
val result = future.toFormattedMatchTime(context)
|
val result = future.toFormattedMatchTime(context, today = now().toLocalDate())
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
val expected = future.format(DateTimeFormatter.ofPattern("dd.MM HH:mm"))
|
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"
|
every { context.getString(R.string.match_time_tbd) } returns "A definir"
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
val result = date.toFormattedMatchTime(context)
|
val result = date.toFormattedMatchTime(context, today = now().toLocalDate())
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
assertEquals("A definir", result)
|
assertEquals("A definir", result)
|
||||||
|
|
Loading…
Add table
Reference in a new issue