improve readability of test code

This commit is contained in:
Hongrui Fang 2022-06-20 21:49:39 +08:00
parent 3b15fedc26
commit 2d3f8f0463
No known key found for this signature in database
GPG key ID: 1C4711FFF64C0254
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") UnlockStake.mkProposalValidatorTestCase
[ group UnlockStake.UnlockStakeParameters
"legal" { UnlockStake.proposalCount = proposalCount
[ group , UnlockStake.stakeUsage = UnlockStake.Voter
"retract votes and unlock stake while voting" , UnlockStake.retractVotes = True
[ UnlockStake.mkProposalValidatorTestCase , UnlockStake.proposalStatus = VotingReady
( UnlockStake.UnlockStakeParameters }
pc True
UnlockStake.Voter creatorUnlockStakeWhileFinished =
True UnlockStake.mkProposalValidatorTestCase
VotingReady UnlockStake.UnlockStakeParameters
) { UnlockStake.proposalCount = proposalCount
True , UnlockStake.stakeUsage = UnlockStake.Creator
] , UnlockStake.retractVotes = False
, group , UnlockStake.proposalStatus = Finished
"unlock the stake that has been used to create the proposal" }
[ UnlockStake.mkProposalValidatorTestCase True
( UnlockStake.UnlockStakeParameters
pc let voterUnlockStakeAfterVoting = group "voter unlocks stake after voting" $ do
UnlockStake.Creator status <- [Finished, Locked]
False
Finished pure $
) UnlockStake.mkProposalValidatorTestCase
True UnlockStake.UnlockStakeParameters
] { UnlockStake.proposalCount = proposalCount
, group "unlock stake after voting" $ , UnlockStake.stakeUsage = UnlockStake.Voter
map , UnlockStake.retractVotes = False
( \ps -> , UnlockStake.proposalStatus = status
UnlockStake.mkProposalValidatorTestCase }
( UnlockStake.UnlockStakeParameters True
pc
UnlockStake.Voter [ voterRetractVotesAndUnlockStakeWhileVoting
False , creatorUnlockStakeWhileFinished
ps , voterUnlockStakeAfterVoting
)
True
)
[Finished, Locked]
]
, group
"illegal"
[ group "retract votes while the proposal is not voting ready" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Voter
True
ps
)
False
)
[Draft, Locked, Finished]
, group "irrelevant stake" $
join $
map
( \rv ->
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Irrelevant
rv
ps
)
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
ps
)
False
)
[Draft, VotingReady, Locked, Finished]
]
] ]
)
[1, 25] 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
unlockIrrelevantStake =
group "unlock an irrelevant stake" $ do
status <- [Draft, VotingReady, Locked, Finished]
shouldRetractVotes <- [True, False]
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