diff --git a/src/planmysem/logic/commands/ViewCommand.java b/src/planmysem/logic/commands/ViewCommand.java index f36137d19..87edafd07 100644 --- a/src/planmysem/logic/commands/ViewCommand.java +++ b/src/planmysem/logic/commands/ViewCommand.java @@ -7,6 +7,7 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.WeekFields; import java.util.ArrayList; +import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -459,4 +460,11 @@ public int compare(final Slot o1, final Slot o2) { return sb.toString(); } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof ViewCommand // instanceof handles nulls + && Arrays.equals(viewArgs, ((ViewCommand) other).viewArgs)); + } } diff --git a/src/planmysem/logic/parser/ViewCommandParser.java b/src/planmysem/logic/parser/ViewCommandParser.java index 2ba7d549a..42521b83e 100644 --- a/src/planmysem/logic/parser/ViewCommandParser.java +++ b/src/planmysem/logic/parser/ViewCommandParser.java @@ -3,11 +3,9 @@ import static planmysem.common.Messages.MESSAGE_ILLEGAL_WEEK_VALUE; import static planmysem.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static planmysem.common.Messages.MESSAGE_INVALID_DATE; -import static planmysem.common.Utils.getNearestDayOfWeek; import java.time.LocalDate; -import planmysem.common.Clock; import planmysem.common.Utils; import planmysem.logic.commands.ViewCommand; import planmysem.logic.parser.exceptions.ParseException; @@ -55,7 +53,7 @@ public ViewCommand parse(String args) throws ParseException { } return new ViewCommand(viewArgs); - } else if (viewArgs.length == 3 ) { + } else if (viewArgs.length == 3) { viewArgs[1] = viewArgs[1].substring(0, 1).toUpperCase() + viewArgs[1].substring(1).toLowerCase(); viewArgs[2] = viewArgs[2].substring(0, 1).toUpperCase() + viewArgs[2].substring(1).toLowerCase(); if ("Exam".equals(viewArgs[1])) { @@ -94,9 +92,6 @@ public ViewCommand parse(String args) throws ParseException { if (day == -1 && date == null) { throw new ParseException(String.format(MESSAGE_INVALID_DATE, ViewCommand.MESSAGE_USAGE)); } - if (day != -1) { - date = getNearestDayOfWeek(LocalDate.now(Clock.get()), day); - } return new ViewCommand(viewArgs); } diff --git a/test/java/planmysem/logic/Commands/ViewCommandTest.java b/test/java/planmysem/logic/Commands/ViewCommandTest.java new file mode 100644 index 000000000..a7ee13f80 --- /dev/null +++ b/test/java/planmysem/logic/Commands/ViewCommandTest.java @@ -0,0 +1,228 @@ +package planmysem.logic.Commands; + +import static junit.framework.TestCase.assertEquals; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.Map; +import java.util.TreeMap; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import javafx.util.Pair; +import planmysem.common.Clock; +import planmysem.logic.CommandHistory; +import planmysem.logic.commands.CommandResult; +import planmysem.logic.commands.ViewCommand; +import planmysem.model.Model; +import planmysem.model.ModelManager; +import planmysem.model.semester.Day; +import planmysem.model.semester.ReadOnlyDay; +import planmysem.model.slot.ReadOnlySlot; +import planmysem.testutil.SlotBuilder; + +public class ViewCommandTest { + private Model model; + private Model expectedModel; + private Pair> pair1; + private Pair> pair2; + private Pair> pair3; + private Pair> pair4; + private CommandHistory commandHistory = new CommandHistory(); + + private SlotBuilder slotBuilder = new SlotBuilder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setup() throws Exception { + Clock.set("2019-01-14T10:00:00Z"); + + // Create typical planner + model = new ModelManager(); + pair1 = new Pair<>( + LocalDate.of(2019, 02, 01), + new Pair<>( + new Day( + DayOfWeek.FRIDAY, + "Week 3" + ), + slotBuilder.slotOne() + ) + ); + pair2 = new Pair<>( + LocalDate.of(2019, 02, 02), + new Pair<>( + new Day( + DayOfWeek.SATURDAY, + "Week 3" + ), + slotBuilder.slotOne() + ) + ); + pair3 = new Pair<>( + LocalDate.of(2019, 02, 03), + new Pair<>( + new Day( + DayOfWeek.SUNDAY, + "Week 3" + ), + slotBuilder.slotOne() + ) + ); + pair4 = new Pair<>( + LocalDate.of(2019, 02, 04), + new Pair<>( + new Day( + DayOfWeek.MONDAY, + "Week 4" + ), + slotBuilder.slotOne() + ) + ); + model.addSlot(LocalDate.of(2019, 02, 01), slotBuilder.slotOne()); + model.addSlot(LocalDate.of(2019, 02, 02), slotBuilder.slotOne()); + model.addSlot(LocalDate.of(2019, 02, 02), slotBuilder.slotOne()); + model.addSlot(LocalDate.of(2019, 02, 03), slotBuilder.slotOne()); + model.addSlot(LocalDate.of(2019, 02, 04), slotBuilder.slotOne()); + + Map> list = new TreeMap<>(); + list.put(pair4.getKey(), pair4.getValue()); + list.put(pair3.getKey(), pair3.getValue()); + list.put(pair2.getKey(), pair2.getValue()); + list.put(pair2.getKey(), pair2.getValue()); + list.put(pair1.getKey(), pair1.getValue()); + model.setLastShownList(list); + + expectedModel = new ModelManager(); + expectedModel.addSlot(LocalDate.of(2019, 02, 01), slotBuilder.slotOne()); + expectedModel.addSlot(LocalDate.of(2019, 02, 02), slotBuilder.slotOne()); + expectedModel.addSlot(LocalDate.of(2019, 02, 02), slotBuilder.slotOne()); + expectedModel.addSlot(LocalDate.of(2019, 02, 03), slotBuilder.slotOne()); + expectedModel.addSlot(LocalDate.of(2019, 02, 04), slotBuilder.slotOne()); + expectedModel.setLastShownList(model.getLastShownList()); + } + + @Test + public void execute_displayMonthView_success() { + ViewCommand expectedCommand = new ViewCommand(new String[]{"month"}); + CommandResult expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + ViewCommand actualCommand = new ViewCommand(new String[]{"month"}); + CommandResult actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + } + + @Test + public void execute_displayWeekView_success() { + ViewCommand expectedCommand = new ViewCommand(new String[]{"week"}); + CommandResult expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + ViewCommand actualCommand = new ViewCommand(new String[]{"week"}); + CommandResult actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"week", "Examination"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"week", "Examination"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"week", "3"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"week", "3"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + } + + @Test + public void execute_displayDetailedWeekView_success() { + ViewCommand expectedCommand = new ViewCommand(new String[]{"week", "Details"}); + CommandResult expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + ViewCommand actualCommand = new ViewCommand(new String[]{"week", "Details"}); + CommandResult actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"week", "Examination", "Details"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"week", "Examination", "Details"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"week", "3", "Details"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"week", "3", "Details"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + } + + @Test + public void execute_displayDayView_success() { + ViewCommand expectedCommand = new ViewCommand(new String[]{"day"}); + CommandResult expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + ViewCommand actualCommand = new ViewCommand(new String[]{"day"}); + CommandResult actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"day", "1"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"day", "1"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"day", "02-02"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"day", "02-02"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + } + + @Test + public void execute_displayDayView_failure() { + ViewCommand expectedCommand = new ViewCommand(new String[]{"day", "29-02"}); + CommandResult expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + ViewCommand actualCommand = new ViewCommand(new String[]{"day", "29-02"}); + CommandResult actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"day", "01-01"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"day", "01-01"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + + expectedCommand = new ViewCommand(new String[]{"day", "0"}); + expectedCommandResult = expectedCommand.execute(expectedModel, commandHistory); + + actualCommand = new ViewCommand(new String[]{"day", "0"}); + actualCommandResult = actualCommand.execute(model, commandHistory); + + assertEquals(expectedCommandResult.getFeedbackToUser(), actualCommandResult.getFeedbackToUser()); + } +} diff --git a/test/java/planmysem/logic/parser/AddCommandParserTest.java b/test/java/planmysem/logic/parser/AddCommandParserTest.java index 1b76612db..c0879af69 100644 --- a/test/java/planmysem/logic/parser/AddCommandParserTest.java +++ b/test/java/planmysem/logic/parser/AddCommandParserTest.java @@ -40,8 +40,7 @@ public void parse_minimalFields_success() { LocalTime.of(8, 0), LocalTime.of(9, 0), null - ) - , new Recurrence( + ), new Recurrence( null, 1 ))); diff --git a/test/java/planmysem/logic/parser/ViewCommandParserTest.java b/test/java/planmysem/logic/parser/ViewCommandParserTest.java new file mode 100644 index 000000000..dd1d14f2e --- /dev/null +++ b/test/java/planmysem/logic/parser/ViewCommandParserTest.java @@ -0,0 +1,159 @@ +package planmysem.logic.parser; + +import static planmysem.common.Messages.MESSAGE_ILLEGAL_WEEK_VALUE; +import static planmysem.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static planmysem.common.Messages.MESSAGE_INVALID_DATE; + +import static planmysem.logic.parser.CommandParserTestUtil.assertParseFailure; +import static planmysem.logic.parser.CommandParserTestUtil.assertParseSuccess; + +import org.junit.Test; + +import planmysem.logic.commands.ViewCommand; + +public class ViewCommandParserTest { + private ViewCommandParser parser = new ViewCommandParser(); + + @Test + public void parse_validMonthArgs_success() { + assertParseSuccess(parser, + "month", + new ViewCommand(new String[]{"month"})); + } + + @Test + public void parse_validWeekArgs_success() { + assertParseSuccess(parser, + "week", + new ViewCommand(new String[]{"week"})); + + assertParseSuccess(parser, + "week 1", + new ViewCommand(new String[]{"week", "1"})); + + assertParseSuccess(parser, + "week 13", + new ViewCommand(new String[]{"week", "13"})); + + assertParseSuccess(parser, + "week exam", + new ViewCommand(new String[]{"week", "Examination"})); + + assertParseSuccess(parser, + "week recess", + new ViewCommand(new String[]{"week", "Recess"})); + + assertParseSuccess(parser, + "week reading", + new ViewCommand(new String[]{"week", "Reading"})); + + assertParseSuccess(parser, + "week orientation", + new ViewCommand(new String[]{"week", "Orientation"})); + + assertParseSuccess(parser, + "week details", + new ViewCommand(new String[]{"week", "Details"})); + + assertParseSuccess(parser, + "week exam details", + new ViewCommand(new String[]{"week", "Examination", "Details"})); + + assertParseSuccess(parser, + "week details exam", + new ViewCommand(new String[]{"week", "Examination", "Details"})); + + assertParseSuccess(parser, + "week recess details", + new ViewCommand(new String[]{"week", "Recess", "Details"})); + + assertParseSuccess(parser, + "week reading details", + new ViewCommand(new String[]{"week", "Reading", "Details"})); + + assertParseSuccess(parser, + "week orientation details", + new ViewCommand(new String[]{"week", "Orientation", "Details"})); + } + + @Test + public void parse_validDayArgs_success() { + assertParseSuccess(parser, + "day", + new ViewCommand(new String[]{"day"})); + + assertParseSuccess(parser, + "day 1", + new ViewCommand(new String[]{"day", "1"})); + + assertParseSuccess(parser, + "day 7", + new ViewCommand(new String[]{"day", "7"})); + + assertParseSuccess(parser, + "day mon", + new ViewCommand(new String[]{"day", "mon"})); + + assertParseSuccess(parser, + "day sun", + new ViewCommand(new String[]{"day", "sun"})); + + assertParseSuccess(parser, + "day Monday", + new ViewCommand(new String[]{"day", "Monday"})); + + assertParseSuccess(parser, + "day Sunday", + new ViewCommand(new String[]{"day", "Sunday"})); + } + + @Test + public void parse_invalidArgs_failure() { + assertParseFailure(parser, + "", + String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, + "month 1", + String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, + "test test test test", + String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE)); + } + + @Test + public void parse_invalidWeekArgs_failure() { + assertParseFailure(parser, + "week -1", + String.format(MESSAGE_ILLEGAL_WEEK_VALUE, ViewCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, + "week 14", + String.format(MESSAGE_ILLEGAL_WEEK_VALUE, ViewCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, + "week something", + String.format(MESSAGE_ILLEGAL_WEEK_VALUE, ViewCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, + "week detail detail", + String.format(MESSAGE_ILLEGAL_WEEK_VALUE, ViewCommand.MESSAGE_USAGE)); + } + + @Test + public void parse_invalidDayArgs_failure() { + assertParseFailure(parser, + "day 0", + String.format(MESSAGE_INVALID_DATE, ViewCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, + "day 32", + String.format(MESSAGE_INVALID_DATE, ViewCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, + "day test", + String.format(MESSAGE_INVALID_DATE, ViewCommand.MESSAGE_USAGE)); + } + +}