packages feed

genvalidity-hspec-aeson 0.3.1.1 → 1.0.0.0

raw patch · 6 files changed

+124/−107 lines, 6 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.Aeson: jsonSpecOnValid :: forall a. (Show a, Eq a, Typeable a, GenValid a, FromJSON a, ToJSON a) => Spec
- Test.Validity.Aeson: jsonSpec :: forall a. (Show a, Eq a, Typeable a, GenUnchecked a, FromJSON a, ToJSON a) => Spec
+ Test.Validity.Aeson: jsonSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, FromJSON a, ToJSON a) => Spec

Files

+ CHANGELOG.md view
@@ -0,0 +1,24 @@+# 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.1.1] - 2020-02-10++### Changed++* Improved the cabal file+* Removed the doctests+* Removed the shrinking tests++## [0.3.1.0] - 2019-03-06++### Changed++* Test suite compatibility with genvalidity >=0.8
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
− Setup.hs
@@ -1,3 +0,0 @@-import Distribution.Simple--main = defaultMain
genvalidity-hspec-aeson.cabal view
@@ -1,23 +1,24 @@ 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: b565ce5a4a7956da9ac040351b844f450cf7046a945063ebc2988b047fd1544d  name:           genvalidity-hspec-aeson-version:        0.3.1.1+version:        1.0.0.0 synopsis:       Standard spec's for aeson-related instances category:       Testing homepage:       http://cs-syd.eu bug-reports:    https://github.com/NorfairKing/validity/issues author:         Tom Sydney Kerckhove maintainer:     syd@cs-syd.eu-copyright:      Copyright: (c) 2016-2020 Tom Sydney Kerckhove+copyright:      Copyright: (c) 2016-2021 Tom Sydney Kerckhove license:        MIT license-file:   LICENSE build-type:     Simple+extra-source-files:+    LICENSE+    CHANGELOG.md  source-repository head   type: git@@ -37,7 +38,7 @@     , base >=4.9 && <=5     , bytestring     , deepseq-    , genvalidity >=0.5+    , genvalidity >=1.0     , genvalidity-hspec >=0.6     , hspec   default-language: Haskell2010@@ -55,11 +56,11 @@       QuickCheck     , aeson     , base >=4.9 && <=5-    , genvalidity >=0.7+    , genvalidity     , genvalidity-aeson     , genvalidity-hspec     , genvalidity-hspec-aeson-    , genvalidity-property >=0.3+    , genvalidity-property     , genvalidity-text     , hspec     , text
src/Test/Validity/Aeson.hs view
@@ -1,26 +1,25 @@+{-# LANGUAGE AllowAmbiguousTypes #-} {-# 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+  ( jsonSpec,+    jsonSpecOnArbitrary,+    jsonSpecOnGen,+    neverFailsToEncodeOnGen,+    encodeAndDecodeAreInversesOnGen,+  )+where  import Control.DeepSeq (deepseq) import Control.Exception (evaluate) import Control.Monad import Data.Aeson (FromJSON, ToJSON) import qualified Data.Aeson as JSON+import Data.GenValidity import Data.Typeable import Test.Hspec import Test.QuickCheck@@ -30,22 +29,12 @@ -- -- Example usage: ----- > jsonSpecOnValid @Rational-jsonSpecOnValid ::-       forall a. (Show a, Eq a, Typeable a, GenValid a, FromJSON a, ToJSON a)-    => Spec-jsonSpecOnValid = jsonSpecOnGen (genValid @a) "valid" shrinkValid---- | 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" shrinkUnchecked+  forall a.+  (Show a, Eq a, Typeable a, GenValid a, FromJSON a, ToJSON a) =>+  Spec+jsonSpec = jsonSpecOnGen (genValid @a) "valid" shrinkValid  -- | Standard test spec for properties of JSON-related functions for arbitrary values --@@ -53,8 +42,9 @@ -- -- > jsonSpecOnArbitrary @Int jsonSpecOnArbitrary ::-       forall a. (Show a, Eq a, Typeable a, Arbitrary a, FromJSON a, ToJSON a)-    => Spec+  forall a.+  (Show a, Eq a, Typeable a, Arbitrary a, FromJSON a, ToJSON a) =>+  Spec jsonSpecOnArbitrary = jsonSpecOnGen (arbitrary @a) "arbitrary" shrink  -- | Standard test spec for properties of JSON-related functions for a given generator (and a name for that generator).@@ -63,81 +53,85 @@ -- -- > jsonSpecOnGen (genListOf $ pure 'a') "sequence of 'a's" jsonSpecOnGen ::-       forall a. (Show a, Eq a, Typeable a, FromJSON a, ToJSON a)-    => Gen a-    -> String-    -> (a -> [a])-    -> Spec+  forall a.+  (Show a, Eq a, Typeable a, FromJSON a, ToJSON a) =>+  Gen a ->+  String ->+  (a -> [a]) ->+  Spec jsonSpecOnGen gen genname s =-    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 s-            describe-                ("decode :: Data.ByteString.Lazy.ByteString -> Either String " ++-                 name) $-                it-                    (unwords-                         [ "ensures that encode and decode are inverses for"-                         , "\"" ++ genname-                         , name ++ "\"" ++ "'s"-                         ]) $-                encodeAndDecodeAreInversesOnGen gen s+  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 s+      describe+        ( "decode :: Data.ByteString.Lazy.ByteString -> Either String "+            ++ name+        )+        $ it+          ( unwords+              [ "ensures that encode and decode are inverses for",+                "\"" ++ genname,+                name ++ "\"" ++ "'s"+              ]+          )+          $ encodeAndDecodeAreInversesOnGen gen s  -- | -- -- prop> neverFailsToEncodeOnGen @Bool arbitrary shrink--- prop> neverFailsToEncodeOnGen @Bool genUnchecked shrinkUnchecked -- prop> neverFailsToEncodeOnGen @Bool genValid shrinkValid+-- prop> neverFailsToEncodeOnGen @Bool genValid shrinkValid -- prop> neverFailsToEncodeOnGen @Int arbitrary shrink--- prop> neverFailsToEncodeOnGen @Int genUnchecked shrinkUnchecked -- prop> neverFailsToEncodeOnGen @Int genValid shrinkValid+-- prop> neverFailsToEncodeOnGen @Int genValid shrinkValid neverFailsToEncodeOnGen :: (Show a, ToJSON a) => Gen a -> (a -> [a]) -> Property neverFailsToEncodeOnGen gen s =-    forAllShrink gen s $ \(a :: a) ->-        evaluate (deepseq (JSON.encode a) ()) `shouldReturn` ()+  forAllShrink gen s $ \(a :: a) ->+    evaluate (deepseq (JSON.encode a) ()) `shouldReturn` ()  -- | -- -- prop> encodeAndDecodeAreInversesOnGen @Bool arbitrary shrink--- prop> encodeAndDecodeAreInversesOnGen @Bool genUnchecked shrinkUnchecked -- prop> encodeAndDecodeAreInversesOnGen @Bool genValid shrinkValid+-- prop> encodeAndDecodeAreInversesOnGen @Bool genValid shrinkValid -- prop> encodeAndDecodeAreInversesOnGen @Int arbitrary shrink--- prop> encodeAndDecodeAreInversesOnGen @Int genUnchecked shrinkUnchecked -- prop> encodeAndDecodeAreInversesOnGen @Int genValid shrinkValid+-- prop> encodeAndDecodeAreInversesOnGen @Int genValid shrinkValid encodeAndDecodeAreInversesOnGen ::-       (Show a, Eq a, FromJSON a, ToJSON a) => Gen a -> (a -> [a]) -> Property+  (Show a, Eq a, FromJSON a, ToJSON a) => Gen a -> (a -> [a]) -> Property encodeAndDecodeAreInversesOnGen gen s =-    forAllShrink gen s $ \(a :: a) ->-        let encoded = JSON.encode a-            errOrDecoded = JSON.eitherDecode encoded-         in case errOrDecoded of-                Left err ->-                    expectationFailure $-                    unlines-                        [ "Decoding failed with error"-                        , err-                        , "instead of decoding to"-                        , show a-                        , "'encode' encoded it to the json"-                        , show encoded-                        ]-                Right decoded ->-                    unless (decoded == a) $-                    expectationFailure $-                    unlines-                        [ "Decoding succeeded, but the decoded value"-                        , show decoded-                        , "differs from expected decoded value"-                        , show a-                        , "'encode' encoded it to the json"-                        , show encoded-                        ]+  forAllShrink gen s $ \(a :: a) ->+    let encoded = JSON.encode a+        errOrDecoded = JSON.eitherDecode encoded+     in case errOrDecoded of+          Left err ->+            expectationFailure $+              unlines+                [ "Decoding failed with error",+                  err,+                  "instead of decoding to",+                  show a,+                  "'encode' encoded it to the json",+                  show encoded+                ]+          Right decoded ->+            unless (decoded == a) $+              expectationFailure $+                unlines+                  [ "Decoding succeeded, but the decoded value",+                    show decoded,+                    "differs from expected decoded value",+                    show a,+                    "'encode' encoded it to the json",+                    show encoded+                  ]
test/Test/Validity/AesonSpec.hs view
@@ -1,32 +1,32 @@-{-# LANGUAGE TypeApplications #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications #-}  module Test.Validity.AesonSpec where -import Test.Hspec- import Data.Aeson import Data.GenValidity import Data.GenValidity.Aeson () import Data.GenValidity.Text () import Data.Text (Text) import GHC.Generics+import Test.Hspec import Test.Validity.Aeson  spec :: Spec spec = do   jsonSpecOnGen (genListOf $ pure 'a') "sequence of 'a's" (const [])-    -- jsonSpec @Double DOES NOT HOLD-  jsonSpecOnValid @Rational+  -- jsonSpec @Double DOES NOT HOLD+  jsonSpec @Rational   jsonSpec @Int   jsonSpecOnArbitrary @Int-  jsonSpecOnValid @ForShow-  jsonSpecOnValid @Value-    -- shrinkValidSpec @Value+  jsonSpec @ForShow+  jsonSpec @Value -newtype ForShow =-  ForShow Text+-- shrinkValidSpec @Value++newtype ForShow+  = ForShow Text   deriving (Show, Eq, Generic)  instance Validity ForShow@@ -38,5 +38,6 @@ instance FromJSON ForShow  instance ToJSON ForShow+ -- >>> decode (Data.Aeson.encode (ForShow "\248")) :: Maybe ForShow -- Just (ForShow "\248")