Skip to content

Commit

Permalink
throw exception if too many operands (#347)
Browse files Browse the repository at this point in the history
Throw an exception if too many operands are found for an operator
  • Loading branch information
HSGamer authored Feb 11, 2023
1 parent 9c78907 commit 568e8ac
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public ASTNode toAbstractSyntaxTree() throws ParseException {
throw new ParseException(this.originalExpression, "Empty expression");
}

if (operandStack.size() > 1) {
throw new ParseException(this.originalExpression, "Too many operands");
}

return operandStack.pop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,76 @@ void testFunctionTooManyParameters() {
.isInstanceOf(ParseException.class)
.hasMessage("Too many parameters for function");
}

@Test
void testTooManyOperands() {
Expression expression = new Expression("1 2");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}

@Test
void testTooManyOperandsString() {
Expression expression = new Expression("Hello World");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}

@Test
void testTooManyOperandsStringWithNumbers() {
Expression expression = new Expression("Hello 1");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}

@Test
void testTooManyOperandsStringWithNumbersAndOperators() {
Expression expression = new Expression("Hello 1 + 1");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}

@Test
void testTooManyOperandsStringWithNumbersAndOperatorsAndBraces() {
Expression expression = new Expression("Hello 1 + (1 + 1)");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}

@Test
void testTooManyOperandsStringWithFunctions() {
Expression expression = new Expression("Hello ROUND(1,2)");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}

@Test
void testTooManyOperandsStringWithFunctionsAndBraces() {
Expression expression = new Expression("Hello ROUND(1,2) + (1 + 1)");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}

@Test
void testTooManyOperandsStringWithSpecialCharacters() {
Expression expression = new Expression("Hello, World");

assertThatThrownBy(expression::evaluate)
.isInstanceOf(ParseException.class)
.hasMessage("Too many operands");
}
}

0 comments on commit 568e8ac

Please sign in to comment.