{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
#ifndef HAVE_AESON
module Hedgehog.Classes.Json () where
#else
module Hedgehog.Classes.Json (jsonLaws) where
import Hedgehog
import Hedgehog.Classes.Common
import Data.Aeson (FromJSON, ToJSON(toJSON))
import qualified Data.Aeson as Aeson
jsonLaws :: (FromJSON a, ToJSON a, Eq a, Show a) => Gen a -> Laws
jsonLaws :: forall a. (FromJSON a, ToJSON a, Eq a, Show a) => Gen a -> Laws
jsonLaws Gen a
gen = String -> [(String, Property)] -> Laws
Laws String
"ToJSON/FromJSON"
[ (String
"Partial Isomorphism", forall a. (ToJSON a, FromJSON a, Show a, Eq a) => Gen a -> Property
jsonEncodingPartialIsomorphism Gen a
gen)
, (String
"Encoding equals value", forall a. (ToJSON a, Show a) => Gen a -> Property
jsonEncodingEqualsValue Gen a
gen)
]
jsonEncodingPartialIsomorphism :: forall a. (ToJSON a, FromJSON a, Show a, Eq a) => Gen a -> Property
jsonEncodingPartialIsomorphism :: forall a. (ToJSON a, FromJSON a, Show a, Eq a) => Gen a -> Property
jsonEncodingPartialIsomorphism Gen a
gen = HasCallStack => PropertyT IO () -> Property
property forall a b. (a -> b) -> a -> b
$ do
a
x <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
let encoded :: ByteString
encoded = forall a. ToJSON a => a -> ByteString
Aeson.encode a
x
let lhs :: Maybe a
lhs = forall a. FromJSON a => ByteString -> Maybe a
Aeson.decode ByteString
encoded
let rhs :: Maybe a
rhs = forall a. a -> Maybe a
Just a
x
let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
{ lawContextLawName :: String
lawContextLawName = String
"Partial Isomorphism", lawContextTcName :: String
lawContextTcName = String
"ToJSON/FromJSON"
, lawContextLawBody :: String
lawContextLawBody = String
"decode . encode" String -> String -> String
`congruency` String
"Just"
, lawContextTcProp :: String
lawContextTcProp =
let showX :: String
showX = forall a. Show a => a -> String
show a
x
showEncoded :: String
showEncoded = forall a. Show a => a -> String
show ByteString
encoded
in [String] -> String
lawWhere
[ String
"decode . encode $ x" String -> String -> String
`congruency` String
"Just x, where"
, String
"x = " forall a. [a] -> [a] -> [a]
++ String
showX
, String
"encode x = " forall a. [a] -> [a] -> [a]
++ String
showEncoded
]
, lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Maybe a
lhs Maybe a
rhs
}
forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx Maybe a
lhs Maybe a
rhs Context
ctx
jsonEncodingEqualsValue :: forall a. (ToJSON a, Show a) => Gen a -> Property
jsonEncodingEqualsValue :: forall a. (ToJSON a, Show a) => Gen a -> Property
jsonEncodingEqualsValue Gen a
gen = HasCallStack => PropertyT IO () -> Property
property forall a b. (a -> b) -> a -> b
$ do
a
x <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
let encoded :: ByteString
encoded = forall a. ToJSON a => a -> ByteString
Aeson.encode a
x
let decoded :: Maybe Value
decoded = forall a. FromJSON a => ByteString -> Maybe a
Aeson.decode ByteString
encoded :: Maybe Aeson.Value
let lhs :: Maybe Value
lhs = Maybe Value
decoded
let rhs :: Maybe Value
rhs = forall a. a -> Maybe a
Just (forall a. ToJSON a => a -> Value
toJSON a
x)
let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
{ lawContextLawName :: String
lawContextLawName = String
"Encoding equals value", lawContextTcName :: String
lawContextTcName = String
"ToJSON"
, lawContextLawBody :: String
lawContextLawBody = String
"decode . encode" String -> String -> String
`congruency` String
"Just . toJSON"
, lawContextTcProp :: String
lawContextTcProp =
let showX :: String
showX = forall a. Show a => a -> String
show a
x
showEncoded :: String
showEncoded = forall a. Show a => a -> String
show ByteString
encoded
showDecoded :: String
showDecoded = forall a. Show a => a -> String
show Maybe Value
decoded
in [String] -> String
lawWhere
[ String
"decode . encode $ x" String -> String -> String
`congruency` String
"Just . toJSON, where"
, String
"x = " forall a. [a] -> [a] -> [a]
++ String
showX
, String
"encoded = " forall a. [a] -> [a] -> [a]
++ String
showEncoded
, String
"decoded = " forall a. [a] -> [a] -> [a]
++ String
showDecoded
]
, lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Maybe Value
lhs Maybe Value
rhs
}
forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx Maybe Value
lhs Maybe Value
rhs Context
ctx
#endif