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"]