Skip to content

Instantly share code, notes, and snippets.

@tomphp
Created April 15, 2019 21:08
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 tomphp/e0b6e537e6d0472068f9dbde2152ecb6 to your computer and use it in GitHub Desktop.
Save tomphp/e0b6e537e6d0472068f9dbde2152ecb6 to your computer and use it in GitHub Desktop.
Incomplete tennis kata created with @JDurstberger at the LSCC coding dojo in the Moonpig office.
module TennisSpec where
import Test.Hspec
data Point
= Love
| Fifteen
| Thirty
| Forty
deriving (Enum, Eq, Show)
data Score
= Score Point Point
| Deuce
| Advantage Player
| Game Player
deriving (Eq, Show)
data Player = P1 | P2 deriving (Eq, Show)
score :: [Player] -> Score
score scores = foldl scorePoint (Score Love Love) scores
scorePoint :: Score -> Player -> Score
scorePoint Deuce player = Advantage player
scorePoint (Score Thirty Forty ) P1 = Deuce
scorePoint (Score Forty Thirty) P2 = Deuce
scorePoint (Score Forty _ ) P1 = Game P1
scorePoint (Score _ Forty ) P2 = Game P2
scorePoint (Score p1 p2 ) P1 = Score (succ p1) p2
scorePoint (Score p1 p2 ) P2 = Score p1 (succ p2)
spec :: Spec
spec = describe "something" $ do
it "scores love-love when noone has scored" $
score [] `shouldBe` Score Love Love
it "scores fifteen-love when player1 has scored once" $
score [P1] `shouldBe` Score Fifteen Love
it "scores love-fifteen when player2 has scored once" $
score [P2] `shouldBe` Score Love Fifteen
it "scores thirty-love when player1 has scored twice" $
score [P1, P1] `shouldBe` Score Thirty Love
it "scores love-thirty when player2 has scored twice" $
score [P2, P2] `shouldBe` Score Love Thirty
it "scores fifteen-fifteen when player1 scores and then player2 scores" $
score [P1, P2] `shouldBe` Score Fifteen Fifteen
it "scores fifteen-fifteen when player2 scores and then player1 scores" $
score [P2, P1] `shouldBe` Score Fifteen Fifteen
it "scores forty-love when player1 scored three times" $
score [P1, P1, P1] `shouldBe` Score Forty Love
it "scores a win for player1 one when player1 scored four times" $
score [P1, P1, P1, P1] `shouldBe` Game P1
it "scores a win for player2 one when player2 scored four times" $
score [P2, P2, P2, P2] `shouldBe` Game P2
it "scores a deuce when both players scored three times (ending with P2)" $
score [P1, P1, P1, P2, P2, P2] `shouldBe` Deuce
it "scores a deuce when both players scored three times (ending with P2)" $
score [P2, P2, P2, P1, P1, P1] `shouldBe` Deuce
it "scores an advantage for player1 " $
(score $ deuce <> [P1]) `shouldBe` Advantage P1
it "scores an advantage for player2 " $
(score $ deuce <> [P2]) `shouldBe` Advantage P2
deuce :: [Player]
deuce = replicate 3 P1 <> replicate 3 P2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment