Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nixonwidjaja committed Oct 30, 2023
1 parent 3213b3a commit 3c31062
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/seedu/address/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class SortCommand extends Command {

private final boolean desc;
private final Comparator<Person> comparator;
private final String param;

/**
* Constructs a SortCommand.
Expand All @@ -38,6 +39,7 @@ public class SortCommand extends Command {
public SortCommand(String param) {
this.comparator = new PersonComparator(param);
this.desc = false;
this.param = param;
}

/**
Expand All @@ -48,6 +50,7 @@ public SortCommand(String param) {
public SortCommand(String param, boolean desc) {
this.comparator = new PersonComparator(param);
this.desc = desc;
this.param = param;
}

@Override
Expand All @@ -66,4 +69,19 @@ public CommandResult execute(Model model, String commandText) throws CommandExce
throw new CommandException(ERROR_MESSAGE);
}
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof SortCommand)) {
return false;
}

SortCommand otherCommand = (SortCommand) other;
return param.equals(otherCommand.param) && desc == otherCommand.desc;
}
}
32 changes: 32 additions & 0 deletions src/test/java/seedu/address/logic/commands/SortCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.logic.commands;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;

import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;

public class SortCommandTest {
private Model model = new ModelManager();
private Model reversed = new ModelManager();

@Test
public void execute_success() {
model.addPerson(ALICE);
model.addPerson(BENSON);
reversed.addPerson(BENSON);
reversed.addPerson(ALICE);
assertCommandSuccess(new SortCommand("name", true), model, SortCommand.MESSAGE_SUCCESS, reversed);
assertCommandSuccess(new SortCommand("name"), model, SortCommand.MESSAGE_SUCCESS, model);
}

@Test
public void execute_failure() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.logic.commands.SortCommand;
import seedu.address.logic.parser.exceptions.ParseException;

class SortCommandParserTest {

private SortCommandParser parser = new SortCommandParser();

@Test
void parse_success() throws ParseException {
assertParseSuccess(parser, " name", new SortCommand("name"));
assertParseSuccess(parser, " name desc", new SortCommand("name", true));
}

@Test
void parse_failure() {
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE));
assertParseFailure(parser, " a b c ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE));
assertParseFailure(parser, " a b ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, SortCommand.MESSAGE_USAGE));
}
}
8 changes: 8 additions & 0 deletions src/test/java/seedu/address/model/person/AddressTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.person;

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.testutil.Assert.assertThrows;
Expand Down Expand Up @@ -53,4 +54,11 @@ public void equals() {
// different values -> returns false
assertFalse(address.equals(new Address("Other Valid Address")));
}

@Test
public void compareTo() {
assertEquals(new Address("asdf").compareTo(new Address("bcda")), -1);
assertEquals(new Address("asdf").compareTo(new Address("asdf")), 0);
assertEquals(new Address("c").compareTo(new Address("bcda")), 1);
}
}
8 changes: 8 additions & 0 deletions src/test/java/seedu/address/model/person/EmailTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.person;

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.testutil.Assert.assertThrows;
Expand Down Expand Up @@ -85,4 +86,11 @@ public void equals() {
// different values -> returns false
assertFalse(email.equals(new Email("other.valid@email")));
}

@Test
public void compareTo() {
assertEquals(new Email("[email protected]").compareTo(new Email("[email protected]")), -1);
assertEquals(new Email("[email protected]").compareTo(new Email("[email protected]")), 0);
assertEquals(new Email("[email protected]").compareTo(new Email("[email protected]")), 1);
}
}
8 changes: 8 additions & 0 deletions src/test/java/seedu/address/model/person/NameTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.person;

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.testutil.Assert.assertThrows;
Expand Down Expand Up @@ -62,4 +63,11 @@ public void equals() {
// different values -> returns false
assertFalse(name.equals(new Name("Other Valid Name")));
}

@Test
public void compareTo() {
assertEquals(new Name("asdf").compareTo(new Name("bcda")), -1);
assertEquals(new Name("asdf").compareTo(new Name("asdf")), 0);
assertEquals(new Name("c").compareTo(new Name("bcda")), 1);
}
}
8 changes: 8 additions & 0 deletions src/test/java/seedu/address/model/person/PhoneTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.model.person;

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.testutil.Assert.assertThrows;
Expand Down Expand Up @@ -57,4 +58,11 @@ public void equals() {
// different values -> returns false
assertFalse(phone.equals(new Phone("995")));
}

@Test
public void compareTo() {
assertEquals(new Phone("111").compareTo(new Phone("222")), -1);
assertEquals(new Phone("111").compareTo(new Phone("111")), 0);
assertEquals(new Phone("222").compareTo(new Phone("111")), 1);
}
}

0 comments on commit 3c31062

Please sign in to comment.