packages feed

genvalidity-hspec-cereal (empty) → 0.0.0.0

raw patch · 7 files changed

+219/−0 lines, 7 filesdep +QuickCheckdep +basedep +cerealsetup-changed

Dependencies added: QuickCheck, base, cereal, deepseq, doctest, genvalidity, genvalidity-hspec, genvalidity-hspec-cereal, hspec

Files

+ 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-cereal.cabal view
@@ -0,0 +1,57 @@+name: genvalidity-hspec-cereal+version: 0.0.0.0+synopsis: Standard spec's for cereal-related instances+description: Standard spec's for cereal-related Instances+cabal-version: >=1.10+build-type: Simple+license: MIT+license-file: LICENSE+copyright: Copyright: (c) 2016 Tom Sydney Kerckhove+maintainer: syd.kerckhove@gmail.com+homepage: http://cs-syd.eu+category: Testing+author: Tom Sydney Kerckhove++library+    exposed-modules:+        Test.Validity.Cereal+    build-depends:+        base >=4.9 && <=5,+        genvalidity-hspec >= 0.3 && < 0.4,+        genvalidity >= 0.3 && < 0.4,+        hspec >= 2.2 && < 2.3,+        cereal >= 0.5 && < 0.6,+        QuickCheck >= 2.8 && < 2.9,+        deepseq >= 1.4 && < 1.5+    default-language: Haskell2010+    hs-source-dirs: src/+    ghc-options: -Wall++test-suite genvalidity-hspec-cereal-doctests+  default-language:   Haskell2010+  hs-source-dirs:     test+  type:               exitcode-stdio-1.0+  ghc-options:        -threaded+  main-is:            DocTest.hs+  build-depends:      base+    , doctest               >= 0.11      && < 0.12+    , genvalidity-hspec-cereal++test-suite genvalidity-hspec-cereal-test+    type: exitcode-stdio-1.0+    main-is: Spec.hs+    other-modules:+        Test.Validity.CerealSpec+    build-depends:+        base >=4.9 && <=5,+        genvalidity >= 0.3 && < 0.4,+        genvalidity-hspec-cereal,+        hspec >= 2.2 && < 2.3+    default-language: Haskell2010+    hs-source-dirs: test/+    ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall+++source-repository head+  type:     git+  location: https://github.com/NorfairKing/validity
+ src/Test/Validity/Cereal.hs view
@@ -0,0 +1,119 @@+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE AllowAmbiguousTypes #-}++-- | Standard test `Spec`s and raw `Property`s for `Serialize` instances.+--+-- You will need @TypeApplications@ to use these.+module Test.Validity.Cereal+    ( serializeSpecOnValid+    , serializeSpec+    , serializeSpecOnArbitrary+    , serializeSpecOnGen+    , neverFailsToEncodeOnGen+    , encodeAndDecodeAreInversesOnGen+    ) where++import Data.GenValidity++import Control.DeepSeq (deepseq)+import Control.Exception (evaluate)+import qualified Data.Serialize as Serialize+import Data.Serialize (Serialize)+import Data.Typeable+import Test.Hspec+import Test.QuickCheck+import Test.Validity.Utils++-- | Standard test spec for properties of 'Serialize'-related functions for valid values+--+-- Example usage:+--+-- > serializeSpecOnValid @Double+serializeSpecOnValid+    :: forall a.+       (Show a, Eq a, Typeable a, GenValid a, Serialize a)+    => Spec+serializeSpecOnValid = serializeSpecOnGen (genValid @a) "valid"++-- | Standard test spec for properties of 'Serialize'-related functions for unchecked values+--+-- Example usage:+--+-- > serializeSpec @Int+serializeSpec+    :: forall a.+       (Show a, Eq a, Typeable a, GenUnchecked a, Serialize a)+    => Spec+serializeSpec = serializeSpecOnGen (genUnchecked @a) "unchecked"++-- | Standard test spec for properties of 'Serialize'-related functions for arbitrary values+--+-- Example usage:+--+-- > serializeSpecOnArbitrary @Int+serializeSpecOnArbitrary+    :: forall a.+       (Show a, Eq a, Typeable a, Arbitrary a, Serialize a)+    => Spec+serializeSpecOnArbitrary = serializeSpecOnGen (arbitrary @a) "arbitrary"++-- | Standard test spec for properties of 'Serialize'-related functions for a given generator (and a name for that generator).+--+-- Example usage:+--+-- > serializeSpecOnGen (genListOf $ pure 'a') "sequence of 'a's"+serializeSpecOnGen+    :: forall a.+       (Show a, Eq a, Typeable a, Serialize a)+    => Gen a -> String -> Spec+serializeSpecOnGen gen genname =+    parallel $ do+        let name = nameOf @a+        describe ("Serialize " ++ name ++ " (" ++ genname ++ ")") $ do+            describe ("encode :: " ++ name ++ " -> Data.ByteString.ByteString") $+                it+                    (unwords+                         [ "never fails to encode a"+                         , "\"" ++ genname+                         , name ++ "\""+                         ]) $+                neverFailsToEncodeOnGen gen+            describe ("decode :: " ++ name ++ " -> Data.ByteString.ByteString") $+                it+                    (unwords+                         [ "ensures that encode and decode are inverses for"+                         , "\"" ++ genname+                         , name ++ "\"" ++ "'s"+                         ]) $+                encodeAndDecodeAreInversesOnGen gen++-- |+--+-- prop> neverFailsToEncodeOnGen @Bool arbitrary+-- prop> neverFailsToEncodeOnGen @Bool genUnchecked+-- prop> neverFailsToEncodeOnGen @Bool genValid+-- prop> neverFailsToEncodeOnGen @Int arbitrary+-- prop> neverFailsToEncodeOnGen @Int genUnchecked+-- prop> neverFailsToEncodeOnGen @Int genValid+neverFailsToEncodeOnGen+    :: (Show a, Serialize a)+    => Gen a -> Property+neverFailsToEncodeOnGen gen =+    forAll gen $ \(a :: a) ->+        evaluate (deepseq (Serialize.encode a) ()) `shouldReturn` ()++-- |+--+-- prop> encodeAndDecodeAreInversesOnGen @Bool arbitrary+-- prop> encodeAndDecodeAreInversesOnGen @Bool genUnchecked+-- prop> encodeAndDecodeAreInversesOnGen @Bool genValid+-- prop> encodeAndDecodeAreInversesOnGen @Int arbitrary+-- prop> encodeAndDecodeAreInversesOnGen @Int genUnchecked+-- prop> encodeAndDecodeAreInversesOnGen @Int genValid+encodeAndDecodeAreInversesOnGen+    :: (Show a, Eq a, Serialize a)+    => Gen a -> Property+encodeAndDecodeAreInversesOnGen gen =+    forAll gen $ \(a :: a) ->+        Serialize.decode (Serialize.encode a) `shouldBe` Right a
+ test/DocTest.hs view
@@ -0,0 +1,4 @@+import Test.DocTest++main :: IO ()+main = doctest ["-isrc", "src", "-XTypeApplications"]
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
+ test/Test/Validity/CerealSpec.hs view
@@ -0,0 +1,15 @@+{-# LANGUAGE TypeApplications #-}++module Test.Validity.CerealSpec where++import Test.Hspec++import Data.GenValidity+import Test.Validity.Cereal++spec :: Spec+spec = do+    serializeSpecOnGen (genListOf $ pure 'a') "sequence of 'a's"+    serializeSpecOnValid @Double+    serializeSpec @Int+    serializeSpecOnArbitrary @Int