Skip to content

Commit ab665a8

Browse files
committed
Implement review suggestions
1 parent 085601c commit ab665a8

File tree

3 files changed

+87
-73
lines changed

3 files changed

+87
-73
lines changed

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

+75-61
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function assertArgsArraysEqual(
5555
ssfi: Ssfi
5656
) {
5757
try {
58-
assertArgsArraysEqualNested(
58+
innerAssertArgsArraysEqual(
5959
Assertion,
6060
expectedArgs,
6161
actualArgs,
@@ -69,79 +69,28 @@ export function assertArgsArraysEqual(
6969
}
7070
}
7171

72-
export function assertArgsArraysEqualNested(
72+
export function innerAssertArgsArraysEqual(
7373
Assertion: Chai.AssertionStatic,
7474
expectedArgs: any[],
7575
actualArgs: any[],
7676
assertionType: "event" | "error",
7777
assert: AssertWithSsfi,
7878
ssfi: Ssfi
7979
) {
80-
const ethers = require("ethers") as typeof EthersT;
8180
assert(
8281
actualArgs.length === expectedArgs.length,
8382
`Expected arguments array to have length ${expectedArgs.length}, but it has ${actualArgs.length}`
8483
);
8584
for (const [index, expectedArg] of expectedArgs.entries()) {
86-
const actualArg = actualArgs[index];
8785
try {
88-
if (typeof expectedArg === "function") {
89-
try {
90-
if (expectedArg(actualArg) === true) continue;
91-
} catch (e: any) {
92-
assert(
93-
false,
94-
`The predicate threw when called: ${e.message}`
95-
// no need for a negated message, since we disallow mixing .not. with
96-
// .withArgs
97-
);
98-
}
99-
assert(
100-
false,
101-
`The predicate did not return true`
102-
// no need for a negated message, since we disallow mixing .not. with
103-
// .withArgs
104-
);
105-
} else if (expectedArg instanceof Uint8Array) {
106-
new Assertion(actualArg, undefined, ssfi, true).equal(
107-
ethers.hexlify(expectedArg)
108-
);
109-
} else if (
110-
expectedArg?.length !== undefined &&
111-
typeof expectedArg !== "string"
112-
) {
113-
assertArgsArraysEqualNested(
114-
Assertion,
115-
expectedArg,
116-
actualArg,
117-
assertionType,
118-
assert,
119-
ssfi
120-
);
121-
} else {
122-
if (actualArg.hash !== undefined && actualArg._isIndexed === true) {
123-
if (assertionType !== "event")
124-
assert(
125-
false,
126-
"Should not get here. Please open an issue about that"
127-
);
128-
129-
new Assertion(actualArg.hash, undefined, ssfi, true).to.not.equal(
130-
expectedArg,
131-
"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."
132-
);
133-
const expectedArgBytes = ethers.isHexString(expectedArg)
134-
? ethers.getBytes(expectedArg)
135-
: ethers.toUtf8Bytes(expectedArg);
136-
const expectedHash = ethers.keccak256(expectedArgBytes);
137-
new Assertion(actualArg.hash, undefined, ssfi, true).to.equal(
138-
expectedHash,
139-
`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 ${actualArg.hash} did not match`
140-
);
141-
} else {
142-
new Assertion(actualArg, undefined, ssfi, true).equal(expectedArg);
143-
}
144-
}
86+
innerAssertArgEqual(
87+
Assertion,
88+
expectedArg,
89+
actualArgs[index],
90+
assertionType,
91+
assert,
92+
ssfi
93+
);
14594
} catch (err: any) {
14695
const ordinal = require("ordinal") as typeof OrdinalT;
14796
err.message = `Error in the ${ordinal(index + 1)} argument assertion: ${
@@ -151,3 +100,68 @@ export function assertArgsArraysEqualNested(
151100
}
152101
}
153102
}
103+
104+
function innerAssertArgEqual(
105+
Assertion: Chai.AssertionStatic,
106+
expectedArg: any,
107+
actualArg: any,
108+
assertionType: "event" | "error",
109+
assert: AssertWithSsfi,
110+
ssfi: Ssfi
111+
) {
112+
const ethers = require("ethers") as typeof EthersT;
113+
if (typeof expectedArg === "function") {
114+
try {
115+
if (expectedArg(actualArg) === true) return;
116+
} catch (e: any) {
117+
assert(
118+
false,
119+
`The predicate threw when called: ${e.message}`
120+
// no need for a negated message, since we disallow mixing .not. with
121+
// .withArgs
122+
);
123+
}
124+
assert(
125+
false,
126+
`The predicate did not return true`
127+
// no need for a negated message, since we disallow mixing .not. with
128+
// .withArgs
129+
);
130+
} else if (expectedArg instanceof Uint8Array) {
131+
new Assertion(actualArg, undefined, ssfi, true).equal(
132+
ethers.hexlify(expectedArg)
133+
);
134+
} else if (
135+
expectedArg?.length !== undefined &&
136+
typeof expectedArg !== "string"
137+
) {
138+
innerAssertArgsArraysEqual(
139+
Assertion,
140+
expectedArg,
141+
actualArg,
142+
assertionType,
143+
assert,
144+
ssfi
145+
);
146+
} else {
147+
if (actualArg.hash !== undefined && actualArg._isIndexed === true) {
148+
if (assertionType !== "event")
149+
assert(false, "Should not get here. Please open an issue about that");
150+
151+
new Assertion(actualArg.hash, undefined, ssfi, true).to.not.equal(
152+
expectedArg,
153+
"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."
154+
);
155+
const expectedArgBytes = ethers.isHexString(expectedArg)
156+
? ethers.getBytes(expectedArg)
157+
: ethers.toUtf8Bytes(expectedArg);
158+
const expectedHash = ethers.keccak256(expectedArgBytes);
159+
new Assertion(actualArg.hash, undefined, ssfi, true).to.equal(
160+
expectedHash,
161+
`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 ${actualArg.hash} did not match`
162+
);
163+
} else {
164+
new Assertion(actualArg, undefined, ssfi, true).equal(expectedArg);
165+
}
166+
}
167+
}

packages/hardhat-chai-matchers/test/events.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe(".to.emit (contract events)", () => {
133133
.withArgs(2)
134134
).to.be.eventually.rejectedWith(
135135
AssertionError,
136-
'Error in "WithUintArg" event: Error in the 1st argument assertion: expected 1 to equal 2. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
136+
'Error in "WithUintArg" event: Error in the 1st argument assertion: expected 1 to equal 2.'
137137
);
138138
});
139139

@@ -391,7 +391,7 @@ describe(".to.emit (contract events)", () => {
391391
.withArgs([3, 4])
392392
).to.be.eventually.rejectedWith(
393393
AssertionError,
394-
`Error in "WithUintArray" event: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3. The numerical values of the given "bigint" and "number" inputs were compared, and they differed`
394+
`Error in "WithUintArray" event: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3.`
395395
);
396396
});
397397

@@ -495,7 +495,7 @@ describe(".to.emit (contract events)", () => {
495495
.withArgs([3, 4])
496496
).to.be.eventually.rejectedWith(
497497
AssertionError,
498-
'Error in "WithStructArg" event: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
498+
'Error in "WithStructArg" event: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3.'
499499
);
500500
});
501501
});
@@ -514,7 +514,7 @@ describe(".to.emit (contract events)", () => {
514514
.withArgs(2, 2)
515515
).to.be.eventually.rejectedWith(
516516
AssertionError,
517-
'Error in "WithTwoUintArgs" event: Error in the 1st argument assertion: expected 1 to equal 2. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
517+
'Error in "WithTwoUintArgs" event: Error in the 1st argument assertion: expected 1 to equal 2'
518518
);
519519
});
520520

@@ -525,7 +525,7 @@ describe(".to.emit (contract events)", () => {
525525
.withArgs(1, 1)
526526
).to.be.eventually.rejectedWith(
527527
AssertionError,
528-
'Error in "WithTwoUintArgs" event: Error in the 2nd argument assertion: expected 2 to equal 1. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
528+
'Error in "WithTwoUintArgs" event: Error in the 2nd argument assertion: expected 2 to equal 1.'
529529
);
530530
});
531531

@@ -684,7 +684,7 @@ describe(".to.emit (contract events)", () => {
684684
.and.to.emit(contract, "WithStringArg")
685685
).to.be.eventually.rejectedWith(
686686
AssertionError,
687-
'Error in "WithUintArg" event: Error in the 1st argument assertion: expected 1 to equal 2. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
687+
'Error in "WithUintArg" event: Error in the 1st argument assertion: expected 1 to equal 2.'
688688
);
689689
});
690690

packages/hardhat-chai-matchers/test/reverted/revertedWithCustomError.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { anyUint, anyValue } from "../../src/withArgs";
1616
import { MatchersContract } from "../contracts";
1717

1818
describe("INTEGRATION: Reverted with custom error", function () {
19-
describe.skip("with the in-process hardhat network", function () {
19+
describe("with the in-process hardhat network", function () {
2020
useEnvironment("hardhat-project");
2121

2222
runTests();
@@ -237,7 +237,7 @@ describe("INTEGRATION: Reverted with custom error", function () {
237237
.withArgs(2)
238238
).to.be.rejectedWith(
239239
AssertionError,
240-
'Error in "CustomErrorWithUint" custom error: Error in the 1st argument assertion: expected 1 to equal 2. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
240+
'Error in "CustomErrorWithUint" custom error: Error in the 1st argument assertion: expected 1 to equal 2.'
241241
);
242242
});
243243
});
@@ -264,7 +264,7 @@ describe("INTEGRATION: Reverted with custom error", function () {
264264
.withArgs(2, "foo")
265265
).to.be.rejectedWith(
266266
AssertionError,
267-
'Error in "CustomErrorWithUintAndString" custom error: Error in the 1st argument assertion: expected 1 to equal 2. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
267+
'Error in "CustomErrorWithUintAndString" custom error: Error in the 1st argument assertion: expected 1 to equal 2.'
268268
);
269269
});
270270

@@ -282,7 +282,7 @@ describe("INTEGRATION: Reverted with custom error", function () {
282282
);
283283
});
284284

285-
it("Should if first prefdicate fails", async function () {
285+
it("Should fail if first predicate throws", async function () {
286286
await expect(
287287
expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
288288
.to.be.revertedWithCustomError(
@@ -300,7 +300,7 @@ describe("INTEGRATION: Reverted with custom error", function () {
300300
});
301301

302302
describe("different number of arguments", function () {
303-
it("Should reject if expected less arguments", async function () {
303+
it("Should reject if expected fewer arguments", async function () {
304304
await expect(
305305
expect(matchers.revertWithCustomErrorWithUintAndString(1, "s"))
306306
.to.be.revertedWithCustomError(
@@ -343,7 +343,7 @@ describe("INTEGRATION: Reverted with custom error", function () {
343343
.withArgs([3, 2])
344344
).to.be.rejectedWith(
345345
AssertionError,
346-
'Error in "CustomErrorWithPair" custom error: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
346+
'Error in "CustomErrorWithPair" custom error: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3.'
347347
);
348348
});
349349
});

0 commit comments

Comments
 (0)