aboutsummaryrefslogtreecommitdiff
path: root/calculator-java/src/test/java/StatementParserTest.java
diff options
context:
space:
mode:
authorMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 17:28:55 +0200
committerMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 17:28:55 +0200
commit98ddff97ec1d092b18ef2d176e83bd92f9671e03 (patch)
treeaf1457e05063421dc2860e6c033e58b28f9a8e4e /calculator-java/src/test/java/StatementParserTest.java
parent49f30529d9cd2ed62902079a45d4d3c1f45afbda (diff)
[+] Added StatementParser, [~] Restructured the Parsers
Diffstat (limited to 'calculator-java/src/test/java/StatementParserTest.java')
-rw-r--r--calculator-java/src/test/java/StatementParserTest.java91
1 files changed, 91 insertions, 0 deletions
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
+
+}