{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Data.SafeJSON.Test (
testConsistency
, testConsistency'
, testMigration
, testReverseMigration
, (<=?)
, (>=?)
, testRoundTrip
, migrateRoundTrip
, migrateReverseRoundTrip
, TestMigrate
, TestReverseMigrate
, testRoundTripProp
, migrateRoundTripProp
, migrateReverseRoundTripProp
, testRoundTripProp'
, migrateRoundTripProp'
, migrateReverseRoundTripProp'
, Proxy(..)
) where
import Data.Aeson.Types (parseEither)
#if !MIN_VERSION_base(4,11,0)
import Data.Monoid ((<>))
#endif
import Data.Proxy
import Data.SafeJSON.Internal
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (Assertion, assertEqual)
import Test.Tasty.QuickCheck (Arbitrary, testProperty)
testConsistency :: forall a. SafeJSON a => Assertion
testConsistency :: forall a. SafeJSON a => Assertion
testConsistency = forall a (m :: * -> *) b.
(SafeJSON a, MonadFail m) =>
Proxy a -> (ProfileVersions -> m b) -> m b
checkConsistency Proxy a
p forall a b. (a -> b) -> a -> b
$ \ProfileVersions
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
where p :: Proxy a
p = forall {k} (t :: k). Proxy t
Proxy :: Proxy a
testConsistency' :: forall a. SafeJSON a => Proxy a -> Assertion
testConsistency' :: forall a. SafeJSON a => Proxy a -> Assertion
testConsistency' = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a (m :: * -> *) b.
(SafeJSON a, MonadFail m) =>
Proxy a -> (ProfileVersions -> m b) -> m b
checkConsistency forall a b. (a -> b) -> a -> b
$ \ProfileVersions
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
testRoundTrip :: forall a. (Show a, Eq a, SafeJSON a) => a -> Assertion
testRoundTrip :: forall a. (Show a, Eq a, SafeJSON a) => a -> Assertion
testRoundTrip a
a = (forall a. SafeJSON a => Proxy a -> String
typeName (forall {k} (t :: k). Proxy t
Proxy :: Proxy a) forall a. Semigroup a => a -> a -> a
<> String
": to JSON and back not consistent") forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
`assertEqual` forall a b. b -> Either a b
Right a
a forall a b. (a -> b) -> a -> b
$
forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) a
a
testRoundTripProp' :: forall a. (Eq a, Show a, Arbitrary a, SafeJSON a) => Proxy a -> String -> TestTree
testRoundTripProp' :: forall a.
(Eq a, Show a, Arbitrary a, SafeJSON a) =>
Proxy a -> String -> TestTree
testRoundTripProp' Proxy a
_ String
s = forall a. Testable a => String -> a -> TestTree
testProperty String
s forall a b. (a -> b) -> a -> b
$ \a
a ->
forall a b. b -> Either a b
Right (a
a :: a) forall a. Eq a => a -> a -> Bool
== forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) a
a
testRoundTripProp :: forall a. (Eq a, Show a, Arbitrary a, SafeJSON a) => String -> TestTree
testRoundTripProp :: forall a.
(Eq a, Show a, Arbitrary a, SafeJSON a) =>
String -> TestTree
testRoundTripProp String
s = forall a. Testable a => String -> a -> TestTree
testProperty String
s forall a b. (a -> b) -> a -> b
$ \a
a ->
forall a b. b -> Either a b
Right (a
a :: a) forall a. Eq a => a -> a -> Bool
== forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) a
a
testMigration :: (Show a, Eq a, Migrate a) => MigrateFrom a -> a -> Assertion
testMigration :: forall a.
(Show a, Eq a, Migrate a) =>
MigrateFrom a -> a -> Assertion
testMigration = forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"Unexpected result of SafeJSON migration" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Migrate a => MigrateFrom a -> a
migrate
testReverseMigration :: (Show a, Eq a, Migrate (Reverse a)) => MigrateFrom (Reverse a) -> a -> Assertion
testReverseMigration :: forall a.
(Show a, Eq a, Migrate (Reverse a)) =>
MigrateFrom (Reverse a) -> a -> Assertion
testReverseMigration = forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"Unexpected result of SafeJSON migration" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Reverse a -> a
unReverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Migrate a => MigrateFrom a -> a
migrate
infix 1 >=?, <=?
(<=?) :: (Show a, Eq a, Migrate a) => MigrateFrom a -> a -> Assertion
<=? :: forall a.
(Show a, Eq a, Migrate a) =>
MigrateFrom a -> a -> Assertion
(<=?) = forall a.
(Show a, Eq a, Migrate a) =>
MigrateFrom a -> a -> Assertion
testMigration
(>=?) :: (Show a, Eq a, Migrate (Reverse a)) => MigrateFrom (Reverse a) -> a -> Assertion
>=? :: forall a.
(Show a, Eq a, Migrate (Reverse a)) =>
MigrateFrom (Reverse a) -> a -> Assertion
(>=?) = forall a.
(Show a, Eq a, Migrate (Reverse a)) =>
MigrateFrom (Reverse a) -> a -> Assertion
testReverseMigration
migrateRoundTrip :: forall a. (Eq a, Show a, SafeJSON a, Migrate a) => MigrateFrom a -> Assertion
migrateRoundTrip :: forall a.
(Eq a, Show a, SafeJSON a, Migrate a) =>
MigrateFrom a -> Assertion
migrateRoundTrip MigrateFrom a
oldType = String
"Unexpected result of decoding encoded older type" forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
`assertEqual` forall a b. b -> Either a b
Right (forall a. Migrate a => MigrateFrom a -> a
migrate MigrateFrom a
oldType :: a) forall a b. (a -> b) -> a -> b
$
forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) MigrateFrom a
oldType
migrateReverseRoundTrip :: forall a. (Eq a, Show a, SafeJSON a, Migrate (Reverse a)) => MigrateFrom (Reverse a) -> Assertion
migrateReverseRoundTrip :: forall a.
(Eq a, Show a, SafeJSON a, Migrate (Reverse a)) =>
MigrateFrom (Reverse a) -> Assertion
migrateReverseRoundTrip MigrateFrom (Reverse a)
newType = String
"Unexpected result of decoding encoded newer type" forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
`assertEqual` forall a b. b -> Either a b
Right (forall a. Reverse a -> a
unReverse forall a b. (a -> b) -> a -> b
$ forall a. Migrate a => MigrateFrom a -> a
migrate MigrateFrom (Reverse a)
newType :: a) forall a b. (a -> b) -> a -> b
$
forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) MigrateFrom (Reverse a)
newType
type TestMigrate a b =
( Eq a
, Show b
, Arbitrary b
, SafeJSON a
, Migrate a
, MigrateFrom a ~ b
)
migrateRoundTripProp' :: forall a b. TestMigrate a b => Proxy (a,b) -> String -> TestTree
migrateRoundTripProp' :: forall a b. TestMigrate a b => Proxy (a, b) -> String -> TestTree
migrateRoundTripProp' Proxy (a, b)
_ String
s = forall a. Testable a => String -> a -> TestTree
testProperty String
s forall a b. (a -> b) -> a -> b
$ \b
a ->
forall a b. b -> Either a b
Right (forall a. Migrate a => MigrateFrom a -> a
migrate b
a :: a) forall a. Eq a => a -> a -> Bool
== forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) b
a
migrateRoundTripProp :: forall a b. TestMigrate a b => String -> TestTree
migrateRoundTripProp :: forall a b. TestMigrate a b => String -> TestTree
migrateRoundTripProp String
s = forall a. Testable a => String -> a -> TestTree
testProperty String
s forall a b. (a -> b) -> a -> b
$ \b
a ->
forall a b. b -> Either a b
Right (forall a. Migrate a => MigrateFrom a -> a
migrate b
a :: a) forall a. Eq a => a -> a -> Bool
== forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) b
a
type TestReverseMigrate a b =
( Eq a
, Show b
, Arbitrary b
, SafeJSON a
, Migrate (Reverse a)
, MigrateFrom (Reverse a) ~ b
)
migrateReverseRoundTripProp' :: forall a b. TestReverseMigrate a b => Proxy (a,b) -> String -> TestTree
migrateReverseRoundTripProp' :: forall a b.
TestReverseMigrate a b =>
Proxy (a, b) -> String -> TestTree
migrateReverseRoundTripProp' Proxy (a, b)
_ String
s = forall a. Testable a => String -> a -> TestTree
testProperty String
s forall a b. (a -> b) -> a -> b
$ \b
a ->
forall a b. b -> Either a b
Right (forall a. Reverse a -> a
unReverse forall a b. (a -> b) -> a -> b
$ forall a. Migrate a => MigrateFrom a -> a
migrate b
a :: a) forall a. Eq a => a -> a -> Bool
== forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) b
a
migrateReverseRoundTripProp :: forall a b. TestReverseMigrate a b => String -> TestTree
migrateReverseRoundTripProp :: forall a b. TestReverseMigrate a b => String -> TestTree
migrateReverseRoundTripProp String
s = forall a. Testable a => String -> a -> TestTree
testProperty String
s forall a b. (a -> b) -> a -> b
$ \b
a ->
forall a b. b -> Either a b
Right (forall a. Reverse a -> a
unReverse forall a b. (a -> b) -> a -> b
$ forall a. Migrate a => MigrateFrom a -> a
migrate b
a :: a) forall a. Eq a => a -> a -> Bool
== forall a b. (a -> Parser b) -> a -> Either String b
parseEither (forall a. SafeJSON a => Value -> Parser a
safeFromJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SafeJSON a => a -> Value
safeToJSON) b
a