genvalidity-hspec-hashable 0.2.0.5 → 1.0.0.0
raw patch · 7 files changed
+112/−113 lines, 7 filesdep ~genvaliditydep ~genvalidity-propertysetup-changedPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: genvalidity, genvalidity-property
API changes (from Hackage documentation)
- Test.Validity.Hashable: hashableSpecOnInvalid :: forall a. (Show a, Eq a, Typeable a, GenInvalid a, Hashable a) => Spec
- Test.Validity.Hashable: hashableSpecOnValid :: forall a. (Show a, Eq a, Typeable a, GenValid a, Hashable a) => Spec
- Test.Validity.Hashable: hashableSpec :: forall a. (Show a, Eq a, Typeable a, GenUnchecked a, Hashable a) => Spec
+ Test.Validity.Hashable: hashableSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, Hashable a) => Spec
Files
- CHANGELOG.md +30/−0
- LICENSE +1/−1
- README.md +0/−1
- Setup.hs +0/−3
- genvalidity-hspec-hashable.cabal +8/−9
- src/Test/Validity/Hashable.hs +47/−65
- test/Test/Validity/HashableSpec.hs +26/−34
+ CHANGELOG.md view
@@ -0,0 +1,30 @@+# Changelog++## [1.0.0.0] - 2021-11-20++* Compatibility with `genvalidity >= 1.0.0.0`+* Renamed every combinator that ends in `OnValid` (or similar) to not have that suffix anymore.++### Removed++* Every combinator that relates to unchecked or invalid values.++## [0.3.0.5] - 2020-02-10++### Changed++* Removed the doctests+* Improved the cabal file+++## [0.2.0.4] - 2019-05-10++### Changed++* Removed upper-bound for hashable >=1.3.0 and added in a test now that the double bug has been fixed.++## [0.2.0.3] - 2018-10-07++### Changed++* Compatibility with validity >=0.9, genvalidity >=0.7 and genvalidity-property >=0.3
LICENSE view
@@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016-2020 Tom Sydney Kerckhove+Copyright (c) 2016-2021 Tom Sydney Kerckhove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
− README.md
@@ -1,1 +0,0 @@-This code provides the means to test the laws of the typeclass Hashable for any instance.
− Setup.hs
@@ -1,3 +0,0 @@-import Distribution.Simple--main = defaultMain
genvalidity-hspec-hashable.cabal view
@@ -1,13 +1,11 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.33.0.+-- This file has been generated from package.yaml by hpack version 0.34.4. -- -- see: https://github.com/sol/hpack------ hash: 34593a6c38fd857fad7e733cf3be583475d920522e25b35b799568fbd50e7436 name: genvalidity-hspec-hashable-version: 0.2.0.5+version: 1.0.0.0 synopsis: Standard spec's for Hashable instances description: Standard spec's for Hashable instances category: Testing@@ -15,12 +13,13 @@ bug-reports: https://github.com/NorfairKing/validity/issues author: Nick Van den Broeck maintainer: syd@cs-syd.eu-copyright: 2017-2020 Tom Sydney Kerckhove+copyright: Copyright: (c) 2016-2021 Tom Sydney Kerckhove license: MIT license-file: LICENSE build-type: Simple extra-source-files:- README.md+ LICENSE+ CHANGELOG.md source-repository head type: git@@ -36,7 +35,7 @@ build-depends: QuickCheck , base >=4.9 && <5- , genvalidity >=0.5+ , genvalidity >=1.0 , genvalidity-hspec >=0.6 , genvalidity-property >=0.2 , hashable >=1.2@@ -56,10 +55,10 @@ build-depends: QuickCheck , base >=4.9 && <5- , genvalidity >=0.7+ , genvalidity , genvalidity-hspec , genvalidity-hspec-hashable- , genvalidity-property >=0.3+ , genvalidity-property , hashable , hspec , hspec-core
src/Test/Validity/Hashable.hs view
@@ -1,59 +1,37 @@+{-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE TypeApplications #-} {-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE AllowAmbiguousTypes #-}+{-# LANGUAGE TypeApplications #-} -- | Hashable properties -- -- You will need @TypeApplications@ to use these. module Test.Validity.Hashable- ( hashableSpecOnValid- , hashableSpecOnInvalid- , hashableSpec- , hashableSpecOnArbitrary- , hashableSpecOnGen- ) where+ ( hashableSpec,+ hashableSpecOnArbitrary,+ hashableSpecOnGen,+ )+where import Control.Monad import Data.Data-import Data.Hashable-import Test.Validity.Property.Utils-import Test.Validity.Utils- import Data.GenValidity-+import Data.Hashable import Test.Hspec import Test.QuickCheck+import Test.Validity.Property.Utils+import Test.Validity.Utils -- | Standard test spec for properties of Hashable instances for valid values -- -- Example usage: ----- > hashableSpecOnValid @Double-hashableSpecOnValid ::- forall a. (Show a, Eq a, Typeable a, GenValid a, Hashable a)- => Spec-hashableSpecOnValid = hashableSpecOnGen @a genValid "valid" shrinkValid---- | Standard test spec for properties of Hashable instances for invalid values------ Example usage:------ > hashableSpecOnInvalid @Double-hashableSpecOnInvalid ::- forall a. (Show a, Eq a, Typeable a, GenInvalid a, Hashable a)- => Spec-hashableSpecOnInvalid = hashableSpecOnGen @a genInvalid "invalid" shrinkInvalid---- | Standard test spec for properties of Hashable instances for unchecked values------ Example usage:--- -- > hashableSpec @Int hashableSpec ::- forall a. (Show a, Eq a, Typeable a, GenUnchecked a, Hashable a)- => Spec-hashableSpec = hashableSpecOnGen @a genUnchecked "unchecked" shrinkUnchecked+ forall a.+ (Show a, Eq a, Typeable a, GenValid a, Hashable a) =>+ Spec+hashableSpec = hashableSpecOnGen @a genValid "valid" shrinkValid -- | Standard test spec for properties of Hashable instances for arbitrary values --@@ -61,8 +39,9 @@ -- -- > hashableSpecOnArbitrary @Int hashableSpecOnArbitrary ::- forall a. (Show a, Eq a, Typeable a, Arbitrary a, Hashable a)- => Spec+ forall a.+ (Show a, Eq a, Typeable a, Arbitrary a, Hashable a) =>+ Spec hashableSpecOnArbitrary = hashableSpecOnGen @a arbitrary "arbitrary" shrink -- | Standard test spec for properties of Hashable instances for values generated by a given generator (and name for that generator).@@ -71,37 +50,40 @@ -- -- > hashableSpecOnGen ((* 2) <$> genValid @Int) "even" hashableSpecOnGen ::- forall a. (Show a, Eq a, Typeable a, Hashable a)- => Gen a- -> String- -> (a -> [a])- -> Spec+ forall a.+ (Show a, Eq a, Typeable a, Hashable a) =>+ Gen a ->+ String ->+ (a -> [a]) ->+ Spec hashableSpecOnGen gen = checkGen $ (,) <$> gen <*> gen -- | Test spec like hashableSpecOnGen but with a special generator -- | which is documented to generate equal values by (==) most of the time. checkGen ::- forall a. (Show a, Eq a, Typeable a, Hashable a)- => Gen (a, a)- -> String- -> (a -> [a])- -> Spec+ forall a.+ (Show a, Eq a, Typeable a, Hashable a) =>+ Gen (a, a) ->+ String ->+ (a -> [a]) ->+ Spec checkGen gen genname s =- parallel $ do- let name = nameOf @a- hashablestr = unwords ["hashWithSalt :: Int ->", name, "-> Int"]- describe ("Hashable " ++ name) $- describe hashablestr $- it- (unwords- [ "satisfies (a == b) => (hashWithSalt n a) =="- , "(hashWithSalt n b), for every n and for"- , genname- , name- ]) $- let ss (a, b) = (,) <$> s a <*> s b+ parallel $ do+ let name = nameOf @a+ hashablestr = unwords ["hashWithSalt :: Int ->", name, "-> Int"]+ describe ("Hashable " ++ name) $+ describe hashablestr $+ it+ ( unwords+ [ "satisfies (a == b) => (hashWithSalt n a) ==",+ "(hashWithSalt n b), for every n and for",+ genname,+ name+ ]+ )+ $ let ss (a, b) = (,) <$> s a <*> s b in forAllShrink gen ss $ \(a1, a2) ->- forAllUnchecked $ \int ->- when (a1 == a2) $- let h = hashWithSalt int- in h a1 `shouldBe` h a2+ forAllValid $ \int ->+ when (a1 == a2) $+ let h = hashWithSalt int+ in h a1 `shouldBe` h a2
test/Test/Validity/HashableSpec.hs view
@@ -1,57 +1,51 @@-{-# LANGUAGE TypeApplications #-} {-# LANGUAGE DeriveGeneric #-}-{-# LANGUAGE CPP #-}+{-# LANGUAGE TypeApplications #-} -- | Standard 'Spec's for 'Hashable' instances. -- -- You will need @TypeApplications@ to use these. module Test.Validity.HashableSpec where +import Data.GenValidity import Data.Hashable import GHC.Generics import Test.Hspec-import Test.Validity.Utils--import Data.GenValidity import Test.Validity.Hashable+import Test.Validity.Utils spec :: Spec spec = do- hashableSpecOnValid @Rational-#if MIN_VERSION_hashable(1,3,0)- hashableSpecOnValid @Double-#endif- hashableSpec @Int- hashableSpecOnArbitrary @Int- hashableSpec @HashableValid- failsBecause "Two equal elements aren't hashed to the same value!" $- hashableSpec @HashableInvalid+ hashableSpec @Rational+ hashableSpec @Double+ hashableSpec @Int+ hashableSpecOnArbitrary @Int+ hashableSpec @HashableValid+ failsBecause "Two equal elements aren't hashed to the same value!" $+ hashableSpec @HashableInvalid -newtype HashableValid =- HashableValid Int- deriving (Show, Generic)+newtype HashableValid+ = HashableValid Int+ deriving (Show, Generic) hT :: Int -- Number used in the definition of HashableValid hT = 7 instance Eq HashableValid where- (==) (HashableValid x) (HashableValid y) = (x `mod` hT) == (y `mod` hT)+ (==) (HashableValid x) (HashableValid y) = (x `mod` hT) == (y `mod` hT) instance Hashable HashableValid where- hashWithSalt n (HashableValid a) = (int ^ expo) `mod` hT- where- int = 1 + (a `mod` hT)- expo = 1 + (n `mod` hT)+ hashWithSalt n (HashableValid a) = (int ^ expo) `mod` hT+ where+ int = 1 + (a `mod` hT)+ expo = 1 + (n `mod` hT) instance Validity HashableValid instance GenValid HashableValid -instance GenUnchecked HashableValid--newtype HashableInvalid =- HashableInvalid Int- deriving (Show, Generic)+newtype HashableInvalid+ = HashableInvalid Int+ deriving (Show, Generic) hF :: Int -- Numbers used in the definition of HashableInvalid hF = 8@@ -60,16 +54,14 @@ hM = 3 instance Eq HashableInvalid where- (==) (HashableInvalid x) (HashableInvalid y) = (x `mod` hF) == (y `mod` hF)+ (==) (HashableInvalid x) (HashableInvalid y) = (x `mod` hF) == (y `mod` hF) instance Hashable HashableInvalid where- hashWithSalt n (HashableInvalid a) = (int ^ expo) `mod` hM- where- int = 1 + (a `mod` hM)- expo = 1 + (n `mod` hM)+ hashWithSalt n (HashableInvalid a) = (int ^ expo) `mod` hM+ where+ int = 1 + (a `mod` hM)+ expo = 1 + (n `mod` hM) instance Validity HashableInvalid instance GenValid HashableInvalid--instance GenUnchecked HashableInvalid