Skip to content

Commit

Permalink
Added unit tests for Lesson and Subject classes
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlengch committed Mar 20, 2024
1 parent 282e984 commit 1f5277d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AddCommand extends Command {
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_SUBJECT + "Maths "
+ PREFIX_REMARK + "He is a slow learner. "
+ PREFIX_LESSON + "Maths|23-05-2024|10:00";
+ PREFIX_LESSON + "Maths|23-05-2024|10:00|1";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/seedu/address/model/person/Lesson.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model.person;

import static java.util.Objects.requireNonNull;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -11,11 +13,12 @@
*/
public class Lesson {
public static final String MESSAGE_CONSTRAINTS =
"Lessons should only be of the form subject|dd-MM-yyyy|hh:mm|0/1, where subject contains only alphabeths"
"Lessons must be of the form subject|dd-MM-yyyy|hh:mm|0/1, where subject contains only alphabeths"
+ " and spaces, and indicate lesson incomplete/completed with 0 or 1 respectively.";
public static final String VALIDATION_REGEX = "^[a-zA-Z][a-zA-Z ]*$";
public static final String DATE_REGEX = "\\d{2}-\\d{2}-\\d{4}";
public static final String TIME_REGEX = "\\d{2}:\\d{2}";
public static final String INT_REGEX = "[01]";
private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
private String value;
Expand All @@ -30,15 +33,23 @@ public class Lesson {
* @param lesson The details of the lesson.
*/
public Lesson(String lesson) {
requireNonNull(lesson);
// split lesson into its attributes based on "|" character
String[] lessonDetails = lesson.trim().split("\\|");
// throws IllegalArgumentException if the lesson is not in the correct format.
if (lessonDetails.length != 4) {
throw new IllegalArgumentException(MESSAGE_CONSTRAINTS);
}
String subjectDetail = lessonDetails[0];
String dateDetail = lessonDetails[1];
String timeDetail = lessonDetails[2];
int isCompletedDetail = Integer.parseInt(lessonDetails[3]);
// assign the attributes to the lesson
this.subject = new Subject(subjectDetail);
this.date = LocalDate.parse(dateDetail, dateFormatter);
this.time = LocalTime.parse(timeDetail, timeFormatter);
this.isCompleted = isCompletedDetail;
// parseable form of lesson (temporary)
this.value = subjectDetail + "|" + dateDetail + "|" + timeDetail + "|" + isCompleted;
}

Expand All @@ -59,7 +70,8 @@ public static boolean isValidLesson(String lessonValue) {
}
return lessonDetails[0].matches(VALIDATION_REGEX)
&& lessonDetails[1].matches(DATE_REGEX)
&& lessonDetails[2].matches(TIME_REGEX);
&& lessonDetails[2].matches(TIME_REGEX)
&& lessonDetails[3].matches(INT_REGEX);
}

/**
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/seedu/address/model/person/Remark.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class Remark {
public static final String MESSAGE_CONSTRAINTS = "Remarks can take any values, and it should not be blank";
public static final String VALIDATION_REGEX = "\\S.*";

public final String value;
/**
* Constructs a {@code Remark}.
Expand All @@ -19,13 +19,6 @@ public Remark(String remark) {
requireNonNull(remark);
value = remark;
}
/**
* Returns true if a given string is a valid remark.
*/
public static boolean isValidRemark(String remark) {
String trimmedRemark = remark.trim();
return trimmedRemark.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
Expand Down
62 changes: 0 additions & 62 deletions src/main/java/seedu/address/model/tag/Tag.java

This file was deleted.

59 changes: 59 additions & 0 deletions src/test/java/seedu/address/model/person/LessonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package seedu.address.model.person;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;

public class LessonTest {

@Test
public void constructor_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new Lesson(null));
}

@Test
public void constructor_invalidLesson_throwsIllegalArgumentException() {
String invalidLesson = "";
assertThrows(IllegalArgumentException.class, () -> new Lesson(invalidLesson));
}

@Test
public void isValidLesson() {
// null lesson
assertThrows(NullPointerException.class, () -> Lesson.isValidLesson(null));

// invalid lessons
assertFalse(Lesson.isValidLesson("")); // empty string
assertFalse(Lesson.isValidLesson(" ")); // spaces only
assertFalse(Lesson.isValidLesson("Math|invalidDate|09:00|0")); // invalid date format
assertFalse(Lesson.isValidLesson("Math|01-01-2023|invalidTime|0")); // invalid time format
assertFalse(Lesson.isValidLesson("Math|01-01-2023|09:00|2")); // invalid isCompleted value

// valid lessons
assertTrue(Lesson.isValidLesson("Math|01-01-2023|09:00|0"));
assertTrue(Lesson.isValidLesson("Science|01-01-2023|10:00|1"));
}

@Test
public void equals() {
Lesson lesson = new Lesson("Math|01-01-2023|09:00|0");

// same values -> returns true
assertTrue(lesson.equals(new Lesson("Math|01-01-2023|09:00|0")));

// same object -> returns true
assertEquals(lesson, lesson);

// null -> returns false
assertFalse(lesson.equals(null));

// different types -> returns false
assertFalse(lesson.equals(5.0f));

// different values -> returns false
assertFalse(lesson.equals(new Lesson("Science|01-01-2023|09:00|0")));
}
}
61 changes: 61 additions & 0 deletions src/test/java/seedu/address/model/person/SubjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package seedu.address.model.person;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;

public class SubjectTest {

@Test
public void constructor_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new Subject(null));
}

@Test
public void constructor_invalidSubject_throwsIllegalArgumentException() {
String invalidSubject = "";
assertThrows(IllegalArgumentException.class, () -> new Subject(invalidSubject));
}

@Test
public void isValidSubject() {
// null subject
assertThrows(NullPointerException.class, () -> Subject.isValidSubject(null));

// invalid subjects
assertFalse(Subject.isValidSubject("")); // empty string
assertFalse(Subject.isValidSubject(" ")); // spaces only
assertFalse(Subject.isValidSubject("123")); // numeric characters
assertFalse(Subject.isValidSubject("!@#$%")); // special characters

// valid subjects
assertTrue(Subject.isValidSubject("Math"));
assertTrue(Subject.isValidSubject("English Literature"));
assertTrue(Subject.isValidSubject("Computer Science"));
assertTrue(Subject.isValidSubject("Social Studies"));
}

@Test
public void equals() {
Subject subject = new Subject("Math");

// same values -> returns true
assertEquals(subject, new Subject("Math"));

// same object -> returns true
assertEquals(subject, subject);

// null -> returns false
assertNotEquals(null, subject);

// different types -> returns false
assertNotEquals(5.0f, subject, String.valueOf(0.0));

// different values -> returns false
assertNotEquals(subject, new Subject("Science"));
}
}
26 changes: 0 additions & 26 deletions src/test/java/seedu/address/model/tag/TagTest.java

This file was deleted.

0 comments on commit 1f5277d

Please sign in to comment.