mtlstats/test/UtilSpec.hs

96 lines
2.6 KiB
Haskell
Raw Permalink Normal View History

2019-09-18 02:16:10 -04:00
{-
mtlstats
Copyright (C) 2019 Rhéal Lamothe
<rheal.lamothe@gmail.com>
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 <https://www.gnu.org/licenses/>.
-}
module UtilSpec (spec) where
2019-10-11 01:10:50 -04:00
import qualified Data.Map as M
2019-09-18 02:16:10 -04:00
import Test.Hspec (Spec, context, describe, it, shouldBe)
import Mtlstats.Util
spec :: Spec
2019-10-11 01:10:50 -04:00
spec = describe "Mtlstats.Util" $ do
2019-09-18 02:16:10 -04:00
nthSpec
2019-10-11 01:10:50 -04:00
modifyNthSpec
updateMapSpec
2019-10-11 23:13:00 -04:00
sliceSpec
2019-09-18 02:16:10 -04:00
nthSpec :: Spec
nthSpec = describe "nth" $ mapM_
(\(n, expected) -> context (show n) $
it ("should be " ++ show expected) $ let
xs = ["foo", "bar", "baz"]
in nth n xs `shouldBe` expected)
-- index, expected
[ ( 0, Just "foo" )
, ( 1, Just "bar" )
, ( 2, Just "baz" )
, ( 3, Nothing )
, ( -1, Nothing )
]
2019-10-11 01:10:50 -04:00
modifyNthSpec :: Spec
modifyNthSpec = describe "modifyNth" $ do
2019-11-12 17:01:08 -05:00
let list = [1, 2, 3] :: [Int]
2019-10-11 01:10:50 -04:00
context "in bounds" $
it "should modify the value" $
2019-11-12 17:01:08 -05:00
modifyNth 1 succ list `shouldBe` [1, 3, 3]
2019-10-11 01:10:50 -04:00
context "out of bounds" $
it "should not modify the value" $
2019-11-12 17:01:08 -05:00
modifyNth 3 succ list `shouldBe` [1, 2, 3]
2019-10-11 01:10:50 -04:00
context "negative index" $
it "should not modify the value" $
2019-11-12 17:01:08 -05:00
modifyNth (-1) succ list `shouldBe` [1, 2, 3]
2019-10-11 01:10:50 -04:00
updateMapSpec :: Spec
updateMapSpec = describe "updateMap" $ do
let
input = M.fromList [(1, 2), (3, 5)]
context "key found" $ let
2019-11-12 17:01:08 -05:00
expected = M.fromList [(1, 3), (3, 5)] :: M.Map Int Int
2019-10-11 01:10:50 -04:00
in it "should update the value" $
updateMap 1 10 succ input `shouldBe` expected
context "key not found" $ let
expected = M.fromList [(1, 2), (3, 5), (10, 11)]
in it "should create a new value and update the default" $
updateMap 10 10 succ input `shouldBe` expected
2019-10-11 23:13:00 -04:00
sliceSpec :: Spec
sliceSpec = describe "slice" $ do
2019-11-12 17:01:08 -05:00
let list = [2, 4, 6, 8] :: [Int]
2019-10-11 23:13:00 -04:00
context "sublist" $
it "should return the sublist" $
slice 1 2 list `shouldBe` [4, 6]
context "too large" $
it "should return as much of the list as possible" $
slice 1 100 list `shouldBe` [4, 6, 8]
context "negative offset" $
it "should return the correct number of elements from the beginning" $
slice (-10) 2 list `shouldBe` [2, 4]