Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept Typed object in matcher #4715

Merged
merged 18 commits into from
Jan 31, 2024
9 changes: 7 additions & 2 deletions packages/hardhat-chai-matchers/src/internal/addressable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type EthersT from "ethers";

import { tryDereference } from "./typed";

export function supportAddressable(
Assertion: Chai.AssertionStatic,
chaiUtils: Chai.ChaiUtils
Expand Down Expand Up @@ -27,10 +29,13 @@ function override(
// otherwise undefined is returned.
function tryGetAddressSync(value: any): string | undefined {
const { isAddress, isAddressable } = require("ethers") as typeof EthersT;

value = tryDereference(value, "address");
if (isAddressable(value)) {
value = (value as any).address ?? (value as any).target;
}
if (isAddress(value)) {
return value;
} else if (isAddressable(value)) {
return tryGetAddressSync((value as any).address ?? (value as any).target);
} else {
return undefined;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/hardhat-chai-matchers/src/internal/typed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function tryDereference(value: any, type: string) {
const { Typed } = require("ethers") as typeof import("ethers");
try {
return Typed.dereference(value, type);
} catch {
return undefined;
}
}
26 changes: 25 additions & 1 deletion packages/hardhat-chai-matchers/test/addressable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,49 @@ describe("Addressable matcher", () => {
const signer = ethers.Wallet.createRandom();
const address = signer.address;
const contract = new ethers.Contract(address, []);
const typedAddress = ethers.Typed.address(address);
const typedSigner = ethers.Typed.address(signer);
const typedContract = ethers.Typed.address(contract);

const otherSigner = ethers.Wallet.createRandom();
const otherAddress = otherSigner.address;
const otherContract = new ethers.Contract(otherAddress, []);
const otherTypedAddress = ethers.Typed.address(otherAddress);
const otherTypedSigner = ethers.Typed.address(otherSigner);
const otherTypedContract = ethers.Typed.address(otherContract);

const elements = [
{ name: "address", object: address, class: address },
{ name: "signer", object: signer, class: address },
{ name: "contract", object: contract, class: address },
{ name: "typed address", object: typedAddress, class: address },
{ name: "typed signer", object: typedSigner, class: address },
{ name: "typed contract", object: typedContract, class: address },
{ name: "other address", object: otherAddress, class: otherAddress },
{ name: "other signer", object: otherSigner, class: otherAddress },
{ name: "other contract", object: otherContract, class: otherAddress },
{
name: "other typed address",
object: otherTypedAddress,
class: otherAddress,
},
{
name: "other typed signer",
object: otherTypedSigner,
class: otherAddress,
},
{
name: "other typed contract",
object: otherTypedContract,
class: otherAddress,
},
];

for (const el1 of elements)
for (const el2 of elements) {
const expectEqual = el1.class === el2.class;

describe(`expect "${el1.name}" to equal "${el1.name}"`, () => {
describe(`expect "${el1.name}" to equal "${el2.name}"`, () => {
if (expectEqual) {
it("should not revert", () => {
expect(el1.object).to.equal(el2.object);
Expand Down
Loading