{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Test.Cardano.Ledger.Plutus.ToPlutusData (
roundTripPlutusDataSpec,
sameAsToBuiltinDataSpec,
) where
import Cardano.Ledger.Plutus.ToPlutusData
import qualified PlutusLedgerApi.V4 as PV4
import Test.Cardano.Ledger.Common
import Test.Cardano.Ledger.Core.Arbitrary ()
import Type.Reflection (Typeable, typeRep)
roundTripPlutusDataSpec ::
forall x. (HasCallStack, Typeable x, Show x, Eq x, Arbitrary x, ToPlutusData x) => Spec
roundTripPlutusDataSpec :: forall x.
(HasCallStack, Typeable x, Show x, Eq x, Arbitrary x,
ToPlutusData x) =>
Spec
roundTripPlutusDataSpec =
String -> (x -> Expectation) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop (TypeRep x -> String
forall a. Show a => a -> String
show (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @x)) ((x -> Expectation) -> Spec) -> (x -> Expectation) -> Spec
forall a b. (a -> b) -> a -> b
$ \x
y ->
Data -> Maybe x
forall x. ToPlutusData x => Data -> Maybe x
fromPlutusData (x -> Data
forall x. ToPlutusData x => x -> Data
toPlutusData x
y) Maybe x -> Maybe x -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` x -> Maybe x
forall a. a -> Maybe a
Just (x
y :: x)
sameAsToBuiltinDataSpec ::
forall x y.
(HasCallStack, Typeable x, Show x, Arbitrary x, ToPlutusData x, PV4.ToData y) =>
(x -> y) ->
Spec
sameAsToBuiltinDataSpec :: forall x y.
(HasCallStack, Typeable x, Show x, Arbitrary x, ToPlutusData x,
ToData y) =>
(x -> y) -> Spec
sameAsToBuiltinDataSpec x -> y
toPlutusType =
String -> (x -> Expectation) -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop (TypeRep x -> String
forall a. Show a => a -> String
show (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @x)) ((x -> Expectation) -> Spec) -> (x -> Expectation) -> Spec
forall a b. (a -> b) -> a -> b
$ \(x
x :: x) ->
Data -> BuiltinData
PV4.dataToBuiltinData (x -> Data
forall x. ToPlutusData x => x -> Data
toPlutusData x
x) BuiltinData -> BuiltinData -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` y -> BuiltinData
forall a. ToData a => a -> BuiltinData
PV4.toBuiltinData (x -> y
toPlutusType x
x)