Skip to content

Instantly share code, notes, and snippets.

@kevinrutherford
Created December 27, 2014 16:18
Show Gist options
  • Select an option

  • Save kevinrutherford/bf4c2057d0223f5c0f2c to your computer and use it in GitHub Desktop.

Select an option

Save kevinrutherford/bf4c2057d0223f5c0f2c to your computer and use it in GitHub Desktop.

Revisions

  1. kevinrutherford created this gist Dec 27, 2014.
    66 changes: 66 additions & 0 deletions CalculatorTests.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    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"));
    }

    }