Skip to content

Instantly share code, notes, and snippets.

@westonal
Created April 6, 2017 13:16
Show Gist options
  • Select an option

  • Save westonal/df70ec5d85f3183fddaeb8b79bd249b2 to your computer and use it in GitHub Desktop.

Select an option

Save westonal/df70ec5d85f3183fddaeb8b79bd249b2 to your computer and use it in GitHub Desktop.
import com.redspace.fullbowlingkata.BowlingScorer
import com.redspace.fullbowlingkata.given
import org.junit.Assert.assertEquals
import org.junit.Test
class FullBowlingKataTest1 {
val spare: (BowlingScorer) -> Unit = {
it.bowl(5)
it.bowl(5)
}
val strike: (BowlingScorer) -> Unit = {
it.bowl(10)
}
fun bowlMany(n: Int, pins: Int): (BowlingScorer) -> Unit {
return {
it: BowlingScorer ->
for (i in 1..n) {
it.bowl(pins)
}
}
}
val allGutterBalls: (BowlingScorer) -> Unit = bowlMany(20, 0)
val givenBowlingScorer = given { BowlingScorer() }
val bowl: (Int) -> (BowlingScorer) -> Unit = { bowl -> { scorer -> scorer.bowl(bowl) } }
val scoreIs: (Int) -> (BowlingScorer) -> Unit = { expected -> { scorer -> assertEquals(expected, scorer.score()) } }
val noScore: (BowlingScorer) -> Unit = scoreIs(0)
@Test
fun `All gutter balls scores zero`() {
givenBowlingScorer whenever allGutterBalls then noScore
}
@Test
fun `All 1s scores 20`() {
givenBowlingScorer whenever bowlMany(20, 1) then scoreIs(20)
}
@Test
fun `Next ball after spare scores double`() {
givenBowlingScorer given spare whenever bowl(3) then scoreIs(10 + 3 * 2)
}
@Test
fun `A strike doubles the next two balls`() {
givenBowlingScorer given strike given bowl(3) whenever bowl(4) then scoreIs(10 + 3 * 2 + 4 * 2)
}
@Test
fun `Perfect game`() {
givenBowlingScorer whenever bowlMany(12, 10) then scoreIs(300)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment