Skip to content

Commit

Permalink
Improved Schedule Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfjw committed Nov 6, 2016
1 parent 28554ba commit 495cd68
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
26 changes: 17 additions & 9 deletions src/main/java/seedu/taskman/model/event/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public String toString() {

public static final String ERROR_NEGATIVE_DURATION = String.format(Messages.MESSAGE_INVALID_ARGUMENTS,
"Duration is negative");
public static final String ERROR_BAD_DATETIME_START = String.format(Messages.MESSAGE_INVALID_ARGUMENTS,
public static final String ERROR_FORMAT_BAD_DATETIME_START = String.format(Messages.MESSAGE_INVALID_ARGUMENTS,
"Bad start datetime, %1$s");
public static final String ERROR_BAD_DATETIME_END = String.format(Messages.MESSAGE_INVALID_ARGUMENTS,
public static final String ERROR_FORMAT_BAD_DATETIME_END = String.format(Messages.MESSAGE_INVALID_ARGUMENTS,
"Bad end datetime, %1$s");
public static final String ERROR_BAD_DURATION = String.format(Messages.MESSAGE_INVALID_ARGUMENTS,
public static final String ERROR_FORMAT_BAD_DURATION = String.format(Messages.MESSAGE_INVALID_ARGUMENTS,
"Bad duration, %1$s");

private static final String SCHEDULE_DIVIDER_GROUP = "((?:, )|(?: to )|(?: for ))";
Expand Down Expand Up @@ -110,7 +110,7 @@ private long parseRawStartTime(String rawStartTime) throws IllegalValueException
} catch (DateTimeParser.IllegalDateTimeException e) {

String errorMessage = Formatter.appendWithNewlines(
String.format(ERROR_BAD_DATETIME_START, rawStartTime),
String.format(ERROR_FORMAT_BAD_DATETIME_START, rawStartTime),
e.getMessage()
);
throw new IllegalValueException(errorMessage);
Expand All @@ -126,10 +126,11 @@ private long parseRawEndTime(String rawEndTime, long startEpochSecond) throws Il
// Inputs: Start Time - Friday, End Time - Monday
// Since next Monday comes before next Friday, this input is illegal if not rectified

boolean tryNextOccurrenceOfEndTime = false;
try {
long candidateResult = DateTimeParser.getEpochTime(rawEndTime);

boolean tryNextOccurrenceOfEndTime = startEpochSecond > candidateResult;
tryNextOccurrenceOfEndTime = startEpochSecond > candidateResult;
if (tryNextOccurrenceOfEndTime) {
String revisedEndTime = "next " + rawEndTime;
return DateTimeParser.getEpochTime(revisedEndTime);
Expand All @@ -138,11 +139,18 @@ private long parseRawEndTime(String rawEndTime, long startEpochSecond) throws Il
}

} catch (DateTimeParser.IllegalDateTimeException e) {

String errorMessage = Formatter.appendWithNewlines(
String.format(ERROR_BAD_DATETIME_END, rawEndTime),
e.getMessage()
String.format(ERROR_FORMAT_BAD_DATETIME_END, rawEndTime), e.getMessage()
);

// appending "next" may give unpredictable errors
// return the more sensible response of a negative duration,
// since it did parse correctly before our attempt at fixing the error

if (tryNextOccurrenceOfEndTime) {
errorMessage = ERROR_NEGATIVE_DURATION;
}

throw new IllegalValueException(errorMessage);
}
}
Expand All @@ -153,7 +161,7 @@ private long convertRawDurationToEndTime(String rawDuration, long startEpochSeco
} catch (DateTimeParser.IllegalDateTimeException e) {

String errorMessage = Formatter.appendWithNewlines(
String.format(ERROR_BAD_DURATION, rawDuration),
String.format(ERROR_FORMAT_BAD_DURATION, rawDuration),
e.getMessage()
);
throw new IllegalValueException(errorMessage);
Expand Down
42 changes: 31 additions & 11 deletions src/test/java/seedu/taskman/model/ScheduleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.concurrent.TimeUnit;

import static java.time.temporal.TemporalAdjusters.next;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;

public class ScheduleTest {
Expand All @@ -23,8 +24,8 @@ public class ScheduleTest {
@Test
public void schedule_twoDateTimes_success() throws IllegalValueException {

String start = "05/07/2016 00:01";
String end = "07/07/2016 00:02";
String start = "05-07-2016 00:01";
String end = "07-07-2016 00:02";

Schedule schedule = new Schedule(start + " " +
Schedule.ScheduleDivider.SCHEDULE + " " +
Expand All @@ -47,7 +48,7 @@ public void schedule_twoDateTimes_success() throws IllegalValueException {

@Test
public void schedule_dateTimeWithDuration_success() throws IllegalValueException {
String start = "05/07/2016 00:01";
String start = "05-07-2016 00:01";
int numHours = 2, numMinutes = 1;
String duration =
numHours + " hours, " +
Expand Down Expand Up @@ -106,32 +107,51 @@ public void create_convenienceConstructor_success() throws IllegalValueException

@Test
public void schedule_useBadDivider_failureWithCorrectMessage() throws IllegalValueException {
String start = "05/07/2016 0001";
String end = "07/07/2016 0002";
String start = "05-07-2016 00:01";
String end = "07-07-2016 00:02";

exception.expect(IllegalValueException.class);
exception.expectMessage(Schedule.MESSAGE_SCHEDULE_CONSTRAINTS);
new Schedule(start + " bad divider " + end);
}

// TODO: write tests
@Test
public void schedule_negativeDurationMainConstructor_failureWithCorrectMessage() {
public void schedule_negativeDurationMainConstructor_failureWithCorrectMessage() throws IllegalValueException {
String start = "07-07-2016 00:02";
String end = "05-07-2016 00:01";

exception.expectMessage(Schedule.ERROR_NEGATIVE_DURATION);
new Schedule(start + " " + Schedule.ScheduleDivider.SCHEDULE + " " + end);
}

@Test
public void schedule_negativeDurationConvenienceConstructor_failureWithCorrectMessage() {
public void schedule_negativeDurationConvenienceConstructor_failureWithCorrectMessage()
throws IllegalValueException {

long start = 10000;
long end = 1;
assertTrue(start > end);

exception.expectMessage(Schedule.ERROR_NEGATIVE_DURATION);
new Schedule(start, end);

}

@Test
public void schedule_unknownDateTime_failureWithCorrectMessage() {
public void schedule_unknownStartDateTime_failureWithCorrectMessage() throws IllegalValueException {
String start = "Unknown";
String end = "05-07-2016 00:01";

exception.expectMessage(String.format(Schedule.ERROR_FORMAT_BAD_DATETIME_START, start));
new Schedule(start + " " + Schedule.ScheduleDivider.SCHEDULE + " " + end);
}

@Test
public void schedule_unknownDuration_failureWithCorrectMessage() {
public void schedule_unknownDuration_failureWithCorrectMessage() throws IllegalValueException {
String start = "05-07-2016 00:01";
String end = "Unknown";

exception.expectMessage(String.format(Schedule.ERROR_FORMAT_BAD_DATETIME_END, end));
new Schedule(start + " " + Schedule.ScheduleDivider.SCHEDULE + " " + end);
}

//@@author
Expand Down

0 comments on commit 495cd68

Please sign in to comment.