From a2968595d8552006e7fbc7519420a3ed02784fcb Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Wed, 12 Feb 2020 00:27:28 -0500 Subject: [PATCH] implemented posSearchExact --- src/Mtlstats/Helpers/Position.hs | 7 ++++++- test/Helpers/PositionSpec.hs | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Mtlstats/Helpers/Position.hs b/src/Mtlstats/Helpers/Position.hs index e1a752e..6b6b93c 100644 --- a/src/Mtlstats/Helpers/Position.hs +++ b/src/Mtlstats/Helpers/Position.hs @@ -54,7 +54,12 @@ posSearchExact -- ^ The database -> Maybe Int -- ^ The index of the result (or 'Nothing' if not found) -posSearchExact = undefined +posSearchExact sStr db = case filter sFunc $ zip [0..] ps of + [] -> Nothing + (n,_):_ -> Just n + where + sFunc (_, pos) = sStr == pos + ps = getPositions db -- | Builds a callback function for when a 'Player' position is -- selected diff --git a/test/Helpers/PositionSpec.hs b/test/Helpers/PositionSpec.hs index f1197c6..b9d9c8b 100644 --- a/test/Helpers/PositionSpec.hs +++ b/test/Helpers/PositionSpec.hs @@ -30,6 +30,7 @@ import Mtlstats.Types spec :: Spec spec = describe "Position" $ do posSearchSpec + posSearchExactSpec getPositionsSpec posSearchSpec :: Spec @@ -48,6 +49,21 @@ posSearchSpec = describe "posSearch" $ mapM_ ) ] +posSearchExactSpec :: Spec +posSearchExactSpec = describe "posSearchExact" $ mapM_ + (\(input, expected) -> context ("input: " ++ show input) $ + it ("should be " ++ show expected) $ + posSearchExact input db `shouldBe` expected) + + -- input, expected + [ ( "foo", Just 2 ) + , ( "FOO", Nothing ) + , ( "bar", Just 0 ) + , ( "baz", Just 1 ) + , ( "a", Nothing ) + , ( "quux", Nothing ) + ] + getPositionsSpec :: Spec getPositionsSpec = describe "getPositions" $ let expected = ["bar", "baz", "foo"]