Skip to content

Commit

Permalink
backfill tests for getters/setters
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Apr 5, 2021
1 parent d19f759 commit d7ea770
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/Player.elm
Expand Up @@ -3,7 +3,7 @@ module Player exposing
, PlayerId, id, idSorter
, name
, rating, setRating
, matchesPlayed, setMatchesPlayed, incrementMatchesPlayed
, matchesPlayed, setMatchesPlayedTestOnly, incrementMatchesPlayed
, encode
, decoder
)
Expand All @@ -18,7 +18,7 @@ module Player exposing
@docs rating, setRating
@docs matchesPlayed, setMatchesPlayed, incrementMatchesPlayed
@docs matchesPlayed, setMatchesPlayedTestOnly, incrementMatchesPlayed
@docs encode, decode
Expand Down Expand Up @@ -88,7 +88,7 @@ rating (Player player) =

setRating : Int -> Player -> Player
setRating rating_ (Player player) =
Player { player | rating = rating_ }
Player { player | rating = max 0 rating_ }



Expand All @@ -100,8 +100,8 @@ matchesPlayed (Player player) =
player.matches


setMatchesPlayed : Int -> Player -> Player
setMatchesPlayed matches (Player player) =
setMatchesPlayedTestOnly : Int -> Player -> Player
setMatchesPlayedTestOnly matches (Player player) =
Player { player | matches = matches }


Expand Down
54 changes: 53 additions & 1 deletion tests/PlayerTest.elm
@@ -1,5 +1,6 @@
module PlayerTest exposing (..)

import Elo
import Expect
import Fuzz exposing (Fuzzer)
import Json.Decode as Decode
Expand Down Expand Up @@ -33,13 +34,64 @@ interopTest =
]


nameTest : Test
nameTest =
describe "name"
[ test "you get the same name out as you put in" <|
\_ ->
Player.init "Babaganoush"
|> Player.name
|> Expect.equal "Babaganoush"
]


matchesTest : Test
matchesTest =
describe "matches"
[ test "you start off having played zero matches" <|
\_ ->
Player.init "Pita"
|> Player.matchesPlayed
|> Expect.equal 0
, test "when you play a match, you can see it" <|
\_ ->
Player.init "Pita"
|> Player.incrementMatchesPlayed
|> Player.matchesPlayed
|> Expect.equal 1
]


ratingTest : Test
ratingTest =
describe "rating"
[ test "you start off at the default Elo rating" <|
\_ ->
Player.init "Shish Taouk"
|> Player.rating
|> Expect.equal Elo.initialRating
, fuzz (Fuzz.intRange 0 (Elo.initialRating * 2)) "your rating can be set to whatever" <|
\rating ->
Player.init "Shish Taouk"
|> Player.setRating rating
|> Player.rating
|> Expect.equal rating
, test "your rating cannot go below zero" <|
\_ ->
Player.init "Shish Taouk"
|> Player.setRating -1
|> Player.rating
|> Expect.equal 0
]


playerFuzzer : Fuzzer Player
playerFuzzer =
Fuzz.map3
(\name rating matches ->
Player.init name
|> Player.setRating rating
|> Player.setMatchesPlayed matches
|> Player.setMatchesPlayedTestOnly matches
)
nameFuzzer
(Fuzz.intRange 1000 3000)
Expand Down

0 comments on commit d7ea770

Please sign in to comment.