Skip to content

Commit

Permalink
Merge pull request #385 from AY1920S2-CS2103T-W17-2/sug-test-util
Browse files Browse the repository at this point in the history
Create Sug Test Util Class to remove code duplication
  • Loading branch information
johannagwan authored Apr 10, 2020
2 parents f737fc0 + 064610f commit 08a9401
Show file tree
Hide file tree
Showing 18 changed files with 876 additions and 1,283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ public class DeleteSuggestionCommand implements SuggestionCommand {
public DeleteSuggestionCommand(AbsolutePath path, String oldTitle) {
Objects.requireNonNull(path);
Objects.requireNonNull(oldTitle);

if (oldTitle.isBlank()) {
throw new IllegalArgumentException("The old title must contain at least one element");
}

this.path = path;
this.oldTitle = oldTitle;
}

@Override
public void execute(Model model) {
// Nullity check
Objects.requireNonNull(model);

if (!oldTitle.isEmpty()) {
// Set suggestions
List<AbsolutePath> possiblePaths = getPossiblePaths(path, model);
List<SuggestionItem> suggestions = getSuggestions(possiblePaths, model);
List<AbsolutePath> possiblePaths = getPossiblePaths(path, model);
List<SuggestionItem> suggestions = getSuggestions(possiblePaths, model);

model.setSuggestions(suggestions);
}
model.setSuggestions(suggestions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ public class OpenSuggestionCommand implements SuggestionCommand {
public OpenSuggestionCommand(AbsolutePath path, String oldTitle) {
Objects.requireNonNull(path);
Objects.requireNonNull(oldTitle);

if (oldTitle.isBlank()) {
throw new IllegalArgumentException("The old title must contain at least one element");
}

this.path = path;
this.oldTitle = oldTitle;
}

@Override
public void execute(Model model) {
// Nullity check
Objects.requireNonNull(model);

if (!oldTitle.isEmpty()) {
// Set suggestions
List<AbsolutePath> possiblePaths = getPossiblePaths(path, model);
List<SuggestionItem> suggestions = getSuggestions(possiblePaths, model);
List<AbsolutePath> possiblePaths = getPossiblePaths(path, model);
List<SuggestionItem> suggestions = getSuggestions(possiblePaths, model);

model.setSuggestions(suggestions);
}
model.setSuggestions(suggestions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ public SearchSuggestionCommand(String keyword) {

@Override
public void execute(Model model) {
// Nullity check
Objects.requireNonNull(model);

// Set suggestions
List<SuggestionItem> suggestions = traverseTree(model);

model.setSuggestions(suggestions);
}

Expand Down Expand Up @@ -69,7 +66,8 @@ private List<SuggestionItem> traverseTree(Model model) {
and setting action to open that particular block when the user chooses that suggestion. */
String bodyLowerCase = blockBody.toLowerCase();
if (bodyLowerCase.contains(keyword.toLowerCase())) {
String[] blockBodies = bodyLowerCase.split(keyword, -1);
String[] blockBodies = bodyLowerCase.split(keyword.toLowerCase(), -1);

int frequency = blockBodies.length - 1;
String displayText = absolutePath.getStringRepresentation();
Runnable action = () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.notably.logic.parser.ArgumentMultimap;
import com.notably.logic.parser.ArgumentTokenizer;
import com.notably.logic.parser.ParserUtil;
import com.notably.logic.parser.exceptions.ParseException;
import com.notably.model.Model;
import com.notably.model.block.Title;

Expand All @@ -32,11 +31,12 @@ public class NewSuggestionCommandParser implements SuggestionCommandParser<Sugge
public NewSuggestionCommandParser(Model model) {
this.model = model;
}

/**
* Parses input and displays the appropriate response text.
*
* @param userInput .
* @return List of command to execute.
* @throws ParseException when input is invalid.
* @return Optional.empty()
*/
public Optional<SuggestionCommand> parse(String userInput) {
ArgumentMultimap argMultimap =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.notably.model.suggestion;

import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;

/**
Expand All @@ -11,13 +12,20 @@ public class SuggestionItemImpl implements SuggestionItem {
private HashMap<String, String> hmap;

public SuggestionItemImpl(String displayText, Runnable action) {
Objects.requireNonNull(displayText);
Objects.requireNonNull(action);

this.action = action;

hmap = new HashMap<>();
hmap.put("displayText", displayText);
}

public SuggestionItemImpl(String displayText, int frequency, Runnable action) {
Objects.requireNonNull(displayText);
Objects.requireNonNull(frequency);
Objects.requireNonNull(action);

this.action = action;

hmap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,34 @@
package com.notably.logic.commands.suggestion;

import static com.notably.logic.parser.CliSyntax.PREFIX_TITLE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.notably.commons.path.AbsolutePath;
import com.notably.commons.path.exceptions.InvalidPathException;
import com.notably.logic.suggestion.SuggestionTestUtil;
import com.notably.model.Model;
import com.notably.model.ModelManager;
import com.notably.model.block.Block;
import com.notably.model.block.BlockImpl;
import com.notably.model.block.BlockModel;
import com.notably.model.block.BlockModelImpl;
import com.notably.model.block.Title;
import com.notably.model.suggestion.SuggestionItem;
import com.notably.model.suggestion.SuggestionItemImpl;
import com.notably.model.suggestion.SuggestionModel;
import com.notably.model.suggestion.SuggestionModelImpl;
import com.notably.model.viewstate.ViewStateModel;
import com.notably.model.viewstate.ViewStateModelImpl;

public class DeleteSuggestionCommandTest {
private static AbsolutePath toRoot;
private static AbsolutePath toCs2103;
private static AbsolutePath toCs3230;
private static AbsolutePath toCs2103Week1;
private static AbsolutePath toCs2103Week2;
private static AbsolutePath toCs2103Week3;
private static AbsolutePath toCs2103Week1Lecture;
private static AbsolutePath toCs2103t;
private static Model model;

private static final String COMMAND_WORD = "delete";

@BeforeAll
public static void setUp() throws InvalidPathException {
// Set up paths
toRoot = AbsolutePath.fromString("/");
toCs2103 = AbsolutePath.fromString("/CS2103");
toCs3230 = AbsolutePath.fromString("/CS3230");
toCs2103Week1 = AbsolutePath.fromString("/CS2103/Week1");
toCs2103Week2 = AbsolutePath.fromString("/CS2103/Week2");
toCs2103Week3 = AbsolutePath.fromString("/CS2103/Week3");
toCs2103Week1Lecture = AbsolutePath.fromString("/CS2103/Week1/Lecture");

// Set up model
BlockModel blockModel = new BlockModelImpl();
SuggestionModel suggestionModel = new SuggestionModelImpl();
ViewStateModel viewStateModel = new ViewStateModelImpl();
model = new ModelManager(blockModel, suggestionModel, viewStateModel);

// Add test data to model
Block cs2103 = new BlockImpl(new Title("CS2103"));
Block cs3230 = new BlockImpl(new Title("CS3230"));
model.addBlockToCurrentPath(cs2103);
model.addBlockToCurrentPath(cs3230);

Block week1 = new BlockImpl(new Title("Week1"));
Block week2 = new BlockImpl(new Title("Week2"));
Block week3 = new BlockImpl(new Title("Week3"));
model.setCurrentlyOpenBlock(toCs2103);
model.addBlockToCurrentPath(week1);
model.addBlockToCurrentPath(week2);
model.addBlockToCurrentPath(week3);

Block lecture = new BlockImpl(new Title("Lecture"));
model.setCurrentlyOpenBlock(toCs2103Week1);
model.addBlockToCurrentPath(lecture);
public static void setUp() {
toCs2103t = SuggestionTestUtil.getToCs2103t();
model = SuggestionTestUtil.getModel();
}

@AfterEach
public void clearSuggestions() {
model.clearSuggestions();
}

@Test
Expand All @@ -81,105 +38,57 @@ public void constructor_nullPath_throwsNullPointerException() {

@Test
public void constructor_nullTitle_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new DeleteSuggestionCommand(toCs2103, null));
assertThrows(NullPointerException.class, () -> new DeleteSuggestionCommand(toCs2103t,
null));
}

@Test
public void execute_nullModel_throwsNullPointerException() {
DeleteSuggestionCommand deleteSuggestionCommand = new DeleteSuggestionCommand(toRoot,
toRoot.getStringRepresentation());
DeleteSuggestionCommand deleteSuggestionCommand = new DeleteSuggestionCommand(toCs2103t,
toCs2103t.getStringRepresentation());
assertThrows(NullPointerException.class, () -> deleteSuggestionCommand.execute(null));
}

@Test
public void execute_blankOldTitle_throwsIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> new DeleteSuggestionCommand(toCs2103t, " "));
}

@Test
public void execute_correctAbsolutePathWithPrefix_generatesResponseCorrectly() {
model.setInput(COMMAND_WORD + " " + PREFIX_TITLE + " " + toCs2103.getStringRepresentation());
DeleteSuggestionCommand deleteSuggestionCommand =
new DeleteSuggestionCommand(toCs2103, toCs2103.getStringRepresentation());
String userInputWithoutPath = COMMAND_WORD + " " + PREFIX_TITLE + " ";
model.setInput(userInputWithoutPath + toCs2103t.getStringRepresentation());
DeleteSuggestionCommand deleteSuggestionCommand = new DeleteSuggestionCommand(toCs2103t,
toCs2103t.getStringRepresentation());
deleteSuggestionCommand.execute(model);

// Expected result
SuggestionItem cs2103 = new SuggestionItemImpl(toCs2103.getStringRepresentation(), null);
SuggestionItem cs2103Week1 = new SuggestionItemImpl(toCs2103Week1.getStringRepresentation(), null);
SuggestionItem cs2103Week1Lecture = new SuggestionItemImpl(toCs2103Week1Lecture.getStringRepresentation(),
null);
SuggestionItem cs2103Week2 = new SuggestionItemImpl(toCs2103Week2.getStringRepresentation(), null);
SuggestionItem cs2103Week3 = new SuggestionItemImpl(toCs2103Week3.getStringRepresentation(), null);

List<SuggestionItem> expectedSuggestions = new ArrayList<>();
expectedSuggestions.add(cs2103);
expectedSuggestions.add(cs2103Week1);
expectedSuggestions.add(cs2103Week2);
expectedSuggestions.add(cs2103Week3);
expectedSuggestions.add(cs2103Week1Lecture);

List<SuggestionItem> suggestions = model.getSuggestions();

for (int i = 0; i < expectedSuggestions.size(); i++) {
SuggestionItem suggestion = suggestions.get(i);
SuggestionItem expectedSuggestion = expectedSuggestions.get(i);
assertEquals(expectedSuggestion.getProperty("displayText"), suggestion.getProperty("displayText"));
}

List<String> expectedInputs = new ArrayList<>();
expectedInputs.add(COMMAND_WORD + " " + PREFIX_TITLE + " " + toCs2103.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + PREFIX_TITLE + " " + toCs2103Week1.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + PREFIX_TITLE + " " + toCs2103Week2.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + PREFIX_TITLE + " " + toCs2103Week3.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + PREFIX_TITLE + " " + toCs2103Week1Lecture.getStringRepresentation());

for (int i = 0; i < expectedInputs.size(); i++) {
SuggestionItem suggestionItem = suggestions.get(i);
String expectedInput = expectedInputs.get(i);
suggestionItem.getAction().run();
String input = model.getInput();
assertEquals(expectedInput, input);
}
List<SuggestionItem> expectedSuggestions = SuggestionTestUtil.getExpectedSugForCs2103tPathInput();

SuggestionTestUtil.assertSuggestions(expectedSuggestions, suggestions);

List<String> expectedInputs = SuggestionTestUtil.getExpectedInputsForCs2103tPathInput(userInputWithoutPath);

SuggestionTestUtil.assertInputs(expectedInputs, suggestions, model);
}

@Test
public void execute_correctAbsolutePathWithoutPrefix_generatesResponseCorrectly() {
model.setInput(COMMAND_WORD + " " + toCs2103.getStringRepresentation());
DeleteSuggestionCommand deleteSuggestionCommand =
new DeleteSuggestionCommand(toCs2103, toCs2103.getStringRepresentation());
String userInputWithoutPath = COMMAND_WORD + " ";
model.setInput(userInputWithoutPath + toCs2103t.getStringRepresentation());
DeleteSuggestionCommand deleteSuggestionCommand = new DeleteSuggestionCommand(toCs2103t,
toCs2103t.getStringRepresentation());
deleteSuggestionCommand.execute(model);

// Expected result
SuggestionItem cs2103 = new SuggestionItemImpl(toCs2103.getStringRepresentation(), null);
SuggestionItem cs2103Week1 = new SuggestionItemImpl(toCs2103Week1.getStringRepresentation(), null);
SuggestionItem cs2103Week1Lecture = new SuggestionItemImpl(toCs2103Week1Lecture.getStringRepresentation(),
null);
SuggestionItem cs2103Week2 = new SuggestionItemImpl(toCs2103Week2.getStringRepresentation(), null);
SuggestionItem cs2103Week3 = new SuggestionItemImpl(toCs2103Week3.getStringRepresentation(), null);

List<SuggestionItem> expectedSuggestions = new ArrayList<>();
expectedSuggestions.add(cs2103);
expectedSuggestions.add(cs2103Week1);
expectedSuggestions.add(cs2103Week2);
expectedSuggestions.add(cs2103Week3);
expectedSuggestions.add(cs2103Week1Lecture);

List<SuggestionItem> suggestions = model.getSuggestions();

for (int i = 0; i < expectedSuggestions.size(); i++) {
SuggestionItem suggestion = suggestions.get(i);
SuggestionItem expectedSuggestion = expectedSuggestions.get(i);
assertEquals(expectedSuggestion.getProperty("displayText"), suggestion.getProperty("displayText"));
}

List<String> expectedInputs = new ArrayList<>();
expectedInputs.add(COMMAND_WORD + " " + toCs2103.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + toCs2103Week1.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + toCs2103Week2.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + toCs2103Week3.getStringRepresentation());
expectedInputs.add(COMMAND_WORD + " " + toCs2103Week1Lecture.getStringRepresentation());

for (int i = 0; i < expectedInputs.size(); i++) {
SuggestionItem suggestionItem = suggestions.get(i);
String expectedInput = expectedInputs.get(i);
suggestionItem.getAction().run();
String input = model.getInput();
assertEquals(expectedInput, input);
}
List<SuggestionItem> expectedSuggestions = SuggestionTestUtil.getExpectedSugForCs2103tPathInput();

SuggestionTestUtil.assertSuggestions(expectedSuggestions, suggestions);

List<String> expectedInputs = SuggestionTestUtil.getExpectedInputsForCs2103tPathInput(userInputWithoutPath);

SuggestionTestUtil.assertInputs(expectedInputs, suggestions, model);
}
}
Loading

0 comments on commit 08a9401

Please sign in to comment.