From 98ddff97ec1d092b18ef2d176e83bd92f9671e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Fri, 11 Jun 2021 17:28:55 +0200 Subject: [+] Added StatementParser, [~] Restructured the Parsers --- .../src/test/java/ExpressionParserTest.java | 2 +- .../src/test/java/StatementParserTest.java | 91 ++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 calculator-java/src/test/java/StatementParserTest.java (limited to 'calculator-java/src/test/java') diff --git a/calculator-java/src/test/java/ExpressionParserTest.java b/calculator-java/src/test/java/ExpressionParserTest.java index 8a9814c..b15e844 100644 --- a/calculator-java/src/test/java/ExpressionParserTest.java +++ b/calculator-java/src/test/java/ExpressionParserTest.java @@ -110,6 +110,6 @@ public class ExpressionParserTest { assertEquals(6.75, ep.getValue(), 0.0); } - //TODO - test error messages + //TODO - test error detection } diff --git a/calculator-java/src/test/java/StatementParserTest.java b/calculator-java/src/test/java/StatementParserTest.java new file mode 100644 index 0000000..4b2e635 --- /dev/null +++ b/calculator-java/src/test/java/StatementParserTest.java @@ -0,0 +1,91 @@ +import ch.bfh.CalculatorLexer; +import ch.bfh.parser.StatementParser; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class StatementParserTest { + + StatementParser sp = new StatementParser(); + // Only testing correct expressions first + @Test + public void emptyExpression() { + sp.parseStatement(""); + assertEquals(0.0, sp.getValue(), 0.0); + } + + @Test + public void oneFactor() { + sp.parseStatement("10"); + assertEquals(10.0, sp.getValue(), 0.0); + } + + @Test + public void oneParenthesisedFactor() { + sp.parseStatement("(10)"); + assertEquals(10.0, sp.getValue(), 0.0); + } + + @Test + public void oneNegativeFactor() { + sp.parseStatement("-10"); + assertEquals(-10.0, sp.getValue(), 0.0); + } + + @Test + public void oneNegativeParenthesisedFactor() { + sp.parseStatement("-(10)"); + assertEquals(-10.0, sp.getValue(), 0.0); + } + + @Test + public void oneNegativeParenthesisedNegativeFactor() { + sp.parseStatement("-(-10)"); + assertEquals(10.0, sp.getValue(), 0.0); + } + + @Test + public void twoFactorSum() { + sp.parseStatement("2+3"); + assertEquals(5.0, sp.getValue(), 0.0); + } + + @Test + public void twoFactorSub() { + sp.parseStatement("2-3"); + assertEquals(-1.0, sp.getValue(), 0.0); + } + + @Test + public void twoFactorSumWithSub() { + sp.parseStatement("2--3"); + assertEquals(5.0, sp.getValue(), 0.0); + } + + @Test + public void twoFactorSumWithSubParenthesised() { + sp.parseStatement("2-(-3)"); + assertEquals(5.0, sp.getValue(), 0.0); + } + + @Test + public void twoFactorMul() { + sp.parseStatement("2*3"); + assertEquals(6.0, sp.getValue(), 0.0); + } + + @Test + public void twoFactorDiv() { + sp.parseStatement("11/4"); + assertEquals(2.75, sp.getValue(), 0.0); + } + + @Test + public void completeArithmeticOperationWithPriorities() { + sp.parseStatement("(4+5)*3/4"); + assertEquals(6.75, sp.getValue(), 0.0); + } + + //TODO - test error detection + +} -- cgit v1.2.3