add intentionally failing examples

This commit is contained in:
Emily Martins 2022-03-16 13:55:00 +01:00
parent 611e6fa2a6
commit fd7ef68b04
4 changed files with 77 additions and 9 deletions

View file

@ -9,8 +9,12 @@ module Spec.Sample.Stake (
stake, stake,
policy, policy,
policySymbol, policySymbol,
stakeCreation,
validatorHashTN, validatorHashTN,
-- * Script contexts
stakeCreation,
stakeCreationWrongDatum,
stakeCreationUnsigned,
) where ) where
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -67,6 +71,7 @@ validator = mkValidator (stakeValidator stake)
validatorHashTN :: TokenName validatorHashTN :: TokenName
validatorHashTN = let ValidatorHash vh = validatorHash validator in TokenName vh validatorHashTN = let ValidatorHash vh = validatorHash validator in TokenName vh
-- | This script context should be a valid transaction
stakeCreation :: ScriptContext stakeCreation :: ScriptContext
stakeCreation = stakeCreation =
let st = Value.singleton policySymbol validatorHashTN 1 -- Stake ST let st = Value.singleton policySymbol validatorHashTN 1 -- Stake ST
@ -94,3 +99,24 @@ stakeCreation =
} }
, scriptContextPurpose = Minting policySymbol , scriptContextPurpose = Minting policySymbol
} }
-- | This ScriptContext should fail because the datum has too much GT
stakeCreationWrongDatum :: ScriptContext
stakeCreationWrongDatum =
let datum :: Datum
datum = Datum (toBuiltinData $ StakeDatum 4242424242424242 signer) -- Too much GT
in ScriptContext
{ scriptContextTxInfo = stakeCreation.scriptContextTxInfo {txInfoData = [("", datum)]}
, scriptContextPurpose = Minting policySymbol
}
-- | This ScriptContext should fail because the datum has too much GT
stakeCreationUnsigned :: ScriptContext
stakeCreationUnsigned =
ScriptContext
{ scriptContextTxInfo =
stakeCreation.scriptContextTxInfo
{ txInfoSignatories = []
}
, scriptContextPurpose = Minting policySymbol
}

View file

@ -10,7 +10,6 @@ import Test.Tasty (TestTree, testGroup)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Plutarch (compile)
import Plutarch.Builtin (pforgetData) import Plutarch.Builtin (pforgetData)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -20,7 +19,7 @@ import Agora.Stake (stakePolicy)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Spec.Sample.Stake qualified as Stake import Spec.Sample.Stake qualified as Stake
import Spec.Util (scriptTest) import Spec.Util (policyFailsWith, policySucceedsWith)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -28,6 +27,20 @@ tests :: [TestTree]
tests = tests =
[ testGroup [ testGroup
"policy" "policy"
[ scriptTest "minting" (compile $ stakePolicy Stake.stake # pforgetData (pconstantData ()) # pconstant Stake.stakeCreation) [ policySucceedsWith
"stakeCreation"
(stakePolicy Stake.stake)
(pforgetData (pconstantData ()))
Stake.stakeCreation
, policyFailsWith
"stakeCreationWrongDatum"
(stakePolicy Stake.stake)
(pforgetData (pconstantData ()))
Stake.stakeCreationWrongDatum
, policyFailsWith
"stakeCreationUnsigned"
(stakePolicy Stake.stake)
(pforgetData (pconstantData ()))
Stake.stakeCreationUnsigned
] ]
] ]

View file

@ -1,4 +1,9 @@
module Spec.Util (scriptTest) where module Spec.Util (
scriptSucceeds,
scriptFails,
policySucceedsWith,
policyFailsWith,
) where
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -11,15 +16,38 @@ import Test.Tasty.HUnit (assertFailure, testCase)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Plutarch
import Plutarch.Api.V1 (PMintingPolicy)
import Plutarch.Evaluate (evalScript) import Plutarch.Evaluate (evalScript)
import Plutarch.Prelude ()
import Plutus.V1.Ledger.Scripts (Script) import Plutus.V1.Ledger.Scripts (Script)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
scriptTest :: String -> Script -> TestTree policySucceedsWith :: String -> ClosedTerm PMintingPolicy -> ClosedTerm PData -> _ -> TestTree
scriptTest name script = testCase name $ do policySucceedsWith tag policy redeemer scriptContext =
scriptSucceeds tag $ compile (policy # redeemer # pconstant scriptContext)
policyFailsWith :: String -> ClosedTerm PMintingPolicy -> ClosedTerm PData -> _ -> TestTree
policyFailsWith tag policy redeemer scriptContext =
scriptFails tag $ compile (policy # redeemer # pconstant scriptContext)
scriptSucceeds :: String -> Script -> TestTree
scriptSucceeds name script = testCase name $ do
let (res, _budget, traces) = evalScript script let (res, _budget, traces) = evalScript script
case res of case res of
Left e -> do Left e -> do
assertFailure (show e <> " Traces: " <> show traces) assertFailure $
Right _v -> pure () show e <> " Traces: " <> show traces
Right _v ->
pure ()
scriptFails :: String -> Script -> TestTree
scriptFails name script = testCase name $ do
let (res, _budget, traces) = evalScript script
case res of
Left _e ->
pure ()
Right v ->
assertFailure $
"Expected failure, but succeeded. " <> show v <> " Traces: " <> show traces

View file

@ -32,6 +32,7 @@
overlays = [ haskell-nix.overlay ]; overlays = [ haskell-nix.overlay ];
inherit (haskell-nix) config; inherit (haskell-nix) config;
}; };
nixpkgsFor' = system: nixpkgsFor' = system:
import nixpkgs { import nixpkgs {
inherit system; inherit system;