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,
policy,
policySymbol,
stakeCreation,
validatorHashTN,
-- * Script contexts
stakeCreation,
stakeCreationWrongDatum,
stakeCreationUnsigned,
) where
--------------------------------------------------------------------------------
@ -67,6 +71,7 @@ validator = mkValidator (stakeValidator stake)
validatorHashTN :: TokenName
validatorHashTN = let ValidatorHash vh = validatorHash validator in TokenName vh
-- | This script context should be a valid transaction
stakeCreation :: ScriptContext
stakeCreation =
let st = Value.singleton policySymbol validatorHashTN 1 -- Stake ST
@ -94,3 +99,24 @@ stakeCreation =
}
, 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)
--------------------------------------------------------------------------------
@ -20,7 +19,7 @@ import Agora.Stake (stakePolicy)
--------------------------------------------------------------------------------
import Spec.Sample.Stake qualified as Stake
import Spec.Util (scriptTest)
import Spec.Util (policyFailsWith, policySucceedsWith)
--------------------------------------------------------------------------------
@ -28,6 +27,20 @@ tests :: [TestTree]
tests =
[ testGroup
"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.Prelude ()
import Plutus.V1.Ledger.Scripts (Script)
--------------------------------------------------------------------------------
scriptTest :: String -> Script -> TestTree
scriptTest name script = testCase name $ do
policySucceedsWith :: String -> ClosedTerm PMintingPolicy -> ClosedTerm PData -> _ -> TestTree
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
case res of
Left e -> do
assertFailure (show e <> " Traces: " <> show traces)
Right _v -> pure ()
assertFailure $
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 ];
inherit (haskell-nix) config;
};
nixpkgsFor' = system:
import nixpkgs {
inherit system;