Skip to content

Commit

Permalink
Merge pull request #122 from avrilmohh/remark-command-tests
Browse files Browse the repository at this point in the history
Add test cases for remark implementation
  • Loading branch information
avrilmohh authored Oct 22, 2024
2 parents 7368ff4 + 9f8913c commit 2d6e6c1
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/commands/RemarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
Expand Down Expand Up @@ -82,4 +83,12 @@ public boolean equals(Object other) {
return studentIndex.equals(otherRemarkCommand.studentIndex)
&& remark.equals(otherRemarkCommand.remark);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("studentIndex", studentIndex)
.add("remark", remark)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT_INDEX;
import static seedu.address.logic.parser.ParserUtil.arePrefixesPresent;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.RemarkCommand;
Expand All @@ -23,10 +24,12 @@ public RemarkCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_STUDENT_INDEX, PREFIX_REMARK);

if (!argMultimap.getValue(PREFIX_STUDENT_INDEX).isPresent()) {
if (!arePrefixesPresent(argMultimap, PREFIX_STUDENT_INDEX, PREFIX_REMARK)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STUDENT_INDEX, PREFIX_REMARK);

// Parse student index
Index studentIndex;
try {
Expand All @@ -35,10 +38,6 @@ public RemarkCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE), pe);
}

if (!argMultimap.getValue(PREFIX_REMARK).isPresent()) {
throw new ParseException((String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE)));
}

// Extract remark, ensuring it is non-empty and trimmed
String remark = argMultimap.getValue(PREFIX_REMARK).orElse("").trim();
if (remark.isEmpty()) {
Expand Down
120 changes: 120 additions & 0 deletions src/test/java/seedu/address/logic/commands/RemarkCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.VALID_REMARK_MATH;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showStudentAtIndex;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_STUDENT;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_STUDENT;
import static seedu.address.testutil.TypicalStudents.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.student.Student;

/**
* Contains integration tests (interaction with the Model) and unit tests for
* {@code RemarkCommand}.
*/
public class RemarkCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void execute_validIndexUnfilteredList_success() {
Student studentToAddRemark = model.getFilteredStudentList().get(INDEX_FIRST_STUDENT.getZeroBased());
RemarkCommand remarkCommand = new RemarkCommand(INDEX_FIRST_STUDENT, VALID_REMARK_MATH);

Student updatedStudent = new Student(studentToAddRemark, VALID_REMARK_MATH);

String expectedMessage = String.format(RemarkCommand.MESSAGE_ADD_REMARK_SUCCESS,
Messages.format(updatedStudent));

ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.setStudent(studentToAddRemark, updatedStudent);

assertCommandSuccess(remarkCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_invalidIndexUnfilteredList_throwsCommandException() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredStudentList().size() + 1);
RemarkCommand remarkCommand = new RemarkCommand(outOfBoundIndex, VALID_REMARK_MATH);

assertCommandFailure(remarkCommand, model, Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX);
}

@Test
public void execute_validIndexFilteredList_success() {
showStudentAtIndex(model, INDEX_FIRST_STUDENT);

Student studentToAddRemark = model.getFilteredStudentList().get(INDEX_FIRST_STUDENT.getZeroBased());
RemarkCommand remarkCommand = new RemarkCommand(INDEX_FIRST_STUDENT, VALID_REMARK_MATH);

Student updatedStudent = new Student(studentToAddRemark, VALID_REMARK_MATH);

String expectedMessage = String.format(RemarkCommand.MESSAGE_ADD_REMARK_SUCCESS,
Messages.format(updatedStudent));

Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.setStudent(studentToAddRemark, updatedStudent);

assertCommandSuccess(remarkCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_emptyRemark_throwsCommandException() {
RemarkCommand remarkCommand = new RemarkCommand(INDEX_FIRST_STUDENT, "");

assertCommandFailure(remarkCommand, model, RemarkCommand.MESSAGE_NO_REMARK);
}

@Test
public void constructor_nullRemark_throwsNullPointerException() {
// null remark should throw a NullPointerException
assertThrows(NullPointerException.class, () -> new RemarkCommand(INDEX_FIRST_STUDENT, null));
}

@Test
public void equals() {
String firstRemark = "Weak in Math";
String secondRemark = "Strong in Science";

RemarkCommand remarkFirstCommand = new RemarkCommand(INDEX_FIRST_STUDENT, firstRemark);
RemarkCommand remarkSecondCommand = new RemarkCommand(INDEX_SECOND_STUDENT, secondRemark);

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

// same values -> returns true
RemarkCommand remarkFirstCommandCopy = new RemarkCommand(INDEX_FIRST_STUDENT, firstRemark);
assertTrue(remarkFirstCommand.equals(remarkFirstCommandCopy));

// different types -> returns false
assertFalse(remarkFirstCommand.equals(1));

// null -> returns false
assertFalse(remarkFirstCommand.equals(null));

// different student -> returns false
assertFalse(remarkFirstCommand.equals(remarkSecondCommand));
}

@Test
public void toStringMethod() {
String remark = "Weak at English";
RemarkCommand remarkCommand = new RemarkCommand(INDEX_FIRST_STUDENT, remark);
String expected = RemarkCommand.class.getCanonicalName() + "{studentIndex=" + INDEX_FIRST_STUDENT
+ ", remark=" + remark + "}";
assertEquals(expected, remarkCommand.toString());
}
}
100 changes: 100 additions & 0 deletions src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_INDEX;
import static seedu.address.logic.commands.CommandTestUtil.VALID_REMARK_MATH;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT_INDEX;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_STUDENT;

import org.junit.jupiter.api.Test;

import seedu.address.logic.Messages;
import seedu.address.logic.commands.RemarkCommand;

public class RemarkCommandParserTest {

private final RemarkCommandParser parser = new RemarkCommandParser();

@Test
public void parse_validArgs_returnsRemarkCommand() {
String userInput = " " + PREFIX_STUDENT_INDEX + INDEX_FIRST_STUDENT.getOneBased() + " "
+ PREFIX_REMARK + VALID_REMARK_MATH;
assertParseSuccess(parser, userInput, new RemarkCommand(INDEX_FIRST_STUDENT, VALID_REMARK_MATH));
}

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, INVALID_INDEX, String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE));
}

@Test
public void parse_missingStudentIndex_throwsParseException() {
String userInput = " " + PREFIX_STUDENT_INDEX + PREFIX_REMARK + VALID_REMARK_MATH;
assertParseFailure(parser, userInput, String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE));
}

@Test
public void parse_missingRemark_throwsParseException() {
String userInput = " " + PREFIX_STUDENT_INDEX + INDEX_FIRST_STUDENT.getOneBased() + " " + PREFIX_REMARK;
assertParseFailure(parser, userInput, RemarkCommand.MESSAGE_NO_REMARK);
}

@Test
public void parse_missingPrefixes_throwsParseException() {
// Missing both prefixes
String userInput = "1 " + VALID_REMARK_MATH;
assertParseFailure(parser, userInput, String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE));

// Missing student prefix
userInput = "1 " + PREFIX_REMARK + VALID_REMARK_MATH;
assertParseFailure(parser, userInput, String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE));

// Missing remark prefix
userInput = PREFIX_STUDENT_INDEX + "1";
assertParseFailure(parser, userInput, String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE));
}

@Test
public void parse_duplicatePrefixes_throwsParseException() {
// Duplicate student prefix
String duplicateStudentInput = " " + PREFIX_STUDENT_INDEX + INDEX_FIRST_STUDENT.getOneBased() + " "
+ PREFIX_STUDENT_INDEX + INDEX_FIRST_STUDENT.getOneBased() + " " + PREFIX_REMARK + VALID_REMARK_MATH;
assertParseFailure(parser, duplicateStudentInput,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_STUDENT_INDEX));

// Duplicate remark prefix
String duplicateRemarkInput = " " + PREFIX_STUDENT_INDEX + INDEX_FIRST_STUDENT.getOneBased() + " "
+ PREFIX_REMARK + VALID_REMARK_MATH + " " + PREFIX_REMARK + VALID_REMARK_MATH;
assertParseFailure(parser, duplicateRemarkInput,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_REMARK));
}

@Test
public void parse_emptyRemark_throwsParseException() {
String userInput = " " + PREFIX_STUDENT_INDEX + INDEX_FIRST_STUDENT.getOneBased() + " "
+ PREFIX_REMARK + " "; // Empty remark
assertParseFailure(parser, userInput, RemarkCommand.MESSAGE_NO_REMARK);
}

@Test
public void parse_nonNumericStudentIndex_throwsParseException() {
String userInput = PREFIX_STUDENT_INDEX + INVALID_INDEX + " " + PREFIX_REMARK + VALID_REMARK_MATH;
assertParseFailure(parser, userInput, String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE));
}

@Test
public void parse_emptyInput_throwsParseException() {
// Empty input
assertParseFailure(parser, "", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE));
}
}

0 comments on commit 2d6e6c1

Please sign in to comment.