genvalidity-hspec (empty) → 0.1.0.0
raw patch · 4 files changed
+150/−0 lines, 4 filesdep +QuickCheckdep +basedep +genvaliditysetup-changed
Dependencies added: QuickCheck, base, genvalidity, hspec, validity
Files
- LICENSE +21/−0
- Setup.hs +2/−0
- genvalidity-hspec.cabal +28/−0
- src/Data/GenValidity/Hspec.hs +99/−0
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2016 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+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ genvalidity-hspec.cabal view
@@ -0,0 +1,28 @@+name: genvalidity-hspec+version: 0.1.0.0+synopsis: Standard spec's for GenValidity instances+description: Please see README.md+homepage: https://github.com/NorfairKing/validity#readme+license: MIT+license-file: LICENSE+author: Tom Sydney Kerckhove+maintainer: syd.kerckhove@gmail.com+copyright: Copyright: (c) 2016 Tom Sydney Kerckhove+category: Testing+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Data.GenValidity.Hspec+ build-depends: base < 5+ , validity + , genvalidity+ , QuickCheck+ , hspec >= 2.2 && < 2.3+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/NorfairKing/validity
+ src/Data/GenValidity/Hspec.hs view
@@ -0,0 +1,99 @@+{-|++ These are standard tests that you should add to your @hspec@ test suite+ if you implemented @GenValidity@ instances for your own data types.++ Use them like this:++ > mySpec :: Spec+ > mySpec = do+ > genspec (proxy :: MyType)+ > genspec (proxy :: MyOtherType)++ HSpec will take care of the rest.++ -}+module Data.GenValidity.Hspec+ ( module Data.GenValidity+ , module Data.GenValidity.Hspec+ ) where++import Data.GenValidity++import Test.Hspec+import Test.QuickCheck++import Data.Data++-- | A value of arbitrary type, used to specify which type to generate a spec+-- for.+proxy :: a+proxy = undefined++-- | A combination of @arbitrarySpec@ and @validitySpec@+--+-- Example usage:+--+-- > genspec (proxy :: MyData)+genspec+ :: (Show a, Eq a, Data a, GenValidity a, Arbitrary a)+ => a+ -> Spec+genspec proxy = do+ let name = show $ typeOf proxy+ describe ("GenSpec for " ++ name) $ do+ arbitrarySpec proxy+ validitySpec proxy++-- | A @Spec@ that specifies that @arbitrary@ only generates data that+-- satisfy @isValid@ and that @shrink@ only produces data that satisfy+-- @isValid@.+--+-- Example usage:+--+-- > arbitrarySpec (proxy :: MyData)+arbitrarySpec+ :: (Typeable a, Show a, Eq a, Data a, GenValidity a, Arbitrary a)+ => a+ -> Spec+arbitrarySpec proxy = do+ let name = show $ typeOf proxy+ describe ("Arbitrary " ++ name) $ do+ it ("is instantiated such that 'arbitrary' only generates valid \'"+ ++ name+ ++ "\'s") $ do+ forAll arbitrary $ \a ->+ (a `asTypeOf` proxy) `shouldSatisfy` isValid++ it ("is instantiated such that 'shrink' only produces valid \'"+ ++ name+ ++ "\'s") $ do+ forAll arbitrary $ \a ->+ shrink (a `asTypeOf` proxy) `shouldSatisfy` all isValid++-- | A @Spec@ that specifies that @genValid@ only generates valid data and that+-- @genInvalid@ only generates invalid data.+--+-- In general it is a good idea to add this spec to your test suite if you+-- write a custom implementation of @genValid@ or @genInvalid@.+--+-- Example usage:+--+-- > validitySpec (proxy :: MyData)+validitySpec+ :: (Typeable a, Show a, Eq a, Data a, GenValidity a, Arbitrary a)+ => a+ -> Spec+validitySpec proxy = do+ let name = show $ typeOf proxy+ describe "genValid" $ do+ it ("only generates valid \'" ++ name ++ "\'s") $ do+ forAll genValid $ \a ->+ (a `asTypeOf` proxy) `shouldSatisfy` isValid++ describe "genInvalid" $ do+ it ("only generates invalid \'" ++ name ++ "\'s") $ do+ forAll genInvalid $ \a ->+ (a `asTypeOf` proxy) `shouldNotSatisfy` isValid++