From 1f5277db3131fa1893f4c47111469715b949cfd9 Mon Sep 17 00:00:00 2001 From: justinlengch Date: Thu, 21 Mar 2024 05:42:11 +0800 Subject: [PATCH] Added unit tests for Lesson and Subject classes --- .../address/logic/commands/AddCommand.java | 2 +- .../seedu/address/model/person/Lesson.java | 16 ++++- .../seedu/address/model/person/Remark.java | 9 +-- .../java/seedu/address/model/tag/Tag.java | 62 ------------------- .../address/model/person/LessonTest.java | 59 ++++++++++++++++++ .../address/model/person/SubjectTest.java | 61 ++++++++++++++++++ .../java/seedu/address/model/tag/TagTest.java | 26 -------- 7 files changed, 136 insertions(+), 99 deletions(-) delete mode 100644 src/main/java/seedu/address/model/tag/Tag.java create mode 100644 src/test/java/seedu/address/model/person/LessonTest.java create mode 100644 src/test/java/seedu/address/model/person/SubjectTest.java delete mode 100644 src/test/java/seedu/address/model/tag/TagTest.java diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index a7c2692b623..1c8242b8f2a 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -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"; diff --git a/src/main/java/seedu/address/model/person/Lesson.java b/src/main/java/seedu/address/model/person/Lesson.java index 922ecb098c3..4901190e0b4 100644 --- a/src/main/java/seedu/address/model/person/Lesson.java +++ b/src/main/java/seedu/address/model/person/Lesson.java @@ -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; @@ -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; @@ -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; } @@ -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); } /** diff --git a/src/main/java/seedu/address/model/person/Remark.java b/src/main/java/seedu/address/model/person/Remark.java index ed9aa1e99ad..d6918654603 100644 --- a/src/main/java/seedu/address/model/person/Remark.java +++ b/src/main/java/seedu/address/model/person/Remark.java @@ -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}. @@ -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() { diff --git a/src/main/java/seedu/address/model/tag/Tag.java b/src/main/java/seedu/address/model/tag/Tag.java deleted file mode 100644 index f1a0d4e233b..00000000000 --- a/src/main/java/seedu/address/model/tag/Tag.java +++ /dev/null @@ -1,62 +0,0 @@ -package seedu.address.model.tag; - -import static java.util.Objects.requireNonNull; -import static seedu.address.commons.util.AppUtil.checkArgument; - -/** - * Represents a Tag in the address book. - * Guarantees: immutable; name is valid as declared in {@link #isValidTagName(String)} - */ -public class Tag { - - public static final String MESSAGE_CONSTRAINTS = "Tags names should be alphanumeric"; - public static final String VALIDATION_REGEX = "\\p{Alnum}+"; - - public final String tagName; - - /** - * Constructs a {@code Tag}. - * - * @param tagName A valid tag name. - */ - public Tag(String tagName) { - requireNonNull(tagName); - checkArgument(isValidTagName(tagName), MESSAGE_CONSTRAINTS); - this.tagName = tagName; - } - - /** - * Returns true if a given string is a valid tag name. - */ - public static boolean isValidTagName(String test) { - return test.matches(VALIDATION_REGEX); - } - - @Override - public boolean equals(Object other) { - if (other == this) { - return true; - } - - // instanceof handles nulls - if (!(other instanceof Tag)) { - return false; - } - - Tag otherTag = (Tag) other; - return tagName.equals(otherTag.tagName); - } - - @Override - public int hashCode() { - return tagName.hashCode(); - } - - /** - * Format state as text for viewing. - */ - public String toString() { - return '[' + tagName + ']'; - } - -} diff --git a/src/test/java/seedu/address/model/person/LessonTest.java b/src/test/java/seedu/address/model/person/LessonTest.java new file mode 100644 index 00000000000..f971833989c --- /dev/null +++ b/src/test/java/seedu/address/model/person/LessonTest.java @@ -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"))); + } +} diff --git a/src/test/java/seedu/address/model/person/SubjectTest.java b/src/test/java/seedu/address/model/person/SubjectTest.java new file mode 100644 index 00000000000..569ec321cbc --- /dev/null +++ b/src/test/java/seedu/address/model/person/SubjectTest.java @@ -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")); + } +} diff --git a/src/test/java/seedu/address/model/tag/TagTest.java b/src/test/java/seedu/address/model/tag/TagTest.java deleted file mode 100644 index 64d07d79ee2..00000000000 --- a/src/test/java/seedu/address/model/tag/TagTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package seedu.address.model.tag; - -import static seedu.address.testutil.Assert.assertThrows; - -import org.junit.jupiter.api.Test; - -public class TagTest { - - @Test - public void constructor_null_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new Tag(null)); - } - - @Test - public void constructor_invalidTagName_throwsIllegalArgumentException() { - String invalidTagName = ""; - assertThrows(IllegalArgumentException.class, () -> new Tag(invalidTagName)); - } - - @Test - public void isValidTagName() { - // null tag name - assertThrows(NullPointerException.class, () -> Tag.isValidTagName(null)); - } - -}