Skip to content

Commit 64707ae

Browse files
authoredOct 31, 2022
🧅 Allow special characters in revertedWith regex (#784)
1 parent e1c352a commit 64707ae

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed
 
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ethereum-waffle/chai": patch
3+
---
4+
5+
Allow special characters in revertedWith regex

‎waffle-chai/src/matchers/revertedWith.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ const decodeHardhatError = (error: any, context: any) => {
6161
}
6262
const errorString = String(error);
6363
{
64-
const regexp = /VM Exception while processing transaction: reverted with custom error '([a-zA-Z0-9]+)\((.*)\)'/g;
64+
// eslint-disable-next-line max-len
65+
const regexp = /VM Exception while processing transaction: reverted with custom error '([a-zA-Z0-9$_]+)\((.*)\)'/g;
6566
const matches = regexp.exec(errorString);
6667
if (matches && matches.length >= 1) {
6768
// needs to be wrapped in list to be consistent with the emit matcher

‎waffle-hardhat/contracts/CustomError.sol

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pragma solidity ^0.8.0;
22

33
error One(uint value, string msg, bytes32 encoded);
44
error Two(uint256[3] value, bytes32[2] encoded);
5+
error $__DecoratedCustomErrorName(uint value, string msg, bytes32 encoded);
56

67
contract Matchers {
78
function doRevertWithOne() public pure {
@@ -21,4 +22,8 @@ contract Matchers {
2122
]
2223
);
2324
}
25+
26+
function doRevertWithDecoratedCustomErrorName() public pure {
27+
revert $__DecoratedCustomErrorName(0, 'message', 0x00cFBbaF7DDB3a1476767101c12a0162e241fbAD2a0162e2410cFBbaF7162123);
28+
}
2429
}

‎waffle-hardhat/test/reverted.test.ts

+57
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,61 @@ describe('INTEGRATION: Matchers: revertedWith', () => {
133133
);
134134
});
135135
});
136+
137+
it('Revert success (decorated)', async () => {
138+
const matchers = await deploy();
139+
await expect(matchers.doRevertWithDecoratedCustomErrorName()).to.be.revertedWith('$__DecoratedCustomErrorName');
140+
});
141+
142+
it('Revert fail (decorated)', async () => {
143+
const matchers = await deploy();
144+
await expect(expect(matchers.doRevertWithDecoratedCustomErrorName())
145+
.to.be.revertedWith('Two')
146+
).to.be.eventually.rejectedWith(
147+
'Expected transaction to be reverted with "Two", but other reason was found: "$__DecoratedCustomErrorName"'
148+
);
149+
});
150+
151+
it('With args success (decorated)', async () => {
152+
const matchers = await deploy();
153+
await expect(matchers.doRevertWithDecoratedCustomErrorName())
154+
.to.be.revertedWith('$__DecoratedCustomErrorName')
155+
.withArgs(
156+
0,
157+
'message',
158+
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
159+
);
160+
});
161+
162+
it('With args failure (decorated)', async () => {
163+
const matchers = await deploy();
164+
await expect(expect(matchers.doRevertWithDecoratedCustomErrorName())
165+
.to.be.revertedWith('$__DecoratedCustomErrorName')
166+
.withArgs(
167+
1,
168+
'message',
169+
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
170+
)
171+
).to.be.eventually.rejectedWith(/Expected (")?0(")? to (be )?equal 1/i); // It may or may not have the quote marks
172+
await expect(expect(matchers.doRevertWithDecoratedCustomErrorName())
173+
.to.be.revertedWith('$__DecoratedCustomErrorName')
174+
.withArgs(
175+
0,
176+
'messagr',
177+
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123'
178+
)
179+
).to.be.eventually.rejectedWith('expected \'message\' to equal \'messagr\'');
180+
await expect(expect(matchers.doRevertWithDecoratedCustomErrorName())
181+
.to.be.revertedWith('$__DecoratedCustomErrorName')
182+
.withArgs(
183+
0,
184+
'message',
185+
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162124'
186+
)
187+
).to.be.eventually.rejectedWith('expected ' +
188+
'\'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123\'' +
189+
' to equal ' +
190+
'\'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162124\''
191+
);
192+
});
136193
});

0 commit comments

Comments
 (0)
Please sign in to comment.