Skip to content

Commit cdab64a

Browse files
committed
refactor args
1 parent 6a86c20 commit cdab64a

File tree

1 file changed

+27
-25
lines changed
  • packages/hardhat-chai-matchers/src/internal

1 file changed

+27
-25
lines changed

packages/hardhat-chai-matchers/src/internal/emit.ts

+27-25
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,15 @@ function assertArgsArraysEqual(
166166
actualArgs.length === expectedArgs.length,
167167
`Expected "${eventName}" event to have ${expectedArgs.length} argument(s), but it has ${actualArgs.length}`
168168
);
169-
for (let index = 0; index < expectedArgs.length; index++) {
170-
if (typeof expectedArgs[index] === "function") {
169+
for (const index in expectedArgs) {
170+
const expectedArg = expectedArgs[index];
171+
const actualArg = actualArgs[index];
172+
if (typeof expectedArg === "function") {
171173
const errorPrefix = `The predicate for the ${ordinal(
172-
index + 1
174+
parseInt(index) + 1
173175
)} event argument`;
174176
try {
175-
if (expectedArgs[index](actualArgs[index]) === true) continue;
177+
if (expectedArg(actualArg) === true) continue;
176178
} catch (e: any) {
177179
assert(
178180
false,
@@ -187,55 +189,55 @@ function assertArgsArraysEqual(
187189
// no need for a negated message, since we disallow mixing .not. with
188190
// .withArgs
189191
);
190-
} else if (expectedArgs[index] instanceof Uint8Array) {
191-
new Assertion(actualArgs[index], undefined, ssfi, true).equal(
192-
ethers.hexlify(expectedArgs[index])
192+
} else if (expectedArg instanceof Uint8Array) {
193+
new Assertion(actualArg, undefined, ssfi, true).equal(
194+
ethers.hexlify(expectedArg)
193195
);
194196
} else if (
195-
expectedArgs[index]?.length !== undefined &&
196-
typeof expectedArgs[index] !== "string"
197+
expectedArg?.length !== undefined &&
198+
typeof expectedArg !== "string"
197199
) {
198-
const expectedLength = expectedArgs[index].length;
199-
const actualLength = actualArgs[index].length;
200+
const expectedLength = expectedArg.length;
201+
const actualLength = actualArg.length;
200202
assert(
201203
expectedLength === actualLength,
202204
`Expected the ${ordinal(
203-
index + 1
205+
parseInt(index) + 1
204206
)} argument of the "${eventName}" event to have ${expectedLength} ${
205207
expectedLength === 1 ? "element" : "elements"
206208
}, but it has ${actualLength}`
207209
);
208210

209-
for (let j = 0; j < expectedArgs[index].length; j++) {
210-
new Assertion(actualArgs[index][j], undefined, ssfi, true).equal(
211-
expectedArgs[index][j]
211+
for (let j = 0; j < expectedArg.length; j++) {
212+
new Assertion(actualArg[j], undefined, ssfi, true).equal(
213+
expectedArg[j]
212214
);
213215
}
214216
} else {
215217
if (
216-
actualArgs[index].hash !== undefined &&
217-
actualArgs[index]._isIndexed === true
218+
actualArg.hash !== undefined &&
219+
actualArg._isIndexed === true
218220
) {
219221
new Assertion(
220-
actualArgs[index].hash,
222+
actualArg.hash,
221223
undefined,
222224
ssfi,
223225
true
224226
).to.not.equal(
225-
expectedArgs[index],
227+
expectedArg,
226228
"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."
227229
);
228-
const expectedArgBytes = ethers.isHexString(expectedArgs[index])
229-
? ethers.getBytes(expectedArgs[index])
230-
: ethers.toUtf8Bytes(expectedArgs[index]);
230+
const expectedArgBytes = ethers.isHexString(expectedArg)
231+
? ethers.getBytes(expectedArg)
232+
: ethers.toUtf8Bytes(expectedArg);
231233
const expectedHash = ethers.keccak256(expectedArgBytes);
232-
new Assertion(actualArgs[index].hash, undefined, ssfi, true).to.equal(
234+
new Assertion(actualArg.hash, undefined, ssfi, true).to.equal(
233235
expectedHash,
234236
`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`
235237
);
236238
} else {
237-
new Assertion(actualArgs[index], undefined, ssfi, true).equal(
238-
expectedArgs[index]
239+
new Assertion(actualArg, undefined, ssfi, true).equal(
240+
expectedArg
239241
);
240242
}
241243
}

0 commit comments

Comments
 (0)