Skip to content

Commit

Permalink
Improve unit tests and improve readability of `DisplayedDatesRangeFac…
Browse files Browse the repository at this point in the history
…tory` class
  • Loading branch information
rAseri committed Aug 1, 2019
1 parent 7da03b1 commit c20db63
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ internal object DisplayedDatesRangeFactory {
}

minDate != null && maxDate == null -> {
rangeStart = calculateRangeStart(minDate = minDate, initialDate = initialDate)
rangeStart = calculateRangeStart(dateFrom = minDate, dateTo = initialDate)
rangeEnd = rangeStart.plusMonths(CalendarConst.MONTHS_PER_PAGE)
}

minDate == null && maxDate != null -> {
rangeEnd = calculateRangeEnd(maxDate = maxDate, initialDate = initialDate)
rangeEnd = calculateRangeEnd(dateFrom = initialDate, dateTo = maxDate)
rangeStart = rangeEnd.minusMonths(CalendarConst.MONTHS_PER_PAGE)
}

minDate != null && maxDate != null -> {
if (initialDate.isBetween(minDate, maxDate)) {
rangeStart = calculateRangeStart(minDate = minDate, initialDate = initialDate)
rangeEnd = calculateRangeEnd(maxDate = maxDate, initialDate = initialDate)
rangeStart = calculateRangeStart(dateFrom = minDate, dateTo = initialDate)
rangeEnd = calculateRangeEnd(dateFrom = initialDate, dateTo = maxDate)
} else {
rangeStart = minDate
rangeEnd = calculateRangeEnd(maxDate = maxDate, initialDate = minDate)
rangeEnd = calculateRangeEnd(dateFrom = minDate, dateTo = maxDate)
}
}

Expand All @@ -52,28 +52,24 @@ internal object DisplayedDatesRangeFactory {
}

private fun calculateRangeStart(
minDate: CalendarDate,
initialDate: CalendarDate
dateFrom: CalendarDate,
dateTo: CalendarDate
): CalendarDate {
val monthsBetween = minDate.monthsBetween(initialDate)

return if (monthsBetween > CalendarConst.MONTHS_PER_PAGE) {
initialDate.minusMonths(CalendarConst.MONTHS_PER_PAGE)
return if (dateFrom.monthsBetween(dateTo) > CalendarConst.MONTHS_PER_PAGE) {
dateTo.minusMonths(CalendarConst.MONTHS_PER_PAGE)
} else {
minDate
dateFrom
}
}

private fun calculateRangeEnd(
maxDate: CalendarDate,
initialDate: CalendarDate
dateFrom: CalendarDate,
dateTo: CalendarDate
): CalendarDate {
val monthsBetween = initialDate.monthsBetween(maxDate)

return if (monthsBetween > CalendarConst.MONTHS_PER_PAGE) {
initialDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
return if (dateFrom.monthsBetween(dateTo) > CalendarConst.MONTHS_PER_PAGE) {
dateFrom.plusMonths(CalendarConst.MONTHS_PER_PAGE)
} else {
maxDate
dateTo
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class DisplayedDatesRangeFactoryTest {
@Test
fun `Get displayed dates range when min and max dates aren't defined, as a result correct dates range is returned`() {
// Given
val today = CalendarDate.today
val initialDate = CalendarDate.today

val expectedDatesRange = DatesRange(
dateFrom = today.minusMonths(CalendarConst.MONTHS_PER_PAGE),
dateTo = today.plusMonths(CalendarConst.MONTHS_PER_PAGE)
dateFrom = initialDate.minusMonths(CalendarConst.MONTHS_PER_PAGE),
dateTo = initialDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
)

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = CalendarDate.today,
initialDate = initialDate,
minDate = null,
maxDate = null
)
Expand All @@ -47,6 +47,7 @@ class DisplayedDatesRangeFactoryTest {
fun `Get displayed dates range when min date is defined and max date isn't defined, as a result correct dates range is returned`() {
// Given
val minDate = CalendarDate.today
val initialDate = CalendarDate.today

val expectedDatesRange = DatesRange(
dateFrom = minDate,
Expand All @@ -55,7 +56,7 @@ class DisplayedDatesRangeFactoryTest {

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = CalendarDate.today,
initialDate = initialDate,
minDate = minDate,
maxDate = null
)
Expand All @@ -71,6 +72,7 @@ class DisplayedDatesRangeFactoryTest {
fun `Get displayed dates range when min date isn't defined and max date is defined, as a result correct dates range is returned`() {
// Given
val maxDate = CalendarDate.today
val initialDate = CalendarDate.today

val expectedDatesRange = DatesRange(
dateFrom = maxDate.minusMonths(CalendarConst.MONTHS_PER_PAGE),
Expand All @@ -79,7 +81,7 @@ class DisplayedDatesRangeFactoryTest {

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = CalendarDate.today,
initialDate = initialDate,
minDate = null,
maxDate = maxDate
)
Expand Down Expand Up @@ -108,7 +110,7 @@ class DisplayedDatesRangeFactoryTest {

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = CalendarDate.today,
initialDate = initialDate,
minDate = minDate,
maxDate = null
)
Expand Down Expand Up @@ -149,11 +151,68 @@ class DisplayedDatesRangeFactoryTest {
)
}

@Test
fun `Get displayed dates range when min date isn't defined, max date is defined and initial date is more then max date, as a result correct dates range is returned`() {
// Given
val calendar = Calendar.getInstance()
calendar.set(2019, Calendar.AUGUST, 1)
val maxDate = CalendarDate(calendar.time)

val initialDate = maxDate.plusMonths(1)

val expectedDatesRange = DatesRange(
dateFrom = maxDate.minusMonths(CalendarConst.MONTHS_PER_PAGE),
dateTo = maxDate
)

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = initialDate,
minDate = null,
maxDate = maxDate
)

// Then
Assert.assertTrue(
"Unexpected actual dates range: $actualDatesRange, expected: $expectedDatesRange",
expectedDatesRange == actualDatesRange
)
}

@Test
fun `Get displayed dates range when min date is defined, max date isn't defined and initial date is less then min date, as a result correct dates range is returned`() {
// Given
val calendar = Calendar.getInstance()
calendar.set(2019, Calendar.JANUARY, 1)
val minDate = CalendarDate(calendar.time)

val initialDate = minDate.minusMonths(1)

val expectedDatesRange = DatesRange(
dateFrom = minDate,
dateTo = minDate.plusMonths(CalendarConst.MONTHS_PER_PAGE)
)

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = initialDate,
minDate = minDate,
maxDate = null
)

// Then
Assert.assertTrue(
"Unexpected actual dates range: $actualDatesRange, expected: $expectedDatesRange",
expectedDatesRange == actualDatesRange
)
}

@Test
fun `Get displayed dates range when min-max dates are defined, initial dates is out of min-max date boundaries and days between min-max dates more then 6 month, as a result correct dates range is returned`() {
// Given
val minDate = CalendarDate.today
val maxDate = minDate.plusMonths(CalendarConst.MONTHS_PER_PAGE * 2)
val initialDate = maxDate.plusMonths(1)

val expectedDatesRange = DatesRange(
dateFrom = minDate,
Expand All @@ -162,7 +221,7 @@ class DisplayedDatesRangeFactoryTest {

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = CalendarDate.today,
initialDate = initialDate,
minDate = minDate,
maxDate = maxDate
)
Expand All @@ -179,6 +238,7 @@ class DisplayedDatesRangeFactoryTest {
// Given
val minDate = CalendarDate.today
val maxDate = minDate.plusMonths(CalendarConst.MONTHS_PER_PAGE / 2)
val initialDate = maxDate.plusMonths(1)

val expectedDatesRange = DatesRange(
dateFrom = minDate,
Expand All @@ -187,7 +247,7 @@ class DisplayedDatesRangeFactoryTest {

// When
val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange(
initialDate = CalendarDate.today,
initialDate = initialDate,
minDate = minDate,
maxDate = maxDate
)
Expand Down

0 comments on commit c20db63

Please sign in to comment.