Skip to content

Commit

Permalink
Add JUnit tests for ViewCommandParser and ViewCommand (CS2113-AY1819S…
Browse files Browse the repository at this point in the history
  • Loading branch information
dingheng4448 authored and seanieyap committed Apr 1, 2019
1 parent 5892bf1 commit 7852a46
Show file tree
Hide file tree
Showing 5 changed files with 397 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/planmysem/logic/commands/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
7 changes: 1 addition & 6 deletions src/planmysem/logic/parser/ViewCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -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);
}
Expand Down
228 changes: 228 additions & 0 deletions test/java/planmysem/logic/Commands/ViewCommandTest.java
Original file line number Diff line number Diff line change
@@ -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<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> pair1;
private Pair<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> pair2;
private Pair<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> pair3;
private Pair<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> 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<LocalDate, Pair<ReadOnlyDay, ReadOnlySlot>> 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());
}
}
3 changes: 1 addition & 2 deletions test/java/planmysem/logic/parser/AddCommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public void parse_minimalFields_success() {
LocalTime.of(8, 0),
LocalTime.of(9, 0),
null
)
, new Recurrence(
), new Recurrence(
null,
1
)));
Expand Down
Loading

0 comments on commit 7852a46

Please sign in to comment.