From 61361516145e520a40a43a7711c58c3cfb4b4297 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Mon, 2 Sep 2019 18:50:21 -0400 Subject: [PATCH] implemented homeTeam and awayTeam --- src/Mtlstats/Config.hs | 26 ++++++++++++++++++++++++++ src/Mtlstats/Types.hs | 20 ++++++++++++++++++++ test/TypesSpec.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/Mtlstats/Config.hs diff --git a/src/Mtlstats/Config.hs b/src/Mtlstats/Config.hs new file mode 100644 index 0000000..e9600be --- /dev/null +++ b/src/Mtlstats/Config.hs @@ -0,0 +1,26 @@ +{- | + +mtlstats +Copyright (C) 2019 Rhéal Lamothe + + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or (at +your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +-} + +module Mtlstats.Config (myTeam) where + +-- | The name of the team whose stats we're tracking +myTeam :: String +myTeam = "MONTREAL" diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 22675a4..9a3e9cb 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -97,6 +97,8 @@ module Mtlstats.Types ( -- ** GameState Helpers teamScore, otherScore, + homeTeam, + awayTeam, gameWon, gameLost, gameTied, @@ -124,6 +126,8 @@ import Lens.Micro (Lens', lens, (&), (^.), (.~)) import Lens.Micro.TH (makeLenses) import UI.NCurses (Curses, Update) +import Mtlstats.Config + -- | Action which maintains program state type Action a = StateT ProgState Curses a @@ -509,6 +513,22 @@ otherScore s = case s ^. gameType of Just AwayGame -> s ^. homeScore Nothing -> Nothing +-- | Returns the name of the home team (or an empty string if +-- unavailable) +homeTeam :: GameState -> String +homeTeam gs = case gs^.gameType of + Just HomeGame -> myTeam + Just AwayGame -> gs^.otherTeam + Nothing -> "" + +-- | Returns the name of the visiting team (or an empty string if +-- unavailable) +awayTeam :: GameState -> String +awayTeam gs = case gs^.gameType of + Just HomeGame -> gs^.otherTeam + Just AwayGame -> myTeam + Nothing -> "" + -- | Checks if the game was won gameWon :: GameState -> Maybe Bool gameWon gs = (>) <$> teamScore gs <*> otherScore gs diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index 6ef00b3..b54f85a 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -30,6 +30,7 @@ import qualified Data.HashMap.Strict as HM import Lens.Micro (Lens', (&), (^.), (.~), (?~)) import Test.Hspec (Spec, context, describe, it, shouldBe) +import Mtlstats.Config import Mtlstats.Types import qualified Types.MenuSpec as Menu @@ -43,6 +44,8 @@ spec = describe "Mtlstats.Types" $ do gameStateLSpec teamScoreSpec otherScoreSpec + homeTeamSpec + awayTeamSpec gameWonSpec gameLostSpec gameTiedSpec @@ -248,6 +251,44 @@ dbJSON = Object $ HM.fromList , ( "away_game_stats", gameStatsJSON 2 ) ] +homeTeamSpec :: Spec +homeTeamSpec = describe "homeTeam" $ do + let + gs gt = newGameState + & gameType .~ gt + & otherTeam .~ "foo" + + context "unknown game type" $ + it "should return an empty string" $ + homeTeam (gs Nothing) `shouldBe` "" + + context "home game" $ + it ("should return " ++ show myTeam) $ + homeTeam (gs $ Just HomeGame) `shouldBe` myTeam + + context "away game" $ + it "should return \"foo\"" $ + homeTeam (gs $ Just AwayGame) `shouldBe` "foo" + +awayTeamSpec :: Spec +awayTeamSpec = describe "awayTeam" $ do + let + gs gt = newGameState + & gameType .~ gt + & otherTeam .~ "foo" + + context "unknown game type" $ + it "should return an empty string" $ + awayTeam (gs Nothing) `shouldBe` "" + + context "home game" $ + it "should return \"foo\"" $ + awayTeam (gs $ Just HomeGame) `shouldBe` "foo" + + context "away game" $ + it ("should return " ++ show myTeam) $ + awayTeam (gs $ Just AwayGame) `shouldBe` myTeam + gameWonSpec :: Spec gameWonSpec = describe "gameWon" $ mapM_ (\(t, h, a, expected) -> let