Skip to content
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

More tests + edge case bug fixing #396

Merged
merged 33 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b7a6428
Add files via upload
cyanoei Oct 31, 2019
49a2bd3
Delete parser_seq.png
cyanoei Oct 31, 2019
624721e
Merge pull request #2 from cyanoei/remove_png
cyanoei Oct 31, 2019
71a9884
Merge branch 'master' of https://github.com/cyanoei/duke-team
cyanoei Oct 31, 2019
47e9a97
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Oct 31, 2019
5c9c9da
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Oct 31, 2019
000b5c5
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 1, 2019
b2f8325
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 1, 2019
c337794
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 1, 2019
6856c32
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 5, 2019
3d7d407
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 6, 2019
3dca13e
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 6, 2019
a1175e8
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 7, 2019
5253348
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 7, 2019
7d9f38e
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 7, 2019
4c46411
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 8, 2019
be62b16
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 8, 2019
aede86e
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 8, 2019
8682b7d
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 9, 2019
d2faaa1
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 9, 2019
a92508e
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 9, 2019
c288d4a
Merge commit 'eaa71dca217eb6466d6b4f6821f06cd8cd1304d8'
cyanoei Nov 9, 2019
d4c697c
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 9, 2019
16932f4
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 10, 2019
3138fa8
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 10, 2019
d7c30cf
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/main
cyanoei Nov 10, 2019
cce1458
Bug fix for quantity and parser/loan testing
cyanoei Nov 10, 2019
597d05b
More tests
cyanoei Nov 11, 2019
880339a
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/ma…
cyanoei Nov 11, 2019
06a18c9
Travis ah
cyanoei Nov 11, 2019
6cd7e99
Remove some TODOs
cyanoei Nov 11, 2019
8affc51
Edit the EditStockCommand to return only the modifiable properties.
cyanoei Nov 11, 2019
d6302cf
Merge branch 'master' of https://github.com/AY1920S1-CS2113T-F09-3/ma…
cyanoei Nov 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ public String execute(StockList list, Ui ui, Storage storage) throws BadInputExc
+ "stockcode: %s\n"
+ "quantity: %d\n"
+ "description: %s\n"
+ "minimum: %s\n"
+ "lost: %s\n"
+ "loaned: %s\n"
+ "available: %s",
+ "minimum: %s",
edited.getStockType(), edited.getStockCode(), edited.getQuantity(), edited.getDescription(),
edited.getMinimum(), edited.getLost(), edited.getLoaned(), edited.numAvailable());
edited.getMinimum());
output += QuantityManager.checkMinimum(edited);
storage.save(list);
ui.print(output);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/eggventory/logic/parsers/ParseAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ private int processAddStockMinimum(String input) throws BadInputException {
String minString = parseMinimum[1].split(" ", 2)[0]; //Take only the first word after " -m ".

Parser.isCheckIsInteger(minString, "minimum quantity");
Parser.isNotNegative(Integer.parseInt(minString), "minimum quantity");

return Integer.parseInt(minString);
}
}
Expand Down Expand Up @@ -77,6 +79,8 @@ private Command processAddStock(String input) throws BadInputException {
}

Parser.isCheckIsInteger(addInput[2], "quantity");
Parser.isNotNegative(Integer.parseInt(addInput[2]), "quantity");


return new AddStockCommand(CommandType.ADD, addInput[0], addInput[1],
Integer.parseInt(addInput[2]), addInput[3], minimumQuantity);
Expand Down Expand Up @@ -164,6 +168,9 @@ private Command processAddLoan(String input) throws InsufficientInfoException, B
}

Parser.isCheckIsInteger(addInput[2], "loan quantity");
Parser.isNotNegative(Integer.parseInt(addInput[2]), "loan quantity");
Parser.isNotZero(Integer.parseInt(addInput[2]), "loan quantity");


return new AddLoanCommand(CommandType.ADD, addInput[0], addInput[1], Integer.parseInt(addInput[2]));
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/eggventory/logic/parsers/ParseEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ private Command processEditStock(String input) throws BadInputException {

case "quantity":
Parser.isCheckIsInteger(editInput[2], "quantity");
Parser.isNotNegative(Integer.parseInt(editInput[2]), "quantity");

return new EditStockCommand(CommandType.EDIT, stockCode, StockProperty.QUANTITY,
editInput[2]);
case "description":
return new EditStockCommand(CommandType.EDIT, stockCode, StockProperty.DESCRIPTION,
editInput[2]);
case "minimum":
Parser.isCheckIsInteger(editInput[2], "minimum quantity");
Parser.isNotNegative(Integer.parseInt(editInput[2]), "minimum quantity");

return new EditStockCommand(CommandType.EDIT, stockCode, StockProperty.MINIMUM, editInput[2]);
default:
throw new BadInputException("The property you are trying to edit does not exist.");
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/eggventory/logic/parsers/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import eggventory.logic.commands.Command;
import eggventory.logic.commands.CommandDictionary;
import eggventory.logic.commands.HelpCommand;
import eggventory.logic.commands.UndoCommand;
import eggventory.logic.commands.RedoCommand;

import eggventory.logic.commands.UndoCommand;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -78,6 +77,7 @@ public static boolean isCommandComplete(String command, int reqArguments) {
/**
* Checks if a string input is an integer.
* @param testInteger the input to test.
* @throws BadInputException if the input is not an integer.
*/
public static void isCheckIsInteger(String testInteger, String inputName) throws BadInputException {
try {
Expand All @@ -87,6 +87,28 @@ public static void isCheckIsInteger(String testInteger, String inputName) throws
}
}

/**
* Checks if a int input is negative.
* @param testInteger the input to test.
* @throws BadInputException if the input is negative.
*/
public static void isNotNegative(int testInteger, String inputName) throws BadInputException {
if (testInteger < 0) {
throw new BadInputException(String.format("Sorry, the input for %s cannot be negative!", inputName));
}
}

/**
* Checks if a int input is zero.
* @param testInteger the input to test.
* @throws BadInputException if the input is zero.
*/
public static void isNotZero(int testInteger, String inputName) throws BadInputException {
if (testInteger == 0) {
throw new BadInputException(String.format("Sorry, the input for %s cannot be 0!", inputName));
}
}

/**
* Checks if the command keyword (first word is valid).
* Determines what to do with the remaining string depending on the command.
Expand Down
20 changes: 1 addition & 19 deletions src/main/java/eggventory/model/items/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public Stock(String stockType, String stockCode, int quantity, String descriptio
this.stockCode = stockCode;
this.quantity = quantity;
this.description = description;
this.loaned = 0; //TODO: Determine whether to remove this attribute or update it from the LoanList.
this.lost = 0;
this.lost = 0; //For future use.
this.minimum = 0;
}

Expand Down Expand Up @@ -151,23 +150,6 @@ public void quantitySanityCheck(int quantity) throws BadInputException {
}
}

/**
* Gets the number of this stock that is on loan.
* @return loaned the number of loaned items.
*/
public int getLoaned() {
return loaned;
}


/**
* Sets the number of this stock on loan. To be used by the 'loan' command.
* @param loaned the number of items on loan.
*/
public void setLoaned(int loaned) {
this.loaned = loaned;
}

/**
* Gets the number of this stock that is lost.
* @return lost the number of lost items.
Expand Down
38 changes: 1 addition & 37 deletions src/main/java/eggventory/model/items/StockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import eggventory.commons.enums.StockProperty;
import eggventory.commons.exceptions.BadInputException;
import java.util.List;

import java.util.ArrayList;

//@@author Deculsion
Expand Down Expand Up @@ -107,38 +107,6 @@ public Stock setStockQuantity(String stockCode, int quantity) throws BadInputExc
return null;
}

/**
* Accesses and updates loaned quantity of a stock.
* @param stockCode The current stockcode reffering to this stock
* @param quantity The new loaned quantity to change to
* @return Stock if stockCode is found, else null
*/
public Stock setStockLoaned(String stockCode, int quantity) {
for (Stock stock : stocks) {
if (stock.getStockCode().equals(stockCode)) {
stock.setLoaned(quantity);
return stock;
}
}
return null;
}

/**
* Accesses and updates lost quantity of a stock.
* @param stockCode The current stockcode reffering to this stock
* @param quantity The new lost quantity to change to
* @return Stock if stockCode is found, else null
*/
public Stock setStockLost(String stockCode, int quantity) {
for (Stock stock : stocks) {
if (stock.getStockCode().equals(stockCode)) {
stock.setLost(quantity);
return stock;
}
}
return null;
}

/**
* Accesses and updates stock description of a stock.
* @param stockCode The current stockcode reffering to this stock
Expand Down Expand Up @@ -242,10 +210,6 @@ public Stock setStock(String stockCode, StockProperty property, String newValue)
return this.setStockCode(stockCode, newValue);
case QUANTITY:
return this.setStockQuantity(stockCode, Integer.parseInt(newValue));
case LOANED:
return this.setStockLoaned(stockCode, Integer.parseInt(newValue));
case LOST:
return this.setStockLost(stockCode, Integer.parseInt(newValue));
case DESCRIPTION:
return this.setStockDescription(stockCode, newValue);
case MINIMUM:
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/eggventory/model/items/UniqueStock.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

//@@author cyanoei

//TODO: Since 5 Nov the quantity-related features have been broken by the sanity check for negatives.
//Since 5 Nov the quantity-related features have been broken by the sanity check for negatives.
/**
* A child of the Stock class.
* A UniqueStock is a group of many items (eg. Arduino Uno) that share the same stockCode, description etc.,
Expand Down Expand Up @@ -85,7 +85,7 @@ public void deleteUnique(int index) {

//Note: toString works as per normal, treating UniqueStock as a CollectiveStock.

//TODO: Provide a 'detailed' version of the print string to show individual items.
//Provide a 'detailed' version of the print string to show individual items.
public void printUniqueStocks() {
/*
A header with all the usual info, maybe using toString.
Expand All @@ -99,8 +99,8 @@ public void printUniqueStocks() {
*/
}

//TODO: Update both print and save method in Item class, and iterate through all of them here.
// Should be similar to StockType printing methods.
//Update both print and save method in Item class, and iterate through all of them here.
//Should be similar to StockType printing methods.
/**
* Formats all stock details appropriately to be saved to file.
* @return the string to save.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/eggventory/model/loans/Loan.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eggventory.model.loans;

import eggventory.model.PersonList;

import java.util.ArrayList;
import java.util.Calendar;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ void testExecuteEditStockCode_ValidStockCode_Success() throws BadInputException
+ "stockcode: #TNEW\n"
+ "quantity: 100\n"
+ "description: Test\n"
+ "minimum: 0\n"
+ "lost: 0\n"
+ "loaned: 0\n"
+ "available: 100";
+ "minimum: 0";
String output = new EditStockCommand(CommandType.EDIT, "#T", StockProperty.STOCKCODE,
"#TNEW").execute(testStockList,testCli,testStorage);
assertEquals(expected,output);
Expand All @@ -60,10 +57,7 @@ void testExecuteEditStockQuantity_Success() throws BadInputException {
+ "stockcode: #T\n"
+ "quantity: 5000\n"
+ "description: Test\n"
+ "minimum: 0\n"
+ "lost: 0\n"
+ "loaned: 0\n"
+ "available: 5000";
+ "minimum: 0";
String output = new EditStockCommand(CommandType.EDIT, "#T", StockProperty.QUANTITY,
"5000").execute(testStockList,testCli,testStorage);
assertEquals(expected,output);
Expand All @@ -78,10 +72,7 @@ void testExecuteEditStockDescription_Success() throws BadInputException {
+ "stockcode: #T\n"
+ "quantity: 100\n"
+ "description: Test NEW\n"
+ "minimum: 0\n"
+ "lost: 0\n"
+ "loaned: 0\n"
+ "available: 100";
+ "minimum: 0";
String output = new EditStockCommand(CommandType.EDIT, "#T", StockProperty.DESCRIPTION,
"Test NEW").execute(testStockList,testCli,testStorage);
assertEquals(expected,output);
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/eggventory/logic/parsers/ParseEditTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,16 @@ public void testParseEdit_BadFirstInput_ThrowsBadInputException() {
assertDoesNotThrow(() -> testParser.parse("stocktype arg1 arg2"));
assertThrows(BadInputException.class, () -> testParser.parse("sommething arg1 arg2"));
}

//@@author cyanoei
@Test
public void testParseEdit_NegativeQuantity_ThrowsBadInputException() {
assertDoesNotThrow(() -> testParser.parse("stock test quantity 1"));
assertThrows(BadInputException.class, () -> testParser.parse("stock test quantity -1"));

assertDoesNotThrow(() -> testParser.parse("stock test minimum 1"));
assertThrows(BadInputException.class, () -> testParser.parse("stock test minimum -1"));
}

//@@author
}
25 changes: 25 additions & 0 deletions src/test/java/eggventory/logic/parsers/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ public void checkIsIntegerOutput_StringInput_MessageOutput() {

}

@Test
public void testIfNotNegative_NonNegativeInput_DoesNotThrow() {
assertDoesNotThrow(() -> Parser.isNotNegative(0, "test"));
assertDoesNotThrow(() -> Parser.isNotNegative(100, "test"));
}

@Test
public void testIfNotNegative_NegativeInput_ThrowsBadInputException() {
assertDoesNotThrow(() -> Parser.isNotNegative(100, "test"));
assertThrows(BadInputException.class,() -> Parser.isNotNegative(-1, "test"));
}

@Test
public void testIfNotZero_NonZero_DoesNotThrow() {
assertDoesNotThrow(() -> Parser.isNotZero(-1, "test"));
assertDoesNotThrow(() -> Parser.isNotZero(1, "test"));
}

@Test
public void testIfNotZero_IsZero_ThrowsBadInputException() {
assertDoesNotThrow(() -> Parser.isNotZero(100, "test"));
assertThrows(BadInputException.class, () -> Parser.isNotZero(0, "test"));
}


@Test
public void checkReservedInput_InputNotReserved_ReturnsFalse() {
String[] testInputs = {"Rishab", "Raghav", "Rebecca", "Dana", "Yanbo", "Resistor", "Arduino", "Eggventory"};
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/eggventory/model/LoanListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package eggventory.model;

import eggventory.model.loans.Loan;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class LoanListTest {

private String stockCode = "R5";
private String matric = "A1";

Loan loan = new Loan(matric, stockCode, 10);

@Test
void testDeleteLoan_LoanExists_ReturnTrue() {
LoanList.addLoan("A1", "R5", 10);
Assertions.assertTrue(LoanList.deleteLoan("A1", "R5"));
}

@Test
void testDeleteLoan_LoanDoesNotExist_ReturnFalse() {
LoanList.addLoan("A1", "R5", 10);
Assertions.assertFalse(LoanList.deleteLoan("A2", "R5"));

Assertions.assertFalse(LoanList.deleteLoan("A1", "R1"));

Assertions.assertFalse(LoanList.deleteLoan("a2", "r5"));
}

@Test
void getStockLoanedQuantity_StockDoesNotExist_ReturnZero() {
Assertions.assertEquals(0, LoanList.getStockLoanedQuantity("not stockcode"));
Assertions.assertEquals(0, LoanList.getStockLoanedQuantity(""));
}

@Test
void getStockLoanedQuantity_StockExists_ReturnQuantity() {
Assertions.assertEquals(0, LoanList.getStockLoanedQuantity("abc"));
LoanList.addLoan("A1", "abc", 100);
Assertions.assertEquals(100, LoanList.getStockLoanedQuantity("abc"));
}
}
Loading