Skip to content

Commit

Permalink
Fix parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jxunze committed Mar 17, 2024
1 parent a50fd4b commit 58913e2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_PERSON_NOT_FOUND = "The person does not exist in the address book";
public static final String MESSAGE_DUPLICATE_FIELDS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean equals(Object other) {
@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetPhone)
.add("targetPhone", targetPhone.toString())
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public CommandResult execute(Model model) throws CommandException {
}
}
if (!exists) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_PERSON_NOT_FOUND);
}

Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);
Expand Down
6 changes: 1 addition & 5 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package seedu.address.logic;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.logic.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.commands.CommandTestUtil.ADDRESS_DESC_AMY;
//import static seedu.address.logic.commands.CommandTestUtil.EMAIL_DESC_AMY;
//import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.BANKDETAILS_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.EMPLOYMENTTYPE_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.FIRSTNAME_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.LASTNAME_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.PHONE_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.SEX_DESC_AMY;
//import static seedu.address.logic.commands.CommandTestUtil.TAG_DESC_FRIEND;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.AMY;

Expand Down Expand Up @@ -67,7 +63,7 @@ public void execute_invalidCommandFormat_throwsParseException() {
@Test
public void execute_commandExecutionError_throwsCommandException() {
String deleteCommand = "delete 9";
assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandException(deleteCommand, Messages.MESSAGE_PERSON_NOT_FOUND);
}

@Test
Expand Down
43 changes: 20 additions & 23 deletions src/test/java/seedu/address/logic/commands/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;

/**
* Contains integration tests (interaction with the Model) and unit tests for
Expand All @@ -28,9 +29,10 @@ public class DeleteCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validIndexUnfilteredList_success() {
public void execute_validPhoneUnfilteredList_success() {
Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON);
Phone phoneToDelete = personToDelete.getPhone();
DeleteCommand deleteCommand = new DeleteCommand(phoneToDelete);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
Messages.format(personToDelete));
Expand All @@ -42,19 +44,18 @@ public void execute_validIndexUnfilteredList_success() {
}

@Test
public void execute_invalidIndexUnfilteredList_throwsCommandException() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1);
DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex);
public void execute_invalidPhoneUnfilteredList_throwsCommandException() {
DeleteCommand deleteCommand = new DeleteCommand(new Phone("12345678"));

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandFailure(deleteCommand, model, Messages.MESSAGE_PERSON_NOT_FOUND);
}

@Test
public void execute_validIndexFilteredList_success() {
public void execute_validPhoneFilteredList_success() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteCommand deleteCommand = new DeleteCommand(personToDelete.getPhone());

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
Messages.format(personToDelete));
Expand All @@ -67,28 +68,24 @@ public void execute_validIndexFilteredList_success() {
}

@Test
public void execute_invalidIndexFilteredList_throwsCommandException() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);

Index outOfBoundIndex = INDEX_SECOND_PERSON;
// ensures that outOfBoundIndex is still in bounds of address book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size());

DeleteCommand deleteCommand = new DeleteCommand(outOfBoundIndex);
public void execute_invalidPhoneFilteredList_throwsCommandException() {
DeleteCommand deleteCommand = new DeleteCommand(new Phone("12345678"));

assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandFailure(deleteCommand, model, Messages.MESSAGE_PERSON_NOT_FOUND);
}

@Test
public void equals() {
DeleteCommand deleteFirstCommand = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteCommand deleteSecondCommand = new DeleteCommand(INDEX_SECOND_PERSON);
Phone firstPhone = new Phone("12345678");
Phone secondPhone = new Phone("87654321");
DeleteCommand deleteFirstCommand = new DeleteCommand(firstPhone);
DeleteCommand deleteSecondCommand = new DeleteCommand(secondPhone);

// same object -> returns true
assertTrue(deleteFirstCommand.equals(deleteFirstCommand));

// same values -> returns true
DeleteCommand deleteFirstCommandCopy = new DeleteCommand(INDEX_FIRST_PERSON);
DeleteCommand deleteFirstCommandCopy = new DeleteCommand(firstPhone);
assertTrue(deleteFirstCommand.equals(deleteFirstCommandCopy));

// different types -> returns false
Expand All @@ -103,9 +100,9 @@ public void equals() {

@Test
public void toStringMethod() {
Index targetIndex = Index.fromOneBased(1);
DeleteCommand deleteCommand = new DeleteCommand(targetIndex);
String expected = DeleteCommand.class.getCanonicalName() + "{targetIndex=" + targetIndex + "}";
Phone phone = new Phone("12345678");
DeleteCommand deleteCommand = new DeleteCommand(phone);
String expected = DeleteCommand.class.getCanonicalName() + "{targetPhone=" + phone.toString() + "}";
assertEquals(expected, deleteCommand.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public void parseCommand_clear() throws Exception {
@Test
public void parseCommand_delete() throws Exception {
DeleteCommand command = (DeleteCommand) parser.parseCommand(
DeleteCommand.COMMAND_WORD + " " + INDEX_FIRST_PERSON.getOneBased());
assertEquals(new DeleteCommand(INDEX_FIRST_PERSON), command);
DeleteCommand.COMMAND_WORD + " " + PersonBuilder.DEFAULT_PHONE);
assertEquals(new DeleteCommand(new Phone(PersonBuilder.DEFAULT_PHONE)), command);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.DeleteCommand;
import seedu.address.model.person.Phone;

/**
* As we are only doing white-box testing, our test cases do not cover path variations
Expand All @@ -22,11 +24,11 @@ public class DeleteCommandParserTest {

@Test
public void parse_validArgs_returnsDeleteCommand() {
assertParseSuccess(parser, "1", new DeleteCommand(INDEX_FIRST_PERSON));
assertParseSuccess(parser, VALID_PHONE_AMY, new DeleteCommand(new Phone(VALID_PHONE_AMY)));
}

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
assertParseFailure(parser, "abc", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
}

0 comments on commit 58913e2

Please sign in to comment.