Skip to content

Commit

Permalink
Merge pull request #371 from jianyangg/369-bug-fix-invalid-date
Browse files Browse the repository at this point in the history
Add try catch for invalid date
  • Loading branch information
jianyangg authored Nov 2, 2023
2 parents acb78d2 + d63d0cd commit 8625a16
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,13 @@ public static DeliveryDate parseDeliveryDate(String date) throws ParseException
requireNonNull(date);
String trimmedDate = date.trim();
if (!DeliveryDate.isValidDate(date)) {
throw new ParseException(seedu.address.model.delivery.Date.MESSAGE_CONSTRAINTS);
throw new ParseException(Date.MESSAGE_CONSTRAINTS);
}
try {
return new DeliveryDate(trimmedDate);
} catch (DateTimeException e) {
throw new ParseException(Date.MESSAGE_CONSTRAINTS);
}
return new DeliveryDate(trimmedDate);
}

/**
Expand All @@ -160,7 +164,6 @@ public static Date parseDate(String date) throws ParseException {
throw new ParseException(Date.MESSAGE_CONSTRAINTS);
}


try {
return new Date(trimmedDate);
} catch (DateTimeException e) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.api.Test;

import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.delivery.DeliveryDate;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -174,4 +175,26 @@ public void parseAnswer_validValueWithoutWhitespace_returnsAnswer() throws Excep
String testString = "Blue";
assertEquals(testString, ParserUtil.parseAnswer(testString));
}

@Test
public void parseDeliveryDate_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> ParserUtil.parseDeliveryDate((String) null));
}

@Test
public void parseDeliveryDate_beforeToday_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseDeliveryDate("0000-13-01"));
}

@Test
public void parseDeliveryDate_invalidMonth_throwsParseException() {
assertThrows(ParseException.class, () -> ParserUtil.parseDeliveryDate("9999-13-01"));
}

@Test
public void parseDeliveryDate_validValueWithoutWhitespace_returnsDeliveryDate() throws Exception {
String testString = "9999-12-15";
DeliveryDate expectedDeliveryDate = new DeliveryDate(testString);
assertEquals(expectedDeliveryDate, ParserUtil.parseDeliveryDate(testString));
}
}

0 comments on commit 8625a16

Please sign in to comment.