From ad64a6e4f6f85253a17993e6c34bad9663a6f3f9 Mon Sep 17 00:00:00 2001 From: Ty-stan0417 Date: Mon, 15 Apr 2024 00:53:10 +0800 Subject: [PATCH 1/2] Add more test case to increase coverage --- .../seedu/address/model/person/Person.java | 12 --- .../seedu/address/storage/LibraryStorage.java | 2 +- .../address/commons/core/ConfigTest.java | 8 ++ .../address/commons/core/GuiSettingsTest.java | 17 ++++ .../logic/parser/AddressBookParserTest.java | 51 +++++++++++- .../seedu/address/model/AddressBookTest.java | 9 +++ .../seedu/address/model/UserPrefsTest.java | 12 +++ .../address/model/person/MeritScoreTest.java | 79 +++++++++++++++++++ .../address/model/person/PersonTest.java | 10 +++ .../model/person/UniquePersonListTest.java | 9 +++ .../address/storage/LibraryStorageTest.java | 5 ++ .../seedu/address/testutil/PersonBuilder.java | 5 ++ 12 files changed, 205 insertions(+), 14 deletions(-) create mode 100644 src/test/java/seedu/address/model/person/MeritScoreTest.java diff --git a/src/main/java/seedu/address/model/person/Person.java b/src/main/java/seedu/address/model/person/Person.java index 69b2060a54a..8e43f4b9b3d 100644 --- a/src/main/java/seedu/address/model/person/Person.java +++ b/src/main/java/seedu/address/model/person/Person.java @@ -128,18 +128,6 @@ public ArrayList getBookListWithNewBook(Book book) { return new ArrayList<>(Collections.unmodifiableList(mutableCopy)); } - public String getBookListToString() { - this.bookList.sort(Comparator.comparing(book -> book.bookTitle)); - String result = ""; - for (int i = 0; i < bookList.size(); i++) { - result += this.bookList.get(i).bookTitle.toString(); - if (i != bookList.size() - 1) { - result += "\n"; - } - } - return result; - } - public String getBookListToStringWithIndex() { this.bookList.sort(Comparator.comparing(book -> book.bookTitle)); String result = ""; diff --git a/src/main/java/seedu/address/storage/LibraryStorage.java b/src/main/java/seedu/address/storage/LibraryStorage.java index 6fa5cb9f652..6c9f80d800d 100644 --- a/src/main/java/seedu/address/storage/LibraryStorage.java +++ b/src/main/java/seedu/address/storage/LibraryStorage.java @@ -68,7 +68,7 @@ public LibraryStorage() { * @param bookTitle the book title of a book * @return true if the book title is valid, false otherwise */ - private static boolean isValidBook(String bookTitle) { + public static boolean isValidBook(String bookTitle) { if (bookTitle == null || bookTitle.trim().isEmpty()) { return false; } diff --git a/src/test/java/seedu/address/commons/core/ConfigTest.java b/src/test/java/seedu/address/commons/core/ConfigTest.java index d3ba2a52a89..4529989e397 100644 --- a/src/test/java/seedu/address/commons/core/ConfigTest.java +++ b/src/test/java/seedu/address/commons/core/ConfigTest.java @@ -1,6 +1,7 @@ package seedu.address.commons.core; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -23,5 +24,12 @@ public void equalsMethod() { assertTrue(defaultConfig.equals(defaultConfig)); } + @Test + public void equals_failure() { + Config defaultConfig = new Config(); + // other instance + assertFalse(defaultConfig.equals(3)); + } + } diff --git a/src/test/java/seedu/address/commons/core/GuiSettingsTest.java b/src/test/java/seedu/address/commons/core/GuiSettingsTest.java index b7876c4349d..8334e378a1c 100644 --- a/src/test/java/seedu/address/commons/core/GuiSettingsTest.java +++ b/src/test/java/seedu/address/commons/core/GuiSettingsTest.java @@ -1,6 +1,9 @@ package seedu.address.commons.core; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; @@ -13,4 +16,18 @@ public void toStringMethod() { + guiSettings.getWindowCoordinates() + "}"; assertEquals(expected, guiSettings.toString()); } + + @Test + public void equalsMethod() { + GuiSettings defaultGui = new GuiSettings(); + assertNotNull(defaultGui); + assertTrue(defaultGui.equals(defaultGui)); + } + + @Test + public void equals_failure() { + GuiSettings defaultGui = new GuiSettings(); + // other instance + assertFalse(defaultGui.equals(3)); + } } diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 86be51873de..1cfb60515b1 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -14,8 +14,11 @@ import org.junit.jupiter.api.Test; +import seedu.address.logic.commands.AddBookCommand; import seedu.address.logic.commands.AddCommand; +import seedu.address.logic.commands.BorrowCommand; import seedu.address.logic.commands.ClearCommand; +import seedu.address.logic.commands.DeleteBookCommand; import seedu.address.logic.commands.DeleteCommand; import seedu.address.logic.commands.DonateCommand; import seedu.address.logic.commands.EditCommand; @@ -23,9 +26,12 @@ import seedu.address.logic.commands.ExitCommand; import seedu.address.logic.commands.FindCommand; import seedu.address.logic.commands.HelpCommand; +import seedu.address.logic.commands.LimitCommand; import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.commands.ReturnCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.book.Book; +import seedu.address.model.library.Threshold; import seedu.address.model.person.NameContainsKeywordsPredicate; import seedu.address.model.person.Person; import seedu.address.testutil.EditPersonDescriptorBuilder; @@ -91,7 +97,6 @@ public void parseCommand_list() throws Exception { assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand); } - // todo in later iteration: add donated book to library @Test public void parseCommand_donate() throws Exception { String bookTitle = "Some book"; @@ -101,6 +106,50 @@ public void parseCommand_donate() throws Exception { assertEquals(new DonateCommand(INDEX_KEPLER, new Book(bookTitle)), command); } + @Test + public void parseCommand_borrow() throws Exception { + String bookTitle = "Some book"; + BorrowCommand command = (BorrowCommand) parser.parseCommand( + BorrowCommand.COMMAND_WORD + " " + INDEX_KEPLER.getOneBased() + " " + + CliSyntax.PREFIX_BOOKLIST + bookTitle); + assertEquals(new BorrowCommand(INDEX_KEPLER, new Book(bookTitle)), command); + } + + @Test + public void parseCommand_return() throws Exception { + String bookTitle = "Some book"; + ReturnCommand command = (ReturnCommand) parser.parseCommand( + ReturnCommand.COMMAND_WORD + " " + INDEX_KEPLER.getOneBased() + " " + + CliSyntax.PREFIX_BOOKLIST + bookTitle); + assertEquals(new ReturnCommand(INDEX_KEPLER, new Book(bookTitle)), command); + } + + @Test + public void parseCommand_limit() throws Exception { + int threshold = -10; + LimitCommand command = (LimitCommand) parser.parseCommand( + LimitCommand.COMMAND_WORD + " " + threshold); + assertEquals(new LimitCommand(new Threshold(threshold)), command); + } + + @Test + public void parseCommand_addBook() throws Exception { + String bookTitle = "Some book"; + AddBookCommand command = (AddBookCommand) parser.parseCommand( + AddBookCommand.COMMAND_WORD + " " + + CliSyntax.PREFIX_BOOKLIST + bookTitle); + assertEquals(new AddBookCommand(new Book(bookTitle)), command); + } + + @Test + public void parseCommand_deleteBook() throws Exception { + String bookTitle = "Some book"; + DeleteBookCommand command = (DeleteBookCommand) parser.parseCommand( + DeleteBookCommand.COMMAND_WORD + " " + + CliSyntax.PREFIX_BOOKLIST + bookTitle); + assertEquals(new DeleteBookCommand(new Book(bookTitle)), command); + } + @Test public void parseCommand_unrecognisedInput_throwsParseException() { assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), () diff --git a/src/test/java/seedu/address/model/AddressBookTest.java b/src/test/java/seedu/address/model/AddressBookTest.java index 68c8c5ba4d5..bd31e5451e5 100644 --- a/src/test/java/seedu/address/model/AddressBookTest.java +++ b/src/test/java/seedu/address/model/AddressBookTest.java @@ -89,6 +89,15 @@ public void toStringMethod() { assertEquals(expected, addressBook.toString()); } + @Test + public void equalsMethod() { + // same object -> returns true + assertTrue(addressBook.equals(addressBook)); + + // other instance -> returns false + assertFalse(addressBook.equals("3")); + } + /** * A stub ReadOnlyAddressBook whose persons list can violate interface constraints. */ diff --git a/src/test/java/seedu/address/model/UserPrefsTest.java b/src/test/java/seedu/address/model/UserPrefsTest.java index b1307a70d52..f03e70ab9d4 100644 --- a/src/test/java/seedu/address/model/UserPrefsTest.java +++ b/src/test/java/seedu/address/model/UserPrefsTest.java @@ -1,5 +1,7 @@ package seedu.address.model; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.testutil.Assert.assertThrows; import org.junit.jupiter.api.Test; @@ -18,4 +20,14 @@ public void setAddressBookFilePath_nullPath_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> userPrefs.setAddressBookFilePath(null)); } + @Test + public void equalsMethod() { + // same object -> returns true + UserPrefs userPref = new UserPrefs(); + assertTrue(userPref.equals(userPref)); + + // other instance -> returns false + assertFalse(userPref.equals("3")); + } + } diff --git a/src/test/java/seedu/address/model/person/MeritScoreTest.java b/src/test/java/seedu/address/model/person/MeritScoreTest.java new file mode 100644 index 00000000000..a8526cdb49d --- /dev/null +++ b/src/test/java/seedu/address/model/person/MeritScoreTest.java @@ -0,0 +1,79 @@ +package seedu.address.model.person; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +public class MeritScoreTest { + + @Test + public void constructor_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new MeritScore(null)); + } + + @Test + public void constructor_invalidMeritScore_throwsIllegalArgumentException() { + String invalidMeritScore = ""; + assertThrows(IllegalArgumentException.class, () -> new MeritScore(invalidMeritScore)); + } + + @Test + public void isValidMeritScore() { + // null merit score + assertThrows(NullPointerException.class, () -> MeritScore.isValidMeritScore(null)); + + // invalid merit score + assertFalse(MeritScore.isValidMeritScore("")); // empty string + assertFalse(MeritScore.isValidMeritScore(" ")); // spaces only + assertFalse(MeritScore.isValidMeritScore("^")); // only non-alphanumeric characters + assertFalse(MeritScore.isValidMeritScore("peter*")); // contains non-alphanumeric characters + assertFalse(MeritScore.isValidMeritScore("peter jack")); // alphabets only + assertFalse(MeritScore.isValidMeritScore("peter the 2nd")); // alphanumeric characters + assertFalse(MeritScore.isValidMeritScore("Capital Tan")); // with capital letters + + // valid name + assertTrue(MeritScore.isValidMeritScore("12345")); // numbers only + assertTrue(MeritScore.isValidMeritScore("-999999")); // small integer + assertTrue(MeritScore.isValidMeritScore("-1")); // negative integer + assertTrue(MeritScore.isValidMeritScore("0")); // 0 + assertTrue(MeritScore.isValidMeritScore("1")); // positive integer + assertTrue(MeritScore.isValidMeritScore("999999")); // large integer + } + + @Test + public void getMeritScore_failure() { + // not contains only digits -> throws Exception + assertThrows(IllegalArgumentException.class, () -> new MeritScore("123abc")); + + // format not correc5 -> throws Exception + assertThrows(IllegalArgumentException.class, () -> new MeritScore(" 122 ")); + } + + @Test + public void getScore_success() { + MeritScore meritScore = new MeritScore(1); + assertTrue(meritScore.getScore().equals("1")); + } + + @Test + public void equals() { + MeritScore meritScore = new MeritScore(0); + + // same values -> returns true + assertTrue(meritScore.equals(new MeritScore(0))); + + // same object -> returns true + assertTrue(meritScore.equals(meritScore)); + + // null -> returns false + assertFalse(meritScore.equals(null)); + + // different types -> returns false + assertFalse(meritScore.equals("hello")); + + // different values -> returns false + assertFalse(meritScore.equals(new MeritScore(-1))); + } +} diff --git a/src/test/java/seedu/address/model/person/PersonTest.java b/src/test/java/seedu/address/model/person/PersonTest.java index e07bea398d4..bd1d319cc3a 100644 --- a/src/test/java/seedu/address/model/person/PersonTest.java +++ b/src/test/java/seedu/address/model/person/PersonTest.java @@ -11,6 +11,7 @@ import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.BOB; +import static seedu.address.testutil.TypicalPersons.KEPLER; import org.junit.jupiter.api.Test; @@ -90,6 +91,15 @@ public void equals() { assertFalse(ALICE.equals(editedAlice)); } + @Test + public void getBookListToStringWithIndexMethod() { + String newBook = "New Book"; + Person person = new PersonBuilder(KEPLER).withAnotherBook(newBook).build(); + String expected = "1. " + "How To Become a Better Reader?\n"; + expected += "2. " + newBook; + assertEquals(expected, person.getBookListToStringWithIndex()); + } + @Test public void toStringMethod() { String expected = Person.class.getCanonicalName() + "{name=" + ALICE.getName() + ", phone=" + ALICE.getPhone() diff --git a/src/test/java/seedu/address/model/person/UniquePersonListTest.java b/src/test/java/seedu/address/model/person/UniquePersonListTest.java index 17ae501df08..89e9214eefe 100644 --- a/src/test/java/seedu/address/model/person/UniquePersonListTest.java +++ b/src/test/java/seedu/address/model/person/UniquePersonListTest.java @@ -168,6 +168,15 @@ public void asUnmodifiableObservableList_modifyList_throwsUnsupportedOperationEx -> uniquePersonList.asUnmodifiableObservableList().remove(0)); } + @Test + public void equalsMethod() { + // same object -> returns true + assertTrue(uniquePersonList.equals(uniquePersonList)); + + // other instance -> returns false + assertFalse(uniquePersonList.equals("3")); + } + @Test public void toStringMethod() { assertEquals(uniquePersonList.asUnmodifiableObservableList().toString(), uniquePersonList.toString()); diff --git a/src/test/java/seedu/address/storage/LibraryStorageTest.java b/src/test/java/seedu/address/storage/LibraryStorageTest.java index 9a2ee08c59d..62ca5ec5a0a 100644 --- a/src/test/java/seedu/address/storage/LibraryStorageTest.java +++ b/src/test/java/seedu/address/storage/LibraryStorageTest.java @@ -26,6 +26,11 @@ void setUp() { libraryStorage = new LibraryStorage("test_library.txt"); } + @Test + public void isValidBook() { + assertEquals(LibraryStorage.isValidBook(""), false); + } + @Test void loadLibraryFromFile_booksInOrderValidFile_success() throws IOException, DataLoadingException { // Prepare a test file with valid data diff --git a/src/test/java/seedu/address/testutil/PersonBuilder.java b/src/test/java/seedu/address/testutil/PersonBuilder.java index 6cfd0d806a8..f210c658548 100644 --- a/src/test/java/seedu/address/testutil/PersonBuilder.java +++ b/src/test/java/seedu/address/testutil/PersonBuilder.java @@ -124,6 +124,11 @@ public PersonBuilder withBooks(String ... books) { return this; } + public PersonBuilder withAnotherBook(String book) { + this.bookList.add(new Book(book)); + return this; + } + public Person build() { return new Person(name, phone, email, address, meritScore, bookList, tags); } From 2abd07ce529bfa57dfb3b54c87ff6a8f44bdce73 Mon Sep 17 00:00:00 2001 From: Ty-stan0417 Date: Mon, 15 Apr 2024 01:00:13 +0800 Subject: [PATCH 2/2] Fix checkstyle --- src/test/java/seedu/address/commons/core/GuiSettingsTest.java | 2 +- src/test/java/seedu/address/testutil/PersonBuilder.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/seedu/address/commons/core/GuiSettingsTest.java b/src/test/java/seedu/address/commons/core/GuiSettingsTest.java index 8334e378a1c..66be775eac0 100644 --- a/src/test/java/seedu/address/commons/core/GuiSettingsTest.java +++ b/src/test/java/seedu/address/commons/core/GuiSettingsTest.java @@ -1,9 +1,9 @@ package seedu.address.commons.core; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; diff --git a/src/test/java/seedu/address/testutil/PersonBuilder.java b/src/test/java/seedu/address/testutil/PersonBuilder.java index f210c658548..7ba82729cb1 100644 --- a/src/test/java/seedu/address/testutil/PersonBuilder.java +++ b/src/test/java/seedu/address/testutil/PersonBuilder.java @@ -124,6 +124,9 @@ public PersonBuilder withBooks(String ... books) { return this; } + /** + * Adds the {@code book} of the {@code Person} that we are building. + */ public PersonBuilder withAnotherBook(String book) { this.bookList.add(new Book(book)); return this;