forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from kiatkat/master
Update PPP, add testcases
- Loading branch information
Showing
10 changed files
with
265 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/test/data/JsonUserConfigsStorageTest/EmptyUserConfigs.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/test/data/JsonUserConfigsStorageTest/ExtraValuesUserConfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"settings" : { | ||
"semesterDays" : 180, | ||
"extra" : "some value" | ||
}, | ||
"profPlanFilePath" : "data\\profplan.json" | ||
} |
1 change: 1 addition & 0 deletions
1
src/test/data/JsonUserConfigsStorageTest/NotJsonFormatUserConfigs.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Not a json file! |
6 changes: 6 additions & 0 deletions
6
src/test/data/JsonUserConfigsStorageTest/TypicalUserConfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"settings" : { | ||
"semesterDays" : 180 | ||
}, | ||
"profPlanFilePath" : "data\\profplan.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/java/profplan/logic/parser/DescriptionCommandParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package profplan.logic.parser; | ||
|
||
import static profplan.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import profplan.logic.commands.DescriptionCommand; | ||
import profplan.model.task.Description; | ||
import profplan.testutil.TypicalIndexes; | ||
|
||
public class DescriptionCommandParserTest { | ||
|
||
private DescriptionCommandParser parser = new DescriptionCommandParser(); | ||
|
||
@Test | ||
public void parse_emptyArg_throwsParseException() { | ||
CommandParserTestUtil.assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
DescriptionCommand.MESSAGE_FULL_HELP)); | ||
} | ||
|
||
@Test | ||
public void parse_validArgs_returnsDescriptionCommand() { | ||
DescriptionCommand expectedDescriptionCommand = | ||
new DescriptionCommand(TypicalIndexes.INDEX_FIRST_TASK, new Description("test description")); | ||
|
||
CommandParserTestUtil.assertParseSuccess(parser, " 1 des/ test description ", | ||
expectedDescriptionCommand); | ||
} | ||
|
||
@Test | ||
public void parse_invalidIndex_throwsParseException() { | ||
CommandParserTestUtil.assertParseFailure(parser, " abc des/def ", | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DescriptionCommand.MESSAGE_FULL_HELP)); | ||
} | ||
|
||
@Test | ||
public void parse_emptyDescription_returnsDescriptionCommand() { | ||
DescriptionCommand expectedDescriptionCommand = | ||
new DescriptionCommand(TypicalIndexes.INDEX_FIRST_TASK, new Description("")); | ||
|
||
CommandParserTestUtil.assertParseSuccess(parser, " 1 des/ ", | ||
expectedDescriptionCommand); | ||
} | ||
|
||
@Test | ||
public void parse_specialCharactersDescription_returnsDescriptionCommand() { | ||
DescriptionCommand expectedDescriptionCommand = | ||
new DescriptionCommand(TypicalIndexes.INDEX_FIRST_TASK, new Description("!@#$%^&*()_")); | ||
|
||
CommandParserTestUtil.assertParseSuccess(parser, " 1 des/ !@#$%^&*()_ ", | ||
expectedDescriptionCommand); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/profplan/logic/parser/EditSettingsCommandParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package profplan.logic.parser; | ||
|
||
import static profplan.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import profplan.logic.commands.EditSettingsCommand; | ||
|
||
public class EditSettingsCommandParserTest { | ||
|
||
private EditSettingsCommandParser parser = new EditSettingsCommandParser(); | ||
|
||
@Test | ||
public void parse_emptyArgs_throwsParseException() { | ||
CommandParserTestUtil.assertParseFailure(parser, " ", | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditSettingsCommand.MESSAGE_FULL_HELP)); | ||
} | ||
|
||
@Test | ||
public void parse_invalidParameter_throwsParseException() { | ||
CommandParserTestUtil.assertParseFailure(parser, " set aaaaaa 123 ", | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditSettingsCommand.MESSAGE_FULL_HELP)); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/test/java/profplan/storage/JsonUserConfigsStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package profplan.storage; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import profplan.commons.core.Settings; | ||
import profplan.commons.exceptions.DataLoadingException; | ||
import profplan.model.UserConfigs; | ||
import profplan.testutil.Assert; | ||
|
||
public class JsonUserConfigsStorageTest { | ||
|
||
private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonUserConfigsStorageTest"); | ||
|
||
@TempDir | ||
public Path testFolder; | ||
|
||
@Test | ||
public void readUserConfigs_nullFilePath_throwsNullPointerException() { | ||
Assert.assertThrows(NullPointerException.class, () -> readUserConfigs(null)); | ||
} | ||
|
||
private Optional<UserConfigs> readUserConfigs(String userConfigsFileInTestDataFolder) throws DataLoadingException { | ||
Path configsFilePath = addToTestDataPathIfNotNull(userConfigsFileInTestDataFolder); | ||
return new JsonUserConfigsStorage(configsFilePath).readUserConfigs(configsFilePath); | ||
} | ||
|
||
@Test | ||
public void readUserConfigs_missingFile_emptyResult() throws DataLoadingException { | ||
assertFalse(readUserConfigs("NonExistentFile.json").isPresent()); | ||
} | ||
|
||
@Test | ||
public void readUserConfigs_notJsonFormat_exceptionThrown() { | ||
Assert.assertThrows(DataLoadingException.class, () -> readUserConfigs("NotJsonFormatUserConfigs.json")); | ||
} | ||
|
||
private Path addToTestDataPathIfNotNull(String userConfigsFileInTestDataFolder) { | ||
return userConfigsFileInTestDataFolder != null | ||
? TEST_DATA_FOLDER.resolve(userConfigsFileInTestDataFolder) | ||
: null; | ||
} | ||
|
||
@Test | ||
public void readUserConfigs_fileInOrder_successfullyRead() throws DataLoadingException { | ||
UserConfigs expected = getTypicalUserConfigs(); | ||
UserConfigs actual = readUserConfigs("TypicalUserConfig.json").get(); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void readUserConfigs_valuesMissingFromFile_defaultValuesUsed() throws DataLoadingException { | ||
UserConfigs actual = readUserConfigs("EmptyUserConfigs.json").get(); | ||
assertEquals(new UserConfigs(), actual); | ||
} | ||
|
||
@Test | ||
public void readUserConfigs_extraValuesInFile_extraValuesIgnored() throws DataLoadingException { | ||
UserConfigs expected = getTypicalUserConfigs(); | ||
UserConfigs actual = readUserConfigs("ExtraValuesUserConfig.json").get(); | ||
|
||
assertEquals(expected, actual); | ||
} | ||
|
||
private UserConfigs getTypicalUserConfigs() { | ||
UserConfigs userConfigs = new UserConfigs(); | ||
userConfigs.setSettings(new Settings(180)); | ||
userConfigs.setProfPlanFilePath(Paths.get("data\\profplan.json")); | ||
return userConfigs; | ||
} | ||
|
||
@Test | ||
public void saveConfigs_nullConfigs_throwsNullPointerException() { | ||
Assert.assertThrows(NullPointerException.class, () -> saveUserConfigs(null, "SomeFile.json")); | ||
} | ||
|
||
@Test | ||
public void saveUserConfigs_nullFilePath_throwsNullPointerException() { | ||
Assert.assertThrows(NullPointerException.class, () -> saveUserConfigs(new UserConfigs(), null)); | ||
} | ||
|
||
/** | ||
* Saves {@code userConfigs} at the specified {@code ConfigsFileInTestDataFolder} filepath. | ||
*/ | ||
private void saveUserConfigs(UserConfigs userConfigs, String configsFileInTestDataFolder) { | ||
try { | ||
new JsonUserConfigsStorage(addToTestDataPathIfNotNull(configsFileInTestDataFolder)) | ||
.saveUserConfigs(userConfigs); | ||
} catch (IOException ioe) { | ||
throw new AssertionError("There should not be an error writing to the file", ioe); | ||
} | ||
} | ||
|
||
@Test | ||
public void saveUserConfigs_allInOrder_success() throws DataLoadingException, IOException { | ||
|
||
UserConfigs original = new UserConfigs(); | ||
original.setSettings(new Settings(180)); | ||
|
||
Path pefsFilePath = testFolder.resolve("TempConfigs.json"); | ||
JsonUserConfigsStorage jsonUserConfigsStorage = new JsonUserConfigsStorage(pefsFilePath); | ||
|
||
//Try writing when the file doesn't exist | ||
jsonUserConfigsStorage.saveUserConfigs(original); | ||
UserConfigs readBack = jsonUserConfigsStorage.readUserConfigs().get(); | ||
assertEquals(original, readBack); | ||
|
||
//Try saving when the file exists | ||
original.setSettings(new Settings(180)); | ||
jsonUserConfigsStorage.saveUserConfigs(original); | ||
readBack = jsonUserConfigsStorage.readUserConfigs().get(); | ||
assertEquals(original, readBack); | ||
} | ||
|
||
} |