Skip to content

Commit

Permalink
dry a bit the StateSpec: define forAllCommit.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Feb 23, 2022
1 parent fe9ed40 commit be0a0e8
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions hydra-node/test/Hydra/Chain/Direct/StateSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,43 @@ import Hydra.Chain.Direct.State (
)
import Hydra.Ledger.Cardano (genOneUTxOFor, genTxIn, genVerificationKey)
import Hydra.Party (Party)
import Test.QuickCheck (choose, elements, forAll, property, vector)
import Test.QuickCheck (Property, choose, elements, forAll, property, vector)

spec :: Spec
spec =
describe "commit" $ do
prop "consumes all inputs that are committed." $
forAll genHydraContext $ \ctx ->
forAll (genStInitialized ctx) $ \stInitialized ->
forAll genSingleUTxO $ \utxo ->
let tx = unsafeCommit utxo stInitialized
in case transition @_ @'StInitialized tx stInitialized of
Just (_, st') ->
let knownInputs = UTxO.inputSet (getKnownUTxO st')
in property (knownInputs `Set.disjoint` txInputSet tx)
Nothing ->
property False
forAllCommit $ \st tx ->
case transition @_ @'StInitialized tx st of
Just (_, st') ->
let knownInputs = UTxO.inputSet (getKnownUTxO st')
in property (knownInputs `Set.disjoint` txInputSet tx)
Nothing ->
property False

prop "can only apply / observe the same commit once" $
forAll genHydraContext $ \ctx ->
forAll (genStInitialized ctx) $ \stInitialized ->
forAll genSingleUTxO $ \utxo ->
let tx = unsafeCommit utxo stInitialized
in case transition tx stInitialized of
Just (_, st' :: OnChainHeadState 'StInitialized) ->
case transition @_ @'StInitialized tx st' of
Just{} -> property False
Nothing -> property True
Nothing ->
property False
forAllCommit $ \st tx ->
case transition @_ @'StInitialized tx st of
Just (_, st') ->
case transition @_ @'StInitialized tx st' of
Just{} -> property False
Nothing -> property True
Nothing ->
property False

--
-- QuickCheck Extras
--

forAllCommit
:: (OnChainHeadState 'StInitialized -> Tx -> Property)
-> Property
forAllCommit action = do
forAll genHydraContext $ \ctx ->
forAll (genStInitialized ctx) $ \stInitialized ->
forAll genSingleUTxO $ \utxo ->
let tx = unsafeCommit utxo stInitialized
in action stInitialized tx

--
-- Generators
Expand Down

0 comments on commit be0a0e8

Please sign in to comment.