Skip to content

Commit

Permalink
Add Show command test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
drustanyjt committed Apr 2, 2024
1 parent 79eb85d commit f8d616a
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/logic/commands/ShowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;

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 @@ -93,6 +94,24 @@ public boolean equals(Object other) {
}

ShowCommand e = (ShowCommand) other;
// icPredicate is not important if isClear is true
if (isClear && e.isClear) {
return true;
}
if (isClear != e.isClear) {
return false;
}
if (icPredicate == null || e.icPredicate == null) {
return icPredicate == e.icPredicate;
}
return icPredicate.equals(e.icPredicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", icPredicate)
.add("isClear", isClear)
.toString();
}
}
93 changes: 93 additions & 0 deletions src/test/java/seedu/address/logic/commands/ShowCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.IdentityCardNumber;
import seedu.address.model.person.IdentityCardNumberMatchesPredicate;

/**
* Contains integration tests (interaction with the Model) for {@code ShowCommand}.
*/
public class ShowCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() {
IdentityCardNumberMatchesPredicate firstPredicate =
new IdentityCardNumberMatchesPredicate(new IdentityCardNumber("S1234567A"));
IdentityCardNumberMatchesPredicate secondPredicate =
new IdentityCardNumberMatchesPredicate(new IdentityCardNumber("S9876543B"));

ShowCommand showFirstCommand = ShowCommand.createShowCommand(firstPredicate);
ShowCommand showSecondCommand = ShowCommand.createShowCommand(secondPredicate);

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

// same values -> returns true
ShowCommand showFirstCommandCopy = ShowCommand.createShowCommand(firstPredicate);
assertTrue(showFirstCommand.equals(showFirstCommandCopy));

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

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

// different person -> returns false
assertFalse(showFirstCommand.equals(showSecondCommand));

// different isClear -> returns false
ShowCommand showFirstCommandClear = ShowCommand.createClearCommand();
assertFalse(showFirstCommand.equals(showFirstCommandClear));

// same isClear -> returns true
ShowCommand showSecondCommandClear = ShowCommand.createClearCommand();
assertTrue(showFirstCommandClear.equals(showSecondCommandClear));
}

@Test
public void execute_validIC_singlePersonFound() {
String testNumber = ALICE.getIdentityCardNumber().value;
String expectedMessage = String.format(ShowCommand.MESSAGE_SHOW_NOTE_SUCCESS, testNumber);
IdentityCardNumberMatchesPredicate predicate = preparePredicate(testNumber);
ShowCommand command = ShowCommand.createShowCommand(predicate);
expectedModel.setDisplayNote(ALICE);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
}

@Test
public void execute_noIC_clearNote() {
String expectedMessage = ShowCommand.MESSAGE_CLEAR_NOTE_SUCCESS;
ShowCommand command = ShowCommand.createClearCommand();
expectedModel.clearDisplayNote();
assertCommandSuccess(command, model, expectedMessage, expectedModel);
}

@Test
public void toStringMethod() {
IdentityCardNumberMatchesPredicate predicate = new IdentityCardNumberMatchesPredicate(
new IdentityCardNumber("S1234567A"));
ShowCommand showCommand = ShowCommand.createShowCommand(predicate);
String expected = ShowCommand.class.getCanonicalName() + "{predicate=" + predicate + ", isClear=" + "false}";
assertEquals(expected, showCommand.toString());
}

/**
* Parses {@code userInput} into a {@code IdentityCardNumberMatchesPredicate}.
*/
private IdentityCardNumberMatchesPredicate preparePredicate(String userInput) {
return new IdentityCardNumberMatchesPredicate(new IdentityCardNumber(userInput));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package seedu.address.logic.parser;

import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.ShowCommand;
import seedu.address.model.person.IdentityCardNumber;
import seedu.address.model.person.IdentityCardNumberMatchesPredicate;

public class ShowCommandParserTest {

private ShowCommandParser parser = new ShowCommandParser();

@Test
public void parse_emptyArg_returnsShowCommand() {
ShowCommand expectedShowCommand = ShowCommand.createClearCommand();
assertParseSuccess(parser, "", expectedShowCommand);
assertParseSuccess(parser, " ", expectedShowCommand);
assertParseSuccess(parser, " \n \t \t", expectedShowCommand);
}

@Test
public void parse_validArgs_returnsShowCommand() {
ShowCommand expectedShowCommand = ShowCommand.createShowCommand(new IdentityCardNumberMatchesPredicate(
new IdentityCardNumber("S1234567A")));
assertParseSuccess(parser, "S1234567A", expectedShowCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, " \n"
+ " S1234567A \n"
+ " \t \t", expectedShowCommand);
}

@Test
public void parse_invalidArgs_throwsParseException() {
// IC with incorrect format
assertParseFailure(parser, "S1234", IdentityCardNumber.MESSAGE_CONSTRAINTS);

// IC with incorrect format and additional arguments
assertParseFailure(parser, "S1234 extra", IdentityCardNumber.MESSAGE_CONSTRAINTS);

// IC with correct format but contains non-alphanumeric characters
assertParseFailure(parser, "S1234$%^", IdentityCardNumber.MESSAGE_CONSTRAINTS);
}

@Test
public void parse_nullArgs_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> parser.parse(null));
}

}

0 comments on commit f8d616a

Please sign in to comment.