Skip to content

Commit

Permalink
Add ShowListingsCommandTest and improve tests
Browse files Browse the repository at this point in the history
Add ShowListingsCommandTest
Improve robustness of ListCommandTest
  • Loading branch information
muhdbhz committed Oct 30, 2024
1 parent de1500a commit 0a771cd
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.listing.Listing;
import seedu.address.model.listing.ListingContainsKeywordsPredicate;
import seedu.address.model.person.Name;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -114,6 +116,9 @@ public static void assertCommandSuccess(Command command, Model actualModel, Stri
if (command instanceof ListCommand) {
expectedCommandResult = new CommandResult(expectedMessage, false,
false, false, true);
} else if (command instanceof ShowListingsCommand) {
expectedCommandResult = new CommandResult(expectedMessage, false,
false, true, false);
} else {
expectedCommandResult = new CommandResult(expectedMessage);
}
Expand Down Expand Up @@ -162,4 +167,31 @@ public static void showPersonWithName(Model model, Name targetName) {
assertEquals(1, model.getFilteredPersonList().size());
}

/**
* Updates {@code model}'s filtered list to show only the listing at the given {@code targetIndex} in the
* {@code model}'s address book.
*/
public static void showListingAtIndex(Model model, Index targetIndex) {
assertTrue(targetIndex.getZeroBased() < model.getFilteredListingList().size());

Listing listing = model.getFilteredListingList().get(targetIndex.getZeroBased());
final String[] splitName = listing.getName().fullName.split("\\s+");
model.updateFilteredListingList(new ListingContainsKeywordsPredicate(Arrays.asList(splitName[0])));

assertEquals(1, model.getFilteredListingList().size());
}

/**
* Updates {@code model}'s filtered list to show only the listing with the given {@code targetName} in the
* {@code model}'s address book.
*/
public static void showListingWithName(Model model, Name targetName) {
Listing listing = model.getListingByName(targetName);
assertTrue(model.hasListing(listing));
final String[] splitName = listing.getName().fullName.split("\\s+");
model.updateFilteredListingList(new ListingContainsKeywordsPredicate(Arrays.asList(splitName[0])));

assertEquals(1, model.getFilteredListingList().size());
}

}
39 changes: 39 additions & 0 deletions src/test/java/seedu/address/logic/commands/ListCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.logic.commands.CommandTestUtil.showPersonWithName;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
import static seedu.address.testutil.TypicalPersons.getTypicalNames;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.model.AddressBook;
import seedu.address.model.Listings;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
Expand Down Expand Up @@ -36,5 +42,38 @@ public void execute_listIsNotFiltered_showsSameList() {
public void execute_listIsFiltered_showsEverything() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonAtIndex(model, INDEX_SECOND_PERSON);
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonAtIndex(model, INDEX_THIRD_PERSON);
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonWithName(model, getTypicalNames().get(0));
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonWithName(model, getTypicalNames().get(1));
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonWithName(model, getTypicalNames().get(2));
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonWithName(model, getTypicalNames().get(3));
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonWithName(model, getTypicalNames().get(4));
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonWithName(model, getTypicalNames().get(5));
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);

showPersonWithName(model, getTypicalNames().get(6));
assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel);
}

@Test
public void execute_emptyList_throwsCommandException() {
model = new ModelManager(new AddressBook(), new UserPrefs(), new Listings());
assertCommandFailure(new ListCommand(), model, ListCommand.MESSAGE_NO_CLIENT_IN_LIST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showListingAtIndex;
import static seedu.address.logic.commands.CommandTestUtil.showListingWithName;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_LISTING;
import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_LISTING;
import static seedu.address.testutil.TypicalListings.getTypicalListings;
import static seedu.address.testutil.TypicalListings.getTypicalNames;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.model.Listings;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;

/**
* Contains integration tests (interaction with the Model) and unit tests for ShowListingsCommand.
*/
public class ShowListingsCommandTest {

private Model model;
private Model expectedModel;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings());
expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs(), model.getListings());
}

@Test
public void execute_listIsNotFiltered_showsSameList() {
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);
}

@Test
public void execute_listIsFiltered_showsEverything() {
showListingAtIndex(model, INDEX_FIRST_LISTING);
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingAtIndex(model, INDEX_SECOND_LISTING);
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingAtIndex(model, INDEX_THIRD_LISTING);
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingWithName(model, getTypicalNames().get(0));
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingWithName(model, getTypicalNames().get(1));
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingWithName(model, getTypicalNames().get(2));
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingWithName(model, getTypicalNames().get(3));
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingWithName(model, getTypicalNames().get(4));
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingWithName(model, getTypicalNames().get(5));
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);

showListingWithName(model, getTypicalNames().get(6));
assertCommandSuccess(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_SUCCESS, expectedModel);
}

@Test
public void execute_emptyListings_throwsCommandException() {
model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), new Listings());
assertCommandFailure(new ShowListingsCommand(), model, ShowListingsCommand.MESSAGE_NO_LISTINGS_IN_LIST);
}
}
3 changes: 3 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalIndexes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ public class TypicalIndexes {
public static final Index INDEX_FIRST_PERSON = Index.fromOneBased(1);
public static final Index INDEX_SECOND_PERSON = Index.fromOneBased(2);
public static final Index INDEX_THIRD_PERSON = Index.fromOneBased(3);
public static final Index INDEX_FIRST_LISTING = Index.fromOneBased(1);
public static final Index INDEX_SECOND_LISTING = Index.fromOneBased(2);
public static final Index INDEX_THIRD_LISTING = Index.fromOneBased(3);
}
7 changes: 7 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalListings.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.address.model.Listings;
import seedu.address.model.listing.Listing;
import seedu.address.model.listing.Region;
import seedu.address.model.person.Name;

/**
* A utility class containing a list of {@code Listing} objects to be used in tests.
Expand Down Expand Up @@ -80,6 +81,12 @@ public static Listings getTypicalListings() {
return listings;
}

public static List<Name> getTypicalNames() {
return new ArrayList<>(Arrays.asList(PASIR_RIS.getName(), TAMPINES.getName(), KENT_RIDGE.getName(),
BUONA_VISTA.getName(), SENGKANG.getName(), PUNGGOL.getName(),
SENTOSA.getName()));
}

public static List<Listing> getTypicalPropertyListing() {
return new ArrayList<>(Arrays.asList(PASIR_RIS, TAMPINES, KENT_RIDGE,
BUONA_VISTA, SENGKANG, PUNGGOL, SENTOSA));
Expand Down

0 comments on commit 0a771cd

Please sign in to comment.