forked from nus-cs2103-AY2223S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from seetohjinwei/add-dateparser
Implement DateParser class
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package taskbook.logic.parser; | ||
|
||
import static taskbook.commons.core.Messages.MESSAGE_INVALID_DATE_FORMAT; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.time.format.DateTimeParseException; | ||
|
||
import taskbook.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses user input's date into internal Date object. | ||
* Accepts multiple Date formats. | ||
*/ | ||
public class DateParser { | ||
|
||
private static final DateTimeFormatter PARSER_OPTIONAL_FORMATS = new DateTimeFormatterBuilder() | ||
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE) | ||
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd")) | ||
.appendOptional(DateTimeFormatter.ofPattern("MMM dd yyyy")) | ||
.appendOptional(DateTimeFormatter.ofPattern("MM dd yyyy")) | ||
.appendOptional(DateTimeFormatter.ofPattern("dd MMM yyyy")) | ||
.toFormatter(); | ||
|
||
/** | ||
* Parses {@code userInput} into a Date object and returns it. | ||
* @throws ParseException if {@code userInput} does not conform the expected formats. | ||
*/ | ||
public static LocalDate parse(String userInput) throws ParseException { | ||
try { | ||
return LocalDate.parse(userInput, PARSER_OPTIONAL_FORMATS); | ||
} catch (DateTimeParseException e) { | ||
throw new ParseException(MESSAGE_INVALID_DATE_FORMAT); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package taskbook.logic.parser; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.time.LocalDate; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import taskbook.logic.parser.exceptions.ParseException; | ||
|
||
public class DateParserTest { | ||
|
||
private final LocalDate expectedOutput = LocalDate.parse("2022-10-31"); | ||
|
||
@Test | ||
public void parse_validArgument1_returnsCorrectOutput() throws ParseException { | ||
LocalDate actual = DateParser.parse("2022-10-31"); | ||
assertEquals(actual, expectedOutput); | ||
} | ||
|
||
@Test | ||
public void parse_validArgument2_returnsCorrectOutput() throws ParseException { | ||
LocalDate actual = DateParser.parse("Oct 31 2022"); | ||
assertEquals(actual, expectedOutput); | ||
} | ||
|
||
@Test | ||
public void parse_validArgument3_returnsCorrectOutput() throws ParseException { | ||
LocalDate actual = DateParser.parse("10 31 2022"); | ||
assertEquals(actual, expectedOutput); | ||
} | ||
|
||
@Test | ||
public void parse_validArgument4_returnsCorrectOutput() throws ParseException { | ||
LocalDate actual = DateParser.parse("31 Oct 2022"); | ||
assertEquals(actual, expectedOutput); | ||
} | ||
|
||
@Test | ||
public void parse_emptyArgument_throwsException() { | ||
assertThrowsException(""); | ||
} | ||
|
||
@Test | ||
public void parse_invalidInput_throwsException() { | ||
assertThrowsException("Halloween"); | ||
} | ||
|
||
@Test | ||
public void parse_invalidFormat_throwsException() { | ||
assertThrowsException("31st October 2022"); | ||
} | ||
|
||
public void assertThrowsException(String userInput) { | ||
try { | ||
DateParser.parse(userInput); | ||
throw new AssertionError("The expected ParseException is not thrown."); | ||
} catch (ParseException e) { | ||
// intentionally empty catch block | ||
} | ||
} | ||
} |