Skip to content

Commit 5077cdd

Browse files
committed
test: improve error diagnostics in evaluation context test utilities
Replaces unsafe fromJust usage with explicit error messages and HasCallStack constraint in costModelParamsForTesting. This provides better debugging context when cost model parameter extraction fails, including stack traces that pinpoint the exact call site.
1 parent cd615cb commit 5077cdd

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

plutus-ledger-api/testlib/PlutusLedgerApi/Test/V1/Data/EvaluationContext.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import PlutusPrelude
1616

1717
import Data.Int (Int64)
1818
import Data.Map qualified as Map
19-
import Data.Maybe
19+
import GHC.Stack (HasCallStack)
2020

2121
-- | Example values of costs for @PlutusV1@, in expected ledger order.
2222
-- Suitable to be used in testing.
23-
costModelParamsForTesting :: [(V1.ParamName, Int64)]
24-
costModelParamsForTesting = Map.toList $ fromJust $
25-
Common.extractCostModelParamsLedgerOrder mCostModel
23+
costModelParamsForTesting :: HasCallStack => [(V1.ParamName, Int64)]
24+
costModelParamsForTesting =
25+
case Common.extractCostModelParamsLedgerOrder mCostModel of
26+
Nothing -> error "extractCostModelParamsLedgerOrder (V1): nothing extracted"
27+
Just xs -> Map.toList xs
2628

2729
-- | The PlutusV1 "cost model" is constructed by the v2 "cost model", by clearing v2 introductions.
2830
mCostModel :: MCostModel

plutus-ledger-api/testlib/PlutusLedgerApi/Test/V1/EvaluationContext.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import PlutusPrelude
1616

1717
import Data.Int (Int64)
1818
import Data.Map qualified as Map
19-
import Data.Maybe
19+
import GHC.Stack (HasCallStack)
2020

2121
-- | Example values of costs for @PlutusV1@, in expected ledger order.
2222
-- Suitable to be used in testing.
23-
costModelParamsForTesting :: [(V1.ParamName, Int64)]
24-
costModelParamsForTesting = Map.toList $ fromJust $
25-
Common.extractCostModelParamsLedgerOrder mCostModel
23+
costModelParamsForTesting :: HasCallStack => [(V1.ParamName, Int64)]
24+
costModelParamsForTesting =
25+
case Common.extractCostModelParamsLedgerOrder mCostModel of
26+
Nothing -> error "extractCostModelParamsLedgerOrder (V1): nothing extracted"
27+
Just xs -> Map.toList xs
2628

2729
-- | The PlutusV1 "cost model" is constructed by the v2 "cost model", by clearing v2 introductions.
2830
mCostModel :: MCostModel

plutus-ledger-api/testlib/PlutusLedgerApi/Test/V2/Data/EvaluationContext.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import PlutusPrelude
1818

1919
import Data.Int (Int64)
2020
import Data.Map qualified as Map
21-
import Data.Maybe
21+
import GHC.Stack (HasCallStack)
2222

2323
-- | Example values of costs for @PlutusV2@, in expected ledger order.
2424
-- Suitable to be used in testing.
25-
costModelParamsForTesting :: [(V2.ParamName, Int64)]
26-
costModelParamsForTesting = Map.toList $ fromJust $
27-
Common.extractCostModelParamsLedgerOrder mCostModel
25+
costModelParamsForTesting :: HasCallStack => [(V2.ParamName, Int64)]
26+
costModelParamsForTesting =
27+
case Common.extractCostModelParamsLedgerOrder mCostModel of
28+
Nothing -> error "extractCostModelParamsLedgerOrder (V2): nothing extracted"
29+
Just xs -> Map.toList xs
2830

2931
-- | The PlutusV2 "cost model" is constructed by the v3 "cost model", by clearing v3 introductions.
3032
mCostModel :: MCostModel

plutus-ledger-api/testlib/PlutusLedgerApi/Test/V2/EvaluationContext.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import PlutusPrelude
1818

1919
import Data.Int (Int64)
2020
import Data.Map qualified as Map
21-
import Data.Maybe
21+
import GHC.Stack (HasCallStack)
2222

2323
-- | Example values of costs for @PlutusV2@, in expected ledger order.
2424
-- Suitable to be used in testing.
25-
costModelParamsForTesting :: [(V2.ParamName, Int64)]
26-
costModelParamsForTesting = Map.toList $ fromJust $
27-
Common.extractCostModelParamsLedgerOrder mCostModel
25+
costModelParamsForTesting :: HasCallStack => [(V2.ParamName, Int64)]
26+
costModelParamsForTesting =
27+
case Common.extractCostModelParamsLedgerOrder mCostModel of
28+
Nothing -> error "extractCostModelParamsLedgerOrder (V2): nothing extracted"
29+
Just xs -> Map.toList xs
2830

2931
-- | The PlutusV2 "cost model" is constructed by the v3 "cost model", by clearing v3 introductions.
3032
mCostModel :: MCostModel

plutus-ledger-api/testlib/PlutusLedgerApi/Test/V3/Data/EvaluationContext.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import UntypedPlutusCore.Evaluation.Machine.Cek.CekMachineCosts
1717

1818
import Data.Int (Int64)
1919
import Data.Map qualified as Map
20-
import Data.Maybe
2120
import GHC.Stack (HasCallStack)
2221

2322
-- | Example values of costs for @PlutusV3@, in expected ledger order.
2423
-- Suitable to be used in testing.
2524
costModelParamsForTesting :: HasCallStack => [(V3.ParamName, Int64)]
26-
costModelParamsForTesting = Map.toList $ fromJust $
27-
Common.extractCostModelParamsLedgerOrder mCostModel
25+
costModelParamsForTesting =
26+
case Common.extractCostModelParamsLedgerOrder mCostModel of
27+
Nothing -> error "extractCostModelParamsLedgerOrder (V3): nothing extracted"
28+
Just xs -> Map.toList xs
2829

2930
-- | The PlutusV3 "cost model" is constructed by the v4 "cost model", by clearing v4 introductions.
3031
mCostModel :: MCostModel

plutus-ledger-api/testlib/PlutusLedgerApi/Test/V3/EvaluationContext.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import UntypedPlutusCore.Evaluation.Machine.Cek.CekMachineCosts
1818

1919
import Data.Int (Int64)
2020
import Data.Map qualified as Map
21-
import Data.Maybe
2221
import GHC.Stack (HasCallStack)
2322

2423

@@ -29,8 +28,10 @@ import GHC.Stack (HasCallStack)
2928
-- | Example values of costs for @PlutusV3@, in expected ledger order.
3029
-- Suitable to be used in testing.
3130
costModelParamsForTesting :: HasCallStack => [(V3.ParamName, Int64)]
32-
costModelParamsForTesting = Map.toList $ fromJust $
33-
Common.extractCostModelParamsLedgerOrder mCostModel
31+
costModelParamsForTesting =
32+
case Common.extractCostModelParamsLedgerOrder mCostModel of
33+
Nothing -> error "extractCostModelParamsLedgerOrder (V3): nothing extracted"
34+
Just xs -> Map.toList xs
3435

3536
-- | The PlutusV3 "cost model" is constructed by the v4 "cost model", by clearing v4 introductions.
3637
mCostModel :: MCostModel

0 commit comments

Comments
 (0)