Skip to content

Commit

Permalink
Unbounded month calculations should not exceed the number of days… (#90)
Browse files Browse the repository at this point in the history
Unbounded month calculations should not exceed the number of days in …

Co-authored-by: Kizito Nwose <[email protected]>
  • Loading branch information
kizitonwose authored Dec 7, 2019
2 parents 735d09e + 6a6a5a9 commit 36bd821
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,8 @@ internal data class MonthConfig(

val lastDay = monthWeeks.last().last()

val lastDayIsEndOfFirstOutDates =
lastDay.owner == DayOwner.NEXT_MONTH && lastDay.date == lastDay.date.yearMonth.atEndOfMonth()

// Move to the second outDates if we have reached the end of
// the first outDates, use the first outDates otherwise.
val outMonth = lastDay
.date.yearMonth.plusMonths(if (lastDay.owner == DayOwner.THIS_MONTH || lastDayIsEndOfFirstOutDates) 1 else 0)

val nextRowDates = (1..7).map {
val dayValue =
if (lastDay.owner == DayOwner.THIS_MONTH || lastDayIsEndOfFirstOutDates) it else it + lastDay.day

CalendarDay(LocalDate.of(outMonth.year, outMonth.month, dayValue), DayOwner.NEXT_MONTH)
CalendarDay(lastDay.date.plusDays(it.toLong()), DayOwner.NEXT_MONTH)
}

if (monthWeeks.last().size < 7) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,15 @@ class CalenderViewTests {
// all weeks by maxRowCount value.
assertTrue(months.all { it.weekDays.count() == maxRowCount })
}

@Test
fun `test unbounded month calculation does not exceed days of month`() {
val maxRowCount = 6
MonthConfig.generateUnboundedMonths(
YearMonth.of(2019, 12), YearMonth.of(2020, 2),
DayOfWeek.SUNDAY, maxRowCount, InDateStyle.ALL_MONTHS, OutDateStyle.END_OF_GRID
)
// No assertion neccessary, as this particular range would throw an exception previously
// when trying to build a day that is out of bounds (eg: December 32).
}
}

0 comments on commit 36bd821

Please sign in to comment.