Skip to content

Commit

Permalink
Use CoArbitrary Address instance to strengthen filterByAddress te…
Browse files Browse the repository at this point in the history
…sts.

Instead of using constant functions, we use arbitrary functions of type
`Address -> Bool`.
  • Loading branch information
jonathanknowles committed Sep 3, 2021
1 parent 5f9ff22 commit 9eb9b66
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions lib/core/test/unit/Cardano/Wallet/Primitive/ModelSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Address.Gen
( Parity (..), addressParity )
( Parity (..), addressParity, coarbitraryAddress )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
Expand Down Expand Up @@ -120,6 +120,7 @@ import Test.Hspec.Extra
( parallel )
import Test.QuickCheck
( Arbitrary (..)
, CoArbitrary (..)
, Gen
, Positive (..)
, Property
Expand Down Expand Up @@ -1427,18 +1428,13 @@ prop_applyTxToUTxO_entries tx u =
`Map.difference`
unUTxO (u `UTxO.restrictedBy` Set.fromList (inputs tx))

prop_filterByAddress_balance_applyTxToUTxO :: Bool -> Tx -> Property
prop_filterByAddress_balance_applyTxToUTxO b tx =
let
f = const b
in
checkCoverage $
cover 30 (b) "True function" $
cover 30 (not b) "False function" $
balance (filterByAddress f (applyTxToUTxO tx mempty))
=== foldMap (\o -> do
if f (address o) then tokens o else mempty
) (outputs tx)
prop_filterByAddress_balance_applyTxToUTxO
:: (Address -> Bool) -> Tx -> Property
prop_filterByAddress_balance_applyTxToUTxO f tx =
balance (filterByAddress f (applyTxToUTxO tx mempty))
=== foldMap
(\o -> if f (address o) then tokens o else mempty)
(outputs tx)

prop_utxoFromTx_balance :: Tx -> Property
prop_utxoFromTx_balance tx =
Expand Down Expand Up @@ -1488,12 +1484,12 @@ prop_applyTxToUTxO_spendTx_utxoFromTx :: Tx -> UTxO -> Property
prop_applyTxToUTxO_spendTx_utxoFromTx tx u=
applyTxToUTxO tx u === spendTx tx u <> utxoFromTx tx

prop_spendTx_filterByAddress :: Bool -> Tx -> UTxO -> Property
prop_spendTx_filterByAddress b tx u =
let
f = const b
in
checkCoverage $
cover 30 (b) "True function" $
cover 30 (not b) "False function" $
filterByAddress f (spendTx tx u) === spendTx tx (filterByAddress f u)
prop_spendTx_filterByAddress :: (Address -> Bool) -> Tx -> UTxO -> Property
prop_spendTx_filterByAddress f tx u =
filterByAddress f (spendTx tx u) === spendTx tx (filterByAddress f u)

instance CoArbitrary Address where
coarbitrary = coarbitraryAddress

instance Show (Address -> Bool) where
show = const "(Address -> Bool)"

0 comments on commit 9eb9b66

Please sign in to comment.