aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 11:43:49 +0200
committerMaël Gassmann <mael.gassmann@students.bfh.ch>2021-06-11 11:43:49 +0200
commit49f30529d9cd2ed62902079a45d4d3c1f45afbda (patch)
tree0ec8186d4ad35ee9bfd8077a682b451a1b43951d
parent403c6a951e890d3e1914779ee35576859a38d33e (diff)
[+] Added JUnit tests (only testign valid expressions as of now)
-rw-r--r--calculator-java/pom.xml14
-rw-r--r--calculator-java/src/main/java/ch/bfh/Main.java2
-rw-r--r--calculator-java/src/test/java/ExpressionParserTest.java115
3 files changed, 129 insertions, 2 deletions
diff --git a/calculator-java/pom.xml b/calculator-java/pom.xml
index 1edc3ba..d181b7d 100644
--- a/calculator-java/pom.xml
+++ b/calculator-java/pom.xml
@@ -7,6 +7,20 @@
<groupId>ch.bfh</groupId>
<artifactId>calculator-java</artifactId>
<version>1.0-SNAPSHOT</version>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.13.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <version>5.7.0</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
diff --git a/calculator-java/src/main/java/ch/bfh/Main.java b/calculator-java/src/main/java/ch/bfh/Main.java
index 302f5ed..e57fc8a 100644
--- a/calculator-java/src/main/java/ch/bfh/Main.java
+++ b/calculator-java/src/main/java/ch/bfh/Main.java
@@ -1,8 +1,6 @@
package ch.bfh;
import ch.bfh.parser.ExpressionParser;
-
-import java.util.Arrays;
import java.util.Scanner;
public class Main {
diff --git a/calculator-java/src/test/java/ExpressionParserTest.java b/calculator-java/src/test/java/ExpressionParserTest.java
new file mode 100644
index 0000000..8a9814c
--- /dev/null
+++ b/calculator-java/src/test/java/ExpressionParserTest.java
@@ -0,0 +1,115 @@
+import ch.bfh.CalculatorLexer;
+import ch.bfh.parser.ExpressionParser;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+public class ExpressionParserTest {
+
+ // Only testing correct expressions first
+ @Test
+ public void emptyExpression() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(0.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void oneFactor() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("10");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(10.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void oneParenthesisedFactor() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("(10)");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(10.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void oneNegativeFactor() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("-10");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(-10.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void oneNegativeParenthesisedFactor() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("-(10)");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(-10.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void oneNegativeParenthesisedNegativeFactor() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("-(-10)");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(10.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void twoFactorSum() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("2+3");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(5.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void twoFactorSub() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("2-3");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(-1.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void twoFactorSumWithSub() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("2--3");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(5.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void twoFactorSumWithSubParenthesised() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("2-(-3)");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(5.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void twoFactorMul() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("2*3");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(6.0, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void twoFactorDiv() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("11/4");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(2.75, ep.getValue(), 0.0);
+ }
+
+ @Test
+ public void completeArithmeticOperationWithPriorities() {
+ CalculatorLexer cl = new CalculatorLexer();
+ cl.initLexer("(4+5)*3/4");
+ ExpressionParser ep = new ExpressionParser(cl);
+ assertEquals(6.75, ep.getValue(), 0.0);
+ }
+
+ //TODO - test error messages
+
+}