Skip to content

Commit

Permalink
Add tests for cddlTypeToEra
Browse files Browse the repository at this point in the history
  • Loading branch information
palas authored and ffakenz committed Sep 18, 2024
1 parent 495734b commit 83a54b5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ test-suite cardano-api-test
tasty,
tasty-hedgehog,
tasty-quickcheck,
text,
time,

other-modules:
Expand Down
54 changes: 28 additions & 26 deletions cardano-api/internal/Cardano/Api/SerialiseLedgerCddl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
module Cardano.Api.SerialiseLedgerCddl
( TextEnvelopeCddlError (..)
, FromSomeTypeCDDL (..)
, cddlTypeToEra

-- * Reading one of several transaction or

Expand Down Expand Up @@ -315,32 +316,33 @@ deserialiseFromTextEnvelopeCddlAnyOf types teCddl =
-- will make it easier to keep track of the different Cddl descriptions via
-- a single sum data type.
cddlTypeToEra :: Text -> Either TextEnvelopeCddlError AnyShelleyBasedEra
cddlTypeToEra = \case
"TxSignedShelley" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"Tx AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"Tx MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"Tx AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"Tx BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"Tx ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
"Witnessed Tx ShelleyEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"Witnessed Tx AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"Witnessed Tx MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"Witnessed Tx AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"Witnessed Tx BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"Witnessed Tx ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
"Unwitnessed Tx ShelleyEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"Unwitnessed Tx AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"Unwitnessed Tx MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"Unwitnessed Tx AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"Unwitnessed Tx BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"Unwitnessed Tx ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
"TxWitness ShelleyEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"TxWitness AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"TxWitness MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"TxWitness AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"TxWitness BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"TxWitness ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
unknownCddlType -> Left $ TextEnvelopeCddlErrUnknownType unknownCddlType
cddlTypeToEra =
\case
"TxSignedShelley" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"Tx AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"Tx MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"Tx AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"Tx BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"Tx ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
"Witnessed Tx ShelleyEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"Witnessed Tx AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"Witnessed Tx MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"Witnessed Tx AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"Witnessed Tx BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"Witnessed Tx ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
"Unwitnessed Tx ShelleyEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"Unwitnessed Tx AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"Unwitnessed Tx MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"Unwitnessed Tx AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"Unwitnessed Tx BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"Unwitnessed Tx ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
"TxWitness ShelleyEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraShelley
"TxWitness AllegraEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAllegra
"TxWitness MaryEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraMary
"TxWitness AlonzoEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
"TxWitness BabbageEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraBabbage
"TxWitness ConwayEra" -> return $ AnyShelleyBasedEra ShelleyBasedEraConway
unknownCddlType -> Left $ TextEnvelopeCddlErrUnknownType unknownCddlType

readFileTextEnvelopeCddlAnyOf
:: [FromSomeTypeCDDL TextEnvelope b]
Expand Down
32 changes: 32 additions & 0 deletions cardano-api/test/cardano-api-test/Test/Cardano/Api/CBOR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ module Test.Cardano.Api.CBOR
where

import Cardano.Api
import Cardano.Api.SerialiseLedgerCddl (cddlTypeToEra)
import Cardano.Api.SerialiseTextEnvelope (TextEnvelopeDescr (TextEnvelopeDescr))
import Cardano.Api.Shelley (AsType (..))

import Data.Proxy (Proxy (..))
import qualified Data.Text as T

import Test.Gen.Cardano.Api.Typed

Expand Down Expand Up @@ -188,6 +190,30 @@ prop_roundtrip_UpdateProposal_CBOR = H.property $ do
proposal <- H.forAll $ genUpdateProposal era
H.trippingCbor AsUpdateProposal proposal

prop_Tx_cddlTypeToEra :: Property
prop_Tx_cddlTypeToEra = H.property $ do
AnyShelleyBasedEra era <- H.noteShowM . H.forAll $ Gen.element [minBound .. maxBound]
x <- forAll $ genTx era
shelleyBasedEraConstraints era $ do
let TextEnvelopeType d = textEnvelopeType (proxyToAsType (getProxy x))
H.note_ $ "Envelope type: " <> show d
cddlTypeToEra (T.pack d) H.=== Right (AnyShelleyBasedEra era)
where
getProxy :: forall a. a -> Proxy a
getProxy _ = Proxy

prop_TxWitness_cddlTypeToEra :: Property
prop_TxWitness_cddlTypeToEra = H.property $ do
AnyShelleyBasedEra era <- H.noteShowM . H.forAll $ Gen.element [minBound .. maxBound]
x <- forAll $ genCardanoKeyWitness era
shelleyBasedEraConstraints era $ do
let TextEnvelopeType d = textEnvelopeType (proxyToAsType (getProxy x))
H.note_ $ "Envelope type: " <> show d
cddlTypeToEra (T.pack d) H.=== Right (AnyShelleyBasedEra era)
where
getProxy :: forall a. a -> Proxy a
getProxy _ = Proxy

prop_roundtrip_Tx_Cddl :: Property
prop_roundtrip_Tx_Cddl = H.property $ do
AnyShelleyBasedEra era <- H.noteShowM . H.forAll $ Gen.element [minBound .. maxBound]
Expand Down Expand Up @@ -281,6 +307,12 @@ tests =
, testProperty
"roundtrip script PlutusScriptV2 CBOR"
prop_roundtrip_script_PlutusScriptV2_CBOR
, testProperty
"cddlTypeToEra for Tx types"
prop_Tx_cddlTypeToEra
, testProperty
"cddlTypeToEra for TxWitness types"
prop_TxWitness_cddlTypeToEra
, testProperty
"roundtrip UpdateProposal CBOR"
prop_roundtrip_UpdateProposal_CBOR
Expand Down

0 comments on commit 83a54b5

Please sign in to comment.