Skip to content

Commit

Permalink
Add test coverage for assertion test
Browse files Browse the repository at this point in the history
  • Loading branch information
muller317 committed Nov 7, 2024
1 parent 3f4215d commit f4ebdd6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_VIP;
import static seedu.address.testutil.Assert.assertThrows;

import java.util.ArrayList;
Expand Down Expand Up @@ -47,6 +48,7 @@ public class CommandTestUtil {
public static final String ADDRESS_DESC_BOB = " " + PREFIX_ADDRESS + VALID_ADDRESS_BOB;
public static final String TAG_DESC_FRIEND = " " + PREFIX_TAG + VALID_TAG_FRIEND;
public static final String TAG_DESC_HUSBAND = " " + PREFIX_TAG + VALID_TAG_HUSBAND;
public static final String VIP_DESC_TRUE = " " + PREFIX_VIP + "true";

public static final String INVALID_NAME_DESC = " " + PREFIX_NAME + "James&"; // '&' not allowed in names
public static final String INVALID_PHONE_DESC = " " + PREFIX_PHONE + "911a"; // 'a' not allowed in phones
Expand Down
78 changes: 78 additions & 0 deletions src/test/java/seedu/address/logic/parser/AddCommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_FRIEND;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
import static seedu.address.logic.commands.CommandTestUtil.VIP_DESC_TRUE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
Expand All @@ -37,6 +38,7 @@

import seedu.address.logic.Messages;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -193,4 +195,80 @@ public void parse_invalidValue_failure() {
+ ADDRESS_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

/**
* Test to ensure the parser throws a ParseException when a required field is missing.
* This indirectly tests the assertions by creating conditions that should not pass assertion checks.
*/
@Test
public void parse_missingRequiredFields_assertionFailure() {
// Missing name
assertParseFailure(parser, PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));

// Missing phone
assertParseFailure(parser, NAME_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));

// Missing email
assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + ADDRESS_DESC_BOB,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));

// Missing address
assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB,
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));

// All required fields missing
assertParseFailure(parser, "", String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

/**
* Test to check that the VIP status is set to false if the VIP prefix is not provided.
* This indirectly validates the assertion related to VIP status defaulting.
*/
@Test
public void parse_vipStatusDefault_assertionSuccess() throws ParseException {
// Person without VIP prefix should default to non-VIP
Person expectedPerson = new PersonBuilder(BOB).withTags(VALID_TAG_FRIEND).withVipState(false).build();

// VIP prefix is not included; should default to VIP status as false
assertParseSuccess(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + TAG_DESC_FRIEND,
new AddCommand(expectedPerson));
}

/**
* Test to check that the VIP status can be set to true when explicitly specified.
*/
@Test
public void parse_vipStatusExplicitlyTrue_success() throws ParseException {
// Person with VIP explicitly set to true
Person expectedPerson = new PersonBuilder(BOB).withTags(VALID_TAG_FRIEND).withVipState(true).build();

// VIP prefix explicitly set to true
assertParseSuccess(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB
+ TAG_DESC_FRIEND + VIP_DESC_TRUE, new AddCommand(expectedPerson));
}

/**
* Test to ensure multiple names, phones, emails, or addresses result in duplicate prefix errors.
*/
@Test
public void parse_multiplePrefixes_assertionFailure() {
// Duplicate name prefix
assertParseFailure(parser, NAME_DESC_AMY + NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NAME));

// Duplicate phone prefix
assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_AMY + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE));

// Duplicate email prefix
assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_AMY + EMAIL_DESC_BOB + ADDRESS_DESC_BOB,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_EMAIL));

// Duplicate address prefix
assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_AMY
+ ADDRESS_DESC_BOB,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_ADDRESS));
}
}
20 changes: 12 additions & 8 deletions src/test/java/seedu/address/testutil/PersonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PersonBuilder {
public static final String DEFAULT_EMAIL = "[email protected]";
public static final String DEFAULT_ADDRESS = "123 Jurong West Ave 6 #08-111";
public static final String DEFAULT_COMMENT = "She likes fast delivery.";
public static final boolean DEFAULT_IS_VIP = false;

private Name name;
private Phone phone;
Expand All @@ -41,7 +42,7 @@ public PersonBuilder() {
address = new Address(DEFAULT_ADDRESS);
comment = new Comment(DEFAULT_COMMENT);
tags = new HashSet<>();
isVip = false;
isVip = DEFAULT_IS_VIP;
}

/**
Expand All @@ -66,7 +67,7 @@ public PersonBuilder withName(String name) {
}

/**
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code Person} that we are building.
* Parses the {@code tags} into a {@code Set<Tag>} and sets it to the {@code Person} that we are building.
*/
public PersonBuilder withTags(String ... tags) {
this.tags = SampleDataUtil.getTagSet(tags);
Expand Down Expand Up @@ -98,21 +99,24 @@ public PersonBuilder withEmail(String email) {
}

/**
* Sets the {@code isVip} of the {@code Person} that we are building.
* Sets the {@code Comment} of the {@code Person} that we are building.
*/
public PersonBuilder withVipState(boolean isVip) {
this.isVip = isVip;
public PersonBuilder withComment(String comment) {
this.comment = new Comment(comment);
return this;
}

/**
* Sets the {@code Comment} of the {@code Person} that we are building.
* Sets the {@code isVip} state of the {@code Person} that we are building.
*/
public PersonBuilder withComment(String comment) {
this.comment = new Comment(comment);
public PersonBuilder withVipState(boolean isVip) {
this.isVip = isVip;
return this;
}

/**
* Builds and returns a {@code Person} object with the specified attributes.
*/
public Person build() {
return new Person(name, phone, email, address, comment, tags, isVip);
}
Expand Down

0 comments on commit f4ebdd6

Please sign in to comment.