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

Improve tests #209

Merged
merged 3 commits into from
Nov 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ListConflictsCommand extends Command {
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_PRESCRIPTION_SUCCESS = "Here are the conflicting drugs.";
public static final String MESSAGE_SUCCESS = "Here are the conflicting drugs.";


private final Index targetIndex;
Expand All @@ -51,7 +51,7 @@ public CommandResult execute(Model model) throws CommandException {
for (Name drug : prescriptionToListConflicts.getConflictingDrugs()) {
conflictingDrugsString.append(drug.toString() + "\n");
}
return new CommandResult(MESSAGE_DELETE_PRESCRIPTION_SUCCESS + "\n" + conflictingDrugsString.toString());
return new CommandResult(MESSAGE_SUCCESS + "\n" + conflictingDrugsString.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class FindCommandParser implements Parser<FindCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public FindCommand parse(String args) throws ParseException {

// assert args != null : "args should not be null";
if (args == null) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.CompletedPrescriptions.getCompletedPrescriptionList;
import static seedu.address.testutil.TypicalPrescriptions.getTypicalPrescriptionList;

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

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;


public class ListConflictsCommandTest {
private Model model;
private Model expectedModel;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalPrescriptionList(), getCompletedPrescriptionList(), new UserPrefs());
expectedModel = new ModelManager(model.getPrescriptionList(),
model.getCompletedPrescriptionList(), new UserPrefs());
}

// EP: Index > size of prescription list
@Test
public void execute_listConflicts_failure() {
Index invalidIndex = Index.fromZeroBased(model.getPrescriptionList().getPrescriptionList().size());
assertThrows(CommandException.class,
Messages.MESSAGE_INVALID_PRESCRIPTION_DISPLAYED_INDEX, () -> new ListConflictsCommand(invalidIndex)
.execute(model));
}

// EP: Has conflicting drugs in prescription at an index
@Test
public void execute_listConflicts_success() {
String expectedSuccessMessage = ListConflictsCommand.MESSAGE_SUCCESS + "\nIbuprofen\n";
assertCommandSuccess(new ListConflictsCommand(Index.fromOneBased(1)), model,
expectedSuccessMessage, expectedModel);
}

// EP: No conflicting drugs in current prescriptions
// @Test
// public void execute_listConflicts_noConflictsSuccess() {
// // Only the first prescription has a conflict in our test data
// model.deletePrescription(model.getFilteredPrescriptionList().get(0));
// expectedModel.deletePrescription(expectedModel.getFilteredPrescriptionList().get(0));
// assertCommandSuccess(new ListAllConflictsCommand(), model,
// ListAllConflictsCommand.MESSAGE_EMPTY_LIST, expectedModel);
// }

@Test
public void equals() {
ListConflictsCommand listConflictsCommand = new ListConflictsCommand(Index.fromOneBased(1));
ListConflictsCommand listConflictsCommandCopy = new ListConflictsCommand(Index.fromOneBased(1));
ListConflictsCommand listConflictsCommandDifferentIndex = new ListConflictsCommand(Index.fromOneBased(2));

// same object -> returns true
assert(listConflictsCommand.equals(listConflictsCommand));
// same index -> returns true
assert(listConflictsCommand.equals(listConflictsCommandCopy));
// different index -> returns false
assert(!listConflictsCommand.equals(listConflictsCommandDifferentIndex));
// different types -> returns false
assert(!listConflictsCommand.equals(1));
// null -> returns false
assert(!listConflictsCommand.equals(null));
}

@Test
public void toStringMethod() {
Index targetIndex = Index.fromOneBased(1);
ListConflictsCommand listConflictsCommand = new ListConflictsCommand(targetIndex);
String expected = ListConflictsCommand.class.getCanonicalName() + "{toListConflicts=" + targetIndex + "}";
assertEquals(expected, listConflictsCommand.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import java.util.List;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.FindCommand;
import seedu.address.model.prescription.NameContainsKeywordsPredicate;

public class FindCommandParserTest {
private FindCommandParser parser = new FindCommandParser();

//write some test cases for the parser
// EP: non empty string
@Test
public void parse_nonEmptyString_success() {
String userInput = "abc";
assertParseSuccess(parser, userInput,
new FindCommand(new NameContainsKeywordsPredicate(List.of("abc"))));
}

// EP: empty string
@Test
public void parse_emptyString_failure() {
String userInput = "";
assertParseFailure(parser, userInput,
String.format(MESSAGE_INVALID_COMMAND_FORMAT,
FindCommand.MESSAGE_USAGE));
}

// EP: null string
@Test
public void parse_nullString_failure() {
String userInput = null;
assertParseFailure(parser, userInput,
String.format(MESSAGE_INVALID_COMMAND_FORMAT,
FindCommand.MESSAGE_USAGE));
}

// EP: multiple keywords malicious input
@Test
public void parse_extraValues_success() {
// Random prefixes
assertParseSuccess(parser, "mn/ABCD d/2",
new FindCommand(new NameContainsKeywordsPredicate(
List.of("mn/ABCD", "d/2"))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package seedu.address.logic.parser;

import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import org.junit.jupiter.api.Test;

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


public class ListAllConflictsCommandParserTest {
private ListAllConflictsCommandParser parser = new ListAllConflictsCommandParser();

@Test
public void parse_emptyPreamble_success() {
assertParseSuccess(parser, "", new ListAllConflictsCommand());
}

@Test
public void parse_preambleWhitespace_success() {
assertParseSuccess(parser, " ", new ListAllConflictsCommand());
}

@Test
public void parse_extraValues_failure() {
// Random values
assertParseFailure(parser, "ABCDEFGH",
String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, ListAllConflictsCommand.MESSAGE_USAGE));

// Random prefixes
assertParseFailure(parser, "mn/ABCD d/2",
String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, ListAllConflictsCommand.MESSAGE_USAGE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.ListConflictsCommand;


public class ListConflictsCommandParserTest {
private ListConflictsCommandParser parser = new ListConflictsCommandParser();

//write some test cases for the parser
@Test
public void parse_validArgs_success() {
// Valid index
assertParseSuccess(parser, "1", new ListConflictsCommand(Index.fromOneBased(1)));
}

@Test
public void parse_zeroIndex_failure() {
// Valid index
assertParseFailure(parser, "0", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_negativeIndex_failure() {
// Valid index
assertParseFailure(parser, "-1", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_nonNumberIndex_failure() {
// Valid index
assertParseFailure(parser, "abc", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_float_failure() {
// Valid index
assertParseFailure(parser, "1.0", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_fraction_failure() {
// Valid index
assertParseFailure(parser, "1/2", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_leadingZeroes_success() {
// Valid index
assertParseSuccess(parser, "001", new ListConflictsCommand(Index.fromOneBased(1)));
}

@Test
public void parse_multipleNumbers_failure() {
// Valid index
assertParseFailure(parser, "1 2 3", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_emptyPreamble_failure() {
// No index specified
assertParseFailure(parser, "", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_whitespacePreamble_failure() {
// No index specified
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}

@Test
public void parse_extraValues_failure() {
// Random values
assertParseFailure(parser, "ABCDEFGH",
String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));

// Random prefixes
assertParseFailure(parser, "mn/ABCD d/2",
String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListConflictsCommand.MESSAGE_USAGE));
}
}
Loading