Skip to content

Commit

Permalink
Merge pull request #305 from Ty-stan0417/branch-IncreaseCoverage
Browse files Browse the repository at this point in the history
Add more test cases to increase coverage
  • Loading branch information
jinhanfromNUS authored Apr 14, 2024
2 parents 4fff878 + 2abd07c commit 79c76cc
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 14 deletions.
12 changes: 0 additions & 12 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,6 @@ public ArrayList<Book> 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 = "";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/storage/LibraryStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/seedu/address/commons/core/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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));
}


}
17 changes: 17 additions & 0 deletions src/test/java/seedu/address/commons/core/GuiSettingsTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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;

import org.junit.jupiter.api.Test;

Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@

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;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
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;
Expand Down Expand Up @@ -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";
Expand All @@ -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), ()
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/seedu/address/model/AddressBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/seedu/address/model/UserPrefsTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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"));
}

}
79 changes: 79 additions & 0 deletions src/test/java/seedu/address/model/person/MeritScoreTest.java
Original file line number Diff line number Diff line change
@@ -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)));
}
}
10 changes: 10 additions & 0 deletions src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/seedu/address/storage/LibraryStorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/seedu/address/testutil/PersonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ 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;
}

public Person build() {
return new Person(name, phone, email, address, meritScore, bookList, tags);
}
Expand Down

0 comments on commit 79c76cc

Please sign in to comment.