JSON encoding/decoding of players

This commit is contained in:
Jonathan Lamothe
2019-08-10 10:01:36 -04:00
parent 57b0b74cd1
commit 59eb7491f6
3 changed files with 102 additions and 3 deletions

View File

@@ -1,13 +1,20 @@
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
module TypesSpec (spec) where
import Data.Aeson (decode, encode)
import Data.ByteString.Lazy (ByteString)
import Lens.Micro ((&), (.~))
import Test.Hspec (Spec, context, describe, it, shouldBe)
import Text.RawString.QQ (r)
import Mtlstats.Types
spec :: Spec
spec = describe "Mtlstats.Types" pPointsSpec
spec = describe "Mtlstats.Types" $ do
pPointsSpec
playerSpec
pPointsSpec :: Spec
pPointsSpec = describe "pPoints" $ mapM_
(\(goals, assists, points) -> let
desc = "goals: " ++ show goals ++
@@ -24,3 +31,40 @@ pPointsSpec = describe "pPoints" $ mapM_
, ( 0, 1, 1 )
, ( 2, 3, 5 )
]
playerSpec :: Spec
playerSpec = describe "Player" $ do
describe "decode" $
it "should decode" $
decode playerJSON `shouldBe` Just player
describe "encode" $
it "should encode" $
decode (encode player) `shouldBe` Just player
player :: Player
player = newPlayer 1 "Joe" "centre"
& pYtd . psGoals .~ 2
& pYtd . psAssists .~ 3
& pYtd . psPMin .~ 4
& pLifetime . psGoals .~ 5
& pLifetime . psAssists .~ 6
& pLifetime . psPMin .~ 7
playerJSON :: ByteString
playerJSON = [r|
{ "number": 1
, "name": "Joe"
, "position": "centre"
, "ytd":
{ "goals": 2
, "assists": 3
, "penalty_mins": 4
}
, "lifetime":
{ "goals": 5
, "assists": 6
, "penalty_mins": 7
}
}|]