Skip to content

Commit

Permalink
Add JUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren159 committed Sep 1, 2023
1 parent 29d04b9 commit fce4966
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/test/java/duke/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package duke;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;

import duke.Parser;
import duke.command.AddCommand;
import duke.command.DeleteCommand;
import duke.command.ExitCommand;
import duke.command.ListCommand;
import duke.command.MarkCommand;
import duke.command.UnmarkCommand;
import duke.command.Command;
import duke.exception.InvalidArgumentException;
import org.junit.jupiter.api.Test;

public class ParserTest {

@Test
public void testParseListCommand() throws InvalidArgumentException {
Command command = Parser.parse("list");
assertTrue(command instanceof ListCommand);
}

@Test
public void testParseDeleteCommand() throws InvalidArgumentException {
Command command = Parser.parse("delete 1");
assertTrue(command instanceof DeleteCommand);
}

@Test
public void testParseMarkCommand() throws InvalidArgumentException {
Command command = Parser.parse("mark 1");
assertTrue(command instanceof MarkCommand);
}

@Test
public void testParseUnmarkCommand() throws InvalidArgumentException {
Command command = Parser.parse("unmark 1");
assertTrue(command instanceof UnmarkCommand);
}

@Test
public void testParseAddCommand() throws InvalidArgumentException {
Command command = Parser.parse("todo Test");
assertTrue(command instanceof AddCommand);
}

@Test
public void testParseExitCommand() throws InvalidArgumentException {
Command command = Parser.parse("bye");
assertTrue(command instanceof ExitCommand);
}

@Test
public void testInvalidCommand() {
assertThrows(InvalidArgumentException.class, () -> Parser.parse("invalid_command"));
}
}
48 changes: 48 additions & 0 deletions src/test/java/duke/UiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package duke;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class UiTest {
private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

@BeforeEach
public void setUp() {
System.setOut(new PrintStream(outputStreamCaptor));
}

@Test
public void testShowWelcome() {
Ui ui = new Ui();
ui.showWelcome();
String expected = "__________________________________________________________________________" +
" ___ _ ___ ________ __ ____ __________ ____ _ __\n"
+ " / | / | / / | / / __ \\ \\/ / / __ \\ /_ __/ __ \\/ __ \\/ | / /\n"
+ " / /| | / |/ / |/ / / / /\\ /_____/ / / /_____/ / / /_/ / / / / |/ / \n"
+ " / ___ |/ /| / /| / /_/ / / /_____/ /_/ /_____/ / / _, _/ /_/ / /| / \n"
+ "/_/ |_/_/ |_/_/ |_/\\____/ /_/ \\____/ /_/ /_/ |_|\\____/_/ |_/ \n"
+ "Hello! I'm ANNOY-O-TRON!\nWhat can I do for you?\n"
+ "__________________________________________________________________________";
assertEquals(expected, outputStreamCaptor.toString().trim());
}

@Test
public void testShowBye() {
Ui ui = new Ui();
ui.showBye();
String expected = "Bye. Hope to see you again soon!";
assertEquals(expected, outputStreamCaptor.toString().trim());
}


@AfterEach
public void tearDown() {
System.setOut(standardOut);
}
}
21 changes: 21 additions & 0 deletions src/test/java/duke/task/ToDoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package duke.task;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ToDoTest {
@Test
public void testToString() {
Todo todo = new Todo("Sample Task");
String expected = "[T][ ] Sample Task";
assertEquals(expected, todo.toString());
}

@Test
public void testToFileString() {
Todo todo = new Todo("Sample Task");
String expected = "T | 0 | Sample Task";
assertEquals(expected, todo.toFileString());
}
}

0 comments on commit fce4966

Please sign in to comment.