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 }