Skip to content

Commit

Permalink
add toString version of datetime support methods
Browse files Browse the repository at this point in the history
-Tweak test method execution order for robustness of `LocalDateTime.now()`
- add methods to fetch portion of list in string representation using dates.
  • Loading branch information
0xEljh committed Oct 28, 2020
1 parent 1a58bc1 commit 61c0f21
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/main/java/seedu/dietbook/list/FoodList.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public List<Integer> getPortionSizes() {
return FoodListManager.listToPortionSizes(foodEntries);
}


/**
* Obtain list of LocalDateTimes for when the entries were made.
* (For storage purposes)
Expand All @@ -171,4 +172,25 @@ public String toString() {
return FoodListManager.listToString(foodEntries);
}

/**
* Returns toString representation of the segmented list based on DateTime.
* @param dateTime Start DateTime.
* @return string representation of FoodList
*/
public String getAfterDateTimeToString(LocalDateTime dateTime) {
List<FoodEntry> entriesAfterDateTime = FoodListManager.filterListByDate(foodEntries, dateTime);
return FoodListManager.listToString(entriesAfterDateTime);
}

/**
* Returns toString representation of the segmented list based on DateTime (within a range of 2 datetimes).
* @param start start DateTime.
* @param end end DateTime.
* @return string representation of FoodList
*/
public String getInDateTimeRangeToString(LocalDateTime start, LocalDateTime end) {
List<FoodEntry> entriesInRange = FoodListManager.filterListByDate(foodEntries, start, end);
return FoodListManager.listToString(entriesInRange);
}

}
15 changes: 11 additions & 4 deletions src/test/java/seedu/dietbook/list/FoodListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ void dateComparisonTest() {

@Test
void dateFilterAfterTest() {
assertEquals(list.toString(), list.getAfterDateTimeToString(LocalDateTime.MIN));

LocalDateTime timeNow = LocalDateTime.now();

assertTrue(list.getFoodsAfterDateTime(timeNow).size() == 0);
assertEquals(list.getFoodsAfterDateTime(LocalDateTime.MIN).toString(),
list.getFoods().toString());
assertEquals(list.getFoods().toString(),
list.getFoodsAfterDateTime(LocalDateTime.MIN).toString());


// add new entries:
if (! LocalDateTime.now().isAfter(timeNow)) { // Execution is too fast that now() = timeNow.
Expand All @@ -77,11 +80,17 @@ void dateFilterAfterTest() {
}
list.addFood(1, food);
assertEquals(food.toString(), list.getFoodsAfterDateTime(timeNow).get(0).toString());


}

@Test
void dateFilterRangeTest() {

assertEquals(list.getPortionedFoods().toString(),
list.getPortionedFoodsInDateTimeRange(LocalDateTime.MIN, LocalDateTime.MAX).toString());
assertEquals(list.toString(), list.getInDateTimeRangeToString(LocalDateTime.MIN, LocalDateTime.MAX));

LocalDateTime timeNow = LocalDateTime.now();

if (! LocalDateTime.now().isAfter(timeNow)) { // Execution is too fast that now() = timeNow.
Expand All @@ -95,8 +104,6 @@ void dateFilterRangeTest() {

assertTrue(list.getFoodsInDateTimeRange(timeNow, LocalDateTime.MAX).size() == 0);

assertEquals(list.getPortionedFoods().toString(),
list.getPortionedFoodsInDateTimeRange(LocalDateTime.MIN, LocalDateTime.MAX).toString());
}

@Test
Expand Down

0 comments on commit 61c0f21

Please sign in to comment.