Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S2#96 from yeozongyao/branch-zong…
Browse files Browse the repository at this point in the history
…yao-setgenre

Bug fix for set-genre feature
  • Loading branch information
yeozongyao authored Apr 4, 2024
2 parents c0194a2 + d73c73c commit bf0a3ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void parseSetGenre(BookList books, String[] inputArray) {
return;
}

if (index < 0 || index >= books.getSize()) {
if (index < 0 || index > books.getSize()) {
System.out.println("Invalid book index. Please enter a valid index. Type 'list' to view the " +
"list of books.");
return;
Expand Down
43 changes: 25 additions & 18 deletions src/test/java/seedu/bookbuddy/ParserMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import seedu.bookbuddy.book.Genre;
import seedu.bookbuddy.book.Label;
import seedu.bookbuddy.book.Read;
import seedu.bookbuddy.book.Title;
Expand All @@ -11,7 +12,9 @@
import seedu.bookbuddy.booklist.BookListModifier;
import seedu.bookbuddy.parser.ParserMain;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -92,24 +95,28 @@ void parseLabelCommand() {
assertEquals("Great Book", Label.getLabel(books.getBook(1)));
}

// @Test
// void parseGenreCommand() {
// BookList books = new BookList();
// BookListModifier.addBook(books, "The Great Gatsby");
// // Simulate user input for genre selection "Classic"
// String simulatedUserInput = "6\nClassic\n"; // Assuming '3' is the option to add a new genre
// InputStream savedStandardInputStream = System.in;
// System.setIn(new ByteArrayInputStream(simulatedUserInput.getBytes()));
// ParserMain.parseCommand("set-genre 1", books); // Changed to fit your updated command-handling logic
// assertEquals("Classic", Genre.getGenre(books.getBook(1))); // Indexes are typically 0-based in lists
//
// BookListModifier.addBook(books, "Geronimo");
// String nextSimulatedUserInput = "3\n";
// System.setIn(new ByteArrayInputStream(nextSimulatedUserInput.getBytes()));
// ParserMain.parseCommand("set-genre 2", books);
// assertEquals("Mystery", Genre.getGenre(books.getBook(2)));
// System.setIn(savedStandardInputStream);
// }
@Test
void parseGenreCommand() {
BookList books = new BookList();
BookListModifier.addBook(books, "The Great Gatsby");
// Simulate user input for genre selection "Classic"
String simulatedUserInput = "6\nClassic\n"; // Assuming '3' is the option to add a new genre
InputStream savedStandardInputStream = System.in;
System.setIn(new ByteArrayInputStream(simulatedUserInput.getBytes()));
ParserMain.parseCommand("set-genre 1", books); // Changed to fit your updated command-handling logic
assertEquals("Classic", Genre.getGenre(books.getBook(1))); // Indexes are typically 0-based in lists

BookListModifier.addBook(books, "Geronimo");
String nextSimulatedUserInput = "3\n";
System.setIn(new ByteArrayInputStream(nextSimulatedUserInput.getBytes()));
ParserMain.parseCommand("set-genre 2", books);
assertEquals("Mystery", Genre.getGenre(books.getBook(2)));
System.setIn(savedStandardInputStream);

BookListModifier.addBook(books, "Tom And Jerry");
ParserMain.parseCommand("set-genre 3 Fantasy", books);
assertEquals("Fantasy", Genre.getGenre(books.getBook(3)));
}

@Test
void parseInvalidAddCommandThrowsException() {
Expand Down

0 comments on commit bf0a3ea

Please sign in to comment.