commit b853df175a49f846950f061b2bba375efe7375ea Author: Thomas Papendieck Date: Wed Nov 18 09:42:34 2020 +0100 Beispiel TDD diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..55013c1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,24 @@ + + 4.0.0 + de.edu.hsfulda.ciip + tdd-pure-function + 0.1.0-SNAPSHOT + + 11 + 11 + + + + org.junit.jupiter + junit-jupiter-engine + 5.5.2 + test + + + org.junit.platform + junit-platform-runner + 1.5.2 + test + + + diff --git a/src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java b/src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java new file mode 100644 index 0000000..e2a5450 --- /dev/null +++ b/src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java @@ -0,0 +1,43 @@ +package de.edu.hsfulda.ccip.tdd.purefunction; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class BowlingCalculator { + @FunctionalInterface + interface FrameScorer { + int score(Matcher frameMatcher); + } + + private static final int INITIAL_SCORE = 0; + private static final Pattern SINGLE_ROLL_PATTERN = Pattern.compile("\\d"); + private static final Pattern SPARE_PATTERN = Pattern.compile("(\\d)/ ([-\\d])"); + + public int score(String rolls) { + int score = INITIAL_SCORE; + Matcher singleRollMatcher = SINGLE_ROLL_PATTERN.matcher(rolls); + score = scoreFrameType(score, singleRollMatcher, matcher -> Integer.parseInt(singleRollMatcher.group())); + + Matcher spareMatcher = SPARE_PATTERN.matcher(rolls); + score = scoreFrameType(score, spareMatcher, matcher -> scoreSpareFrame(spareMatcher)); + + return score; + } + + private int scoreSpareFrame(Matcher spareMatcher) { + int frameScore = 10 - Integer.parseInt(spareMatcher.group(1)); + String nextRoll = spareMatcher.group(2); + if(!"-".equalsIgnoreCase(nextRoll)) { + frameScore += Integer.parseInt(spareMatcher.group(1)); + } + return frameScore; + } + + private int scoreFrameType(int score, Matcher scoreMatcher, FrameScorer frameScorer) { + while (scoreMatcher.find()) { + score += frameScorer.score(scoreMatcher); + } + return score; + } + +} diff --git a/src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java b/src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java new file mode 100644 index 0000000..470c22d --- /dev/null +++ b/src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java @@ -0,0 +1,62 @@ +package de.edu.hsfulda.ccip.tdd.purefunction; + +import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class BowlingCalculatorTest { + + private BowlingCalculator bowlingCalculator; + + @BeforeEach + void setUp() throws Exception { + bowlingCalculator = new BowlingCalculator(); + } + + @Test + void resultOfWorstGameIsZero() { + // arrange + String rolls = "-- -- -- -- -- -- -- -- -- --"; + int expectedScore = 0; + // act + int score = bowlingCalculator.score(rolls); + // assert + assertThat("score", score, CoreMatchers.equalTo(expectedScore)); + } + + @Test + void addUpSingleRollInIncompleteFrames() { + // arrange + String rolls = "-1 23 -4 5- -6 -7 8- 9- -- --"; + int expectedScore = 45; + // act + int score = bowlingCalculator.score(rolls); + // assert + assertThat("score", score, CoreMatchers.equalTo(expectedScore)); + } + + @Test + void frameWithSpareCounts10() { + // arrange + String rolls = "-- -- 3/ -- 7/ -- -- -- -- --"; + int expectedScore = 20; + // act + int score = bowlingCalculator.score(rolls); + // assert + assertThat("score", score, CoreMatchers.equalTo(expectedScore)); + } + @Test + void frameWithSpareAddsNextRollTwice() { + // arrange + String rolls = "-- -- 3/ 2- 7/ 8- -- -- -- --"; + int expectedScore = 40; + // act + int score = bowlingCalculator.score(rolls); + // assert + assertThat("score", score, CoreMatchers.equalTo(expectedScore)); + } +}