Add clear function to Enumerable{Set,Map} (#5486)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
@ -117,6 +117,49 @@ function shouldBehaveLikeMap() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('clear', function () {
|
||||
it('clears a single entry', async function () {
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
|
||||
await this.methods.clear();
|
||||
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
await expectMembersMatch(this.methods, [], []);
|
||||
});
|
||||
|
||||
it('clears multiple entries', async function () {
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
await this.methods.set(this.keyC, this.valueC);
|
||||
|
||||
await this.methods.clear();
|
||||
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
expect(await this.methods.contains(this.keyB)).to.be.false;
|
||||
expect(await this.methods.contains(this.keyC)).to.be.false;
|
||||
await expectMembersMatch(this.methods, [], []);
|
||||
});
|
||||
|
||||
it('does not revert on empty map', async function () {
|
||||
await this.methods.clear();
|
||||
});
|
||||
|
||||
it('clear then add entry', async function () {
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
await this.methods.set(this.keyC, this.valueC);
|
||||
|
||||
await this.methods.clear();
|
||||
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
|
||||
expect(await this.methods.contains(this.keyA)).to.be.true;
|
||||
expect(await this.methods.contains(this.keyB)).to.be.false;
|
||||
expect(await this.methods.contains(this.keyC)).to.be.false;
|
||||
await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('read', function () {
|
||||
beforeEach(async function () {
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
|
||||
@ -26,6 +26,7 @@ async function fixture() {
|
||||
get: `$get_EnumerableMap_${name}(uint256,${keyType})`,
|
||||
tryGet: `$tryGet_EnumerableMap_${name}(uint256,${keyType})`,
|
||||
remove: `$remove_EnumerableMap_${name}(uint256,${keyType})`,
|
||||
clear: `$clear_EnumerableMap_${name}(uint256)`,
|
||||
length: `$length_EnumerableMap_${name}(uint256)`,
|
||||
at: `$at_EnumerableMap_${name}(uint256,uint256)`,
|
||||
contains: `$contains_EnumerableMap_${name}(uint256,${keyType})`,
|
||||
|
||||
@ -109,6 +109,49 @@ function shouldBehaveLikeSet() {
|
||||
expect(await this.methods.contains(this.valueB)).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('clear', function () {
|
||||
it('clears a single value', async function () {
|
||||
await this.methods.add(this.valueA);
|
||||
|
||||
await this.methods.clear();
|
||||
|
||||
expect(await this.methods.contains(this.valueA)).to.be.false;
|
||||
await expectMembersMatch(this.methods, []);
|
||||
});
|
||||
|
||||
it('clears multiple values', async function () {
|
||||
await this.methods.add(this.valueA);
|
||||
await this.methods.add(this.valueB);
|
||||
await this.methods.add(this.valueC);
|
||||
|
||||
await this.methods.clear();
|
||||
|
||||
expect(await this.methods.contains(this.valueA)).to.be.false;
|
||||
expect(await this.methods.contains(this.valueB)).to.be.false;
|
||||
expect(await this.methods.contains(this.valueC)).to.be.false;
|
||||
await expectMembersMatch(this.methods, []);
|
||||
});
|
||||
|
||||
it('does not revert on empty set', async function () {
|
||||
await this.methods.clear();
|
||||
});
|
||||
|
||||
it('clear then add value', async function () {
|
||||
await this.methods.add(this.valueA);
|
||||
await this.methods.add(this.valueB);
|
||||
await this.methods.add(this.valueC);
|
||||
|
||||
await this.methods.clear();
|
||||
|
||||
await this.methods.add(this.valueA);
|
||||
|
||||
expect(await this.methods.contains(this.valueA)).to.be.true;
|
||||
expect(await this.methods.contains(this.valueB)).to.be.false;
|
||||
expect(await this.methods.contains(this.valueC)).to.be.false;
|
||||
await expectMembersMatch(this.methods, [this.valueA]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@ -30,6 +30,7 @@ async function fixture() {
|
||||
methods: getMethods(mock, {
|
||||
add: `$add(uint256,${type})`,
|
||||
remove: `$remove(uint256,${type})`,
|
||||
clear: `$clear_EnumerableSet_${name}(uint256)`,
|
||||
contains: `$contains(uint256,${type})`,
|
||||
length: `$length_EnumerableSet_${name}(uint256)`,
|
||||
at: `$at_EnumerableSet_${name}(uint256,uint256)`,
|
||||
|
||||
Reference in New Issue
Block a user