Skip to content

Commit

Permalink
[#401] Add unit tests for ParserUtil#parseX() (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
limmlingg authored and yamgent committed May 22, 2017
1 parent e209ff6 commit a74cc95
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]";
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<Name> 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<Phone> 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<Address> 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<Email> 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<Tag> actualTagSet = ParserUtil.parseTags(Arrays.asList(VALID_TAG_1, VALID_TAG_2));
Set<Tag> expectedTagSet = new HashSet<Tag>(Arrays.asList(new Tag(VALID_TAG_1), new Tag(VALID_TAG_2)));

assertEquals(expectedTagSet, actualTagSet);
}
}

0 comments on commit a74cc95

Please sign in to comment.