From 3c31062aeda32cee3046529784a315cc84436663 Mon Sep 17 00:00:00 2001 From: nixonwidjaja Date: Mon, 30 Oct 2023 23:51:22 +0800 Subject: [PATCH] Add tests --- .../address/logic/commands/SortCommand.java | 18 +++++++++++ .../logic/commands/SortCommandTest.java | 32 +++++++++++++++++++ .../logic/parser/SortCommandParserTest.java | 28 ++++++++++++++++ .../address/model/person/AddressTest.java | 8 +++++ .../seedu/address/model/person/EmailTest.java | 8 +++++ .../seedu/address/model/person/NameTest.java | 8 +++++ .../seedu/address/model/person/PhoneTest.java | 8 +++++ 7 files changed, 110 insertions(+) create mode 100644 src/test/java/seedu/address/logic/commands/SortCommandTest.java create mode 100644 src/test/java/seedu/address/logic/parser/SortCommandParserTest.java diff --git a/src/main/java/seedu/address/logic/commands/SortCommand.java b/src/main/java/seedu/address/logic/commands/SortCommand.java index 8466bbfd389..a145def4a1c 100644 --- a/src/main/java/seedu/address/logic/commands/SortCommand.java +++ b/src/main/java/seedu/address/logic/commands/SortCommand.java @@ -30,6 +30,7 @@ public class SortCommand extends Command { private final boolean desc; private final Comparator comparator; + private final String param; /** * Constructs a SortCommand. @@ -38,6 +39,7 @@ public class SortCommand extends Command { public SortCommand(String param) { this.comparator = new PersonComparator(param); this.desc = false; + this.param = param; } /** @@ -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 @@ -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; + } } diff --git a/src/test/java/seedu/address/logic/commands/SortCommandTest.java b/src/test/java/seedu/address/logic/commands/SortCommandTest.java new file mode 100644 index 00000000000..15e99f0fd11 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/SortCommandTest.java @@ -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() { + + } +} diff --git a/src/test/java/seedu/address/logic/parser/SortCommandParserTest.java b/src/test/java/seedu/address/logic/parser/SortCommandParserTest.java new file mode 100644 index 00000000000..b066a3c3ddb --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/SortCommandParserTest.java @@ -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)); + } +} diff --git a/src/test/java/seedu/address/model/person/AddressTest.java b/src/test/java/seedu/address/model/person/AddressTest.java index 314885eca26..2caee8745b6 100644 --- a/src/test/java/seedu/address/model/person/AddressTest.java +++ b/src/test/java/seedu/address/model/person/AddressTest.java @@ -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; @@ -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); + } } diff --git a/src/test/java/seedu/address/model/person/EmailTest.java b/src/test/java/seedu/address/model/person/EmailTest.java index f08cdff0a64..2e3628951eb 100644 --- a/src/test/java/seedu/address/model/person/EmailTest.java +++ b/src/test/java/seedu/address/model/person/EmailTest.java @@ -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; @@ -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("asdf@gmail.com").compareTo(new Email("bcda@gmail.com")), -1); + assertEquals(new Email("asdf@gmail.com").compareTo(new Email("asdf@gmail.com")), 0); + assertEquals(new Email("c@gmail.com").compareTo(new Email("bcda@gmail.com")), 1); + } } diff --git a/src/test/java/seedu/address/model/person/NameTest.java b/src/test/java/seedu/address/model/person/NameTest.java index d4b51737a0e..0cef1aa0d5d 100644 --- a/src/test/java/seedu/address/model/person/NameTest.java +++ b/src/test/java/seedu/address/model/person/NameTest.java @@ -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; @@ -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); + } } diff --git a/src/test/java/seedu/address/model/person/PhoneTest.java b/src/test/java/seedu/address/model/person/PhoneTest.java index deaaa5ba190..e6a329843db 100644 --- a/src/test/java/seedu/address/model/person/PhoneTest.java +++ b/src/test/java/seedu/address/model/person/PhoneTest.java @@ -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; @@ -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); + } }