diff options
Diffstat (limited to '')
| -rw-r--r-- | specs/Main.hs | 2 | ||||
| -rw-r--r-- | specs/Specs/AppStateSpec.hs | 17 | ||||
| -rw-r--r-- | specs/Specs/ArgsSpec.hs | 39 |
3 files changed, 55 insertions, 3 deletions
diff --git a/specs/Main.hs b/specs/Main.hs index 4af8694..36404fe 100644 --- a/specs/Main.hs +++ b/specs/Main.hs @@ -2,6 +2,7 @@ module Main (main) where import qualified Specs.AppEventSpec import qualified Specs.AppStateSpec +import qualified Specs.ArgsSpec import qualified Specs.KeySequenceSpec import qualified Specs.ViewSpec import Test.Hspec (hspec) @@ -10,5 +11,6 @@ main :: IO () main = hspec $ do Specs.AppEventSpec.test Specs.AppStateSpec.test + Specs.ArgsSpec.test Specs.KeySequenceSpec.test Specs.ViewSpec.test diff --git a/specs/Specs/AppStateSpec.hs b/specs/Specs/AppStateSpec.hs index de9d552..9864a0e 100644 --- a/specs/Specs/AppStateSpec.hs +++ b/specs/Specs/AppStateSpec.hs @@ -1,9 +1,11 @@ module Specs.AppStateSpec where import Chelleport.AppState (initialState, update) +import Chelleport.Args (Configuration (configMode)) import Chelleport.Types import Chelleport.Utils (uniq) import Control.Monad (join) +import Data.Default (Default (def)) import qualified SDL import Test.Hspec import TestUtils @@ -11,22 +13,31 @@ import TestUtils test :: SpecWith () test = do describe "#initialState" $ do + let config = def + it "returns the initial state of the app" $ do - ((initState, _), _) <- runWithMocks initialState + ((initState, _), _) <- runWithMocks $ initialState config stateKeySequence initState `shouldBe` [] stateIsMatched initState `shouldBe` False stateIsShiftPressed initState `shouldBe` False it "returns grid with 16x9 key sequences" $ do - ((initState, _), _) <- runWithMocks initialState + ((initState, _), _) <- runWithMocks $ initialState config length (stateGrid initState) `shouldBe` 9 stateGrid initState `shouldSatisfy` all ((== 16) . length) stateGrid initState `shouldSatisfy` all (all ((== 2) . length)) it "returns grid with all unique key sequences" $ do - ((initState, _), _) <- runWithMocks initialState + ((initState, _), _) <- runWithMocks $ initialState config join (stateGrid initState) `shouldBe` uniq (join $ stateGrid initState) + context "when config specifies mode" $ do + let currentConfig = config {configMode = defaultSearchMode} + + it "continues to set given mode" $ do + ((_, action), _) <- runWithMocks $ initialState currentConfig + action `shouldBe` Just (SetMode defaultSearchMode) + describe "#update" $ do let defaultState = defaultAppState {stateGrid = [["ABC", "DEF"], ["DJK", "JKL"]]} diff --git a/specs/Specs/ArgsSpec.hs b/specs/Specs/ArgsSpec.hs new file mode 100644 index 0000000..caaab75 --- /dev/null +++ b/specs/Specs/ArgsSpec.hs @@ -0,0 +1,39 @@ +module Specs.ArgsSpec where + +import Chelleport.Args (parseArgs) +import Chelleport.Types +import Data.Default (Default (def)) +import Test.Hspec + +test :: SpecWith () +test = do + describe "#parseArgs" $ do + context "when there are no args" $ do + it "parses default configuration" $ do + let config = parseArgs [] + config `shouldBe` Right def + + context "when args contains --help" $ do + it "enables show help without parsing the rest of the args" $ do + parseArgs ["--help"] `shouldBe` Right (def {configShowHelp = True}) + parseArgs ["--help", "-m", "mode"] `shouldBe` Right (def {configShowHelp = True}) + + context "when args contains -m or --mode with a valid mode" $ do + it "parses configuration with mode" $ do + parseArgs ["-m", "search"] `shouldBe` Right (def {configMode = defaultSearchMode}) + parseArgs ["--mode", "search"] `shouldBe` Right (def {configMode = defaultSearchMode}) + parseArgs ["-m", "hints"] `shouldBe` Right (def {configMode = defaultHintsMode}) + parseArgs ["--mode", "hints"] `shouldBe` Right (def {configMode = defaultHintsMode}) + + context "when args contains -m or --mode with an invalid mode" $ do + it "returns with error message" $ do + parseArgs ["--mode", "invalidmode"] `shouldBe` Left "Invalid mode: invalidmode" + parseArgs ["-m", "invalidmode"] `shouldBe` Left "Invalid mode: invalidmode" + + context "when args contains -m or --mode without any mode" $ do + it "returns with error message" $ do + parseArgs ["--mode"] `shouldBe` Left "Missing value for mode" + + context "when args contains an invalid flag" $ do + it "enables show help without parsing the rest of the args" $ do + parseArgs ["--foobar"] `shouldBe` Left "Unrecognized argument: --foobar" |
