Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases, fix bugs #95

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ private static Exercise createEditedExercise(Exercise exerciseToEdit,
Description updatedDescription = editExerciseDescriptor.getDescription()
.orElse(exerciseToEdit.getDescription());
Date updatedDate = editExerciseDescriptor.getDate().orElse(exerciseToEdit.getDate());
Calories updatedCalories = editExerciseDescriptor.getCalories()
.orElse(exerciseToEdit.getCalories().orElse(null));
Calories updatedCalories = editExerciseDescriptor.getCalories().orElse(exerciseToEdit.getCalories());
List<Muscle> updatedMusclesWorked = editExerciseDescriptor.getMusclesWorked()
.orElse(exerciseToEdit.getMusclesWorked().orElse(null));
Set<ExerciseTag> updatedTags = editExerciseDescriptor.getTags().orElse(exerciseToEdit.getExerciseTags());
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/seedu/address/model/ExerciseModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public void setGuiSettings(GuiSettings guiSettings) {

@Override
public Path getExerciseBookFilePath() {
//To change later
return userPrefs.getAddressBookFilePath();
return userPrefs.getExerciseBookFilePath();
}

@Override
Expand Down Expand Up @@ -169,7 +168,6 @@ public boolean equals(Object obj) {
}

// state check
@SuppressWarnings("unchecked")
ExerciseModelManager other = (ExerciseModelManager) obj;
return exerciseBook.equals(other.exerciseBook)
&& userPrefs.equals(other.userPrefs)
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/seedu/address/model/exercise/Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,8 @@ public Date getDate() {
return date;
}

public Optional<Calories> getCalories() {
return Optional.ofNullable(calories);
}

public String getCaloriesDescription() {
if (!getCalories().isPresent()) {
return "None";
}

return getCalories().get().toString();
public Calories getCalories() {
return calories;
}

public Optional<List<Muscle>> getMusclesWorked() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void addCaloriesForDay(Exercise newEntry) {
private void addCaloriesForDay(Exercise newEntry, HashMap<String, Integer> currentCaloriesByDay) {
String stringDate = newEntry.getDate().value;

int intCalories = newEntry.getCalories().isPresent() ? Integer.parseInt(newEntry.getCalories().get().value) : 0;
int intCalories = Integer.parseInt(newEntry.getCalories().toString());
if (currentCaloriesByDay.containsKey(stringDate)) {
Integer newCalories = currentCaloriesByDay.get(stringDate) + intCalories;
currentCaloriesByDay.put(stringDate, newCalories);
Expand All @@ -180,7 +180,7 @@ private void addCaloriesForDay(Exercise newEntry, HashMap<String, Integer> curre

private void minusCaloriesForDay(Exercise oldEntry) {
String stringDate = oldEntry.getDate().value;
int intCalories = oldEntry.getCalories().isPresent() ? Integer.parseInt(oldEntry.getCalories().get().value) : 0;
int intCalories = Integer.parseInt(oldEntry.getCalories().toString());
assert caloriesByDay.containsKey(stringDate) : "Input for minusCaloriesForDay() is wrong";
Integer newCalories = caloriesByDay.get(stringDate) - intCalories;
caloriesByDay.put(stringDate, newCalories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public JsonAdaptedExercise(Exercise source) {
name = source.getName().fullName;
description = source.getDescription().value;
date = source.getDate().value;
calories = source.getCalories().isPresent() ? source.getCalories().get().value : "None";
calories = source.getCalories().toString();
musclesWorked = source.getMusclesWorkedDescription();
tagged.addAll(source.getExerciseTags().stream()
.map(JsonAdaptedExerciseTag::new)
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/seedu/address/storage/JsonUserPrefsStorage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.address.storage;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
Expand All @@ -17,10 +16,6 @@ public class JsonUserPrefsStorage implements UserPrefsStorage {

private Path filePath;

public JsonUserPrefsStorage() {
this.filePath = new File("preferences.json").toPath();
}

public JsonUserPrefsStorage(Path filePath) {
this.filePath = filePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class StorageManagerForExercise implements StorageForExercise {
/**
* Creates a {@code StorageManager} with the given {@code AddressBookStorage} and {@code UserPrefStorage}.
*/
public StorageManagerForExercise(ExerciseBookStorage addressBookStorage, UserPrefsStorage userPrefsStorage) {
public StorageManagerForExercise(ExerciseBookStorage exerciseBookStorage, UserPrefsStorage userPrefsStorage) {
super();
this.exerciseBookStorage = addressBookStorage;
this.exerciseBookStorage = exerciseBookStorage;
this.userPrefsStorage = userPrefsStorage;
}

Expand Down Expand Up @@ -71,9 +71,9 @@ public void saveExerciseBook(ReadOnlyExerciseBook exerciseBook) throws IOExcepti
}

@Override
public void saveExerciseBook(ReadOnlyExerciseBook addressBook, Path filePath) throws IOException {
public void saveExerciseBook(ReadOnlyExerciseBook exerciseBook, Path filePath) throws IOException {
logger.fine("Attempting to write to data file: " + filePath);
exerciseBookStorage.saveExerciseBook(addressBook, filePath);
exerciseBookStorage.saveExerciseBook(exerciseBook, filePath);
}

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/ui/ExerciseCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ExerciseCard(Exercise exercise, int displayedIndex) {
name.setText(exercise.getName().fullName);
date.setText(exercise.getDate().value);
description.setText(exercise.getDescription().value);
calories.setText(exercise.getCalories().isPresent() ? exercise.getCalories().get().value : "None");
calories.setText(exercise.getCalories().toString());
muscles.setText(exercise.getMusclesWorkedDescription() == null
? "None" : exercise.getMusclesWorkedDescription());
exercise.getExerciseTags().stream()
Expand Down
145 changes: 72 additions & 73 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
package seedu.address.logic;


import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_EXERCISE_DISPLAYED_INDEX;
import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;
//import static seedu.address.logic.commands.CommandTestUtil.CALORIES_DESC_PUSH_UP;
//import static seedu.address.logic.commands.CommandTestUtil.DATE_DESC_PUSH_UP;
//import static seedu.address.logic.commands.CommandTestUtil.DESCRIPTION_DESC_PUSH_UP;
//import static seedu.address.logic.commands.CommandTestUtil.MUSCLES_DESC_PUSH_UP;
//import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_PUSH_UP;
import static seedu.address.testutil.Assert.assertThrows;
//import static seedu.address.testutil.TypicalExercise.PUSH_UP;

import java.io.IOException;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

//import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ExerciseModel;
import seedu.address.model.ExerciseModelManager;
import seedu.address.model.ReadOnlyExerciseBook;
import seedu.address.model.UserPrefs;
//import seedu.address.model.exercise.Exercise;
import seedu.address.storage.JsonExerciseBookStorage;
import seedu.address.storage.JsonUserPrefsStorage;
import seedu.address.storage.StorageManagerForExercise;
//import seedu.address.testutil.ExerciseBuilder;

public class LogicManagerTest {
/*private static final IOException DUMMY_IO_EXCEPTION = new IOException("dummy exception");
private static final IOException DUMMY_IO_EXCEPTION = new IOException("dummy exception");

@TempDir
public Path temporaryFolder;
Expand All @@ -13,7 +46,7 @@ public class LogicManagerTest {
@BeforeEach
public void setUp() {
JsonExerciseBookStorage exerciseBookStorage =
new JsonExerciseBookStorage(temporaryFolder.resolve("addressBook.json"));
new JsonExerciseBookStorage(temporaryFolder.resolve("exerciseBook.json"));
JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.resolve("userPrefs.json"));
StorageManagerForExercise storage = new StorageManagerForExercise(exerciseBookStorage, userPrefsStorage);
logic = new LogicManagerForExercise(model, storage);
Expand All @@ -25,72 +58,41 @@ public void execute_invalidCommandFormat_throwsParseException() {
assertParseException(invalidCommand, MESSAGE_UNKNOWN_COMMAND);
}

// @Test
// public void execute_commandExecutionError_throwsCommandException() {
// String deleteCommand = "delete 9";
// assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
// }

// @Test
// public void execute_validCommand_success() throws Exception {
// String listCommand = ListCommand.COMMAND_WORD;
// assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS, model);
// }

// @Test
// public void execute_storageThrowsIoException_throwsCommandException() {
// // Setup LogicManager with JsonAddressBookIoExceptionThrowingStub
// JsonExerciseBookStorage exerciseBookStorage =
// new JsonExerciseBookIoExceptionThrowingStub(temporaryFolder.resolve("ioExceptionAddressBook.json"));
// JsonUserPrefsStorage userPrefsStorage =
// new JsonUserPrefsStorage(temporaryFolder.resolve("ioExceptionUserPrefs.json"));
// StorageManagerForExercise storage = new StorageManagerForExercise(exerciseBookStorage, userPrefsStorage);
// logic = new LogicManagerForExercise(model, storage);
//
// // Execute add command
// String addCommand = AddCommand.COMMAND_WORD + NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY
// + ADDRESS_DESC_AMY;
// Person expectedPerson = new PersonBuilder(AMY).withTags().build();
// ModelManager expectedModel = new ModelManager();
// expectedModel.addPerson(expectedPerson);
// String expectedMessage = LogicManager.FILE_OPS_ERROR_MESSAGE + DUMMY_IO_EXCEPTION;
// assertCommandFailure(addCommand, CommandException.class, expectedMessage, expectedModel);
// }


// original is person
@Test
<<<<<<< HEAD
public void execute_commandExecutionError_throwsCommandException() {
String deleteCommand = "delete 9";
assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandException(deleteCommand, MESSAGE_INVALID_EXERCISE_DISPLAYED_INDEX);
}


@Test
public void execute_storageThrowsIoException_throwsCommandException() {
// Setup LogicManager with JsonAddressBookIoExceptionThrowingStub
JsonAddressBookStorage addressBookStorage =
new JsonAddressBookIoExceptionThrowingStub(temporaryFolder.resolve("ioExceptionAddressBook.json"));
JsonUserPrefsStorage userPrefsStorage =
new JsonUserPrefsStorage(temporaryFolder.resolve("ioExceptionUserPrefs.json"));
StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage);
logic = new LogicManager(model, storage);

// Execute add command
String addCommand = AddCommand.COMMAND_WORD + NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY
+ ADDRESS_DESC_AMY;
Person expectedPerson = new PersonBuilder(AMY).withTags().build();
ModelManager expectedModel = new ModelManager();
expectedModel.addPerson(expectedPerson);
String expectedMessage = LogicManager.FILE_OPS_ERROR_MESSAGE + DUMMY_IO_EXCEPTION;
assertCommandFailure(addCommand, CommandException.class, expectedMessage, expectedModel);
public void execute_validCommand_success() throws Exception {
String listCommand = ListCommand.COMMAND_WORD;
assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS, model);
}

@Test
public void getFilteredPersonList_modifyList_throwsUnsupportedOperationException() {
assertThrows(UnsupportedOperationException.class, () -> logic.getFilteredPersonList().remove(0));
// @Test
// public void execute_storageThrowsIoException_throwsCommandException() {
// // Setup LogicManager with JsonAddressBookIoExceptionThrowingStub
// JsonExerciseBookStorage exerciseBookStorage =
// new JsonExerciseBookIoExceptionThrowingStub(
// temporaryFolder.resolve("ioExceptionExerciseBook.json"));
// JsonUserPrefsStorage userPrefsStorage =
// new JsonUserPrefsStorage(temporaryFolder.resolve("ioExceptionUserPrefs.json"));
// StorageManagerForExercise storage = new StorageManagerForExercise(exerciseBookStorage, userPrefsStorage);
// logic = new LogicManagerForExercise(model, storage);
//
// // Execute add command
// String addCommand = AddCommand.COMMAND_WORD
// + NAME_DESC_PUSH_UP + DESCRIPTION_DESC_PUSH_UP + DATE_DESC_PUSH_UP
// + CALORIES_DESC_PUSH_UP + MUSCLES_DESC_PUSH_UP;
// Exercise expectedExercise = new ExerciseBuilder(PUSH_UP).withTags().build();
// ExerciseModelManager expectedModel = new ExerciseModelManager();
// expectedModel.addExercise(expectedExercise);
// String expectedMessage = LogicManagerForExercise.FILE_OPS_ERROR_MESSAGE + DUMMY_IO_EXCEPTION;
// assertCommandFailure(addCommand, CommandException.class, expectedMessage, expectedModel);
// }

@Test
public void getFilteredExerciseList_modifyList_throwsUnsupportedOperationException() {
assertThrows(UnsupportedOperationException.class, () -> logic.getFilteredExerciseList().remove(0));
}
Expand All @@ -102,8 +104,8 @@ public void getFilteredExerciseList_modifyList_throwsUnsupportedOperationExcepti
* - the internal model manager state is the same as that in {@code expectedModel} <br>
* @see #assertCommandFailure(String, Class, String, ExerciseModel)
*/
/*private void assertCommandSuccess(String inputCommand, String expectedMessage,
Model expectedModel) throws CommandException, ParseException {
private void assertCommandSuccess(String inputCommand, String expectedMessage,
ExerciseModel expectedModel) throws CommandException, ParseException, IOException {
CommandResult result = logic.execute(inputCommand);
assertEquals(expectedMessage, result.getFeedbackToUser());
assertEquals(expectedModel, model);
Expand All @@ -113,23 +115,23 @@ public void getFilteredExerciseList_modifyList_throwsUnsupportedOperationExcepti
* Executes the command, confirms that a ParseException is thrown and that the result message is correct.
* @see #assertCommandFailure(String, Class, String, ExerciseModel)
*/
/*private void assertParseException(String inputCommand, String expectedMessage) {
private void assertParseException(String inputCommand, String expectedMessage) {
assertCommandFailure(inputCommand, ParseException.class, expectedMessage);
}

/**
* Executes the command, confirms that a CommandException is thrown and that the result message is correct.
* @see #assertCommandFailure(String, Class, String, ExerciseModel)
*/
/*private void assertCommandException(String inputCommand, String expectedMessage) {
private void assertCommandException(String inputCommand, String expectedMessage) {
assertCommandFailure(inputCommand, CommandException.class, expectedMessage);
}

/**
* Executes the command, confirms that the exception is thrown and that the result message is correct.
* @see #assertCommandFailure(String, Class, String, ExerciseModel)
*/
/*private void assertCommandFailure(String inputCommand, Class<? extends Throwable> expectedException,
private void assertCommandFailure(String inputCommand, Class<? extends Throwable> expectedException,
String expectedMessage) {
ExerciseModel expectedModel = new ExerciseModelManager(model.getExerciseBook(), new UserPrefs());
assertCommandFailure(inputCommand, expectedException, expectedMessage, expectedModel);
Expand All @@ -142,27 +144,24 @@ public void getFilteredExerciseList_modifyList_throwsUnsupportedOperationExcepti
* - the internal model manager state is the same as that in {@code expectedModel} <br>
* @see #assertCommandSuccess(String, String, ExerciseModel)
*/



/*private void assertCommandFailure(String inputCommand, Class<? extends Throwable> expectedException,
String expectedMessage, Model expectedModel) {
private void assertCommandFailure(String inputCommand, Class<? extends Throwable> expectedException,
String expectedMessage, ExerciseModel expectedModel) {
assertThrows(expectedException, expectedMessage, () -> logic.execute(inputCommand));
assertEquals(expectedModel, model);
}

/**
* A stub class to throw an {@code IOException} when the save method is called.
*/
/*private static class JsonAddressBookIoExceptionThrowingStub extends JsonAddressBookStorage {
private JsonAddressBookIoExceptionThrowingStub(Path filePath) {
private static class JsonExerciseBookIoExceptionThrowingStub extends JsonExerciseBookStorage {
private JsonExerciseBookIoExceptionThrowingStub(Path filePath) {
super(filePath);
}

@Override
public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) throws IOException {
public void saveExerciseBook(ReadOnlyExerciseBook addressBook, Path filePath) throws IOException {
throw DUMMY_IO_EXCEPTION;
}
}
}*/
}

Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public void addExercise(Exercise exercise) {

private void addCaloriesForDay(Exercise newEntry) {
String stringDate = newEntry.getDate().value;
int intCalories = newEntry.getCalories().isPresent()
? Integer.parseInt(newEntry.getCalories().get().value) : 0;
int intCalories = Integer.parseInt(newEntry.getCalories().toString());
if (caloriesByDay.containsKey(stringDate)) {
Integer newCalories = caloriesByDay.get(stringDate) + intCalories;
caloriesByDay.put(stringDate, newCalories);
Expand Down