|
| 1 | +package com.baeldung.scala.dates |
| 2 | + |
| 3 | +import org.scalatest.flatspec.AnyFlatSpec |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | +import org.scalatest.prop.TableDrivenPropertyChecks |
| 6 | + |
| 7 | +import java.time.LocalDate |
| 8 | + |
| 9 | +class DateListGeneratorUnitTest |
| 10 | + extends AnyFlatSpec |
| 11 | + with Matchers |
| 12 | + with TableDrivenPropertyChecks { |
| 13 | + |
| 14 | + private val start = LocalDate.parse("2023-10-28") |
| 15 | + private val end = LocalDate.parse("2023-11-03") |
| 16 | + |
| 17 | + private val expectedResult = List( |
| 18 | + "2023-10-28", |
| 19 | + "2023-10-29", |
| 20 | + "2023-10-30", |
| 21 | + "2023-10-31", |
| 22 | + "2023-11-01", |
| 23 | + "2023-11-02", |
| 24 | + "2023-11-03" |
| 25 | + ).map(LocalDate.parse) |
| 26 | + |
| 27 | + private val inOutTable = Table( |
| 28 | + ("startDate", "endDate", "expectedOutput", "message"), |
| 29 | + (start, end, expectedResult, "1 week"), |
| 30 | + (start, start, List(start), "start and end as start"), |
| 31 | + (end, end, List(end), "start and end as end"), |
| 32 | + (start, start.minusDays(1), List.empty, "end before start") |
| 33 | + ) |
| 34 | + |
| 35 | + forAll(inOutTable) { (startDate, endDate, expectedDates, prefix) => |
| 36 | + |
| 37 | + it should s"[${prefix}] generate a list of dates between a range using recursion" in { |
| 38 | + val recDateList = DateListGenerator.recursiveDateList(startDate, endDate) |
| 39 | + assert(recDateList == expectedDates) |
| 40 | + } |
| 41 | + |
| 42 | + it should s"[${prefix}] generate a list of dates between a range using foldLeft" in { |
| 43 | + val foldedDateList = |
| 44 | + DateListGenerator.foldLeftDateList(startDate, endDate) |
| 45 | + assert(foldedDateList == expectedDates) |
| 46 | + } |
| 47 | + |
| 48 | + it should s"[${prefix}] generate a list of dates between a range using iterator" in { |
| 49 | + val iteratorDates = DateListGenerator.iteratorDateList(startDate, endDate) |
| 50 | + assert(iteratorDates == expectedDates) |
| 51 | + } |
| 52 | + |
| 53 | + it should s"[${prefix}] generate a list of dates between a range using tabulator" in { |
| 54 | + val tabulateDateList = |
| 55 | + DateListGenerator.tabulateDateList(startDate, endDate) |
| 56 | + assert(tabulateDateList == expectedDates) |
| 57 | + } |
| 58 | + |
| 59 | + it should s"[${prefix}] generate a list of dates between a range using epoch days" in { |
| 60 | + val datesEpoch = DateListGenerator.dateListEpochDays(startDate, endDate) |
| 61 | + assert(datesEpoch == expectedDates) |
| 62 | + } |
| 63 | + |
| 64 | + it should s"[${prefix}] generate a list of dates between a range using daysBetween" in { |
| 65 | + val dates = DateListGenerator.dateListDaysBetween(startDate, endDate) |
| 66 | + assert(dates == expectedDates) |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments