Skip to content

Instantly share code, notes, and snippets.

@kevinrutherford
Created December 23, 2014 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinrutherford/47f4eebaae6ef72c199a to your computer and use it in GitHub Desktop.
Save kevinrutherford/47f4eebaae6ef72c199a to your computer and use it in GitHub Desktop.
Parts of a string calculator
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;
}
}
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