Skip to content

Commit

Permalink
Merge pull request #148 from CS2103AUG2016-F11-C1/add-update-commandt…
Browse files Browse the repository at this point in the history
…ests

Add UpdateCommandTest cases, extract out Assert methods
  • Loading branch information
louietyj committed Nov 5, 2016
2 parents ea14cc6 + 7376872 commit 747e5d3
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 101 deletions.
59 changes: 2 additions & 57 deletions src/test/java/seedu/todo/guitests/AddEventCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package seedu.todo.guitests;

import static org.junit.Assert.*;
import static seedu.todo.testutil.AssertUtil.assertSameDate;

import java.time.LocalDate;
import java.time.LocalDateTime;

import org.junit.Test;

import seedu.todo.commons.util.DateUtil;
import seedu.todo.guitests.guihandles.TaskListDateItemHandle;
import seedu.todo.guitests.guihandles.TaskListEventItemHandle;
import seedu.todo.models.Event;

// @@author A0139812A
Expand All @@ -28,7 +24,7 @@ public void addEvent_eventSameDateInFuture_isVisible() {
event.setName("Presentation in the Future");
event.setStartDate(DateUtil.parseDateTime(String.format("%s 14:00:00", twoDaysFromNowIsoString)));
event.setEndDate(DateUtil.parseDateTime(String.format("%s 19:00:00", twoDaysFromNowIsoString)));
assertAddSuccess(command, event);
assertEventVisibleAfterCmd(command, event);
}

@Test
Expand All @@ -46,7 +42,7 @@ public void addEvent_eventSameDateInPast_isNotVisible() {
event.setName("Presentation in the Past");
event.setStartDate(DateUtil.parseDateTime(String.format("%s 14:00:00", twoDaysBeforeNowIsoString)));
event.setEndDate(DateUtil.parseDateTime(String.format("%s 19:00:00", twoDaysBeforeNowIsoString)));
assertAddNotVisible(command, event);
assertEventNotVisibleAfterCmd(command, event);
}

@Test
Expand Down Expand Up @@ -96,55 +92,4 @@ public void addEvent_unmatchedQuotes_commandRemains() {
assertEquals(console.getConsoleInputText(), command);
}

/**
* Utility method for testing if event has been successfully added to the GUI.
* This runs a command and checks if TaskList contains TaskListEventItem that matches
* the task that was just added. <br><br>
*
* TODO: Extract out method in AddController that can return task from command,
* and possibly remove the need to have eventToAdd.
*/
private void assertAddSuccess(String command, Event eventToAdd) {
// Run the command in the console.
console.runCommand(command);

// Get the event date.
LocalDateTime eventStartDateTime = eventToAdd.getStartDate();
if (eventStartDateTime == null) {
eventStartDateTime = DateUtil.NO_DATETIME_VALUE;
}
LocalDate eventStartDate = eventStartDateTime.toLocalDate();

// Check TaskList if it contains a TaskListDateItem with the date of the event start date.
TaskListDateItemHandle dateItem = taskList.getTaskListDateItem(eventStartDate);
assertSameDate(eventStartDate, dateItem);

// Check TaskListDateItem if it contains the TaskListEventItem with the same data.
TaskListEventItemHandle eventItem = dateItem.getTaskListEventItem(eventToAdd.getName());
assertEquals(eventItem.getName(), eventToAdd.getName());
}

private void assertAddNotVisible(String command, Event eventToAdd) {
// Run the command in the console.
console.runCommand(command);

// Get the event date.
LocalDateTime eventStartDateTime = eventToAdd.getStartDate();
if (eventStartDateTime == null) {
eventStartDateTime = DateUtil.NO_DATETIME_VALUE;
}
LocalDate eventStartDate = eventStartDateTime.toLocalDate();

// Gets the date item that might contain the event
TaskListDateItemHandle dateItem = taskList.getTaskListDateItem(eventStartDate);

// It's fine if there's not date item, because it's not visible.
if (dateItem == null) {
return;
}

TaskListEventItemHandle eventItem = dateItem.getTaskListEventItem(eventToAdd.getName());
assertNull(eventItem);
}

}
40 changes: 2 additions & 38 deletions src/test/java/seedu/todo/guitests/AddTaskCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@
import org.junit.Test;

import seedu.todo.commons.util.DateUtil;
import seedu.todo.guitests.guihandles.TaskListDateItemHandle;
import seedu.todo.guitests.guihandles.TaskListTaskItemHandle;
import seedu.todo.models.Task;

import static seedu.todo.testutil.AssertUtil.assertSameDate;
import static org.junit.Assert.assertEquals;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* @@author A0139812A
*/
Expand All @@ -24,42 +16,14 @@ public void addTaskWithDeadline() {
Task task = new Task();
task.setName("Buy milk");
task.setCalendarDateTime(DateUtil.parseDateTime("2016-10-15 14:00:00"));
assertAddSuccess(command, task);
assertTaskVisibleAfterCmd(command, task);
}

@Test
public void addFloatingTask() {
String command = "add task Buy milk";
Task task = new Task();
task.setName("Buy milk");
assertAddSuccess(command, task);
}

/**
* Utility method for testing if task has been successfully added to the GUI.
* This runs a command and checks if TaskList contains TaskListTaskItem that matches
* the task that was just added. <br><br>
*
* TODO: Extract out method in AddController that can return task from command,
* and possibly remove the need to have taskToAdd.
*/
private void assertAddSuccess(String command, Task taskToAdd) {
// Run the command in the console.
console.runCommand(command);

// Get the task date.
LocalDateTime taskDateTime = taskToAdd.getCalendarDateTime();
if (taskDateTime == null) {
taskDateTime = DateUtil.NO_DATETIME_VALUE;
}
LocalDate taskDate = taskDateTime.toLocalDate();

// Check TaskList if it contains a TaskListDateItem with the date.
TaskListDateItemHandle dateItem = taskList.getTaskListDateItem(taskDate);
assertSameDate(taskDate, dateItem);

// Check TaskListDateItem if it contains the TaskListTaskItem with the same data.
TaskListTaskItemHandle taskItem = dateItem.getTaskListTaskItem(taskToAdd.getName());
assertEquals(taskItem.getName(), taskToAdd.getName());
assertTaskVisibleAfterCmd(command, task);
}
}
97 changes: 97 additions & 0 deletions src/test/java/seedu/todo/guitests/GuiTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package seedu.todo.guitests;

import static org.junit.Assert.*;
import static seedu.todo.testutil.AssertUtil.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.TimeoutException;

import org.junit.After;
Expand All @@ -14,9 +19,15 @@
import seedu.todo.TestApp;
import seedu.todo.commons.core.EventsCenter;
import seedu.todo.commons.events.BaseEvent;
import seedu.todo.commons.util.DateUtil;
import seedu.todo.guitests.guihandles.ConsoleHandle;
import seedu.todo.guitests.guihandles.MainGuiHandle;
import seedu.todo.guitests.guihandles.TaskListDateItemHandle;
import seedu.todo.guitests.guihandles.TaskListEventItemHandle;
import seedu.todo.guitests.guihandles.TaskListHandle;
import seedu.todo.guitests.guihandles.TaskListTaskItemHandle;
import seedu.todo.models.Event;
import seedu.todo.models.Task;
import seedu.todo.models.TodoListDB;

/**
Expand Down Expand Up @@ -88,4 +99,90 @@ public void raise(BaseEvent e) {
//JUnit doesn't run its test cases on the UI thread. Platform.runLater is used to post event on the UI thread.
Platform.runLater(() -> EventsCenter.getInstance().post(e));
}

/* ========= COMMON TEST METHODS ============= */

/**
* Utility method for testing if task has been successfully added to the GUI.
* This runs a command and checks if TaskList contains TaskListTaskItem that matches
* the task that was just added.
*
* Assumption: No two events can have the same name in this test.
*/
protected void assertTaskVisibleAfterCmd(String command, Task taskToAdd) {
// Run the command in the console.
console.runCommand(command);

// Get the task date.
LocalDateTime taskDateTime = taskToAdd.getCalendarDateTime();
if (taskDateTime == null) {
taskDateTime = DateUtil.NO_DATETIME_VALUE;
}
LocalDate taskDate = taskDateTime.toLocalDate();

// Check TaskList if it contains a TaskListDateItem with the date.
TaskListDateItemHandle dateItem = taskList.getTaskListDateItem(taskDate);
assertSameDate(taskDate, dateItem);

// Check TaskListDateItem if it contains the TaskListTaskItem with the same data.
TaskListTaskItemHandle taskItem = dateItem.getTaskListTaskItem(taskToAdd.getName());
assertSameTaskName(taskToAdd, taskItem);
}

/**
* Utility method for testing if event has been successfully added to the GUI.
* This runs a command and checks if TaskList contains TaskListEventItem that matches
* the task that was just added.
*
* Assumption: No two events can have the same name in this test.
*
* TODO: Check event dates if they match.
*/
protected void assertEventVisibleAfterCmd(String command, Event eventToAdd) {
// Run the command in the console.
console.runCommand(command);

// Get the event date.
LocalDateTime eventStartDateTime = eventToAdd.getStartDate();
if (eventStartDateTime == null) {
eventStartDateTime = DateUtil.NO_DATETIME_VALUE;
}
LocalDate eventStartDate = eventStartDateTime.toLocalDate();

// Check TaskList if it contains a TaskListDateItem with the date of the event start date.
TaskListDateItemHandle dateItem = taskList.getTaskListDateItem(eventStartDate);
assertSameDate(eventStartDate, dateItem);

// Check TaskListDateItem if it contains the TaskListEventItem with the same data.
TaskListEventItemHandle eventItem = dateItem.getTaskListEventItem(eventToAdd.getName());
assertSameEventName(eventToAdd, eventItem);
}

/**
* Utility method for testing if event does not appear in the GUI after a command.
* Assumption: No two events can have the same name in this test.
*/
protected void assertEventNotVisibleAfterCmd(String command, Event eventToAdd) {
// Run the command in the console.
console.runCommand(command);

// Get the event date.
LocalDateTime eventStartDateTime = eventToAdd.getStartDate();
if (eventStartDateTime == null) {
eventStartDateTime = DateUtil.NO_DATETIME_VALUE;
}
LocalDate eventStartDate = eventStartDateTime.toLocalDate();

// Gets the date item that might contain the event
TaskListDateItemHandle dateItem = taskList.getTaskListDateItem(eventStartDate);

// It's fine if there's no date item, because it's not visible.
if (dateItem == null) {
return;
}

// If there's a date item, then we make sure that there isn't an event in the date item with the same name.
TaskListEventItemHandle eventItem = dateItem.getTaskListEventItem(eventToAdd.getName());
assertNull(eventItem);
}
}

0 comments on commit 747e5d3

Please sign in to comment.