improve readability of test code

This commit is contained in:
Hongrui Fang 2022-06-20 21:49:39 +08:00
parent fb061de36b
commit 1508bcb5b9
2 changed files with 116 additions and 118 deletions

View file

@ -36,7 +36,6 @@ import Agora.Stake (
StakeRedeemer (PermitVote, WitnessStake), StakeRedeemer (PermitVote, WitnessStake),
) )
import Agora.Stake.Scripts (stakeValidator) import Agora.Stake.Scripts (stakeValidator)
import Control.Monad (join)
import Data.Default.Class (Default (def)) import Data.Default.Class (Default (def))
import Data.Tagged (Tagged (Tagged), untag) import Data.Tagged (Tagged (Tagged), untag)
import PlutusLedgerApi.V1 (ScriptContext (..), ScriptPurpose (..)) import PlutusLedgerApi.V1 (ScriptContext (..), ScriptPurpose (..))
@ -341,112 +340,110 @@ specs =
(Spending Proposal.proposalRef) (Spending Proposal.proposalRef)
) )
] ]
, group , group "unlocking" $ do
"unlocking" proposalCount <- [1, 42]
$ map
( \pc -> let legalGroup = group "legal" $ do
group let voterRetractVotesAndUnlockStakeWhileVoting =
(show pc <> " proposals")
[ group
"legal"
[ group
"retract votes and unlock stake while voting"
[ UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Voter
True
VotingReady
)
True
]
, group
"unlock the stake that has been used to create the proposal"
[ UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Creator
False
Finished
)
True
]
, group "unlock stake after voting" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters UnlockStake.UnlockStakeParameters
pc { UnlockStake.proposalCount = proposalCount
UnlockStake.Voter , UnlockStake.stakeUsage = UnlockStake.Voter
False , UnlockStake.retractVotes = True
ps , UnlockStake.proposalStatus = VotingReady
) }
True True
) creatorUnlockStakeWhileFinished =
[Finished, Locked]
]
, group
"illegal"
[ group "retract votes while the proposal is not voting ready" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters UnlockStake.UnlockStakeParameters
pc { UnlockStake.proposalCount = proposalCount
UnlockStake.Voter , UnlockStake.stakeUsage = UnlockStake.Creator
, UnlockStake.retractVotes = False
, UnlockStake.proposalStatus = Finished
}
True True
ps
) let voterUnlockStakeAfterVoting = group "voter unlocks stake after voting" $ do
False status <- [Finished, Locked]
)
[Draft, Locked, Finished] pure $
, group "irrelevant stake" $
join $
map
( \rv ->
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters UnlockStake.UnlockStakeParameters
pc { UnlockStake.proposalCount = proposalCount
UnlockStake.Irrelevant , UnlockStake.stakeUsage = UnlockStake.Voter
rv , UnlockStake.retractVotes = False
ps , UnlockStake.proposalStatus = status
) }
False
)
[Draft, VotingReady, Locked, Finished]
)
[True, False]
, group "unlock stake that has been used to create the proposal before finished" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Creator
False
ps
)
False
)
[Draft, VotingReady, Locked]
, group "creator stake retract votes" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Creator
True True
ps
) [ voterRetractVotesAndUnlockStakeWhileVoting
, creatorUnlockStakeWhileFinished
, voterUnlockStakeAfterVoting
]
let illegalGroup = group "illegal" $ do
let retractsVotesWhileNotVotingReady =
group "voter retracts votes while not voting" $ do
status <- [Draft, Locked, Finished]
pure $
UnlockStake.mkProposalValidatorTestCase
UnlockStake.UnlockStakeParameters
{ UnlockStake.proposalCount = proposalCount
, UnlockStake.stakeUsage = UnlockStake.Voter
, UnlockStake.retractVotes = True
, UnlockStake.proposalStatus = status
}
False False
)
[Draft, VotingReady, Locked, Finished] unlockIrrelevantStake =
] group "unlock an irrelevant stake" $ do
] status <- [Draft, VotingReady, Locked, Finished]
) shouldRetractVotes <- [True, False]
[1, 25]
pure $
UnlockStake.mkProposalValidatorTestCase
UnlockStake.UnlockStakeParameters
{ UnlockStake.proposalCount = proposalCount
, UnlockStake.stakeUsage = UnlockStake.Irrelevant
, UnlockStake.retractVotes = shouldRetractVotes
, UnlockStake.proposalStatus = status
}
False
unlockCreatorStakeBeforeFinished =
group "unlock creator stake before finished" $ do
status <- [Draft, VotingReady, Locked]
pure $
UnlockStake.mkProposalValidatorTestCase
UnlockStake.UnlockStakeParameters
{ UnlockStake.proposalCount = proposalCount
, UnlockStake.stakeUsage = UnlockStake.Creator
, UnlockStake.retractVotes = False
, UnlockStake.proposalStatus = status
}
False
retractVotesWithCreatorStake =
group "creator stake retracts votes" $ do
status <- [Draft, VotingReady, Locked, Finished]
pure $
UnlockStake.mkProposalValidatorTestCase
UnlockStake.UnlockStakeParameters
{ UnlockStake.proposalCount = proposalCount
, UnlockStake.stakeUsage = UnlockStake.Creator
, UnlockStake.retractVotes = True
, UnlockStake.proposalStatus = status
}
False
[ retractsVotesWhileNotVotingReady
, unlockIrrelevantStake
, unlockCreatorStakeBeforeFinished
, retractVotesWithCreatorStake
]
[legalGroup, illegalGroup]
] ]
] ]

View file

@ -27,7 +27,7 @@ import Data.ByteString.Lazy qualified as ByteString.Lazy
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Data.Bifunctor (second) import Data.Bifunctor (second)
import Data.List (sortBy) import Data.List (sortOn)
import Plutarch.Crypto (pblake2b_256) import Plutarch.Crypto (pblake2b_256)
import PlutusLedgerApi.V1.Interval qualified as PlutusTx import PlutusLedgerApi.V1.Interval qualified as PlutusTx
import PlutusLedgerApi.V1.Scripts (Datum (Datum), DatumHash (DatumHash)) import PlutusLedgerApi.V1.Scripts (Datum (Datum), DatumHash (DatumHash))
@ -93,15 +93,16 @@ updateMap f k =
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
sortMap :: forall k v. Ord k => AssocMap.Map k v -> AssocMap.Map k v sortMap :: forall k v. Ord k => AssocMap.Map k v -> AssocMap.Map k v
sortMap (AssocMap.toList -> l) = sortMap =
AssocMap.fromList $ AssocMap.fromList
sortBy . sortOn fst
( \(k1, _) . AssocMap.toList
(k2, _) -> compare k1 k2
)
l
sortValue :: Value -> Value sortValue :: Value -> Value
sortValue (AssocMap.toList . getValue -> l) = sortValue =
let innerSorted = second sortMap <$> l Value
in Value $ sortMap $ AssocMap.fromList innerSorted . sortMap
. AssocMap.fromList
. fmap (second sortMap)
. AssocMap.toList
. getValue