Skip to content

Commit

Permalink
Merge pull request #192 from yeozongyao/branch-zongyao-testcodes
Browse files Browse the repository at this point in the history
Add Junit test cases for features by zong yao
  • Loading branch information
yeozongyao authored Apr 14, 2024
2 parents dd5ae54 + 44ca3a9 commit f66375c
Show file tree
Hide file tree
Showing 12 changed files with 608 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static boolean isByeCommand(String input) {
return "bye".equalsIgnoreCase(input);
}

private String processSelection(String newInput, Scanner scanner, BookList books)
String processSelection(String newInput, Scanner scanner, BookList books)
throws InvalidInputException, IOException {
int selection = Integer.parseInt(newInput);
if (selection == books.genreList.getAvailableGenres().size() + 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Scanner;

public class ParserGenre {
static void parseSetGenre(BookList books, String[] inputArray) throws IOException {
static void parseSetGenre(BookList books, String[] inputArray) throws IOException, InvalidCommandArgumentException {
Exceptions.validateCommandArguments(inputArray, 2, "The set-genre command requires " +
"at least a book index.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static int parseInteger(String input, String errorMessage) {
}

// Helper method to check if a string is numeric
private static boolean isNotNumeric(String strNum) {
static boolean isNotNumeric(String strNum) {
if (strNum == null) {
return true;
}
Expand Down
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"));
}
}
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"));
}

}
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"));
}

}
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));
}

}
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");
}
}


}

Loading

0 comments on commit f66375c

Please sign in to comment.