From c20db631f638609b85a381c1431fcfad87ec7d8c Mon Sep 17 00:00:00 2001 From: Alexander Surinov Date: Thu, 1 Aug 2019 23:39:37 +0300 Subject: [PATCH] Improve unit tests and improve readability of `DisplayedDatesRangeFactory` class --- .../utils/DisplayedDatesRangeFactory.kt | 34 ++++---- .../utils/DisplayedDatesRangeFactoryTest.kt | 78 ++++++++++++++++--- 2 files changed, 84 insertions(+), 28 deletions(-) diff --git a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactory.kt b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactory.kt index 2e72694..f8668a3 100644 --- a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactory.kt +++ b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactory.kt @@ -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) } } @@ -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 } } diff --git a/crunchycalendar/src/test/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactoryTest.kt b/crunchycalendar/src/test/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactoryTest.kt index 2a5694c..dadf73e 100644 --- a/crunchycalendar/src/test/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactoryTest.kt +++ b/crunchycalendar/src/test/java/ru/cleverpumpkin/calendar/utils/DisplayedDatesRangeFactoryTest.kt @@ -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 ) @@ -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, @@ -55,7 +56,7 @@ class DisplayedDatesRangeFactoryTest { // When val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange( - initialDate = CalendarDate.today, + initialDate = initialDate, minDate = minDate, maxDate = null ) @@ -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), @@ -79,7 +81,7 @@ class DisplayedDatesRangeFactoryTest { // When val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange( - initialDate = CalendarDate.today, + initialDate = initialDate, minDate = null, maxDate = maxDate ) @@ -108,7 +110,7 @@ class DisplayedDatesRangeFactoryTest { // When val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange( - initialDate = CalendarDate.today, + initialDate = initialDate, minDate = minDate, maxDate = null ) @@ -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, @@ -162,7 +221,7 @@ class DisplayedDatesRangeFactoryTest { // When val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange( - initialDate = CalendarDate.today, + initialDate = initialDate, minDate = minDate, maxDate = maxDate ) @@ -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, @@ -187,7 +247,7 @@ class DisplayedDatesRangeFactoryTest { // When val actualDatesRange = DisplayedDatesRangeFactory.getDisplayedDatesRange( - initialDate = CalendarDate.today, + initialDate = initialDate, minDate = minDate, maxDate = maxDate )