import ch.bfh.parser.StatementParser; import org.junit.Test; import static org.junit.Assert.assertEquals; public class StatementParserTest { StatementParser sp = new StatementParser(); @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); } @Test public void variableTest() { sp.parseStatement("let a = 5"); assertEquals(5.0, sp.getValue(), 0.0); sp.parseStatement("a"); assertEquals(5.0, sp.getValue(), 0.0); sp.parseStatement("(a)"); assertEquals(5.0, sp.getValue(), 0.0); sp.parseStatement("-a"); assertEquals(-5.0, sp.getValue(), 0.0); sp.parseStatement("-(a)"); assertEquals(-5.0, sp.getValue(), 0.0); sp.parseStatement("-(-a)"); assertEquals(5.0, sp.getValue(), 0.0); sp.parseStatement("let b=5*50"); assertEquals(250.0, sp.getValue(), 0.0); sp.parseStatement("b/a"); assertEquals(50.0, sp.getValue(), 0.0); sp.parseStatement("b--a"); assertEquals(255.0, sp.getValue(), 0.0); sp.parseStatement("b+a/2"); assertEquals(252.5, sp.getValue(), 0.0); sp.parseStatement("let z = b+a/2"); assertEquals(252.5, sp.getValue(), 0.0); sp.parseStatement("let a = b+a"); assertEquals(255.0, sp.getValue(), 0.0); } }