Skip to content

Commit

Permalink
Add property test for added lookupDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinierMaas authored and OpsBotPrime committed Apr 19, 2022
1 parent 25b7ef3 commit a1b0939
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions nix/haskell-dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ haskellPackages:
megaparsec
optparse-applicative
parser-combinators
quickcheck-instances
retry
text
unix
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ tests:
- hspec-expectations
- directory
- QuickCheck
- quickcheck-instances
25 changes: 25 additions & 0 deletions test/KeyMapSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{-# LANGUAGE ScopedTypeVariables #-}

module KeyMapSpec where

import Data.Text (Text)
import Test.Hspec
import Test.QuickCheck
import Test.QuickCheck.Instances ()

import qualified Data.HashMap.Internal.Strict as HM

import qualified KeyMap as KM

mapFirst :: (a->b) -> [(a,c)] -> [(b,c)]
mapFirst _ [] = []
mapFirst f ((x,y) : rest) = (f x, y) : mapFirst f rest

spec :: SpecWith ()
spec =
describe "KeyMap" $ do
it "lookupDefault matches the HashMap implementation" $
property $ \((fallback :: Text), key, mapping) ->
let keyMapValue = KM.lookupDefault fallback (KM.fromText key) (KM.fromList $ mapFirst KM.fromText mapping)
hashMapValue = HM.lookupDefault fallback key (HM.fromList mapping)
in keyMapValue `shouldBe` hashMapValue

0 comments on commit a1b0939

Please sign in to comment.