Skip to content

Commit

Permalink
Merge pull request #347 from tjtanjin/update-send-toggle-tests
Browse files Browse the repository at this point in the history
Chore: add test cases for Send and Toggle command/parser
  • Loading branch information
tjtanjin authored Mar 23, 2021
2 parents 47e6c80 + 819fa1c commit bbe6a94
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public class SendCommand extends Command {
public SendCommand(Index index) {
requireAllNonNull(index);

assert index.getZeroBased() >= 0;
this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException, RequestException, AbortRequestException {
List<Endpoint> lastShownList = model.getFilteredEndpointList();
assert index.getZeroBased() >= 0;
if (index.getZeroBased() >= lastShownList.size()) {
logger.info("Illegal index found, out of bound");
throw new CommandException(Messages.MESSAGE_INVALID_ENDPOINT_DISPLAYED_INDEX);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/seedu/us/among/logic/commands/SendCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
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.us.among.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.us.among.logic.commands.CommandTestUtil.showEndpointAtIndex;
import static seedu.us.among.testutil.Assert.assertThrows;
import static seedu.us.among.testutil.TypicalEndpoints.getTypicalEndpointList;
import static seedu.us.among.testutil.TypicalIndexes.INDEX_EIGHTH_ENDPOINT;
import static seedu.us.among.testutil.TypicalIndexes.INDEX_FIRST_ENDPOINT;
import static seedu.us.among.testutil.TypicalIndexes.INDEX_SECOND_ENDPOINT;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import seedu.us.among.commons.core.Messages;
import seedu.us.among.commons.core.index.Index;
import seedu.us.among.commons.util.JsonUtil;
import seedu.us.among.logic.commands.exceptions.CommandException;
import seedu.us.among.logic.request.exceptions.AbortRequestException;
import seedu.us.among.logic.request.exceptions.RequestException;
import seedu.us.among.model.Model;
import seedu.us.among.model.ModelManager;
import seedu.us.among.model.UserPrefs;
Expand All @@ -37,6 +46,37 @@ public void execute_endpoint_sendSuccessful() throws Exception {
commandResult.getFeedbackToUser());
}

@Test
public void constructor_validIndex_success() {
new SendCommand(INDEX_FIRST_ENDPOINT);
}

@Test
public void execute_outOfBoundEndpointIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredEndpointList().size() + 1);
SendCommand sendCommand = new SendCommand(outOfBoundIndex);
assertCommandFailure(sendCommand, model, Messages.MESSAGE_INVALID_ENDPOINT_DISPLAYED_INDEX);
}

@Test
public void execute_outOfBoundEndpointIndexFilteredList_failure() {
showEndpointAtIndex(model, INDEX_FIRST_ENDPOINT);
Index outOfBoundIndex = INDEX_SECOND_ENDPOINT;
// ensures that outOfBoundIndex is still in bounds of API endpoint list
assertTrue(outOfBoundIndex.getZeroBased() < model.getEndpointList().getEndpointList().size());

assertCommandFailure(new SendCommand(outOfBoundIndex), model,
Messages.MESSAGE_INVALID_ENDPOINT_DISPLAYED_INDEX);
}

@Test
public void execute_validEndpointIndex_success() throws CommandException, AbortRequestException,
RequestException, IOException {
SendCommand sendCommand = new SendCommand(Index.fromZeroBased(7));
assertEquals(JsonUtil.toPrettyPrintJsonString(SampleDataUtil.getSampleValidResponse()),
sendCommand.execute(model).getFeedbackToUser());
}

@Test
public void equals() {
final SendCommand standardCommand = new SendCommand(INDEX_FIRST_ENDPOINT);
Expand Down
33 changes: 17 additions & 16 deletions src/test/java/seedu/us/among/logic/commands/ToggleCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,37 @@

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.us.among.testutil.TypicalEndpoints.getTypicalEndpointList;

import org.junit.jupiter.api.Test;

import seedu.us.among.model.Model;
import seedu.us.among.model.ModelManager;
import seedu.us.among.model.UserPrefs;
import seedu.us.among.ui.ThemeType;

public class ToggleCommandTest {
private Model model = new ModelManager(getTypicalEndpointList(), new UserPrefs());

@Test
public void equals() {
final ToggleCommand standardCommand = new ToggleCommand(ThemeType.getTheme("light").name());

// same values -> returns true
ToggleCommand commandWithSameValues = new ToggleCommand(ThemeType.getTheme("light").name());
assertTrue(standardCommand.equals(commandWithSameValues));
for (ThemeType theme : ThemeType.values()) {
String userInput = ThemeType.getTheme(theme.toString()).name();
ToggleCommand standardCommand = new ToggleCommand(userInput);

// same object -> returns true
assertTrue(standardCommand.equals(standardCommand));
// same values -> returns true
ToggleCommand commandWithSameValues = new ToggleCommand(userInput);
assertTrue(standardCommand.equals(commandWithSameValues));

// null -> returns false
assertFalse(standardCommand.equals(null));
// same object -> returns true
assertTrue(standardCommand.equals(standardCommand));

// different types -> returns false
assertFalse(standardCommand.equals(new ClearCommand()));
// null -> returns false
assertFalse(standardCommand.equals(null));

// different index -> returns false
// different types -> returns false
assertFalse(standardCommand.equals(new ClearCommand()));
}

ToggleCommand standardCommand = new ToggleCommand(ThemeType.getTheme("light").name());

// different theme toggle -> returns false
assertFalse(standardCommand.equals(new ToggleCommand(ThemeType.getTheme("dark").name())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@

public class SendCommandParserTest {

private static final String MESSAGE_INVALID_FORMAT = String.format(MESSAGE_INVALID_COMMAND_FORMAT,
SendCommand.MESSAGE_USAGE);

private SendCommandParser parser = new SendCommandParser();

@Test
public void parse_validArgs_returnsSendCommand() {
assertParseSuccess(parser, "1", new SendCommand(INDEX_FIRST_ENDPOINT));
}

@Test
public void parse_emptyArgs_returnsSendCommand() {
assertParseFailure(parser, "", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, " ", MESSAGE_INVALID_FORMAT);
}

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, SendCommand.MESSAGE_USAGE));
assertParseFailure(parser, "a", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, ".", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, "1a.", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, "a1", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, ".1", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, "1.", MESSAGE_INVALID_FORMAT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ToggleCommandParserTest {
public void parse_missingParts_failure() {
// no theme specified
assertParseFailure(parser, "", MESSAGE_INVALID_FORMAT);
assertParseFailure(parser, " ", MESSAGE_INVALID_FORMAT);
}

@Test
Expand All @@ -33,11 +34,10 @@ public void parse_invalidValue_failure() {

@Test
public void parse_themeSpecified_success() {
String userInput = ThemeType.getTheme("light").toString();
ToggleCommand expectedCommand = new ToggleCommand(ThemeType.getTheme("light").toString());

assertParseSuccess(parser, userInput, expectedCommand);
for (ThemeType theme: ThemeType.values()) {
String userInput = ThemeType.getTheme(theme.toString()).toString();
ToggleCommand expectedCommand = new ToggleCommand(ThemeType.getTheme(theme.toString()).toString());
assertParseSuccess(parser, userInput, expectedCommand);
}
}


}

0 comments on commit bbe6a94

Please sign in to comment.