-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit tests for ViewAllCommand and ViewCommand classes #111 #131
Changes from 18 commits
20e25f1
017b996
9d049e3
f97c6b1
e942a7e
42f0c7f
b414b98
7140ae2
5457976
ffd7b0f
8b3d3a3
2486c44
2d1260d
73147c3
915c4d9
acfb6a3
ea54ab7
3a068e6
2d4cf4f
3048a59
ea22dbf
5334baf
defefe1
4d30dde
d64b9f4
261fcab
fe3fa09
b01b1e6
39ad9a7
d6a6987
d2e9c4d
449af76
5fcc198
f565130
f6c2d90
f584b82
2a11bd5
beef8ab
c3af8e8
9c60233
b9db7e5
5b654c4
b4aba03
eb7f500
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package seedu.addressbook.commands; | ||
|
||
public class ViewAllCommandTest { | ||
// ViewAll command is tested together with View command | ||
// This is because they function similarly but View command hides private information | ||
// They are tested with same test data input | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import seedu.addressbook.common.Messages; | ||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.person.Address; | ||
import seedu.addressbook.data.person.Email; | ||
import seedu.addressbook.data.person.Name; | ||
import seedu.addressbook.data.person.Person; | ||
import seedu.addressbook.data.person.Phone; | ||
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
import seedu.addressbook.data.tag.UniqueTagList; | ||
import seedu.addressbook.util.TestUtil; | ||
import seedu.addressbook.util.TypicalPersons; | ||
|
||
import static seedu.addressbook.util.TestUtil.assertCommandResult; | ||
|
||
public class ViewCommandTest { | ||
private AddressBook addressBook; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmph, we can't use So, I think you should just add this in to your PR: diff --git a/test/java/seedu/addressbook/util/TestUtil.java b/test/java/seedu/addressbook/util/TestUtil.java
index 8f4eaf8..a1f0836 100644
--- a/test/java/seedu/addressbook/util/TestUtil.java
+++ b/test/java/seedu/addressbook/util/TestUtil.java
@@ -26,13 +26,16 @@ import seedu.addressbook.data.tag.UniqueTagList;
public class TestUtil {
/**
* Creates an address book containing the given persons.
- * @throws DuplicatePersonException if two or more given persons are the same
*/
- public static AddressBook createAddressBook(Person... persons) throws DuplicatePersonException {
+ public static AddressBook createAddressBook(Person... persons) {
AddressBook addressBook = new AddressBook();
for (Person person : persons) {
- addressBook.addPerson(person);
+ try {
+ addressBook.addPerson(person);
+ } catch (DuplicatePersonException e) {
+ throw new AssertionError(e);
+ }
}
return addressBook; No point letting the quality of your PR suffer just because the code base just happens to be like this already. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, maybe name this |
||
private AddressBook emptyAddressBook; | ||
private List<ReadOnlyPerson> emptyDisplayList = new ArrayList<ReadOnlyPerson>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
private List<ReadOnlyPerson> listWithAll; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be useful to name this lists so that first-time readers will know what they will be used for. e.g. |
||
private List<ReadOnlyPerson> listWithSome; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
emptyAddressBook = TestUtil.createAddressBook(); | ||
|
||
TypicalPersons td = new TypicalPersons(); | ||
addressBook = td.getTypicalAddressBook(); | ||
listWithAll = td.getListWithAllPersons(); | ||
listWithSome = td.getListWithSomePersons(); | ||
} | ||
|
||
@Test | ||
public void viewCommand_invalidIndex_returnsInvalidIndexMessage() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// empty addressbook | ||
assertViewErrorInvalidIndex(emptyAddressBook, emptyDisplayList, 1); | ||
|
||
// non-empty addressbook | ||
assertViewErrorInvalidIndex(addressBook, listWithAll, -1); | ||
assertViewErrorInvalidIndex(addressBook, listWithAll, 0); | ||
assertViewErrorInvalidIndex(addressBook, listWithAll, listWithAll.size() + 1); | ||
} | ||
|
||
@Test | ||
public void viewCommand_personNotInAddressBook_returnsPersonNotInAddressBookMessage() throws Exception { | ||
// generate person not in addressbook, add to displayList | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More descriptive: |
||
Person someone = new Person(new Name("me"), | ||
new Phone("123", true), | ||
new Email("[email protected]", true), | ||
new Address("nus", false), | ||
new UniqueTagList(Collections.emptySet())); | ||
listWithAll.add(someone); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This goes against how I think you should just define a local variable for this. |
||
|
||
// empty addressbook | ||
assertViewErrorPersonNotInAddressBook(emptyAddressBook, listWithAll, 1); | ||
|
||
// non-empty addressbook | ||
assertViewErrorPersonNotInAddressBook(addressBook, listWithAll, listWithAll.size()); | ||
} | ||
|
||
@Test | ||
public void viewCommand_validIndex_returnsPersonDetails() { | ||
// person with no private information | ||
assertViewSuccess(addressBook, listWithAll, 1); | ||
|
||
// person with some private information | ||
assertViewSuccess(addressBook, listWithAll, 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need a person with all info private, to confirm that 'privacy' works for each relevant field. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @damithc In There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant such a person must be included as one of the test cases in this method. Your comment in line 74 says There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I get it |
||
|
||
// addressbook has more people than displayList | ||
assertViewSuccess(addressBook, listWithSome, 1); | ||
} | ||
|
||
/** | ||
* Creates a new View command. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this comment doesn't add much value. Can be removed. |
||
*/ | ||
private Command generateViewCommand(AddressBook addressBook, List<ReadOnlyPerson> displayList, int index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why If you prefer the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, |
||
ViewCommand command = new ViewCommand(index); | ||
command.setData(addressBook, displayList); | ||
|
||
return command; | ||
} | ||
|
||
/** | ||
* Creates a new ViewAll command. | ||
*/ | ||
private Command generateViewAllCommand(AddressBook addressBook, List<ReadOnlyPerson> displayList, int index) { | ||
Command command = new ViewAllCommand(index); | ||
command.setData(addressBook, displayList); | ||
|
||
return command; | ||
} | ||
|
||
/** | ||
* Asserts that the details of the person at specific index can be successfully retrieved | ||
* and displayed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add |
||
*/ | ||
private void assertViewSuccess(AddressBook addressBook, List<ReadOnlyPerson> list, int index) { | ||
String expectedMessage = String.format(ViewCommand.MESSAGE_VIEW_PERSON_DETAILS, | ||
list.get(index - 1).getAsTextHidePrivate()); | ||
assertViewCommandBehaviour(addressBook, list, expectedMessage, index); | ||
|
||
expectedMessage = String.format(ViewCommand.MESSAGE_VIEW_PERSON_DETAILS, | ||
list.get(index - 1).getAsTextShowAll()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Further to my previous comment about |
||
assertViewAllCommandBehaviour(addressBook, list, expectedMessage, index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel that a new local variable, eg, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By local, I meant inside the method. Reasons,
Something like this will help clear both problems.
|
||
} | ||
|
||
/** | ||
* Asserts that the details of person at specific index cannot be retrieved due to | ||
* invalid index. | ||
*/ | ||
private void assertViewErrorInvalidIndex(AddressBook addressBook, List<ReadOnlyPerson> list, int index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this really need its own method? Yes, I know the alternative is repeating If the reason is because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If inline this method and put Message into the test case, does it really make sense or help improve readability? eg. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Doesn't your test name already say
OK then. |
||
String expectedMessage = Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; | ||
|
||
assertViewCommandBehaviour(addressBook, list, expectedMessage, index); | ||
assertViewAllCommandBehaviour(addressBook, list, expectedMessage, index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be simplified as this (after extracting common part of this and next method)? |
||
} | ||
|
||
/** | ||
* Asserts that the details of person at specific index cannot be retrieved due to | ||
* person not existing in the addressbook. | ||
*/ | ||
private void assertViewErrorPersonNotInAddressBook(AddressBook addressBook, List<ReadOnlyPerson> list, int index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this really need its own method? Same reasoning as |
||
String expectedMessage = Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK; | ||
|
||
assertViewCommandBehaviour(addressBook, list, expectedMessage, index); | ||
assertViewAllCommandBehaviour(addressBook, list, expectedMessage, index); | ||
} | ||
|
||
/** | ||
* Asserts that the View command gives correct feedback information | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
private void assertViewCommandBehaviour(AddressBook addressBook, List<ReadOnlyPerson> list, String message, int index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reorder parameters so that the three input values come first? |
||
Command command = generateViewCommand(addressBook, list, index); | ||
assertCommandResult(command, message, addressBook, TestUtil.clone(addressBook)); | ||
} | ||
|
||
/** | ||
* Asserts that the ViewAll command gives correct feedback information | ||
*/ | ||
private void assertViewAllCommandBehaviour(AddressBook addressBook, List<ReadOnlyPerson> list, String message, int index) { | ||
Command command = generateViewAllCommand(addressBook, list, index); | ||
assertCommandResult(command, message, addressBook, TestUtil.clone(addressBook)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this class is almost the same as the other test class, we can text both commands in one of the class and make the other class empty with just a comment to explain why it is empty. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.addressbook.commands.Command; | ||
import seedu.addressbook.commands.CommandResult; | ||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.Address; | ||
|
@@ -75,4 +77,18 @@ public static void assertTextFilesEqual(Path path1, Path path2) throws IOExcepti | |
List<String> list2 = Files.readAllLines(path2, Charset.defaultCharset()); | ||
assertEquals(String.join("\n", list1), String.join("\n", list2)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary change. |
||
/** | ||
* Executes the command, and asserts the result message is as expected. | ||
*/ | ||
public static void assertCommandResult(Command command, String expectedMessage, AddressBook addressBook, | ||
AddressBook expectedAddressBook) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, can reorder parameters so that input comes first and expected values come next. |
||
CommandResult result = command.execute(); | ||
|
||
// asserts the result message is correct as expected | ||
assertEquals(expectedMessage, result.feedbackToUser); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be comparing the For example, we may wish to check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that's actually better. I don't mind leaving it as a TODO though. |
||
|
||
// TODO: overwrite equals method in AddressBook and replace with equals method below | ||
assertEquals(addressBook.getAllPersons(), expectedAddressBook.getAllPersons()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
package seedu.addressbook.util; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra space |
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
|
@@ -7,23 +10,27 @@ | |
import seedu.addressbook.data.person.Name; | ||
import seedu.addressbook.data.person.Person; | ||
import seedu.addressbook.data.person.Phone; | ||
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
import seedu.addressbook.data.tag.Tag; | ||
import seedu.addressbook.data.tag.UniqueTagList; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove extra space? |
||
/** | ||
* Class to generate typical test persons | ||
*/ | ||
public class TypicalPersons { | ||
|
||
public Person amy, bill, candy; | ||
public Person amy, bill, candy, dan; | ||
|
||
public TypicalPersons() { | ||
try { | ||
amy = new Person(new Name("Amy Buck"), new Phone("91119111", false), new Email("[email protected]", false), | ||
new Address("1 Clementi Road", false), new UniqueTagList()); | ||
bill = new Person(new Name("Bill Clint"), new Phone("92229222", false), new Email("[email protected]", false), | ||
new Address("2 Clementi Road", false), new UniqueTagList()); | ||
candy = new Person(new Name("Candy Destiny"), new Phone("93339333", false), | ||
new Email("[email protected]", false), new Address("3 Clementi Road", false), new UniqueTagList()); | ||
new Address("2 Clementi Road", true), new UniqueTagList()); | ||
candy = new Person(new Name("Candy Destiny"), new Phone("93339333", true), | ||
new Email("[email protected]", false), new Address("3 Clementi Road", true), new UniqueTagList()); | ||
dan = new Person(new Name("Dan Smith"), new Phone("1234556", true), new Email("[email protected]", true), | ||
new Address("NUS", true), new UniqueTagList(new Tag("Test"))); | ||
} catch (IllegalValueException e) { | ||
e.printStackTrace(); | ||
assert false : "not possible"; | ||
|
@@ -41,7 +48,7 @@ private void loadAddressBookWithSampleData(AddressBook ab) { | |
} | ||
|
||
public Person[] getTypicalPersons() { | ||
return new Person[]{amy, bill, candy}; | ||
return new Person[]{amy, bill, candy, dan}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave at least one blank line between the last method and the closing brace of the class. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why this is shown as a diff. Most of it looks identical. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, this is because there is an extra whitespace for each line, sorry |
||
public AddressBook getTypicalAddressBook() { | ||
|
@@ -50,4 +57,17 @@ public AddressBook getTypicalAddressBook() { | |
return ab; | ||
} | ||
|
||
public List<ReadOnlyPerson> getListWithAllPersons() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, not sure if you need this method. Can you use |
||
List<ReadOnlyPerson> list = new ArrayList<ReadOnlyPerson>(); | ||
for (Person p : getTypicalPersons()) { | ||
list.add(new Person(p)); | ||
} | ||
return list; | ||
} | ||
|
||
public List<ReadOnlyPerson> getListWithSomePersons() { | ||
List<ReadOnlyPerson> list = getListWithAllPersons(); | ||
list.remove(0); //remove a person in the list | ||
return list; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is very specific to your test class and therefore not a good abstraction. Try something like this instead: Can be used like this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noted |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget fullstops.