Skip to content

Instantly share code, notes, and snippets.

@kevinrutherford
Created December 27, 2014 16:18
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/bf4c2057d0223f5c0f2c to your computer and use it in GitHub Desktop.
Save kevinrutherford/bf4c2057d0223f5c0f2c to your computer and use it in GitHub Desktop.
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class OsheroveTests {
private Calculator calculator;
@Before
public void setUp() throws Exception {
StringFormat format = new MultiCharDelimiter(
new SelfDescribingList(
new VanillaNumbersList()));
NumberValidator[] numberRules = {
new PreventNegatives(),
new IgnoreThousands()
};
this.calculator = new Calculator(format, new NonZeroFunction(
new ArraySum(new NumberParser(numberRules))));
}
@Test
public void addsNumbers() {
assertEquals(0, calculator.add(""));
assertEquals(1, calculator.add("1"));
assertEquals(3, calculator.add("1,2"));
}
@Test
public void addsManyNumbers() {
assertEquals(290, calculator.add("1,2,34,56,7,89,101"));
}
@Test
public void allowsNewlineAsSeparator() {
assertEquals(12, calculator.add("3\n4\n5"));
}
@Test
public void supportArbitraryDelimiters() throws Exception {
assertEquals(3, calculator.add("//;\n1;2"));
assertEquals(7, calculator.add("//@\n3@4"));
}
@Test(expected = NegativesNotAllowed.class)
public void negativesNotAllowed() throws Exception {
calculator.add("//;\n1;2;-1");
}
@Test(expected = NegativesNotAllowed.class)
public void loneNegativeNotAllowed() throws Exception {
calculator.add("-1");
}
@Test
public void ignoreNumbersGreaterThan1000() throws Exception {
assertEquals(2, calculator.add("2,1003"));
}
@Test
public void longerDelimiters() throws Exception {
assertEquals(6, calculator.add("//[***]\n1***2***3"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment