Browse Source

Beispiel TDD

master
Thomas Papendieck 3 years ago
commit
b853df175a
  1. 24
      pom.xml
  2. 43
      src/main/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculator.java
  3. 62
      src/test/java/de/edu/hsfulda/ccip/tdd/purefunction/BowlingCalculatorTest.java

24
pom.xml

@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.edu.hsfulda.ciip</groupId>
<artifactId>tdd-pure-function</artifactId>
<version>0.1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

43
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;
}
}

62
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));
}
}
Loading…
Cancel
Save