@@ -4,16 +4,23 @@ import type { AssertWithSsfi, Ssfi } from "../utils";
4
4
5
5
import { AssertionError } from "chai" ;
6
6
import util from "util" ;
7
- import ordinal from "ordinal" ;
8
7
9
8
import { buildAssert } from "../utils" ;
10
9
import { ASSERTION_ABORTED , EMIT_MATCHER } from "./constants" ;
11
10
import { HardhatChaiMatchersAssertionError } from "./errors" ;
12
11
import { assertIsNotNull , preventAsyncMatcherChaining } from "./utils" ;
13
12
14
13
type EventFragment = EthersT . EventFragment ;
15
- type Interface = EthersT . Interface ;
16
14
type Provider = EthersT . Provider ;
15
+ type AssertArgsArraysEqual = (
16
+ context : any ,
17
+ Assertion : Chai . AssertionStatic ,
18
+ chaiUtils : Chai . ChaiUtils ,
19
+ expectedArgs : any [ ] ,
20
+ log : any ,
21
+ assert : AssertWithSsfi ,
22
+ ssfi : Ssfi
23
+ ) => void ;
17
24
18
25
export const EMIT_CALLED = "emitAssertionCalled" ;
19
26
@@ -130,7 +137,8 @@ export async function emitWithArgs(
130
137
Assertion : Chai . AssertionStatic ,
131
138
chaiUtils : Chai . ChaiUtils ,
132
139
expectedArgs : any [ ] ,
133
- ssfi : Ssfi
140
+ ssfi : Ssfi ,
141
+ assertArgsArraysEqual : AssertArgsArraysEqual
134
142
) {
135
143
const negated = false ; // .withArgs cannot be negated
136
144
const assert = buildAssert ( negated , ssfi ) ;
@@ -142,95 +150,9 @@ export async function emitWithArgs(
142
150
expectedArgs ,
143
151
context . logs ,
144
152
assert ,
145
- ssfi
146
- ) ;
147
- }
148
-
149
- function assertArgsArraysEqual (
150
- context : any ,
151
- Assertion : Chai . AssertionStatic ,
152
- chaiUtils : Chai . ChaiUtils ,
153
- expectedArgs : any [ ] ,
154
- log : any ,
155
- assert : AssertWithSsfi ,
156
- ssfi : Ssfi
157
- ) {
158
- const ethers = require ( "ethers" ) as typeof EthersT ;
159
- const parsedLog = (
160
- chaiUtils . flag ( context , "contract" ) . interface as Interface
161
- ) . parseLog ( log ) ;
162
- assertIsNotNull ( parsedLog , "parsedLog" ) ;
163
- const actualArgs = parsedLog . args ;
164
- const eventName = chaiUtils . flag ( context , "eventName" ) ;
165
- assert (
166
- actualArgs . length === expectedArgs . length ,
167
- `Expected "${ eventName } " event to have ${ expectedArgs . length } argument(s), but it has ${ actualArgs . length } `
153
+ ssfi ,
154
+ assertArgsArraysEqual
168
155
) ;
169
- for ( const [ index , expectedArg ] of expectedArgs . entries ( ) ) {
170
- const actualArg = actualArgs [ index ] ;
171
- if ( typeof expectedArg === "function" ) {
172
- const errorPrefix = `The predicate for the ${ ordinal (
173
- index + 1
174
- ) } event argument`;
175
- try {
176
- if ( expectedArg ( actualArg ) === true ) continue ;
177
- } catch ( e : any ) {
178
- assert (
179
- false ,
180
- `${ errorPrefix } threw when called: ${ e . message } `
181
- // no need for a negated message, since we disallow mixing .not. with
182
- // .withArgs
183
- ) ;
184
- }
185
- assert (
186
- false ,
187
- `${ errorPrefix } did not return true `
188
- // no need for a negated message, since we disallow mixing .not. with
189
- // .withArgs
190
- ) ;
191
- } else if ( expectedArg instanceof Uint8Array ) {
192
- new Assertion ( actualArg , undefined , ssfi , true ) . equal (
193
- ethers . hexlify ( expectedArg )
194
- ) ;
195
- } else if (
196
- expectedArg ?. length !== undefined &&
197
- typeof expectedArg !== "string"
198
- ) {
199
- const expectedLength = expectedArg . length ;
200
- const actualLength = actualArg . length ;
201
- assert (
202
- expectedLength === actualLength ,
203
- `Expected the ${ ordinal (
204
- index + 1
205
- ) } argument of the "${ eventName } " event to have ${ expectedLength } ${
206
- expectedLength === 1 ? "element" : "elements"
207
- } , but it has ${ actualLength } `
208
- ) ;
209
-
210
- for ( let j = 0 ; j < expectedArg . length ; j ++ ) {
211
- new Assertion ( actualArg [ j ] , undefined , ssfi , true ) . equal (
212
- expectedArg [ j ]
213
- ) ;
214
- }
215
- } else {
216
- if ( actualArg . hash !== undefined && actualArg . _isIndexed === true ) {
217
- new Assertion ( actualArg . hash , undefined , ssfi , true ) . to . not . equal (
218
- expectedArg ,
219
- "The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion should be the actual event argument (the pre-image of the hash). You provided the hash itself. Please supply the actual event argument (the pre-image of the hash) instead."
220
- ) ;
221
- const expectedArgBytes = ethers . isHexString ( expectedArg )
222
- ? ethers . getBytes ( expectedArg )
223
- : ethers . toUtf8Bytes ( expectedArg ) ;
224
- const expectedHash = ethers . keccak256 ( expectedArgBytes ) ;
225
- new Assertion ( actualArg . hash , undefined , ssfi , true ) . to . equal (
226
- expectedHash ,
227
- `The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion was hashed to produce ${ expectedHash } . The actual hash and the expected hash did not match`
228
- ) ;
229
- } else {
230
- new Assertion ( actualArg , undefined , ssfi , true ) . equal ( expectedArg ) ;
231
- }
232
- }
233
- }
234
156
}
235
157
236
158
const tryAssertArgsArraysEqual = (
@@ -240,7 +162,8 @@ const tryAssertArgsArraysEqual = (
240
162
expectedArgs : any [ ] ,
241
163
logs : any [ ] ,
242
164
assert : AssertWithSsfi ,
243
- ssfi : Ssfi
165
+ ssfi : Ssfi ,
166
+ assertArgsArraysEqual : AssertArgsArraysEqual
244
167
) => {
245
168
if ( logs . length === 1 )
246
169
return assertArgsArraysEqual (
0 commit comments