-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathAssert.purs
192 lines (158 loc) · 7.36 KB
/
Assert.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
-- | Testing assertions interface provided by `Contract.Test.Assert`
module Test.Ctl.Testnet.Contract.Assert (suite) where
import Prelude
import Cardano.Types (ExUnits(ExUnits))
import Cardano.Types.PlutusScript as PlutusScript
import Contract.Monad (liftedM)
import Contract.Numeric.BigNum as BigNum
import Contract.PlutusData (PlutusData(Integer))
import Contract.Test (ContractTest, InitialUTxOs, withWallets)
import Contract.Test.Assert
( checkExUnitsNotExceed
, collectAssertionFailures
, printContractAssertionFailures
)
import Contract.Test.Mote (TestPlanM)
import Contract.Wallet
( ownPaymentPubKeyHashes
, ownStakePubKeyHashes
, withKeyWallet
)
import Control.Monad.Trans.Class (lift)
import Ctl.Examples.ContractTestUtils as ContractTestUtils
import Ctl.Examples.Helpers (mkAssetName)
import Ctl.Examples.PlutusV2.Scripts.AlwaysMints (alwaysMintsPolicyScriptV2)
import Data.Array (head)
import Data.Either (isLeft, isRight)
import Data.Newtype (wrap)
import Data.Tuple.Nested (type (/\), (/\))
import Effect.Class (liftEffect)
import Effect.Exception (throw)
import JS.BigInt as BigInt
import Mote (group, test)
import Partial.Unsafe (unsafePartial)
import Test.Spec.Assertions (shouldEqual, shouldSatisfy)
suite :: TestPlanM ContractTest Unit
suite = do
group "Assertions interface" do
let
initialUtxos :: InitialUTxOs
initialUtxos =
[ BigNum.fromInt 2_000_000_000, BigNum.fromInt 2_000_000_000 ]
distribution :: InitialUTxOs /\ InitialUTxOs
distribution = initialUtxos /\ initialUtxos
test "Successful run" do
withWallets distribution \(alice /\ bob) -> do
receiverPkh <- liftedM "Unable to get Bob's PKH" $
head <$> withKeyWallet bob ownPaymentPubKeyHashes
receiverSkh <- join <<< head <$> withKeyWallet bob ownStakePubKeyHashes
mintingPolicy <- alwaysMintsPolicyScriptV2
let cs = PlutusScript.hash mintingPolicy
tn <- mkAssetName "TheToken"
withKeyWallet alice do
let
params =
{ receiverPkh
, receiverSkh
, adaToSend: wrap $ BigNum.fromInt 5_000_000
, mintingPolicy
, tokensToMint: cs /\ tn /\ BigNum.one /\ unit
, datumToAttach: Integer $ BigInt.fromInt 42
}
checks <- ContractTestUtils.mkChecks params
eiResult /\ failures <- collectAssertionFailures checks $ lift do
ContractTestUtils.mkContract params
eiResult `shouldSatisfy` isRight
failures `shouldEqual` []
test "Incorrect token value" do
withWallets distribution \(alice /\ bob) -> do
receiverPkh <- liftedM "Unable to get Bob's PKH" $
head <$> withKeyWallet bob ownPaymentPubKeyHashes
receiverSkh <- join <<< head <$> withKeyWallet bob ownStakePubKeyHashes
mintingPolicy <- alwaysMintsPolicyScriptV2
let cs = PlutusScript.hash mintingPolicy
tn <- mkAssetName "TheToken"
withKeyWallet alice do
let
params =
{ receiverPkh
, receiverSkh
, adaToSend: wrap $ BigNum.fromInt 5_000_000
, mintingPolicy
, tokensToMint: cs /\ tn /\ BigNum.one /\ unit
, datumToAttach: Integer $ BigInt.fromInt 42
}
checks <- ContractTestUtils.mkChecks params
{ tokensToMint = cs /\ tn
/\ (unsafePartial $ BigNum.one <> BigNum.one)
/\ unit
}
eiResult /\ failures <- collectAssertionFailures checks $ lift do
ContractTestUtils.mkContract params
eiResult `shouldSatisfy` isRight
printContractAssertionFailures failures `shouldEqual`
"In addition to the error above, the following `Contract` assertions have failed:\n\n 1. Unexpected token delta (mkAssetName (hexToByteArrayUnsafe \"546865546f6b656e\")) at address Sender Expected: 2, Actual: 1"
test "ExUnits limit reached" do
withWallets distribution \(alice /\ bob) -> do
receiverPkh <- liftedM "Unable to get Bob's PKH" $
head <$> withKeyWallet bob ownPaymentPubKeyHashes
receiverSkh <- join <<< head <$> withKeyWallet bob ownStakePubKeyHashes
mintingPolicy <- alwaysMintsPolicyScriptV2
let cs = PlutusScript.hash mintingPolicy
tn <- mkAssetName "TheToken"
withKeyWallet alice do
let
params =
{ receiverPkh
, receiverSkh
, adaToSend: wrap $ BigNum.fromInt 5_000_000
, mintingPolicy
, tokensToMint: cs /\ tn /\ BigNum.one /\ unit
, datumToAttach: Integer $ BigInt.fromInt 42
}
checks <- ContractTestUtils.mkChecks params <#>
( _ <>
[ checkExUnitsNotExceed
$ ExUnits
{ mem: BigNum.fromInt 800, steps: BigNum.fromInt 16110 }
]
)
eiResult /\ failures <- collectAssertionFailures checks $ lift do
ContractTestUtils.mkContract params
eiResult `shouldSatisfy` isRight
printContractAssertionFailures failures `shouldEqual`
"In addition to the error above, the following `Contract` assertions have failed:\n\n 1. ExUnits limit exceeded: Expected: { mem: 800, steps: 16110 }, Actual: { mem: 800, steps: 161100 }"
test "An exception is thrown - everything is reported" do
withWallets distribution \(alice /\ bob) -> do
receiverPkh <- liftedM "Unable to get Bob's PKH" $
head <$> withKeyWallet bob ownPaymentPubKeyHashes
receiverSkh <- join <<< head <$> withKeyWallet bob ownStakePubKeyHashes
mintingPolicy <- alwaysMintsPolicyScriptV2
let cs = PlutusScript.hash mintingPolicy
tn <- mkAssetName "TheToken"
withKeyWallet alice do
let
params =
{ receiverPkh
, receiverSkh
, adaToSend: wrap $ BigNum.fromInt 5_000_000
, mintingPolicy
, tokensToMint: cs /\ tn /\ BigNum.one /\ unit
, datumToAttach: Integer $ BigInt.fromInt 42
}
checks <-
ContractTestUtils.mkChecks params
{ tokensToMint = cs /\ tn
/\ (unsafePartial $ BigNum.one <> BigNum.one)
/\ unit
} <#>
( _ <>
[ checkExUnitsNotExceed $ ExUnits
{ mem: BigNum.fromInt 800, steps: BigNum.fromInt 16110 }
]
)
eiResult /\ failures <- collectAssertionFailures checks $ lift do
ContractTestUtils.mkContract params <* liftEffect (throw ":(")
eiResult `shouldSatisfy` isLeft
printContractAssertionFailures failures `shouldEqual`
"In addition to the error above, the following `Contract` assertions have failed:\n\n 1. Error while trying to get expected value: Unable to estimate expected loss in wallet\n\n 2. Unexpected token delta (mkAssetName (hexToByteArrayUnsafe \"546865546f6b656e\")) at address Sender Expected: 2, Actual: 1 \n\n 3. ExUnits limit exceeded: Expected: { mem: 800, steps: 16110 }, Actual: { mem: 800, steps: 161100 } \n\nThe following `Contract` checks have been skipped due to an exception: \n\n 1. Sender's output has a datum\n\n 2. Output has a reference script"