From e0dd80079d6b65c06b6b979fbc894bbe0c298e6f Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Fri, 6 Sep 2019 23:25:13 -0400 Subject: [PATCH] implemented ynHandler --- src/Mtlstats/Handlers.hs | 33 ++++++++++++++++++++++++++++ test/HandlersSpec.hs | 46 ++++++++++++++++++++++++++++++++++++++++ test/Spec.hs | 2 ++ 3 files changed, 81 insertions(+) create mode 100644 src/Mtlstats/Handlers.hs create mode 100644 test/HandlersSpec.hs diff --git a/src/Mtlstats/Handlers.hs b/src/Mtlstats/Handlers.hs new file mode 100644 index 0000000..1c64bf9 --- /dev/null +++ b/src/Mtlstats/Handlers.hs @@ -0,0 +1,33 @@ +{- | + +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.Handlers (ynHandler) where + +import Data.Char (toUpper) +import qualified UI.NCurses as C + +-- | Handler for a yes/no prompt +ynHandler :: C.Event -> Maybe Bool +ynHandler (C.EventCharacter c) = case toUpper c of + 'Y' -> Just True + 'N' -> Just False + _ -> Nothing +ynHandler _ = Nothing diff --git a/test/HandlersSpec.hs b/test/HandlersSpec.hs new file mode 100644 index 0000000..2c9457d --- /dev/null +++ b/test/HandlersSpec.hs @@ -0,0 +1,46 @@ +{- + +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 HandlersSpec (spec) where + +import Test.Hspec (Spec, context, describe, it, shouldBe) +import qualified UI.NCurses as C + +import Mtlstats.Handlers + +spec :: Spec +spec = describe "Mtlstats.Handlers" + ynHandlerSpec + +ynHandlerSpec :: Spec +ynHandlerSpec = describe "ynHandler" $ mapM_ + (\(desc, event, expected) -> + context desc $ + it ("should be " ++ show expected) $ + ynHandler event `shouldBe` expected) + -- description, event, expected + [ ( "Y pressed", C.EventCharacter 'Y', Just True ) + , ( "y pressed", C.EventCharacter 'y', Just True ) + , ( "N pressed", C.EventCharacter 'N', Just False ) + , ( "n pressed", C.EventCharacter 'n', Just False ) + , ( "x pressed", C.EventCharacter 'x', Nothing ) + , ( "other event", C.EventResized, Nothing ) + ] diff --git a/test/Spec.hs b/test/Spec.hs index 8982a05..54c9e24 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -23,6 +23,7 @@ import Test.Hspec (hspec) import qualified ActionsSpec as Actions import qualified FormatSpec as Format +import qualified HandlersSpec as Handlers import qualified TypesSpec as Types main :: IO () @@ -30,3 +31,4 @@ main = hspec $ do Types.spec Actions.spec Format.spec + Handlers.spec