Skip to content

Commit

Permalink
Add generators and shrinkers for type UTxOSelection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Sep 26, 2021
1 parent 3b4e54c commit be3bad0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/core/cardano-wallet-core.cabal
Expand Up @@ -245,6 +245,7 @@ library
Cardano.Wallet.Primitive.Types.Tx.Gen
Cardano.Wallet.Primitive.Types.UTxO.Gen
Cardano.Wallet.Primitive.Types.UTxOIndex.Gen
Cardano.Wallet.Primitive.Types.UTxOSelection.Gen
Cardano.Wallet.Gen
other-modules:
Paths_cardano_wallet_core
Expand Down
62 changes: 62 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Types/UTxOSelection/Gen.hs
@@ -0,0 +1,62 @@
{-# LANGUAGE TypeApplications #-}

module Cardano.Wallet.Primitive.Types.UTxOSelection.Gen
( genUTxOSelection
, genUTxOSelectionNonEmpty
, shrinkUTxOSelection
, shrinkUTxOSelectionNonEmpty
)
where

import Prelude

import Cardano.Wallet.Primitive.Types.Tx
( TxIn )
import Cardano.Wallet.Primitive.Types.Tx.Gen
( coarbitraryTxIn )
import Cardano.Wallet.Primitive.Types.UTxOIndex.Gen
( genUTxOIndex, shrinkUTxOIndex )
import Cardano.Wallet.Primitive.Types.UTxOSelection
( UTxOSelection, UTxOSelectionNonEmpty )
import Data.Maybe
( mapMaybe )
import Test.QuickCheck
( Gen, arbitrary, liftShrink2, shrinkMapBy, suchThatMap )
import Test.QuickCheck.Extra
( genFunction )

import qualified Cardano.Wallet.Primitive.Types.UTxOSelection as UTxOSelection

--------------------------------------------------------------------------------
-- Selections that may be empty
--------------------------------------------------------------------------------

genUTxOSelection :: Gen UTxOSelection
genUTxOSelection = UTxOSelection.fromIndexFiltered
<$> genUTxOIndex
<*> genFilter
where
genFilter :: Gen (TxIn -> Bool)
genFilter = genFunction coarbitraryTxIn (arbitrary @Bool)

shrinkUTxOSelection :: UTxOSelection -> [UTxOSelection]
shrinkUTxOSelection =
shrinkMapBy UTxOSelection.fromIndexPair UTxOSelection.toIndexPair $
liftShrink2
shrinkUTxOIndex
shrinkUTxOIndex

--------------------------------------------------------------------------------
-- Selections that are non-empty
--------------------------------------------------------------------------------

genUTxOSelectionNonEmpty :: Gen UTxOSelectionNonEmpty
genUTxOSelectionNonEmpty =
genUTxOSelection `suchThatMap` UTxOSelection.toNonEmpty

shrinkUTxOSelectionNonEmpty :: UTxOSelectionNonEmpty -> [UTxOSelectionNonEmpty]
shrinkUTxOSelectionNonEmpty
= mapMaybe UTxOSelection.toNonEmpty
. shrinkUTxOSelection
. UTxOSelection.fromNonEmpty

0 comments on commit be3bad0

Please sign in to comment.