Created
December 23, 2014 15:27
-
-
Save kevinrutherford/47f4eebaae6ef72c199a to your computer and use it in GitHub Desktop.
Parts of a string calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ocpCalc; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class CalculatorFactory { | |
public Calculator create() { | |
BinaryInfixParser arithmeticParser = new BinaryInfixParser(operators()); | |
Parser parser = new EmptyStringParser(arithmeticParser); | |
return new Calculator(parser); | |
} | |
private Map<String, Operator> operators() { | |
Map<String, Operator> operators = new HashMap<String, Operator>(); | |
operators.put("+", new Addition()); | |
operators.put("-", new Subtraction()); | |
return operators; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ocpCalc; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class OcpCalculatorTester { | |
private Calculator calc; | |
@Before | |
public void setUp() throws Exception { | |
calc = new CalculatorFactory().create(); | |
} | |
@Test | |
public void simpleAddition() { | |
assertEquals(9, calc.eval("6 + 3")); | |
} | |
@Test | |
public void additionWithIntegers() { | |
assertEquals(46, calc.eval("12 + 34")); | |
} | |
@Test | |
public void simpleSubtraction() { | |
assertEquals(3, calc.eval("6 - 3")); | |
} | |
@Test | |
public void emptyStringIsZero() { | |
assertEquals(0, calc.eval(" ")); | |
} | |
// @Test | |
// public void integerValue() { | |
// assertEquals(234, calc.eval("234")); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment