Skip to content

Commit 09a6d21

Browse files
committed
Inline test_roundtrip_Tx_Cddl and test_roundtrip_TxWitness_Cddl and convert tests into property tests
1 parent 3887983 commit 09a6d21

File tree

6 files changed

+71
-41
lines changed

6 files changed

+71
-41
lines changed

cardano-api/cardano-api.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ test-suite cardano-api-test
240240
, time
241241

242242
other-modules: Test.Cardano.Api.Crypto
243+
Test.Cardano.Api.Eras
243244
Test.Cardano.Api.Genesis
244245
Test.Cardano.Api.Json
245246
Test.Cardano.Api.KeysByron

cardano-api/gen/Test/Hedgehog/Roundtrip/CBOR.hs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
module Test.Hedgehog.Roundtrip.CBOR
77
( roundtrip_CBOR
8-
-- , roundtrip_CDDL_Tx
98
) where
109

1110
import Cardano.Api

cardano-api/src/Cardano/Api/Eras.hs

+2-7
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,7 @@ deriving instance Ord (ShelleyBasedEra era)
342342
deriving instance Show (ShelleyBasedEra era)
343343

344344
instance ToJSON (ShelleyBasedEra era) where
345-
toJSON ShelleyBasedEraShelley = "Shelley"
346-
toJSON ShelleyBasedEraAllegra = "Allegra"
347-
toJSON ShelleyBasedEraMary = "Mary"
348-
toJSON ShelleyBasedEraAlonzo = "Alonzo"
349-
toJSON ShelleyBasedEraBabbage = "Babbage"
350-
toJSON ShelleyBasedEraConway = "Conway"
345+
toJSON = toJSON . shelleyBasedToCardanoEra
351346

352347
instance TestEquality ShelleyBasedEra where
353348
testEquality ShelleyBasedEraShelley ShelleyBasedEraShelley = Just Refl
@@ -436,7 +431,7 @@ instance FromJSON AnyShelleyBasedEra where
436431
"Alonzo" -> pure $ AnyShelleyBasedEra ShelleyBasedEraAlonzo
437432
"Babbage" -> pure $ AnyShelleyBasedEra ShelleyBasedEraBabbage
438433
"Conway" -> pure $ AnyShelleyBasedEra ShelleyBasedEraConway
439-
wrong -> fail $ "Failed to parse unknown era: " <> Text.unpack wrong
434+
wrong -> fail $ "Failed to parse unknown shelley-based era: " <> Text.unpack wrong
440435

441436

442437
-- | This pairs up some era-dependent type with a 'ShelleyBasedEra' value that
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
3+
module Test.Cardano.Api.Eras
4+
( tests
5+
) where
6+
7+
import Cardano.Api
8+
import Cardano.Api.Orphans ()
9+
import Data.Aeson (ToJSON (..), decode, encode)
10+
import Hedgehog (Property, forAll, property, (===))
11+
import qualified Hedgehog as H
12+
import qualified Hedgehog.Gen as Gen
13+
import Test.Tasty (TestTree, testGroup)
14+
import Test.Tasty.Hedgehog (testPropertyNamed)
15+
16+
--------------------------------------------------------------------------------
17+
-- Bounded instances
18+
19+
prop_maxBound_CardanoMatchesShelley :: Property
20+
prop_maxBound_CardanoMatchesShelley = property $ do
21+
AnyCardanoEra era <- forAll $ Gen.element [maxBound]
22+
AnyShelleyBasedEra sbe <- forAll $ Gen.element [maxBound]
23+
24+
fromEnum (AnyCardanoEra era) === fromEnum (AnyCardanoEra (shelleyBasedToCardanoEra sbe))
25+
26+
--------------------------------------------------------------------------------
27+
-- Aeson instances
28+
29+
prop_roundtrip_JSON_Shelley :: Property
30+
prop_roundtrip_JSON_Shelley = property $ do
31+
anySbe <- forAll $ Gen.element @_ @AnyShelleyBasedEra [minBound..maxBound]
32+
33+
H.tripping anySbe encode decode
34+
35+
prop_roundtrip_JSON_Cardano :: Property
36+
prop_roundtrip_JSON_Cardano = property $ do
37+
anyEra <- forAll $ Gen.element @_ @AnyCardanoEra [minBound..maxBound]
38+
39+
H.tripping anyEra encode decode
40+
41+
prop_toJSON_CardanoMatchesShelley :: Property
42+
prop_toJSON_CardanoMatchesShelley = property $ do
43+
AnyShelleyBasedEra sbe <- forAll $ Gen.element [minBound..maxBound]
44+
45+
toJSON (AnyShelleyBasedEra sbe) === toJSON (AnyCardanoEra (shelleyBasedToCardanoEra sbe))
46+
47+
tests :: TestTree
48+
tests = testGroup "Test.Cardano.Api.Json"
49+
[ testPropertyNamed "maxBound cardano matches shelley" "maxBound cardano matches shelley" prop_maxBound_CardanoMatchesShelley
50+
, testPropertyNamed "roundtrip JSON shelley" "roundtrip JSON shelley" prop_roundtrip_JSON_Shelley
51+
, testPropertyNamed "roundtrip JSON cardano" "roundtrip JSON cardano" prop_roundtrip_JSON_Cardano
52+
, testPropertyNamed "toJSON cardano matches shelley" "toJSON cardano matches shelley" prop_toJSON_CardanoMatchesShelley
53+
]

cardano-api/test/Test/Cardano/Api/Typed/CBOR.hs

+13-33
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Cardano.Api
1010

1111
import Data.Proxy (Proxy (..))
1212
import Data.String (IsString (..))
13-
import Hedgehog (Property, forAll, property, success, tripping)
13+
import Hedgehog (Property, forAll, tripping)
1414
import qualified Hedgehog as H
1515
import qualified Hedgehog.Gen as Gen
1616
import Test.Cardano.Api.Typed.Orphans ()
@@ -156,38 +156,18 @@ prop_roundtrip_UpdateProposal_CBOR :: Property
156156
prop_roundtrip_UpdateProposal_CBOR =
157157
roundtrip_CBOR AsUpdateProposal genUpdateProposal
158158

159+
prop_roundtrip_Tx_Cddl :: Property
160+
prop_roundtrip_Tx_Cddl = H.property $ do
161+
AnyCardanoEra era <- H.forAll $ Gen.element [minBound..maxBound]
162+
x <- forAll $ genTx era
163+
H.tripping x serialiseTxLedgerCddl (deserialiseTxLedgerCddl era)
159164

160-
test_roundtrip_Tx_Cddl :: [TestTree]
161-
test_roundtrip_Tx_Cddl =
162-
[ testPropertyNamed (show era) (fromString (show era)) $ roundtrip_Tx_Cddl anyEra
163-
| anyEra@(AnyCardanoEra era) <- [minBound..(AnyCardanoEra AlonzoEra)] --TODO: Babbage era
164-
]
165-
166-
test_roundtrip_TxWitness_Cddl :: [TestTree]
167-
test_roundtrip_TxWitness_Cddl =
168-
[ testPropertyNamed (show era) (fromString (show era)) $ roundtrip_TxWitness_Cddl era
169-
| AnyCardanoEra era <- [minBound..(AnyCardanoEra AlonzoEra)] --TODO: Babbage era
170-
, AnyCardanoEra era /= AnyCardanoEra ByronEra
171-
]
165+
prop_roundtrip_TxWitness_Cddl :: Property
166+
prop_roundtrip_TxWitness_Cddl = H.property $ do
167+
AnyShelleyBasedEra sbe <- H.forAll $ Gen.element [minBound..maxBound]
168+
x <- forAll $ genShelleyKeyWitness $ shelleyBasedToCardanoEra sbe
169+
tripping x (serialiseWitnessLedgerCddl sbe) (deserialiseWitnessLedgerCddl sbe)
172170

173-
roundtrip_TxWitness_Cddl :: CardanoEra era -> Property
174-
roundtrip_TxWitness_Cddl era =
175-
property $
176-
case cardanoEraStyle era of
177-
LegacyByronEra -> success
178-
ShelleyBasedEra sbe -> do
179-
keyWit <- forAll $ genShelleyKeyWitness era
180-
tripping keyWit
181-
(serialiseWitnessLedgerCddl sbe)
182-
(deserialiseWitnessLedgerCddl sbe)
183-
184-
roundtrip_Tx_Cddl :: AnyCardanoEra -> Property
185-
roundtrip_Tx_Cddl (AnyCardanoEra era) =
186-
property $ do
187-
tx <- forAll $ genTx era
188-
tripping tx
189-
serialiseTxLedgerCddl
190-
(deserialiseTxLedgerCddl era)
191171

192172
-- -----------------------------------------------------------------------------
193173

@@ -223,7 +203,7 @@ tests = testGroup "Test.Cardano.Api.Typed.CBOR"
223203
, testPropertyNamed "roundtrip UpdateProposal CBOR" "roundtrip UpdateProposal CBOR" prop_roundtrip_UpdateProposal_CBOR
224204
, testPropertyNamed "roundtrip ScriptData CBOR" "roundtrip ScriptData CBOR" prop_roundtrip_ScriptData_CBOR
225205
, testPropertyNamed "roundtrip txbody CBOR" "roundtrip txbody CBOR" prop_roundtrip_txbody_CBOR
206+
, testPropertyNamed "roundtrip Tx Cddl" "roundtrip Tx Cddl" prop_roundtrip_Tx_Cddl
207+
, testPropertyNamed "roundtrip TxWitness Cddl" "roundtrip TxWitness Cddl" prop_roundtrip_TxWitness_Cddl
226208
, testGroup "roundtrip tx CBOR" test_roundtrip_tx_CBOR
227-
, testGroup "roundtrip Tx Cddl" test_roundtrip_Tx_Cddl
228-
, testGroup "roundtrip TxWitness Cddl" test_roundtrip_TxWitness_Cddl
229209
]

cardano-api/test/cardano-api-test.hs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Cardano.Crypto.Libsodium (sodiumInit)
44
import Test.Tasty (TestTree, defaultMain, testGroup)
55

66
import qualified Test.Cardano.Api.Crypto
7+
import qualified Test.Cardano.Api.Eras
78
import qualified Test.Cardano.Api.Json
89
import qualified Test.Cardano.Api.KeysByron
910
import qualified Test.Cardano.Api.Ledger
@@ -29,6 +30,7 @@ tests :: TestTree
2930
tests =
3031
testGroup "Cardano.Api"
3132
[ Test.Cardano.Api.Crypto.tests
33+
, Test.Cardano.Api.Eras.tests
3234
, Test.Cardano.Api.Json.tests
3335
, Test.Cardano.Api.KeysByron.tests
3436
, Test.Cardano.Api.Ledger.tests

0 commit comments

Comments
 (0)