Use Prettier for JS files (#3913)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
@ -4,9 +4,14 @@ const { expectRevertCustomError } = require('../../helpers/customError');
|
||||
const DoubleEndedQueue = artifacts.require('$DoubleEndedQueue');
|
||||
|
||||
/** Rebuild the content of the deque as a JS array. */
|
||||
const getContent = (deque) => deque.$length(0).then(bn =>
|
||||
Promise.all(Array(bn.toNumber()).fill().map((_, i) => deque.$at(0, i))),
|
||||
);
|
||||
const getContent = deque =>
|
||||
deque.$length(0).then(bn =>
|
||||
Promise.all(
|
||||
Array(bn.toNumber())
|
||||
.fill()
|
||||
.map((_, i) => deque.$at(0, i)),
|
||||
),
|
||||
);
|
||||
|
||||
contract('DoubleEndedQueue', function () {
|
||||
const bytesA = '0xdeadbeef'.padEnd(66, '0');
|
||||
@ -37,7 +42,7 @@ contract('DoubleEndedQueue', function () {
|
||||
await this.deque.$pushBack(0, bytesB);
|
||||
await this.deque.$pushFront(0, bytesA);
|
||||
await this.deque.$pushBack(0, bytesC);
|
||||
this.content = [ bytesA, bytesB, bytesC ];
|
||||
this.content = [bytesA, bytesB, bytesC];
|
||||
});
|
||||
|
||||
it('getters', async function () {
|
||||
|
||||
@ -3,46 +3,39 @@ const { expect } = require('chai');
|
||||
|
||||
const zip = require('lodash.zip');
|
||||
|
||||
function shouldBehaveLikeMap (
|
||||
keys,
|
||||
values,
|
||||
zeroValue,
|
||||
methods,
|
||||
events,
|
||||
) {
|
||||
const [ keyA, keyB, keyC ] = keys;
|
||||
const [ valueA, valueB, valueC ] = values;
|
||||
function shouldBehaveLikeMap(keys, values, zeroValue, methods, events) {
|
||||
const [keyA, keyB, keyC] = keys;
|
||||
const [valueA, valueB, valueC] = values;
|
||||
|
||||
async function expectMembersMatch (map, keys, values) {
|
||||
async function expectMembersMatch(map, keys, values) {
|
||||
expect(keys.length).to.equal(values.length);
|
||||
|
||||
await Promise.all(keys.map(async key =>
|
||||
expect(await methods.contains(map, key)).to.equal(true),
|
||||
));
|
||||
await Promise.all(keys.map(async key => expect(await methods.contains(map, key)).to.equal(true)));
|
||||
|
||||
expect(await methods.length(map)).to.bignumber.equal(keys.length.toString());
|
||||
|
||||
expect(
|
||||
(await Promise.all(keys.map(key => methods.get(map, key)))).map(k => k.toString()),
|
||||
).to.have.same.members(
|
||||
expect((await Promise.all(keys.map(key => methods.get(map, key)))).map(k => k.toString())).to.have.same.members(
|
||||
values.map(value => value.toString()),
|
||||
);
|
||||
|
||||
// To compare key-value pairs, we zip keys and values, and convert BNs to
|
||||
// strings to workaround Chai limitations when dealing with nested arrays
|
||||
expect(await Promise.all([...Array(keys.length).keys()].map(async (index) => {
|
||||
const entry = await methods.at(map, index);
|
||||
return [ entry[0].toString(), entry[1].toString() ];
|
||||
}))).to.have.same.deep.members(
|
||||
zip(keys.map(k => k.toString()), values.map(v => v.toString())),
|
||||
expect(
|
||||
await Promise.all(
|
||||
[...Array(keys.length).keys()].map(async index => {
|
||||
const entry = await methods.at(map, index);
|
||||
return [entry[0].toString(), entry[1].toString()];
|
||||
}),
|
||||
),
|
||||
).to.have.same.deep.members(
|
||||
zip(
|
||||
keys.map(k => k.toString()),
|
||||
values.map(v => v.toString()),
|
||||
),
|
||||
);
|
||||
|
||||
// This also checks that both arrays have the same length
|
||||
expect(
|
||||
(await methods.keys(map)).map(k => k.toString()),
|
||||
).to.have.same.members(
|
||||
keys.map(key => key.toString()),
|
||||
);
|
||||
expect((await methods.keys(map)).map(k => k.toString())).to.have.same.members(keys.map(key => key.toString()));
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
@ -154,29 +147,21 @@ function shouldBehaveLikeMap (
|
||||
|
||||
describe('get', function () {
|
||||
it('existing value', async function () {
|
||||
expect(
|
||||
await methods.get(this.map, keyA).then(r => r.toString()),
|
||||
).to.be.equal(valueA.toString());
|
||||
expect(await methods.get(this.map, keyA).then(r => r.toString())).to.be.equal(valueA.toString());
|
||||
});
|
||||
it('missing value', async function () {
|
||||
await expectRevert(
|
||||
methods.get(this.map, keyB),
|
||||
'EnumerableMap: nonexistent key',
|
||||
);
|
||||
await expectRevert(methods.get(this.map, keyB), 'EnumerableMap: nonexistent key');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get with message', function () {
|
||||
it('existing value', async function () {
|
||||
expect(
|
||||
await methods.getWithMessage(this.map, keyA, 'custom error string').then(r => r.toString()),
|
||||
).to.be.equal(valueA.toString());
|
||||
expect(await methods.getWithMessage(this.map, keyA, 'custom error string').then(r => r.toString())).to.be.equal(
|
||||
valueA.toString(),
|
||||
);
|
||||
});
|
||||
it('missing value', async function () {
|
||||
await expectRevert(
|
||||
methods.getWithMessage(this.map, keyB, 'custom error string'),
|
||||
'custom error string',
|
||||
);
|
||||
await expectRevert(methods.getWithMessage(this.map, keyB, 'custom error string'), 'custom error string');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -5,12 +5,17 @@ const EnumerableMap = artifacts.require('$EnumerableMap');
|
||||
|
||||
const { shouldBehaveLikeMap } = require('./EnumerableMap.behavior');
|
||||
|
||||
const getMethods = (ms) => {
|
||||
return mapValues(ms, m => (self, ...args) => self.methods[m](0, ...args));
|
||||
const getMethods = ms => {
|
||||
return mapValues(
|
||||
ms,
|
||||
m =>
|
||||
(self, ...args) =>
|
||||
self.methods[m](0, ...args),
|
||||
);
|
||||
};
|
||||
|
||||
contract('EnumerableMap', function (accounts) {
|
||||
const [ accountA, accountB, accountC ] = accounts;
|
||||
const [accountA, accountB, accountC] = accounts;
|
||||
|
||||
const keyA = new BN('7891');
|
||||
const keyB = new BN('451');
|
||||
@ -27,8 +32,8 @@ contract('EnumerableMap', function (accounts) {
|
||||
// AddressToUintMap
|
||||
describe('AddressToUintMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[ accountA, accountB, accountC ],
|
||||
[ keyA, keyB, keyC ],
|
||||
[accountA, accountB, accountC],
|
||||
[keyA, keyB, keyC],
|
||||
new BN('0'),
|
||||
getMethods({
|
||||
set: '$set(uint256,address,uint256)',
|
||||
@ -51,8 +56,8 @@ contract('EnumerableMap', function (accounts) {
|
||||
// UintToAddressMap
|
||||
describe('UintToAddressMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[ keyA, keyB, keyC ],
|
||||
[ accountA, accountB, accountC ],
|
||||
[keyA, keyB, keyC],
|
||||
[accountA, accountB, accountC],
|
||||
constants.ZERO_ADDRESS,
|
||||
getMethods({
|
||||
set: '$set(uint256,uint256,address)',
|
||||
@ -75,8 +80,8 @@ contract('EnumerableMap', function (accounts) {
|
||||
// Bytes32ToBytes32Map
|
||||
describe('Bytes32ToBytes32Map', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[ keyA, keyB, keyC ].map(k => '0x' + k.toString(16).padEnd(64, '0')),
|
||||
[ bytesA, bytesB, bytesC ],
|
||||
[keyA, keyB, keyC].map(k => '0x' + k.toString(16).padEnd(64, '0')),
|
||||
[bytesA, bytesB, bytesC],
|
||||
constants.ZERO_BYTES32,
|
||||
getMethods({
|
||||
set: '$set(uint256,bytes32,bytes32)',
|
||||
@ -99,8 +104,8 @@ contract('EnumerableMap', function (accounts) {
|
||||
// UintToUintMap
|
||||
describe('UintToUintMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[ keyA, keyB, keyC ],
|
||||
[ keyA, keyB, keyC ].map(k => k.add(new BN('1332'))),
|
||||
[keyA, keyB, keyC],
|
||||
[keyA, keyB, keyC].map(k => k.add(new BN('1332'))),
|
||||
new BN('0'),
|
||||
getMethods({
|
||||
set: '$set(uint256,uint256,uint256)',
|
||||
@ -123,8 +128,8 @@ contract('EnumerableMap', function (accounts) {
|
||||
// Bytes32ToUintMap
|
||||
describe('Bytes32ToUintMap', function () {
|
||||
shouldBehaveLikeMap(
|
||||
[ bytesA, bytesB, bytesC ],
|
||||
[ keyA, keyB, keyC ],
|
||||
[bytesA, bytesB, bytesC],
|
||||
[keyA, keyB, keyC],
|
||||
new BN('0'),
|
||||
getMethods({
|
||||
set: '$set(uint256,bytes32,uint256)',
|
||||
|
||||
@ -1,14 +1,10 @@
|
||||
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
function shouldBehaveLikeSet (
|
||||
values,
|
||||
methods,
|
||||
events,
|
||||
) {
|
||||
const [ valueA, valueB, valueC ] = values;
|
||||
function shouldBehaveLikeSet(values, methods, events) {
|
||||
const [valueA, valueB, valueC] = values;
|
||||
|
||||
async function expectMembersMatch (set, values) {
|
||||
async function expectMembersMatch(set, values) {
|
||||
const contains = await Promise.all(values.map(value => methods.contains(set, value)));
|
||||
expect(contains.every(Boolean)).to.be.equal(true);
|
||||
|
||||
@ -17,19 +13,15 @@ function shouldBehaveLikeSet (
|
||||
|
||||
// To compare values we convert to strings to workaround Chai
|
||||
// limitations when dealing with nested arrays (required for BNs)
|
||||
const indexedValues = await Promise.all(Array(values.length).fill().map((_, index) => methods.at(set, index)));
|
||||
expect(
|
||||
indexedValues.map(v => v.toString()),
|
||||
).to.have.same.members(
|
||||
values.map(v => v.toString()),
|
||||
const indexedValues = await Promise.all(
|
||||
Array(values.length)
|
||||
.fill()
|
||||
.map((_, index) => methods.at(set, index)),
|
||||
);
|
||||
expect(indexedValues.map(v => v.toString())).to.have.same.members(values.map(v => v.toString()));
|
||||
|
||||
const returnedValues = await methods.values(set);
|
||||
expect(
|
||||
returnedValues.map(v => v.toString()),
|
||||
).to.have.same.members(
|
||||
values.map(v => v.toString()),
|
||||
);
|
||||
expect(returnedValues.map(v => v.toString())).to.have.same.members(values.map(v => v.toString()));
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
|
||||
@ -3,8 +3,13 @@ const { mapValues } = require('../../helpers/map-values');
|
||||
|
||||
const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
|
||||
|
||||
const getMethods = (ms) => {
|
||||
return mapValues(ms, m => (self, ...args) => self.methods[m](0, ...args));
|
||||
const getMethods = ms => {
|
||||
return mapValues(
|
||||
ms,
|
||||
m =>
|
||||
(self, ...args) =>
|
||||
self.methods[m](0, ...args),
|
||||
);
|
||||
};
|
||||
|
||||
contract('EnumerableSet', function (accounts) {
|
||||
@ -15,7 +20,7 @@ contract('EnumerableSet', function (accounts) {
|
||||
// Bytes32Set
|
||||
describe('EnumerableBytes32Set', function () {
|
||||
shouldBehaveLikeSet(
|
||||
[ '0xdeadbeef', '0x0123456789', '0x42424242' ].map(e => e.padEnd(66, '0')),
|
||||
['0xdeadbeef', '0x0123456789', '0x42424242'].map(e => e.padEnd(66, '0')),
|
||||
getMethods({
|
||||
add: '$add(uint256,bytes32)',
|
||||
remove: '$remove(uint256,bytes32)',
|
||||
@ -53,7 +58,7 @@ contract('EnumerableSet', function (accounts) {
|
||||
// UintSet
|
||||
describe('EnumerableUintSet', function () {
|
||||
shouldBehaveLikeSet(
|
||||
[ 1234, 5678, 9101112 ].map(e => web3.utils.toBN(e)),
|
||||
[1234, 5678, 9101112].map(e => web3.utils.toBN(e)),
|
||||
getMethods({
|
||||
add: '$add(uint256,uint256)',
|
||||
remove: '$remove(uint256,uint256)',
|
||||
|
||||
Reference in New Issue
Block a user