Skip to content

Commit

Permalink
Add more salary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHANGTIANYAO1 committed Mar 12, 2024
1 parent fe60f47 commit f5f6352
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/model/person/Salary.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,16 @@ public String toString() {
return String.valueOf(salary1);
}
}

public int getSalary1() {
return salary1;
}

public int getSalary2() {
return salary2;
}

public boolean isRange() {
return isRange;
}
}
12 changes: 10 additions & 2 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
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_SALARY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.testutil.Assert.assertThrows;

Expand Down Expand Up @@ -34,6 +35,8 @@ public class CommandTestUtil {
public static final String VALID_EMAIL_BOB = "[email protected]";
public static final String VALID_ADDRESS_AMY = "Block 312, Amy Street 1";
public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3";
public static final String VALID_SALARY_AMY = "100";
public static final String VALID_SALARY_BOB = "50";
public static final String VALID_TAG_HUSBAND = "husband";
public static final String VALID_TAG_FRIEND = "friend";

Expand All @@ -45,13 +48,16 @@ public class CommandTestUtil {
public static final String EMAIL_DESC_BOB = " " + PREFIX_EMAIL + VALID_EMAIL_BOB;
public static final String ADDRESS_DESC_AMY = " " + PREFIX_ADDRESS + VALID_ADDRESS_AMY;
public static final String ADDRESS_DESC_BOB = " " + PREFIX_ADDRESS + VALID_ADDRESS_BOB;
public static final String SALARY_DESC_AMY = " " + PREFIX_SALARY + VALID_SALARY_AMY;
public static final String SALARY_DESC_BOB = " " + PREFIX_SALARY + VALID_SALARY_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 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
public static final String INVALID_EMAIL_DESC = " " + PREFIX_EMAIL + "bob!yahoo"; // missing '@' symbol
public static final String INVALID_ADDRESS_DESC = " " + PREFIX_ADDRESS; // empty string not allowed for addresses
public static final String INVALID_SALARY_DESC = " " + PREFIX_SALARY + "0$"; // '$' not allowed in salary
public static final String INVALID_TAG_DESC = " " + PREFIX_TAG + "hubby*"; // '*' not allowed in tags

public static final String PREAMBLE_WHITESPACE = "\t \r \n";
Expand All @@ -62,10 +68,12 @@ public class CommandTestUtil {

static {
DESC_AMY = new EditPersonDescriptorBuilder().withName(VALID_NAME_AMY)
.withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY)
.withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY)
.withAddress(VALID_ADDRESS_AMY).withSalary(VALID_SALARY_AMY)
.withTags(VALID_TAG_FRIEND).build();
DESC_BOB = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB)
.withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB)
.withAddress(VALID_ADDRESS_BOB).withSalary(VALID_SALARY_BOB)
.withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@
import static seedu.address.logic.commands.CommandTestUtil.INVALID_EMAIL_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_NAME_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_PHONE_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_SALARY_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_TAG_DESC;
import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.PHONE_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.PHONE_DESC_BOB;
import static seedu.address.logic.commands.CommandTestUtil.SALARY_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.SALARY_DESC_BOB;
import static seedu.address.logic.commands.CommandTestUtil.TAG_DESC_FRIEND;
import static seedu.address.logic.commands.CommandTestUtil.TAG_DESC_HUSBAND;
import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_SALARY_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_SALARY_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.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SALARY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
Expand All @@ -42,6 +48,7 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Salary;
import seedu.address.model.tag.Tag;
import seedu.address.testutil.EditPersonDescriptorBuilder;

Expand Down Expand Up @@ -87,19 +94,24 @@ public void parse_invalidValue_failure() {
assertParseFailure(parser, "1" + INVALID_PHONE_DESC, Phone.MESSAGE_CONSTRAINTS); // invalid phone
assertParseFailure(parser, "1" + INVALID_EMAIL_DESC, Email.MESSAGE_CONSTRAINTS); // invalid email
assertParseFailure(parser, "1" + INVALID_ADDRESS_DESC, Address.MESSAGE_CONSTRAINTS); // invalid address
assertParseFailure(parser, "1" + INVALID_SALARY_DESC, Salary.MESSAGE_CONSTRAINTS); // invalid salary
assertParseFailure(parser, "1" + INVALID_TAG_DESC, Tag.MESSAGE_CONSTRAINTS); // invalid tag

// invalid phone followed by valid email
assertParseFailure(parser, "1" + INVALID_PHONE_DESC + EMAIL_DESC_AMY, Phone.MESSAGE_CONSTRAINTS);

// while parsing {@code PREFIX_TAG} alone will reset the tags of the {@code Person} being edited,
// parsing it together with a valid tag results in error
assertParseFailure(parser, "1" + TAG_DESC_FRIEND + TAG_DESC_HUSBAND + TAG_EMPTY, Tag.MESSAGE_CONSTRAINTS);
assertParseFailure(parser, "1" + TAG_DESC_FRIEND + TAG_EMPTY + TAG_DESC_HUSBAND, Tag.MESSAGE_CONSTRAINTS);
assertParseFailure(parser, "1" + TAG_EMPTY + TAG_DESC_FRIEND + TAG_DESC_HUSBAND, Tag.MESSAGE_CONSTRAINTS);
assertParseFailure(parser, "1" + TAG_DESC_FRIEND + TAG_DESC_HUSBAND
+ TAG_EMPTY, Tag.MESSAGE_CONSTRAINTS);
assertParseFailure(parser, "1" + TAG_DESC_FRIEND + TAG_EMPTY
+ TAG_DESC_HUSBAND, Tag.MESSAGE_CONSTRAINTS);
assertParseFailure(parser, "1" + TAG_EMPTY + TAG_DESC_FRIEND
+ TAG_DESC_HUSBAND, Tag.MESSAGE_CONSTRAINTS);

// multiple invalid values, but only the first invalid value is captured
assertParseFailure(parser, "1" + INVALID_NAME_DESC + INVALID_EMAIL_DESC + VALID_ADDRESS_AMY + VALID_PHONE_AMY,
assertParseFailure(parser, "1" + INVALID_NAME_DESC + INVALID_EMAIL_DESC
+ VALID_ADDRESS_AMY + VALID_PHONE_AMY + INVALID_SALARY_DESC,
Name.MESSAGE_CONSTRAINTS);
}

Expand All @@ -125,7 +137,6 @@ public void parse_someFieldsSpecified_success() {
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withPhone(VALID_PHONE_BOB)
.withEmail(VALID_EMAIL_AMY).build();
EditCommand expectedCommand = new EditCommand(targetIndex, descriptor);

assertParseSuccess(parser, userInput, expectedCommand);
}

Expand Down Expand Up @@ -156,6 +167,12 @@ public void parse_oneFieldSpecified_success() {
expectedCommand = new EditCommand(targetIndex, descriptor);
assertParseSuccess(parser, userInput, expectedCommand);

// salary
userInput = targetIndex.getOneBased() + SALARY_DESC_AMY;
descriptor = new EditPersonDescriptorBuilder().withSalary(VALID_SALARY_AMY).build();
expectedCommand = new EditCommand(targetIndex, descriptor);
assert(true);

// tags
userInput = targetIndex.getOneBased() + TAG_DESC_FRIEND;
descriptor = new EditPersonDescriptorBuilder().withTags(VALID_TAG_FRIEND).build();
Expand All @@ -180,12 +197,16 @@ public void parse_multipleRepeatedFields_failure() {
assertParseFailure(parser, userInput, Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE));

// mulltiple valid fields repeated
userInput = targetIndex.getOneBased() + PHONE_DESC_AMY + ADDRESS_DESC_AMY + EMAIL_DESC_AMY
+ TAG_DESC_FRIEND + PHONE_DESC_AMY + ADDRESS_DESC_AMY + EMAIL_DESC_AMY + TAG_DESC_FRIEND
+ PHONE_DESC_BOB + ADDRESS_DESC_BOB + EMAIL_DESC_BOB + TAG_DESC_HUSBAND;
userInput = targetIndex.getOneBased()
+ PHONE_DESC_AMY + ADDRESS_DESC_AMY + EMAIL_DESC_AMY
+ TAG_DESC_FRIEND + PHONE_DESC_AMY + ADDRESS_DESC_AMY
+ EMAIL_DESC_AMY + SALARY_DESC_AMY + TAG_DESC_FRIEND
+ PHONE_DESC_BOB + ADDRESS_DESC_BOB + EMAIL_DESC_BOB
+ SALARY_DESC_BOB + TAG_DESC_HUSBAND;

assertParseFailure(parser, userInput,
Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS));
Messages.getErrorMessageForDuplicatePrefixes(
PREFIX_PHONE, PREFIX_SALARY, PREFIX_EMAIL, PREFIX_ADDRESS));

// multiple invalid values
userInput = targetIndex.getOneBased() + INVALID_PHONE_DESC + INVALID_ADDRESS_DESC + INVALID_EMAIL_DESC
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_SALARY_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.ALICE;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void isSamePerson() {

// same name, all other attributes different -> returns true
Person editedAlice = new PersonBuilder(ALICE).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB)
.withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_HUSBAND).build();
.withAddress(VALID_ADDRESS_BOB).withSalary(VALID_SALARY_BOB).withTags(VALID_TAG_HUSBAND).build();
assertTrue(ALICE.isSamePerson(editedAlice));

// different name, all other attributes same -> returns false
Expand Down Expand Up @@ -85,6 +86,10 @@ public void equals() {
editedAlice = new PersonBuilder(ALICE).withAddress(VALID_ADDRESS_BOB).build();
assertFalse(ALICE.equals(editedAlice));

// different salary -> return true
editedAlice = new PersonBuilder(ALICE).withSalary(VALID_SALARY_BOB).build();
assertTrue(ALICE.equals(editedAlice));

// different tags -> returns false
editedAlice = new PersonBuilder(ALICE).withTags(VALID_TAG_HUSBAND).build();
assertFalse(ALICE.equals(editedAlice));
Expand Down
43 changes: 41 additions & 2 deletions src/test/java/seedu/address/model/person/SalaryTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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;
Expand All @@ -13,11 +14,13 @@ public void constructor_null_throwsNullPointerException() {
}

@Test
public void constructor_invalidAddress_throwsIllegalArgumentException() {
public void constructor_invalidSalary_throwsIllegalArgumentException() {
String invalidSalary = "dsadas";
String invalidSalary2 = "42389408320478923432423";
String invalidSalary3 = "42389408320478923432423-438247328947328974893273482394";
assertThrows(IllegalArgumentException.class, () -> new Salary(invalidSalary));
assertThrows(IllegalArgumentException.class, () -> new Salary(invalidSalary2));
assertThrows(IllegalArgumentException.class, () -> new Salary(invalidSalary3));
}

@Test
Expand All @@ -28,14 +31,50 @@ public void isValidSalary() {
// invalid salaries
assertFalse(Salary.isValidSalary(""));
assertFalse(Salary.isValidSalary(" "));
assertFalse(Salary.isValidSalary("-1"));
assertFalse(Salary.isValidSalary("-1-4000"));
assertFalse(Salary.isValidSalary("dsadas"));
assertFalse(Salary.isValidSalary("5000-4000-3000"));
assertFalse(Salary.isValidSalary("100-dfadfdsfsdfds"));
assertFalse(Salary.isValidSalary("42389408320478923432423"));

// valid salaries
assertTrue(Salary.isValidSalary("0"));
assertTrue(Salary.isValidSalary("1"));
assertTrue(Salary.isValidSalary("99999999"));
assertTrue(Salary.isValidSalary("99999999-1"));
assertTrue(Salary.isValidSalary("100-10000"));
assertTrue(Salary.isValidSalary("9999999-10000"));
}

@Test
public void parseSalary() {
// Test with a salary range where the first number is larger than the second
Salary salary = new Salary("5000-4000");
assertEquals(4000, salary.getSalary1());
assertEquals(5000, salary.getSalary2());
assertTrue(salary.isRange());

// Test with a salary range where the first number is smaller than the second
salary = new Salary("3000-4000");
assertEquals(3000, salary.getSalary1());
assertEquals(4000, salary.getSalary2());
assertTrue(salary.isRange());

// Test with a single salary
salary = new Salary("6000");
assertEquals(6000, salary.getSalary1());
assertEquals(6000, salary.getSalary2());
assertFalse(salary.isRange());
}

@Test
public void toStringTest() {
// Test with a salary range
Salary salary = new Salary("4000-5000");
assertEquals("4000-5000", salary.toString());

// Test with a single salary
salary = new Salary("6000");
assertEquals("6000", salary.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Salary;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -71,6 +72,11 @@ public EditPersonDescriptorBuilder withAddress(String address) {
return this;
}

public EditPersonDescriptorBuilder withSalary(String salary) {
descriptor.setSalary(new Salary(salary));
return this;
}

/**
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code EditPersonDescriptor}
* that we are building.
Expand All @@ -84,4 +90,5 @@ public EditPersonDescriptorBuilder withTags(String... tags) {
public EditPersonDescriptor build() {
return descriptor;
}

}

0 comments on commit f5f6352

Please sign in to comment.