packages feed

genvalidity-hspec-binary (empty) → 0.0.0.0

raw patch · 8 files changed

+222/−0 lines, 8 filesdep +QuickCheckdep +basedep +binarysetup-changed

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

Files

+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2017 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.
+ README.md view
@@ -0,0 +1,1 @@+This code will create genvalidity specs for the binary serialization library "Data.Binary", as found on https://hackage.haskell.org/package/binary.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ genvalidity-hspec-binary.cabal view
@@ -0,0 +1,58 @@+name:                genvalidity-hspec-binary+version:             0.0.0.0+synopsis:            Standard spec's for binary-related Instances+description:         Standard spec's for cereal-related Instances, see https://hackage.haskell.org/package/binary.+homepage:            https://github.com/NorfairKing/validity#readme+license:             MIT+license-file:        LICENSE+author:              Nick Van den Broeck+maintainer:          syd.kerckhove@gmail.com+copyright:           2017 Tom Sydney Kerckhove+category:            Testing+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+    exposed-modules:+        Test.Validity.Binary+    build-depends:+        base >=4.9 && <=5,+        genvalidity-hspec >=0.3 && <0.5,+        genvalidity >=0.3 && <0.4,+        hspec,+        binary >=0.5 && <0.9,+        QuickCheck,+        deepseq >=1.4 && <1.5+    default-language: Haskell2010+    hs-source-dirs: src/+    ghc-options: -Wall++test-suite genvalidity-hspec-binary-doctests+    type: exitcode-stdio-1.0+    main-is: DocTest.hs+    build-depends:+        base -any,+        doctest >=0.11 && <0.12,+        genvalidity-hspec-binary -any+    default-language: Haskell2010+    hs-source-dirs: test+    ghc-options: -threaded++test-suite genvalidity-hspec-binary-test+    type: exitcode-stdio-1.0+    main-is: Spec.hs+    build-depends:+        base -any,+        genvalidity >=0.3 && <0.4,+        genvalidity-hspec-binary -any,+        hspec+    default-language: Haskell2010+    hs-source-dirs: test/+    other-modules:+        Test.Validity.BinarySpec+    ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall++source-repository head+  type:     git+  location: https://github.com/NorfairKing/validity
+ src/Test/Validity/Binary.hs view
@@ -0,0 +1,119 @@+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE AllowAmbiguousTypes #-}++-- | Standard test `Spec`s and raw `Property`s for `Binary` instances.+--+-- You will need @TypeApplications@ to use these.+module Test.Validity.Binary+    ( binarySpecOnValid+    , binarySpec+    , binarySpecOnArbitrary+    , binarySpecOnGen+    , neverFailsToEncodeOnGen+    , encodeAndDecodeAreInversesOnGen+    ) where++import Data.GenValidity++import Control.DeepSeq (deepseq)+import Control.Exception (evaluate)+import qualified Data.Binary as Binary+import Data.Binary (Binary)+import Data.Typeable+import Test.Hspec+import Test.QuickCheck+import Test.Validity.Utils++-- | Standard test spec for properties of 'Binary'-related functions for valid values+--+-- Example usage:+--+-- > BinarySpecOnValid @Double+binarySpecOnValid ::+       forall a. (Show a, Eq a, Typeable a, GenValid a, Binary a)+    => Spec+binarySpecOnValid = binarySpecOnGen (genValid @a) "valid"++-- | Standard test spec for properties of 'Binary'-related functions for unchecked values+--+-- Example usage:+--+-- > binarySpec @Int+binarySpec ::+       forall a. (Show a, Eq a, Typeable a, GenUnchecked a, Binary a)+    => Spec+binarySpec = binarySpecOnGen (genUnchecked @a) "unchecked"++-- | Standard test spec for properties of 'Binary'-related functions for arbitrary values+--+-- Example usage:+--+-- > binarySpecOnArbitrary @Int+binarySpecOnArbitrary ::+       forall a. (Show a, Eq a, Typeable a, Arbitrary a, Binary a)+    => Spec+binarySpecOnArbitrary = binarySpecOnGen (arbitrary @a) "arbitrary"++-- | Standard test spec for properties of 'Binary'-related functions for a given generator (and a name for that generator).+--+-- Example usage:+--+-- > binarySpecOnGen (genListOf $ pure 'a') "sequence of 'a's"+binarySpecOnGen ::+       forall a. (Show a, Eq a, Typeable a, Binary a)+    => Gen a+    -> String+    -> Spec+binarySpecOnGen gen genname =+    parallel $ do+        let name = nameOf @a+        describe ("Binary " ++ name ++ " (" ++ genname ++ ")") $ do+            describe+                ("encode :: " ++ name ++ " -> Data.ByteString.Lazy.ByteString") $+                it+                    (unwords+                         [ "never fails to encode a"+                         , "\"" ++ genname+                         , name ++ "\""+                         ]) $+                neverFailsToEncodeOnGen gen+            describe+                ("decode :: " ++ name ++ " -> Data.ByteString.Lazy.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, Binary a) => Gen a -> Property+neverFailsToEncodeOnGen gen =+    forAll gen $ \(a :: a) ->+        evaluate (deepseq (Binary.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, Binary a) => Gen a -> Property+encodeAndDecodeAreInversesOnGen gen =+    forAll gen $ \(a :: a) ->+        case Binary.decodeOrFail (Binary.encode a) of+            Right (_, _, b) -> a `shouldBe` b+            Left (_, _, s) ->+                expectationFailure $+                unwords ["decode of encode is not identity:", s]
+ 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/BinarySpec.hs view
@@ -0,0 +1,15 @@+{-# LANGUAGE TypeApplications #-}++module Test.Validity.BinarySpec where++import Test.Hspec++import Data.GenValidity+import Test.Validity.Binary++spec :: Spec+spec = do+    binarySpecOnGen (genListOf $ pure 'a') "sequence of 'a's"+    binarySpecOnValid @Double+    binarySpec @Int+    binarySpecOnArbitrary @Int