@ethereum-waffle/chai
Version:
A sweet set of chai matchers for your blockchain testing needs.
25 lines (24 loc) • 1.09 kB
text/typescript
export function supportHexEqual(Assertion: Chai.AssertionStatic) {
Assertion.addMethod('hexEqual', function (this: any, other: string) {
const subject = this._obj;
const isNegated = this.__flags.negate === true;
const isHex = (a: string) => /^0x[0-9a-fA-F]*$/.test(a);
for (const element of [subject, other]) {
if (!isHex(element)) {
this.assert(
isNegated,
`Expected "${subject}" to be a hex string equal to "${other}", but "${element}" is not a valid hex string`,
`Expected "${subject}" not to be a hex string equal to "${other}", but "${element}" is not a valid hex string`
);
}
}
const extractNumeric = (hex: string) => hex.replace(/^0x0*/, '');
this.assert(
extractNumeric(subject.toLowerCase()) === extractNumeric(other.toLowerCase()),
`Expected "${subject}" to be a hex string equal equal to "${other}"`,
`Expected "${subject}" not to a hex string equal be equal to "${other}", but it was`,
`Hex string representing the same number as ${other}`,
subject
);
});
}