forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from yeozongyao/branch-zongyao-testcodes
Add Junit test cases for features by zong yao
- Loading branch information
Showing
12 changed files
with
608 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/java/seedu/bookbuddy/parser/parsercommands/ParserAddTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package seedu.bookbuddy.parser.parsercommands; | ||
|
||
import exceptions.InvalidCommandArgumentException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import seedu.bookbuddy.book.Title; | ||
import seedu.bookbuddy.booklist.BookList; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ParserAddTest { | ||
BookList books = new BookList(); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
books = new BookList(); | ||
} | ||
|
||
@Test | ||
void testValidBookAddition() { | ||
String[] inputArray = {"add", "1984"}; | ||
ParserAdd.executeParseAdd(books, inputArray); | ||
assertEquals(1, books.getSize()); | ||
assertEquals("1984", Title.getTitle(books.getBook(1))); | ||
} | ||
|
||
@Test | ||
void testInsufficientArguments() { | ||
String[] inputArray = {"add"}; | ||
Exception exception = assertThrows(InvalidCommandArgumentException.class, () -> { | ||
ParserAdd.executeParseAdd(books, inputArray); | ||
}); | ||
assertTrue(exception.getMessage().contains("The add Command requires a book title")); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/seedu/bookbuddy/parser/parsercommands/ParserLabelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package seedu.bookbuddy.parser.parsercommands; | ||
|
||
import exceptions.BookNotFoundException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import seedu.bookbuddy.book.Label; | ||
import seedu.bookbuddy.booklist.BookList; | ||
import seedu.bookbuddy.booklist.BookListModifier; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ParserLabelTest { | ||
BookList books = new BookList(); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
books = new BookList(); | ||
BookListModifier.addBook(books, "Geronimo"); | ||
} | ||
|
||
@Test | ||
void testValidLabelSetting() { | ||
String[] inputArray = {"label", "1 Good Book"}; | ||
ParserLabel.executeParseSetLabel(books, inputArray); | ||
assertEquals("Good Book", Label.getLabel(books.getBook(1))); | ||
} | ||
|
||
@Test | ||
void testInvalidIndexNonNumeric() { | ||
String[] inputArray = {"label", "abc haha"}; | ||
Exception exception = assertThrows(BookNotFoundException.class, () -> { | ||
ParserLabel.executeParseSetLabel(books, inputArray); | ||
}); | ||
assertTrue(exception.getMessage().contains("Book index out of range.")); | ||
} | ||
|
||
@Test | ||
void testInvalidIndexOutOfRange() { | ||
String[] inputArray = {"label", "2 haha"}; | ||
Exception exception = assertThrows(BookNotFoundException.class, () -> { | ||
ParserLabel.executeParseSetLabel(books, inputArray); }); | ||
assertTrue(exception.getMessage().contains("Book index out of range")); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
src/test/java/seedu/bookbuddy/parser/parsercommands/ParserListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package seedu.bookbuddy.parser.parsercommands; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import seedu.bookbuddy.book.Rating; | ||
import seedu.bookbuddy.booklist.BookList; | ||
import seedu.bookbuddy.booklist.BookListModifier; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ParserListTest { | ||
BookList books = new BookList(); | ||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
private final PrintStream originalOut = System.out; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
System.setOut(new PrintStream(outContent)); | ||
books = new BookList(); | ||
BookListModifier.addBook(books, "Geronimo"); | ||
BookListModifier.addBook(books, "Percy Jackson"); | ||
|
||
} | ||
|
||
@AfterEach | ||
void restoreStreams() { | ||
System.setOut(originalOut); | ||
} | ||
|
||
@Test | ||
void testListAllBooks() { | ||
String[] inputArray = {"list"}; | ||
ParserList.executeParseList(books, inputArray, "list"); | ||
assertTrue(outContent.toString().contains("Geronimo")); | ||
} | ||
|
||
@Test | ||
void testListBooksByRating() { | ||
String[] inputArray = {"list-rated"}; | ||
// Assuming book ratings are set and expected to be sorted by ratings | ||
Rating.setRating(books.getBook(2), 5); | ||
ParserList.executeParseList(books, inputArray, "list-rated"); | ||
String expectedOutput = "_____________\n" + | ||
"okii added [Geronimo] to the list.\n" + | ||
"remember to read it soon....\n" + | ||
"_____________\n" + | ||
"_____________\n" + | ||
"okii added [Percy Jackson] to the list.\n" + | ||
" remember to read it soon....\n" + | ||
"_____________\n" + | ||
"Books sorted by rating:\n" + | ||
"Percy Jackson - 5\n" + | ||
"Geronimo - Not Rated"; | ||
|
||
assertEquals(expectedOutput.trim().replaceAll("\\s+", "|"), | ||
outContent.toString().trim().replaceAll("\\s+", "|")); | ||
} | ||
@Test | ||
void testListGenres() { | ||
String[] inputArray = {"list-genre"}; | ||
// Assuming genres are set up | ||
books.genreList.getAvailableGenres().add("Fiction"); | ||
ParserList.executeParseList(books, inputArray, "list-genre"); | ||
assertTrue(outContent.toString().contains("Fiction")); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/seedu/bookbuddy/parser/parsercommands/ParserRemoveGenreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package seedu.bookbuddy.parser.parsercommands; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import seedu.bookbuddy.booklist.BookList; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ParserRemoveGenreTest { | ||
BookList books = new BookList(); | ||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
private final PrintStream originalOut = System.out; | ||
@BeforeEach | ||
void setUp() { | ||
books = new BookList(); | ||
// To add 3 genres to the arraylist in addition to the default | ||
for (int i = 1; i <= 3; i++) { | ||
books.genreList.getAvailableGenres().add("Genre " + i); | ||
} | ||
System.setOut(new PrintStream(outContent)); | ||
} | ||
@AfterEach | ||
void restoreStreams() { | ||
System.setOut(originalOut); | ||
} | ||
|
||
@Test | ||
void testValidGenreRemoval() { | ||
String[] inputArray = {"remove-genre", "6"}; | ||
ParserRemoveGenre.executeParseRemove(books, inputArray); | ||
assertFalse(books.genreList.getAvailableGenres().contains("Genre 1")); | ||
} | ||
|
||
@Test | ||
void testNonNumericIndex() { | ||
String[] inputArray = {"remove-genre", "abc"}; | ||
ParserRemoveGenre.executeParseRemove(books, inputArray); | ||
String expectedOutput = "abc is not a valid index format"; | ||
assertTrue(outContent.toString().contains(expectedOutput)); | ||
} | ||
@Test | ||
void testIndexOutOfBounds() { | ||
String[] inputArray = {"remove-genre", "11"}; | ||
ParserRemoveGenre.executeParseRemove(books, inputArray); | ||
String expectedOutput = "Genre list is out of bounds"; | ||
assertTrue(outContent.toString().contains(expectedOutput)); | ||
} | ||
|
||
} |
122 changes: 122 additions & 0 deletions
122
src/test/java/seedu/bookbuddy/parser/parsercommands/parsegenre/InputLooperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package seedu.bookbuddy.parser.parsercommands.parsegenre; | ||
|
||
import exceptions.InvalidInputException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import seedu.bookbuddy.booklist.BookList; | ||
import seedu.bookbuddy.booklist.BookListModifier; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.Scanner; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
|
||
class InputLooperTest { | ||
BookList books = new BookList(); | ||
Scanner scanner = new Scanner(System.in); | ||
private InputLooper inputLooper; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
inputLooper = new InputLooper(); | ||
books = new BookList(); | ||
scanner = new Scanner(System.in); | ||
} | ||
|
||
@Test | ||
void testValidSelection() throws Exception { | ||
String input = "1"; | ||
scanner = new Scanner(new ByteArrayInputStream(input.getBytes())); | ||
String result = inputLooper.processSelection(input, scanner, books); | ||
assertEquals("Fiction", result); | ||
} | ||
|
||
@Test | ||
void testInvalidSelection() { | ||
String input = "20"; // Beyond available genres size | ||
scanner = new Scanner(new ByteArrayInputStream(input.getBytes())); | ||
|
||
Exception exception = assertThrows(InvalidInputException.class, () -> { | ||
inputLooper.processSelection(input, scanner, books); | ||
}); | ||
|
||
assertTrue(exception.getMessage().contains("is an invalid selection")); | ||
} | ||
|
||
@Test | ||
void testEmptyInputHandling() throws Exception { | ||
String input = "\n\n1"; | ||
ByteArrayInputStream bais = new ByteArrayInputStream(input.getBytes()); | ||
Scanner scanner = new Scanner(bais); | ||
books.genreList.setAvailableGenres(new ArrayList<>(List.of("Fiction"))); | ||
assertEquals("", inputLooper.inputLooper("", scanner, books)); | ||
} | ||
|
||
@Test | ||
void testExitCommand() throws Exception { | ||
String input = "exit\n"; | ||
System.setIn(new ByteArrayInputStream(input.getBytes())); | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
assertNull(inputLooper.inputLooper(null, scanner, books)); | ||
} | ||
|
||
@Test | ||
void testByeCommand() throws Exception { | ||
String input = "bye\n"; | ||
System.setIn(new ByteArrayInputStream(input.getBytes())); | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
assertNull(inputLooper.inputLooper(null, scanner, books)); | ||
} | ||
|
||
@Test | ||
void testSetGenreToFiction() throws Exception { | ||
String input = "1\n"; | ||
System.setIn(new ByteArrayInputStream(input.getBytes())); | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
assertEquals("Fiction", inputLooper.inputLooper(null, scanner, books)); | ||
} | ||
@Test | ||
void testAddNewGenre() throws Exception { | ||
String initialInput = "3"; | ||
String genreInput = "Mystery"; | ||
String combinedInput = genreInput + "\n"; | ||
System.setIn(new ByteArrayInputStream(combinedInput.getBytes())); | ||
Scanner scanner = new Scanner(System.in); | ||
books.genreList.setAvailableGenres(new ArrayList<>(List.of("Fantasy", "Science Fiction"))); | ||
assertEquals("Mystery", inputLooper.processSelection(initialInput, scanner, books)); | ||
assertTrue(books.genreList.getAvailableGenres().contains("Mystery")); | ||
} | ||
|
||
@Test | ||
void setGenreBookSelectIndexOutOfBounds() { | ||
BookList books = new BookList(); | ||
BookListModifier.addBook(books, "Don Quixote"); | ||
try { | ||
ParserGenre.executeParseSetGenre(books, new String[]{"set-genre", "1"}); | ||
fail(); | ||
} catch (Exception ignored) { | ||
System.out.println("Test in progress"); | ||
} | ||
try { | ||
ParserGenre.executeParseSetGenre(books, new String[]{"set-genre", "-1"}); | ||
fail(); | ||
} catch (Exception ignored) { | ||
System.out.println("Test in progress"); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
Oops, something went wrong.