From a74cc95881c51ec7c7e958c6f150ee0a1daa4c68 Mon Sep 17 00:00:00 2001 From: Miao Ling Date: Mon, 22 May 2017 08:21:39 +0800 Subject: [PATCH] [#401] Add unit tests for ParserUtil#parseX() (#425) --- .../address/logic/parser/ParserUtilTest.java | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 src/test/java/seedu/address/logic/parser/ParserUtilTest.java diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java new file mode 100644 index 00000000000..02e0ff5fab1 --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -0,0 +1,165 @@ +package seedu.address.logic.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.model.person.Address; +import seedu.address.model.person.Email; +import seedu.address.model.person.Name; +import seedu.address.model.person.Phone; +import seedu.address.model.tag.Tag; + +public class ParserUtilTest { + private static final String INVALID_NAME = "R@chel"; + private static final String INVALID_PHONE = "+651234"; + private static final String INVALID_ADDRESS = " "; + private static final String INVALID_EMAIL = "example.com"; + private static final String INVALID_TAG = "#friend"; + + private static final String VALID_NAME = "Rachel Walker"; + private static final String VALID_PHONE = "123456"; + private static final String VALID_ADDRESS = "123 Main Street #0505"; + private static final String VALID_EMAIL = "rachel@example.com"; + private static final String VALID_TAG_1 = "friend"; + private static final String VALID_TAG_2 = "neighbour"; + + @Rule + public final ExpectedException thrown = ExpectedException.none(); + + @Test + public void parseName_null_throwsAssertionError() throws Exception { + thrown.expect(AssertionError.class); + ParserUtil.parseName(null); + } + + @Test + public void parseName_invalidValue_throwsIllegalValueException() throws Exception { + thrown.expect(IllegalValueException.class); + ParserUtil.parseName(Optional.of(INVALID_NAME)); + } + + @Test + public void parseName_optionalEmpty_returnsOptionalEmpty() throws Exception { + assertFalse(ParserUtil.parseName(Optional.empty()).isPresent()); + } + + @Test + public void parseName_validValue_returnsName() throws Exception { + Name expectedName = new Name(VALID_NAME); + Optional actualName = ParserUtil.parseName(Optional.of(VALID_NAME)); + + assertEquals(expectedName, actualName.get()); + } + + @Test + public void parsePhone_null_throwsAssertionError() throws Exception { + thrown.expect(AssertionError.class); + ParserUtil.parsePhone(null); + } + + @Test + public void parsePhone_invalidValue_throwsIllegalValueException() throws Exception { + thrown.expect(IllegalValueException.class); + ParserUtil.parsePhone(Optional.of(INVALID_PHONE)); + } + + @Test + public void parsePhone_optionalEmpty_returnsOptionalEmpty() throws Exception { + assertFalse(ParserUtil.parsePhone(Optional.empty()).isPresent()); + } + + @Test + public void parsePhone_validValue_returnsPhone() throws Exception { + Phone expectedPhone = new Phone(VALID_PHONE); + Optional actualPhone = ParserUtil.parsePhone(Optional.of(VALID_PHONE)); + + assertEquals(expectedPhone, actualPhone.get()); + } + + @Test + public void parseAddress_null_throwsAssertionError() throws Exception { + thrown.expect(AssertionError.class); + ParserUtil.parseAddress(null); + } + + @Test + public void parseAddress_invalidValue_throwsIllegalValueException() throws Exception { + thrown.expect(IllegalValueException.class); + ParserUtil.parseAddress(Optional.of(INVALID_ADDRESS)); + } + + @Test + public void parseAddress_optionalEmpty_returnsOptionalEmpty() throws Exception { + assertFalse(ParserUtil.parseAddress(Optional.empty()).isPresent()); + } + + @Test + public void parseAddress_validValue_returnsAddress() throws Exception { + Address expectedAddress = new Address(VALID_ADDRESS); + Optional
actualAddress = ParserUtil.parseAddress(Optional.of(VALID_ADDRESS)); + + assertEquals(expectedAddress, actualAddress.get()); + } + + @Test + public void parseEmail_null_throwsAssertionError() throws Exception { + thrown.expect(AssertionError.class); + ParserUtil.parseEmail(null); + } + + @Test + public void parseEmail_invalidValue_throwsIllegalValueException() throws Exception { + thrown.expect(IllegalValueException.class); + ParserUtil.parseEmail(Optional.of(INVALID_EMAIL)); + } + + @Test + public void parseEmail_optionalEmpty_returnsOptionalEmpty() throws Exception { + assertFalse(ParserUtil.parseEmail(Optional.empty()).isPresent()); + } + + @Test + public void parseEmail_validValue_returnsEmail() throws Exception { + Email expectedEmail = new Email(VALID_EMAIL); + Optional actualEmail = ParserUtil.parseEmail(Optional.of(VALID_EMAIL)); + + assertEquals(expectedEmail, actualEmail.get()); + } + + @Test + public void parseTags_null_throwsAssertionError() throws Exception { + thrown.expect(AssertionError.class); + ParserUtil.parseTags(null); + } + + @Test + public void parseTags_collectionWithInvalidTags_throwsIllegalValueException() throws Exception { + thrown.expect(IllegalValueException.class); + ParserUtil.parseTags(Arrays.asList(VALID_TAG_1, INVALID_TAG)); + } + + @Test + public void parseTags_emptyCollection_returnsEmptySet() throws Exception { + assertTrue(ParserUtil.parseTags(Collections.emptyList()).isEmpty()); + } + + @Test + public void parseTags_collectionWithValidTags_returnsTagSet() throws Exception { + Set actualTagSet = ParserUtil.parseTags(Arrays.asList(VALID_TAG_1, VALID_TAG_2)); + Set expectedTagSet = new HashSet(Arrays.asList(new Tag(VALID_TAG_1), new Tag(VALID_TAG_2))); + + assertEquals(expectedTagSet, actualTagSet); + } +}