Skip to content

Commit ec9f324

Browse files
committed
refactor random generation of types
1 parent 9f28f13 commit ec9f324

File tree

3 files changed

+64
-76
lines changed

3 files changed

+64
-76
lines changed

bot-plutus-interface.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ test-suite bot-plutus-interface-test
186186
Spec.BotPlutusInterface.TxStatusChange
187187
Spec.BotPlutusInterface.UtxoParser
188188
Spec.MockContract
189-
Spec.NatLedger
189+
Spec.RandomLedger
190190

191191
build-depends:
192192
, aeson ^>=1.5.0.0

test/Spec/NatLedger.hs

Lines changed: 0 additions & 75 deletions
This file was deleted.

test/Spec/RandomLedger.hs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{-# OPTIONS_GHC -Wno-orphans #-}
2+
3+
module Spec.RandomLedger (
4+
allAssetClasses,
5+
randomValue,
6+
randomTxOut,
7+
randomTxOuts,
8+
randomTxOutRef,
9+
) where
10+
11+
import Plutus.PAB.Arbitrary ()
12+
13+
import Control.Lens (folded, (%~), (&), (^..))
14+
import Control.Monad (replicateM)
15+
import Data.List.Extra (mconcatMap)
16+
import Data.Set (Set)
17+
import Data.Set qualified as Set
18+
import GHC.Natural (Natural)
19+
import Ledger.Tx (TxOut (..), TxOutRef (..))
20+
import Ledger.Value (AssetClass (AssetClass), Value)
21+
import Ledger.Value qualified as Value
22+
import Test.QuickCheck (Arbitrary (arbitrary), Gen, Property, elements, forAll, listOf, property, resize)
23+
import Prelude
24+
25+
deriving newtype instance Arbitrary AssetClass
26+
27+
allAssetClasses :: Int -> Gen (Set AssetClass)
28+
allAssetClasses n = Set.fromList <$> resize n (listOf arbitrary)
29+
30+
randomValue :: Int -> Set AssetClass -> Gen Value
31+
randomValue samplesize assetclasses =
32+
do
33+
selectedAc <-
34+
Set.fromList
35+
<$> replicateM
36+
samplesize
37+
(assetclasses ^.. folded & id %~ elements)
38+
39+
amounts <- replicateM (length selectedAc) (toInteger <$> arbitrary @Natural)
40+
41+
return $
42+
mconcatMap (uncurry Value.assetClassValue) $
43+
zip (Set.toList selectedAc) amounts
44+
45+
randomTxOut :: Int -> Set AssetClass -> Gen TxOut
46+
randomTxOut samplesize assetclasses =
47+
do
48+
addr <- arbitrary
49+
value <- randomValue samplesize assetclasses
50+
datumhash <- arbitrary
51+
52+
return (TxOut addr value datumhash)
53+
54+
randomTxOuts :: Int -> Int -> Set AssetClass -> Gen [TxOut]
55+
randomTxOuts numTxOuts samplesize =
56+
replicateM numTxOuts . randomTxOut samplesize
57+
58+
randomTxOutRef :: Gen TxOutRef
59+
randomTxOutRef =
60+
do
61+
txId <- arbitrary
62+
txIdx <- toInteger <$> arbitrary @Natural
63+
return (TxOutRef txId txIdx)

0 commit comments

Comments
 (0)