Skip to content

Commit 1bfce04

Browse files
Soupstrawlehins
andcommitted
Address review comments
Co-authored-by: Alexey Kuleshevich <[email protected]>
1 parent a5ac732 commit 1bfce04

File tree

7 files changed

+36
-25
lines changed
  • eras
    • alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Binary
    • conway/impl/testlib/Test/Cardano/Ledger/Conway/Binary
    • dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Binary
    • shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Binary
  • libs/cardano-ledger-core/testlib/Test/Cardano/Ledger

7 files changed

+36
-25
lines changed

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Binary/Golden.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import Test.Cardano.Ledger.Allegra.Binary.Golden hiding (spec)
2727
import Test.Cardano.Ledger.Alonzo.Era (AlonzoEraTest)
2828
import Test.Cardano.Ledger.Binary.Plain.Golden (Enc (..))
2929
import Test.Cardano.Ledger.Common (
30+
NonNegative (..),
3031
Spec,
3132
describe,
3233
it,
34+
prop,
3335
)
3436
import Test.Cardano.Ledger.Imp.Common (forEachEraVersion)
3537

@@ -65,9 +67,13 @@ spec = do
6567
-- from there onwards
6668
it "plutusV2Script" $ expectSuccessOnEmptyFieldRaw 6
6769
it "plutusV3Script" $ expectSuccessOnEmptyFieldRaw 7
68-
it "8th field" . expectFailureOnEmptyField 8 $
69-
DecoderErrorDeserialiseFailure
70-
(Binary.label $ Proxy @(Annotator (TxWits era)))
71-
(DeserialiseFailure 2 "An error occured while decoding (Int,Void) not a valid key:.\nError: 8")
70+
prop "Invalid field" $ \(NonNegative n) ->
71+
let invalidTag = n + 8
72+
in expectFailureOnEmptyField invalidTag $
73+
DecoderErrorDeserialiseFailure
74+
(Binary.label $ Proxy @(Annotator (TxWits era)))
75+
( DeserialiseFailure 2 $
76+
"An error occurred while decoding (Int,Void) not a valid key:.\nError: " <> show invalidTag
77+
)
7278
describe "TxCerts" $ do
7379
forEachEraVersion @era $ allegraDecodeDuplicateDelegCertSucceeds @era

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Binary/Golden.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Test.Cardano.Ledger.Conway.Binary.Golden (
1111
spec,
1212
listRedeemersEnc,
1313
goldenListRedeemers,
14-
witsEmptyFieldWithTag,
14+
witsEmptyFieldWithSetTag,
1515
conwayDecodeDuplicateDelegCertFails,
1616
module Test.Cardano.Ledger.Alonzo.Binary.Golden,
1717
) where
@@ -73,8 +73,8 @@ goldenListRedeemers =
7373
(Redeemers $ Map.singleton (SpendingPurpose $ AsIx 10) (Data $ I 20, ExUnits 30 40))
7474
unRedeemers
7575

76-
witsEmptyFieldWithTag :: Int -> Enc
77-
witsEmptyFieldWithTag k =
76+
witsEmptyFieldWithSetTag :: Int -> Enc
77+
witsEmptyFieldWithSetTag k =
7878
mconcat
7979
[ E $ TkMapLen 1
8080
, E k
@@ -142,7 +142,7 @@ spec = do
142142
describe "Tagged" $ do
143143
let
144144
expectFailureOnEmptyField k =
145-
expectDecoderFailureAnn @(TxWits era) version (witsEmptyFieldWithTag k)
145+
expectDecoderFailureAnn @(TxWits era) version (witsEmptyFieldWithSetTag k)
146146
it "addrTxWits" . expectFailureOnEmptyField 0 $
147147
DecoderErrorDeserialiseFailure
148148
(Binary.label $ Proxy @(Annotator (TxWits era)))

eras/dijkstra/impl/testlib/Test/Cardano/Ledger/Dijkstra/Binary/Golden.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE GADTs #-}
44
{-# LANGUAGE OverloadedStrings #-}
5-
{-# LANGUAGE PatternSynonyms #-}
65
{-# LANGUAGE RankNTypes #-}
76
{-# LANGUAGE ScopedTypeVariables #-}
87
{-# LANGUAGE TypeApplications #-}
@@ -101,7 +100,7 @@ goldenEmptyFields version =
101100
describe "Tagged" $ do
102101
let
103102
expectFailureOnEmptyField k =
104-
expectDecoderFailureAnn @(TxWits era) version (witsEmptyFieldWithTag k)
103+
expectDecoderFailureAnn @(TxWits era) version (witsEmptyFieldWithSetTag k)
105104
it "addrTxWits" . expectFailureOnEmptyField 0 $
106105
DecoderErrorDeserialiseFailure
107106
(Binary.label $ Proxy @(Annotator (TxWits era)))

eras/shelley/impl/testlib/Test/Cardano/Ledger/Shelley/Binary/Golden.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import Test.Cardano.Ledger.Binary.Plain.Golden
3737
import Test.Cardano.Ledger.Common
3838
import Test.Cardano.Ledger.Core.Binary.Golden
3939
import Test.Cardano.Ledger.Core.KeyPair (mkKeyHash)
40-
import Test.Cardano.Ledger.Imp.Common (forEachEraVersion)
4140
import Test.Cardano.Ledger.Shelley.Arbitrary ()
4241
import Test.Cardano.Ledger.Shelley.Era (ShelleyEraTest)
4342

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Common.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE RankNTypes #-}
3+
{-# LANGUAGE ScopedTypeVariables #-}
4+
{-# LANGUAGE TypeApplications #-}
5+
16
module Test.Cardano.Ledger.Common (
27
module X,
38
ledgerTestMain,
@@ -47,8 +52,11 @@ module Test.Cardano.Ledger.Common (
4752

4853
-- * Miscellanous helpers
4954
tracedDiscard,
55+
forEachEraVersion,
5056
) where
5157

58+
import Cardano.Ledger.Binary (Version)
59+
import Cardano.Ledger.Core (Era, eraProtVersions)
5260
import Control.DeepSeq (NFData)
5361
import Control.Monad as X (forM_, replicateM, replicateM_, unless, void, when, (>=>))
5462
import qualified Debug.Trace as Debug
@@ -136,3 +144,7 @@ runGen ::
136144
Gen a ->
137145
a
138146
runGen seed size gen = unGen gen (mkQCGen seed) size
147+
148+
forEachEraVersion :: forall era. (Era era, HasCallStack) => (Version -> Spec) -> Spec
149+
forEachEraVersion sv = forM_ (eraProtVersions @era) $
150+
\version -> describe (show version) $ sv version

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Core/Binary/Golden.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE FlexibleContexts #-}
3-
{-# LANGUAGE RankNTypes #-}
43
{-# LANGUAGE ScopedTypeVariables #-}
54
{-# LANGUAGE TypeApplications #-}
65

@@ -31,10 +30,9 @@ import Test.Cardano.Ledger.Common (
3130
diffExprString,
3231
expectationFailure,
3332
shouldBe,
34-
shouldBeExpr,
3533
showExpr,
3634
)
37-
import Test.Cardano.Ledger.TreeDiff (ToExpr)
35+
import Test.Cardano.Ledger.TreeDiff (ToExpr, expectExprEqualWithMessage)
3836

3937
decodeEnc :: forall a. DecCBOR (Annotator a) => Version -> Enc -> Either DecoderError a
4038
decodeEnc version enc = decodeFullAnnotator @a version (Binary.label $ Proxy @(Annotator a)) decCBOR bytes
@@ -51,11 +49,12 @@ expectDecoderSuccessAnnWith ::
5149
Expectation
5250
expectDecoderSuccessAnnWith equals version enc expected =
5351
case decodeEnc @a version enc of
52+
Left err -> expectationFailure $ "Unexpected decoder failure: " <> show err
5453
Right x | x `equals` expected -> pure ()
55-
decResult -> expectationFailure $ diffExprString (Right expected) decResult
54+
Right result -> expectationFailure $ diffExprString expected result
5655

5756
expectDecoderSuccessAnn ::
58-
(ToExpr a, DecCBOR (Annotator a), Eq a) => Version -> Enc -> a -> Expectation
57+
(ToExpr a, DecCBOR (Annotator a), Eq a, HasCallStack) => Version -> Enc -> a -> Expectation
5958
expectDecoderSuccessAnn = expectDecoderSuccessAnnWith (==)
6059

6160
expectDecoderFailureAnn ::
@@ -78,4 +77,8 @@ expectDecoderResultOn ::
7877
(ToExpr b, DecCBOR (Annotator a), Eq b, HasCallStack) =>
7978
Version -> Enc -> a -> (a -> b) -> Expectation
8079
expectDecoderResultOn version enc expected f =
81-
embedTripAnnExpectation version version (\x _ -> f x `shouldBeExpr` f expected) enc
80+
embedTripAnnExpectation
81+
version
82+
version
83+
(\x _ -> expectExprEqualWithMessage "" (f x) (f expected))
84+
enc

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Imp/Common.hs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ module Test.Cardano.Ledger.Imp.Common (
8484
-- * Re-exports from ImpSpec
8585
withImpInit,
8686
modifyImpInit,
87-
forEachEraVersion,
8887
) where
8988

90-
import Cardano.Ledger.Binary (Version)
91-
import Cardano.Ledger.Core (eraProtVersions)
9289
import Control.Monad.IO.Class
9390
import Data.List (isInfixOf)
9491
import qualified System.Random.Stateful as R
@@ -147,7 +144,6 @@ import Test.Cardano.Ledger.Common as X hiding (
147144
vectorOf,
148145
)
149146
import Test.Cardano.Ledger.Core.KeyPair (KeyPair (..), mkAddr, mkCredential)
150-
import Test.Cardano.Ledger.Era (EraTest)
151147
import Test.ImpSpec (modifyImpInit, withImpInit)
152148
import Test.ImpSpec.Expectations.Lifted
153149
import Test.ImpSpec.Random (
@@ -229,7 +225,3 @@ expectNothingExpr (Just x) =
229225
assertFailure $
230226
"Expected Nothing, got Just:\n" <> showExpr x
231227
expectNothingExpr Nothing = pure ()
232-
233-
forEachEraVersion :: forall era. EraTest era => (Version -> Spec) -> Spec
234-
forEachEraVersion sv = forM_ (eraProtVersions @era) $
235-
\version -> describe (show version) $ sv version

0 commit comments

Comments
 (0)