Skip to content

Commit

Permalink
Edit errors and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SwaminathanViswa committed Nov 5, 2024
1 parent c057210 commit 4391383
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Messages {
public static final String MESSAGE_MARK_ALREADY_SUCCESS = "Attendance is already marked for this student!";
public static final String MESSAGE_UNMARK_ALREADY_SUCCESS = "Attendance is already unmarked for this student";
public static final String MESSAGE_UNMARK_SUCCESS = "%1$s is marked as absent for week %2$d";
public static final String MESSAGE_INVALID_ASSIGNMENT_NAME = "Invalid assignment name: %1$s";


/**
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/seedu/address/logic/commands/RemoveGradeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_ASSIGNMENT_NAME;
import static seedu.address.logic.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ASSIGNMENT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.ParserUtil.parseName;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.assignment.Assignment;
import seedu.address.model.person.Email;
Expand All @@ -32,27 +36,27 @@ public class RemoveGradeCommand extends Command {
+ PREFIX_NAME
+ "NAME "
+ PREFIX_ASSIGNMENT
+ "ASSIGNMENT "
+ "ASSIGNMENT\n"
+ "Example: "
+ COMMAND_WORD
+ " "
+ PREFIX_NAME
+ "John Doe "
+ PREFIX_ASSIGNMENT
+ "Ex09 ";
+ "Ex09";

public static final String MESSAGE_SUCCESS = "Assignment %1$s removed from %2$s";
public static final String MESSAGE_FAILURE = "Assignment %s already removed for %s.";
public static final String MESSAGE_FAILURE = "Assignment %s does not exist for %s.";
private final Name personName;
private final String assignmentName;

/**
* @param personName Name of the person.
* @param assignmentName Name of assignment.
*/
public RemoveGradeCommand(String personName, String assignmentName) {
public RemoveGradeCommand(String personName, String assignmentName) throws ParseException {
requireAllNonNull(personName, assignmentName);
this.personName = new Name(personName);
this.personName = parseName(personName);
this.assignmentName = assignmentName;
}

Expand All @@ -79,12 +83,12 @@ public CommandResult execute(Model model) throws CommandException {

// check if assignment is in predefined list
if (!model.hasAssignment(assignmentName)) {
throw new CommandException("Invalid assignment name: " + assignmentName);
throw new CommandException(String.format(MESSAGE_INVALID_ASSIGNMENT_NAME, assignmentName));
}

Person person = model.getPerson(personName)
.orElseThrow(() ->
new CommandException("Person " + personName + " not in address book"));
new CommandException(MESSAGE_INVALID_PERSON_DISPLAYED_NAME));

// Check if the assignment is already missing from the person's record
if (!person.getAssignment().containsKey(assignmentName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ASSIGNMENT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GITHUB;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PATH;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SCORE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SORTORDER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TELEGRAM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_WEEK;

import java.util.stream.Stream;

Expand All @@ -32,12 +23,8 @@ public RemoveGradeCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_ASSIGNMENT);

// The third OR condition is to check if there are any other PREFIXES present when
// it is not supposed to be.
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ASSIGNMENT)
|| !argMultimap.getPreamble().isEmpty()
|| arePrefixesPresent(argMultimap, PREFIX_TELEGRAM, PREFIX_EMAIL, PREFIX_PHONE,
PREFIX_GITHUB, PREFIX_SCORE, PREFIX_TAG, PREFIX_WEEK, PREFIX_PATH, PREFIX_SORTORDER)) {
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveGradeCommand.MESSAGE_USAGE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
Expand All @@ -30,19 +31,19 @@ public void constructor_nullAssignmentFormat_throwsNullPointerException() {
}

@Test
public void assignment_invalidName() {
public void assignment_invalidName() throws ParseException {
RemoveGradeCommand command = new RemoveGradeCommand("John Doe", "ex10");
assertThrows(CommandException.class, () -> command.execute(model));
}

@Test
public void person_invalidName() {
public void person_invalidName() throws ParseException {
RemoveGradeCommand command = new RemoveGradeCommand("John DoeDoedoe", "ex01");
assertThrows(CommandException.class, () -> command.execute(model));
}

@Test
public void constructor_validRemoveGradeCommandFormat_success() {
public void constructor_validRemoveGradeCommandFormat_success() throws ParseException {
RemoveGradeCommand command = new RemoveGradeCommand("John Doe", "Ex09");
assertNotNull(command);
}
Expand All @@ -65,13 +66,14 @@ public void createPersonWithRemovedGrade_success() {
}

@Test
public void execute_assignmentDoesNotExist_throwsCommandException() {
RemoveGradeCommand command = new RemoveGradeCommand("Alice Pauline", "NonExistentAssignment");
public void execute_assignmentDoesNotExist_throwsCommandException() throws ParseException {
RemoveGradeCommand command =
new RemoveGradeCommand("Alice Pauline", "NonExistentAssignment");
assertThrows(CommandException.class, () -> command.execute(model));
}

@Test
public void execute_personDoesNotExist_throwsCommandException() {
public void execute_personDoesNotExist_throwsCommandException() throws ParseException {
RemoveGradeCommand command = new RemoveGradeCommand("NonExistentPerson", "Ex01");
assertThrows(CommandException.class, () -> command.execute(model));
}
Expand All @@ -91,25 +93,31 @@ public void execute_assignmentPresent_success() throws Exception {

}
@Test
public void execute_assignmentAlreadyAbsent_throwsCommandException() {
public void execute_assignmentAlreadyAbsent_throwsCommandException() throws ParseException {
RemoveGradeCommand command = new RemoveGradeCommand("Alice Pauline", "Ex01");

assertThrows(CommandException.class, () -> command.execute(model));
}

@Test
public void execute_toString() {
public void execute_toString() throws ParseException {
RemoveGradeCommand c = new RemoveGradeCommand("Alice Pauline", "Ex01");

String expectedOutput = "Alice Pauline Ex01";
assertEquals(expectedOutput, c.toString());
}

@Test
public void equals_differentCommands_returnsFalse() {
public void equals_differentCommands_returnsFalse() throws ParseException {
RemoveGradeCommand command1 = new RemoveGradeCommand("Alice Pauline", "Ex01");
RemoveGradeCommand command2 = new RemoveGradeCommand("Alice Pauline", "Ex02");

assertFalse(command1.equals(command2));
}

@Test
public void equals_differentObjectType_returnsFalse() throws ParseException {
RemoveGradeCommand command = new RemoveGradeCommand("Alice Pauline", "Ex01");
assertFalse(command.equals("Not a RemoveGradeCommand"));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package seedu.address.logic.parser;


import static org.junit.jupiter.api.Assertions.assertThrows;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.ASSIGNMENT_DESC_ONE;
import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_ASSIGNMENT_ONE;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY;
import static seedu.address.logic.commands.RemoveGradeCommand.MESSAGE_USAGE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ASSIGNMENT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
Expand All @@ -14,12 +16,13 @@
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.RemoveGradeCommand;
import seedu.address.logic.parser.exceptions.ParseException;

public class RemoveGradeCommandParserTest {
private final RemoveGradeCommandParser parser = new RemoveGradeCommandParser();

@Test
public void parse_allFieldsSpecified_success() {
public void parse_allFieldsSpecified_success() throws ParseException {
String userInput = NAME_DESC_AMY + ASSIGNMENT_DESC_ONE;
RemoveGradeCommand expectedCommand = new RemoveGradeCommand(
VALID_NAME_AMY,
Expand All @@ -30,7 +33,7 @@ public void parse_allFieldsSpecified_success() {
@Test
public void parse_notAllFieldSpecified_error() {
String userInput = NAME_DESC_AMY;
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveGradeCommand.MESSAGE_USAGE);
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE);
assertParseFailure(parser, userInput, expectedMessage);
}

Expand All @@ -52,21 +55,14 @@ public void parse_emptyAsgnNameError() {
@Test
public void parse_emptyArgs() {
String userInput = " ";
String expectedMessage = "Invalid command format! \n" + "removeGrade"
+ ": Removes a grade of an assignment from the person.\n"
+ "Parameters: "
+ PREFIX_NAME
+ "NAME "
+ PREFIX_ASSIGNMENT
+ "ASSIGNMENT "
+ "Example: "
+ "removeGrade"
+ " "
+ PREFIX_NAME
+ "John Doe "
+ PREFIX_ASSIGNMENT
+ "Ex09 "; ;
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_USAGE);
assertParseFailure(parser, userInput, expectedMessage);
}

@Test
public void parse_emptyArgs_throwsParseException() {
assertThrows(ParseException.class, () -> parser.parse(""),
String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveGradeCommand.MESSAGE_USAGE));
}

}

0 comments on commit 4391383

Please sign in to comment.