Skip to content

Commit 4748164

Browse files
committed
Fix events tests, first round
1 parent d4dc2ba commit 4748164

File tree

5 files changed

+175
-152
lines changed

5 files changed

+175
-152
lines changed

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ const tryAssertArgsArraysEqual = (
168168
Assertion,
169169
expectedArgs,
170170
parsedLog.args,
171-
"event",
172-
eventName,
171+
`"${eventName}" event`,
173172
assert,
174173
ssfi
175174
);
@@ -188,8 +187,7 @@ const tryAssertArgsArraysEqual = (
188187
Assertion,
189188
expectedArgs,
190189
parsedLog.args,
191-
"event",
192-
eventName,
190+
`"${eventName}" event`,
193191
assert,
194192
ssfi
195193
);

packages/hardhat-chai-matchers/src/internal/reverted/revertedWithCustomError.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ export async function revertedWithCustomErrorWithArgs(
218218
Assertion,
219219
expectedArgs,
220220
actualArgs,
221-
"custom error",
222-
customError.name,
221+
`"${customError.name}" custom error`,
223222
assert,
224223
ssfi
225224
);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type EthersT from "ethers";
22
import ordinal from "ordinal";
3+
import { AssertionError } from "chai";
34
import { AssertWithSsfi, Ssfi } from "../utils";
45
import { PREVIOUS_MATCHER_NAME } from "./constants";
56
import {
@@ -49,78 +50,94 @@ export function assertArgsArraysEqual(
4950
expectedArgs: any[],
5051
actualArgs: any[],
5152
tag: string,
52-
name: string,
53+
assert: AssertWithSsfi,
54+
ssfi: Ssfi
55+
) {
56+
try {
57+
assertArgsArraysEqualNested(
58+
Assertion,
59+
expectedArgs,
60+
actualArgs,
61+
assert,
62+
ssfi
63+
);
64+
} catch (err: any) {
65+
// throw new AssertionError('Fix this');
66+
err.message = `Error in ${tag}: ${err.message}`;
67+
throw err;
68+
}
69+
}
70+
71+
export function assertArgsArraysEqualNested(
72+
Assertion: Chai.AssertionStatic,
73+
expectedArgs: any[],
74+
actualArgs: any[],
5375
assert: AssertWithSsfi,
5476
ssfi: Ssfi
5577
) {
5678
const ethers = require("ethers") as typeof EthersT;
5779
assert(
5880
actualArgs.length === expectedArgs.length,
59-
`Expected "${name}" ${tag} to have ${expectedArgs.length} argument(s), but it has ${actualArgs.length}`
81+
`Expected arguments array to have length ${expectedArgs.length}, but it has ${actualArgs.length}`
6082
);
6183
for (const [index, expectedArg] of expectedArgs.entries()) {
6284
const actualArg = actualArgs[index];
63-
if (typeof expectedArg === "function") {
64-
const errorPrefix = `The predicate for the ${ordinal(
65-
index + 1
66-
)} ${tag} argument`;
67-
try {
68-
if (expectedArg(actualArg) === true) continue;
69-
} catch (e: any) {
85+
try {
86+
if (typeof expectedArg === "function") {
87+
try {
88+
if (expectedArg(actualArg) === true) continue;
89+
} catch (e: any) {
90+
assert(
91+
false,
92+
`The predicate threw when called: ${e.message}`
93+
// no need for a negated message, since we disallow mixing .not. with
94+
// .withArgs
95+
);
96+
}
7097
assert(
7198
false,
72-
`${errorPrefix} threw when called: ${e.message}`
99+
`The predicate did not return true`
73100
// no need for a negated message, since we disallow mixing .not. with
74101
// .withArgs
75102
);
76-
}
77-
assert(
78-
false,
79-
`${errorPrefix} did not return true `
80-
// no need for a negated message, since we disallow mixing .not. with
81-
// .withArgs
82-
);
83-
} else if (expectedArg instanceof Uint8Array) {
84-
new Assertion(actualArg, undefined, ssfi, true).equal(
85-
ethers.hexlify(expectedArg)
86-
);
87-
} else if (
88-
expectedArg?.length !== undefined &&
89-
typeof expectedArg !== "string"
90-
) {
91-
const expectedLength = expectedArg.length;
92-
const actualLength = actualArg.length;
93-
assert(
94-
expectedLength === actualLength,
95-
`Expected the ${ordinal(
96-
index + 1
97-
)} argument of the "${name}" ${tag} to have ${expectedLength} ${
98-
expectedLength === 1 ? "element" : "elements"
99-
}, but it has ${actualLength}`
100-
);
101-
102-
for (let j = 0; j < expectedArg.length; j++) {
103-
new Assertion(actualArg[j], undefined, ssfi, true).equal(
104-
expectedArg[j]
103+
} else if (expectedArg instanceof Uint8Array) {
104+
new Assertion(actualArg, undefined, ssfi, true).equal(
105+
ethers.hexlify(expectedArg)
105106
);
106-
}
107-
} else {
108-
if (actualArg.hash !== undefined && actualArg._isIndexed === true) {
109-
new Assertion(actualArg.hash, undefined, ssfi, true).to.not.equal(
107+
} else if (
108+
expectedArg?.length !== undefined &&
109+
typeof expectedArg !== "string"
110+
) {
111+
assertArgsArraysEqualNested(
112+
Assertion,
110113
expectedArg,
111-
"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."
112-
);
113-
const expectedArgBytes = ethers.isHexString(expectedArg)
114-
? ethers.getBytes(expectedArg)
115-
: ethers.toUtf8Bytes(expectedArg);
116-
const expectedHash = ethers.keccak256(expectedArgBytes);
117-
new Assertion(actualArg.hash, undefined, ssfi, true).to.equal(
118-
expectedHash,
119-
`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`
114+
actualArg,
115+
assert,
116+
ssfi
120117
);
121118
} else {
122-
new Assertion(actualArg, undefined, ssfi, true).equal(expectedArg);
119+
if (actualArg.hash !== undefined && actualArg._isIndexed === true) {
120+
new Assertion(actualArg.hash, undefined, ssfi, true).to.not.equal(
121+
expectedArg,
122+
"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."
123+
);
124+
const expectedArgBytes = ethers.isHexString(expectedArg)
125+
? ethers.getBytes(expectedArg)
126+
: ethers.toUtf8Bytes(expectedArg);
127+
const expectedHash = ethers.keccak256(expectedArgBytes);
128+
new Assertion(actualArg.hash, undefined, ssfi, true).to.equal(
129+
expectedHash,
130+
`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`
131+
);
132+
} else {
133+
new Assertion(actualArg, undefined, ssfi, true).equal(expectedArg);
134+
}
123135
}
136+
} catch (err: any) {
137+
err.message = `Error in the ${ordinal(index + 1)} argument assertion: ${
138+
err.message
139+
}`;
140+
throw err;
124141
}
125142
}
126143
}

0 commit comments

Comments
 (0)