Skip to content

Commit

Permalink
Merge branch 'Branch-JUnit'
Browse files Browse the repository at this point in the history
  • Loading branch information
DesSnowy committed Aug 30, 2024
2 parents d2194f5 + b2278c4 commit d56f589
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/main/java/snowy/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public Deadline(String name, String date) throws SnowyException {
try {
this.date = LocalDate.parse(date);
} catch (DateTimeException e) {
throw new SnowyException();
throw new SnowyException("Wrong date format");
}
}

@Override
public String toString() {
String temp = super.toString();
return String.format("[D]%s (by %s )", temp, date.format(super.FORMATTER));
return String.format("[D]%s (by %s)", temp, date.format(super.FORMATTER));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/snowy/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Event(String name, String fromDate, String toDate) throws SnowyException
this.fromDate = LocalDate.parse(fromDate);
this.toDate = LocalDate.parse(toDate);
} catch (DateTimeException e) {
throw new SnowyException();
throw new SnowyException("Wrong date format");
}

}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/snowy/Snowy.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private Snowy(String filePath) {
this.tasks = new TaskList(storage.load());
} catch (SnowyException e) {
this.tasks = new TaskList();
System.out.println(e.getMessage());
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/snowy/SnowyException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package snowy;

public class SnowyException extends Exception {
public class SnowyException extends RuntimeException {
public SnowyException(String message) {
super(message);
}
}
4 changes: 2 additions & 2 deletions src/main/java/snowy/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ArrayList<String> load() throws SnowyException{


} catch (IOException e) {
throw new SnowyException();
throw new SnowyException("Unable to create new file");
}
return lines;
}
Expand All @@ -36,7 +36,7 @@ public void save(String saveLines) throws SnowyException{
writer.write(saveLines);
writer.close();
} catch (IOException e) {
throw new SnowyException();
throw new SnowyException("Unable to update file");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/snowy/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class Task {
abstract public class Task {
private boolean isCompleted = false;

public final DateTimeFormatter FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/snowy/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void initializeTask(String description) {

public void addToDo(String description) throws SnowyException{
if (description.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Todo");
}
Task newTask = new ToDo(description);
tasks.add(newTask);
Expand All @@ -82,23 +82,23 @@ public void addToDo(String description) throws SnowyException{

public void addDeadline(String description) throws SnowyException{
if (description.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Deadline");
}

int byIndex = description.indexOf("/by ");

if (byIndex == -1) {
throw new SnowyException();
throw new SnowyException("Invalid input for Deadline");
}
String deadlineName = description.substring(0, byIndex).trim();
String date = description.substring(byIndex + 4);

if (deadlineName.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Deadline");
}

if (date.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Deadline");
}
Task newTask = new Deadline(deadlineName, date);
tasks.add(newTask);
Expand All @@ -107,26 +107,26 @@ public void addDeadline(String description) throws SnowyException{

public void addEvent(String description) throws SnowyException{
if (description.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Event");
}
int fromIndex = description.indexOf("/from ");
int toIndex = description.indexOf("/to ");

if (toIndex == -1 || fromIndex == -1) {
throw new SnowyException();
throw new SnowyException("Invalid input for Event");
}

String eventName = description.substring(0, fromIndex);
String fromDate = description.substring(fromIndex + 6, toIndex).trim();
String toDate = description.substring(toIndex + 4);
if (eventName.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Event");
}
if (fromDate.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Event");
}
if (toDate.isEmpty()) {
throw new SnowyException();
throw new SnowyException("Invalid input for Event");
}
Task newTask = new Event(eventName, fromDate, toDate);
tasks.add(newTask);
Expand All @@ -137,7 +137,7 @@ public Task deleteTask(int index) throws SnowyException {
try {
return tasks.remove(index - 1);
} catch (IndexOutOfBoundsException e) {
throw new SnowyException();
throw new SnowyException("Invalid index input");
}
}

Expand All @@ -146,7 +146,7 @@ public Task markTask(int index) throws SnowyException {
tasks.get(index - 1).markComplete();
return tasks.get(index - 1);
} catch (IndexOutOfBoundsException e) {
throw new SnowyException();
throw new SnowyException("Invalid index input");
}
}

Expand All @@ -155,7 +155,7 @@ public Task unmarkTask(int index) throws SnowyException {
tasks.get(index - 1).markIncomplete();
return tasks.get(index - 1);
} catch (IndexOutOfBoundsException e) {
throw new SnowyException();
throw new SnowyException("Invalid index input");
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/java/snowy/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package snowy;

import org.junit.jupiter.api.Test;

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

public class DeadlineTest {

@Test
public void toString_normalName_success() {
assertEquals("[D][ ] Return Book (by September 11, 2024)",
new Deadline("Return Book", "2024-09-11").toString());
}

@Test
public void toFileStorage_normalName_success() {
assertEquals("D|0|Return Book|2024-09-11",
new Deadline("Return Book", "2024-09-11").toFileStorage());
}

@Test void constructor_invalidDate_exceptionThrown() {
try {
new Deadline("Return book", "12-123-1234");
fail();
} catch (SnowyException e) {
assertEquals("Wrong date format", e.getMessage());
}
}
}
29 changes: 29 additions & 0 deletions src/test/java/snowy/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package snowy;

import org.junit.jupiter.api.Test;

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

public class EventTest {
@Test
public void toString_normalName_success() {
assertEquals("[E][ ] Orientation Camp (from September 11, 2024 to: September 12, 2024)",
new Event("Orientation Camp", "2024-09-11", "2024-09-12").toString());
}

@Test
public void toFileStorage_normalName_success() {
assertEquals("[E][ ] Orientation Camp (from September 11, 2024 to: September 12, 2024)",
new Event("Orientation Camp", "2024-09-11", "2024-09-12").toString());
}

@Test void constructor_invalidDate_exceptionThrown() {
try {
new Event("Return book", "12-123-1234", "12-123-1234");
fail();
} catch (SnowyException e) {
assertEquals("Wrong date format", e.getMessage());
}
}
}
16 changes: 16 additions & 0 deletions src/test/java/snowy/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package snowy;

import org.junit.jupiter.api.Test;

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

public class ParserTest {
@Test
public void parse_allCapitalCommand_returnLowerCase() {
assertArrayEquals(new String[] {"todo", "read book"}, Parser.parse("TODO read book"));
}

public void parse_capitalDescription_retainCapital() {
assertArrayEquals(new String[] {"todo", "Read Book"}, Parser.parse("todo Read Book"));
}
}
101 changes: 101 additions & 0 deletions src/test/java/snowy/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package snowy;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;

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

public class TaskListTest {

@Test
public void toString_correctInput_success() {
ArrayList<String> lines = new ArrayList<>();
lines.add("T|1|read book");
lines.add("D|0|return book|2024-09-05");
lines.add("E|0|Camp|2024-09-01|2024-09-02");
TaskList tasks = new TaskList(lines);

assertEquals("1. [T][X] read book\n" +
"2. [D][ ] return book (by September 5, 2024)\n" +
"3. [E][ ] Camp (from September 1, 2024 to: September 2, 2024)\n"
, tasks.toString());
}

@Test
public void toSaveString_correctInput_success() {
ArrayList<String> lines = new ArrayList<>();
lines.add("T|1|read book");
lines.add("D|0|return book|2024-09-05");
lines.add("E|0|Camp|2024-09-01|2024-09-02");
TaskList tasks = new TaskList(lines);

assertEquals("T|1|read book\n" +
"D|0|return book|2024-09-05\n" +
"E|0|Camp|2024-09-01|2024-09-02\n"
, tasks.toSaveString());
}

@Test
public void addToDo_correctInput_success() {
TaskList tasks = new TaskList();
tasks.addToDo("Read Book");

assertEquals("1. [T][ ] Read Book\n", tasks.toString());
}

@Test
public void addToDo_invalidInput_exceptionThrown() {
try {
TaskList tasks = new TaskList();
tasks.addToDo("");
fail();
} catch (SnowyException e) {
assertEquals("Invalid input for Todo", e.getMessage());
}
}

@Test
public void addDeadline_correctInput_success() {
TaskList tasks = new TaskList();
tasks.addDeadline("Return Book /by 2024-09-05");

assertEquals("1. [D][ ] Return Book (by September 5, 2024)\n", tasks.toString());
}

@Test
public void addDeadline_emptyInput_exceptionThrown() {
try {
TaskList tasks = new TaskList();
tasks.addDeadline("");
fail();
} catch (SnowyException e) {
assertEquals("Invalid input for Deadline", e.getMessage());
}
}

@Test
public void addDeadline_missingBy_exceptionThrown() {
try {
TaskList tasks = new TaskList();
tasks.addDeadline("Return book 2024-09-05");
fail();
} catch (SnowyException e) {
assertEquals("Invalid input for Deadline", e.getMessage());
}
}

@Test
public void addDeadline_InvalidDate_exceptionThrown() {
try {
TaskList tasks = new TaskList();
tasks.addDeadline("Return book /by 123-09-05");
fail();
} catch (SnowyException e) {
assertEquals("Wrong date format", e.getMessage());
}
}


}
16 changes: 16 additions & 0 deletions src/test/java/snowy/ToDoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package snowy;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
public class ToDoTest {
@Test
public void toString_normalName_success() {
assertEquals("[T][ ] Read Book",new ToDo("Read Book").toString());
}

@Test
public void toFileStorage_normalName_success() {
assertEquals("T|0|Read Book",new ToDo("Read Book").toFileStorage());
}
}

0 comments on commit d56f589

Please sign in to comment.