Skip to content

Commit

Permalink
Merge pull request #114 from sushiyade/filter-events
Browse files Browse the repository at this point in the history
Filter events use new DateTimeParser
  • Loading branch information
flexibo authored Oct 27, 2023
2 parents 7fdbd18 + e0ebc31 commit 87c315c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser.events;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.DateTimeParser.parseDateTimeInstance;

import seedu.address.logic.commands.events.FilterEventTimeCommand;
import seedu.address.logic.parser.Parser;
Expand All @@ -23,9 +24,7 @@ public FilterEventTimeCommand parse(String args) throws ParseException {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterEventTimeCommand.MESSAGE_USAGE));
}
trimmedArgs += " 23:59";

return new FilterEventTimeCommand(new EventTimeBeforePredicate(trimmedArgs));
return new FilterEventTimeCommand(new EventTimeBeforePredicate(parseDateTimeInstance(trimmedArgs)));
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface Model {
Predicate<Event> PREDICATE_SHOW_ALL_EVENTS = unused -> true;

/** {@code Predicate} that always evaluate to true */
Predicate<Event> PREDICATE_SHOW_ALL_EVENTS_AFTER_TODAY = event -> !(event.getTimeStart().getTime()
Predicate<Event> PREDICATE_SHOW_ALL_EVENTS_AFTER_TODAY = event -> !(event.getTimeEnd().getTime()
.isBefore(LocalDateTime.now()));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class EventTimeBeforePredicate implements Predicate<Event> {
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
private final LocalDateTime eventTime;

public EventTimeBeforePredicate(String eventTime) {
this.eventTime = LocalDateTime.parse(eventTime, DATE_TIME_FORMATTER);
public EventTimeBeforePredicate(LocalDateTime eventTime) {
this.eventTime = eventTime; //change this to accept LocalDateTime instead of String?
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.DateTimeParser.parseDateTimeInstance;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_EVENT;
import static seedu.address.testutil.TypicalPersons.ALICE;
Expand Down Expand Up @@ -64,11 +65,12 @@ public void parseCommand_delete() throws Exception {
@Test
public void parseCommand_filterEventTime() throws Exception {
String args = "01-01-2023";
String hourMinArgs = args.trim() + " 23:59";
String hourMinArgs = args.trim() + " 00:00";
FilterEventTimeCommand command = (FilterEventTimeCommand) parser.parseCommand(
FilterEventTimeCommand.COMMAND_WORD + " " + args);

assertEquals(new FilterEventTimeCommand(new EventTimeBeforePredicate(hourMinArgs)), command);
assertEquals(new FilterEventTimeCommand(new EventTimeBeforePredicate(
parseDateTimeInstance(hourMinArgs))), command);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.logic.parser.DateTimeParser.parseDateTimeInstance;

import java.time.LocalDateTime;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.events.FilterEventTimeCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.event.EventTimeBeforePredicate;


public class FilterEventTimeCommandParserTest {

private FilterEventTimeCommandParser parser = new FilterEventTimeCommandParser();

@Test
public void parse_validArgs_returnsFilterEventTimeCommand() throws ParseException {
String args = " 01-01-2023";
String hourMinArgs = args.trim() + " 23:59";
FilterEventTimeCommand expectedCommand = new FilterEventTimeCommand(new EventTimeBeforePredicate(hourMinArgs));
String hourMinArgs = args.trim() + " 00:00";
LocalDateTime dateTime = parseDateTimeInstance(hourMinArgs);
FilterEventTimeCommand expectedCommand = new FilterEventTimeCommand(new EventTimeBeforePredicate(dateTime));
assertParseSuccess(parser, args, expectedCommand);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,97 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static seedu.address.logic.parser.DateTimeParser.parseDateTimeInstance;
import static seedu.address.testutil.Assert.assertThrows;

import java.time.format.DateTimeParseException;
import java.time.LocalDateTime;

import org.junit.jupiter.api.Test;

import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.testutil.EventBuilder;


public class EventTimeBeforePredicateTest {
@Test
public void testConstructor_validDateTimeString_success() {
String eventTimeString = "01-01-2023 10:00";
EventTimeBeforePredicate predicate = new EventTimeBeforePredicate(eventTimeString);
LocalDateTime eventTime = null;
try {
eventTime = parseDateTimeInstance(eventTimeString);
} catch (ParseException e) {
fail();
}
EventTimeBeforePredicate predicate = new EventTimeBeforePredicate(eventTime);
assertNotNull(predicate);
}

@Test
public void testConstructor_invalidDateTimeString_throwsException() {
String invalidEventTimeString = "InvalidDateTime";
assertThrows(DateTimeParseException.class, () -> new EventTimeBeforePredicate(invalidEventTimeString));
assertThrows(ParseException.class, () -> new EventTimeBeforePredicate(
parseDateTimeInstance(invalidEventTimeString)));
}

@Test
public void testTest_eventTimeBeforePredicate_returnsTrue() {
Event event = new EventBuilder().withTimeStart("01-01-2023 10:00").withTimeEnd("01-01-2023 11:00").build();
EventTimeBeforePredicate predicate = new EventTimeBeforePredicate("01-01-2023 11:00");
EventTimeBeforePredicate predicate = null;
try {
predicate = new EventTimeBeforePredicate(parseDateTimeInstance("01-01-2023 11:00"));
} catch (ParseException e) {
fail();
}
assertTrue(predicate.test(event));
}

@Test
public void testTest_eventTimeEqualOrAfterPredicate_returnsFalse() {
Event event = new EventBuilder().withTimeStart("01-01-2023 10:00").withTimeEnd("01-01-2023 11:00").build();
EventTimeBeforePredicate predicate = new EventTimeBeforePredicate("01-01-2023 10:00");
EventTimeBeforePredicate predicate = null;
try {
predicate = new EventTimeBeforePredicate(parseDateTimeInstance("01-01-2023 10:00"));
} catch (ParseException e) {
throw new RuntimeException(e);
}
assertFalse(predicate.test(event));
}

@Test
public void testEquals_equalPredicates_returnsTrue() {
EventTimeBeforePredicate predicate1 = new EventTimeBeforePredicate("01-01-2023 10:00");
EventTimeBeforePredicate predicate2 = new EventTimeBeforePredicate("01-01-2023 10:00");
EventTimeBeforePredicate predicate1 = null;
EventTimeBeforePredicate predicate2 = null;
try {
predicate1 = new EventTimeBeforePredicate(parseDateTimeInstance("01-01-2023 10:00"));
predicate2 = new EventTimeBeforePredicate(parseDateTimeInstance("01-01-2023 10:00"));
} catch (ParseException e) {
fail();
}
assertEquals(predicate1, predicate2);
}

@Test
public void testEquals_unequalPredicates_returnsFalse() {
EventTimeBeforePredicate predicate1 = new EventTimeBeforePredicate("01-01-2023 10:00");
EventTimeBeforePredicate predicate2 = new EventTimeBeforePredicate("01-01-2023 11:00");
EventTimeBeforePredicate predicate1 = null;
EventTimeBeforePredicate predicate2 = null;
try {
predicate1 = new EventTimeBeforePredicate(parseDateTimeInstance("01-01-2023 10:00"));
predicate2 = new EventTimeBeforePredicate(parseDateTimeInstance("01-01-2023 11:00"));
} catch (ParseException e) {
fail();
}
assertNotEquals(predicate1, predicate2);
}

@Test
public void testToString_validEventTime_returnsExpectedString() {
EventTimeBeforePredicate predicate = new EventTimeBeforePredicate("01-01-2023 10:00");
EventTimeBeforePredicate predicate = null;
try {
predicate = new EventTimeBeforePredicate(parseDateTimeInstance("01-01-2023 10:00"));
} catch (ParseException e) {
fail();
}
String expectedString = EventTimeBeforePredicate.class.getCanonicalName() + "{event time=2023-01-01T10:00}";
assertEquals(expectedString, predicate.toString());
}
Expand Down

0 comments on commit 87c315c

Please sign in to comment.