genvalidity-hspec-aeson (empty) → 0.0.0.0
raw patch · 7 files changed
+215/−0 lines, 7 filesdep +QuickCheckdep +aesondep +basesetup-changed
Dependencies added: QuickCheck, aeson, base, deepseq, doctest, genvalidity, genvalidity-hspec, genvalidity-hspec-aeson, hspec
Files
- LICENSE +21/−0
- Setup.hs +2/−0
- genvalidity-hspec-aeson.cabal +57/−0
- src/Test/Validity/Aeson.hs +115/−0
- test/DocTest.hs +4/−0
- test/Spec.hs +1/−0
- test/Test/Validity/AesonSpec.hs +15/−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-aeson.cabal view
@@ -0,0 +1,57 @@+name: genvalidity-hspec-aeson+version: 0.0.0.0+synopsis: Standard spec's for aeson-related instances+description: Standard spec's for aeson-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.Aeson+ build-depends:+ base >=4.9 && <=5,+ genvalidity-hspec >= 0.3 && < 0.4,+ genvalidity >= 0.3 && < 0.4,+ hspec >= 2.2 && < 2.3,+ aeson >= 0.11 && < 0.12,+ QuickCheck >= 2.8 && < 2.9,+ deepseq >= 1.4 && < 1.5+ default-language: Haskell2010+ hs-source-dirs: src/+ ghc-options: -Wall++test-suite genvalidity-hspec-aeson-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-aeson++test-suite genvalidity-hspec-aeson-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Test.Validity.AesonSpec+ build-depends:+ base >=4.9 && <=5,+ genvalidity >= 0.3 && < 0.4,+ genvalidity-hspec-aeson,+ 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/Aeson.hs view
@@ -0,0 +1,115 @@+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE AllowAmbiguousTypes #-}++-- | Standard test `Spec`s and raw `Property`s for `FromJSON` and `ToJSON` instances.+--+-- You will need @TypeApplications@ to use these.+module Test.Validity.Aeson+ ( jsonSpecOnValid+ , jsonSpec+ , jsonSpecOnArbitrary+ , jsonSpecOnGen+ , neverFailsToEncodeOnGen+ , encodeAndDecodeAreInversesOnGen+ ) where++import Data.GenValidity++import Control.DeepSeq (deepseq)+import Control.Exception (evaluate)+import Data.Aeson (FromJSON, ToJSON)+import qualified Data.Aeson as JSON+import Data.Typeable+import Test.Hspec+import Test.QuickCheck+import Test.Validity.Utils++-- | Standard test spec for properties of JSON-related functions for valid values+--+-- Example usage:+--+-- > jsonSpecOnValid @Double+jsonSpecOnValid+ :: forall a.+ (Show a, Eq a, Typeable a, GenValid a, FromJSON a, ToJSON a)+ => Spec+jsonSpecOnValid = jsonSpecOnGen (genValid @a) "valid"++-- | Standard test spec for properties of JSON-related functions for unchecked values+--+-- Example usage:+--+-- > jsonSpec @Int+jsonSpec+ :: forall a.+ (Show a, Eq a, Typeable a, GenUnchecked a, FromJSON a, ToJSON a)+ => Spec+jsonSpec = jsonSpecOnGen (genUnchecked @a) "unchecked"++-- | Standard test spec for properties of JSON-related functions for arbitrary values+--+-- Example usage:+--+-- > jsonSpecOnArbitrary @Int+jsonSpecOnArbitrary+ :: forall a.+ (Show a, Eq a, Typeable a, Arbitrary a, FromJSON a, ToJSON a)+ => Spec+jsonSpecOnArbitrary = jsonSpecOnGen (arbitrary @a) "arbitrary"++-- | Standard test spec for properties of JSON-related functions for a given generator (and a name for that generator).+--+-- Example usage:+--+-- > jsonSpecOnGen (genListOf $ pure 'a') "sequence of 'a's"+jsonSpecOnGen+ :: forall a.+ (Show a, Eq a, Typeable a, FromJSON a, ToJSON a)+ => Gen a -> String -> Spec+jsonSpecOnGen gen genname = parallel $ do+ let name = nameOf @a+ describe ("JSON " ++ 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, ToJSON a)+ => Gen a -> Property+neverFailsToEncodeOnGen gen =+ forAll gen $ \(a :: a) ->+ evaluate (deepseq (JSON.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, FromJSON a, ToJSON a)+ => Gen a -> Property+encodeAndDecodeAreInversesOnGen gen =+ forAll gen $ \(a :: a) ->+ JSON.eitherDecode (JSON.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/AesonSpec.hs view
@@ -0,0 +1,15 @@+{-# LANGUAGE TypeApplications #-}++module Test.Validity.AesonSpec where++import Test.Hspec++import Data.GenValidity+import Test.Validity.Aeson++spec :: Spec+spec = do+ jsonSpecOnGen (genListOf $ pure 'a') "sequence of 'a's"+ jsonSpecOnValid @Double+ jsonSpec @Int+ jsonSpecOnArbitrary @Int