Skip to content

Commit 615d0f9

Browse files
committed
use callStatic for view/pure calls, shorten tests
1 parent fe3d5c2 commit 615d0f9

File tree

1 file changed

+56
-137
lines changed

1 file changed

+56
-137
lines changed

test/data/EnumerableMap.ts

Lines changed: 56 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,17 @@ describe('EnumerableMap', () => {
3333
describe('#at(uint256)', () => {
3434
it('returns value coresponding to index provided', async () => {
3535
await instance['set(address,address)'](addressOne, addressFour);
36-
await instance['set(address,address)'](addressTwo, addressFive);
37-
await instance['set(address,address)'](addressThree, addressSix);
3836

39-
expect(await instance['at(uint256)'](0)).to.deep.equal([
40-
addressOne,
41-
addressFour,
42-
]);
43-
expect(await instance['at(uint256)'](1)).to.deep.equal([
44-
addressTwo,
45-
addressFive,
46-
]);
47-
expect(await instance['at(uint256)'](2)).to.deep.equal([
48-
addressThree,
49-
addressSix,
50-
]);
37+
const [key, value] = await instance.callStatic['at(uint256)'](0);
38+
39+
expect(key).to.equal(addressOne);
40+
expect(value).to.equal(addressFour);
5141
});
5242

5343
describe('reverts if', () => {
5444
it('index is out of bounds', async () => {
5545
await expect(
56-
instance['at(uint256)'](0),
46+
instance.callStatic['at(uint256)'](0),
5747
).to.be.revertedWithCustomError(
5848
instance,
5949
'EnumerableMap__IndexOutOfBounds',
@@ -65,60 +55,54 @@ describe('EnumerableMap', () => {
6555
describe('#contains(address)', () => {
6656
it('returns true if value has been added', async () => {
6757
await instance['set(address,address)'](addressOne, addressFour);
68-
await instance['set(address,address)'](addressTwo, addressFive);
69-
await instance['set(address,address)'](addressThree, addressSix);
7058

71-
expect(await instance['contains(address)'](addressOne)).to.be.true;
72-
expect(await instance['contains(address)'](addressTwo)).to.be.true;
73-
expect(await instance['contains(address)'](addressThree)).to.be.true;
59+
expect(await instance.callStatic['contains(address)'](addressOne)).to
60+
.be.true;
7461
});
7562

7663
it('returns false if value has not been added', async () => {
77-
expect(await instance['contains(address)'](addressFour)).to.be.false;
64+
expect(await instance.callStatic['contains(address)'](addressFour)).to
65+
.be.false;
7866
});
7967
});
8068

8169
describe('#length()', () => {
8270
it('returns length of enumerable map', async () => {
83-
expect(await instance['length()']()).to.equal(0);
71+
expect(await instance.callStatic['length()']()).to.equal(0);
8472

8573
await instance['set(address,address)'](addressOne, addressFour);
86-
expect(await instance['length()']()).to.equal(1);
74+
expect(await instance.callStatic['length()']()).to.equal(1);
8775

8876
await instance['set(address,address)'](addressTwo, addressFive);
89-
expect(await instance['length()']()).to.equal(2);
77+
expect(await instance.callStatic['length()']()).to.equal(2);
9078

9179
await instance['set(address,address)'](addressThree, addressSix);
92-
expect(await instance['length()']()).to.equal(3);
80+
expect(await instance.callStatic['length()']()).to.equal(3);
9381

9482
await instance['remove(address)'](addressThree);
95-
expect(await instance['length()']()).to.equal(2);
83+
expect(await instance.callStatic['length()']()).to.equal(2);
9684

9785
await instance['remove(address)'](addressTwo);
98-
expect(await instance['length()']()).to.equal(1);
86+
expect(await instance.callStatic['length()']()).to.equal(1);
9987

10088
await instance['remove(address)'](addressOne);
101-
expect(await instance['length()']()).to.equal(0);
89+
expect(await instance.callStatic['length()']()).to.equal(0);
10290
});
10391
});
10492

10593
describe('#get(address)', () => {
10694
it('returns address stored at key', async () => {
10795
await instance['set(address,address)'](addressOne, addressFour);
108-
await instance['set(address,address)'](addressTwo, addressFive);
109-
await instance['set(address,address)'](addressThree, addressSix);
11096

111-
expect(await instance['get(address)'](addressOne)).to.eq(addressFour);
112-
expect(await instance['get(address)'](addressTwo)).to.eq(addressFive);
113-
expect(await instance['get(address)'](addressThree)).to.eq(
114-
addressSix,
97+
expect(await instance.callStatic['get(address)'](addressOne)).to.eq(
98+
addressFour,
11599
);
116100
});
117101

118102
describe('reverts if', () => {
119103
it('key does not exist', async () => {
120104
await expect(
121-
instance['get(address)'](addressOne),
105+
instance.callStatic['get(address)'](addressOne),
122106
).to.be.revertedWithCustomError(
123107
instance,
124108
'EnumerableMap__NonExistentKey',
@@ -130,17 +114,11 @@ describe('EnumerableMap', () => {
130114
describe('#set(address,address)', () => {
131115
it('sets the address value at address key', async () => {
132116
await instance['set(address,address)'](addressOne, addressFour);
133-
await instance['set(address,address)'](addressTwo, addressFive);
134-
await instance['set(address,address)'](addressThree, addressSix);
135117

136-
expect(await instance['contains(address)'](addressOne)).to.be.true;
137-
expect(await instance['contains(address)'](addressTwo)).to.be.true;
138-
expect(await instance['contains(address)'](addressThree)).to.be.true;
139-
140-
expect(await instance['get(address)'](addressOne)).to.eq(addressFour);
141-
expect(await instance['get(address)'](addressTwo)).to.eq(addressFive);
142-
expect(await instance['get(address)'](addressThree)).to.eq(
143-
addressSix,
118+
expect(await instance.callStatic['contains(address)'](addressOne)).to
119+
.be.true;
120+
expect(await instance.callStatic['get(address)'](addressOne)).to.eq(
121+
addressFour,
144122
);
145123
});
146124

@@ -168,37 +146,17 @@ describe('EnumerableMap', () => {
168146
describe('#remove(address)', () => {
169147
it('removes the address value at given address key', async () => {
170148
await instance['set(address,address)'](addressOne, addressFour);
171-
await instance['set(address,address)'](addressTwo, addressFive);
172-
await instance['set(address,address)'](addressThree, addressSix);
173149

174-
expect(await instance['length()']()).to.eq(3);
150+
expect(await instance.callStatic['length()']()).to.eq(1);
175151

176152
await instance['remove(address)'](addressOne);
177153
await expect(
178-
instance['get(address)'](addressOne),
154+
instance.callStatic['get(address)'](addressOne),
179155
).to.be.revertedWithCustomError(
180156
instance,
181157
'EnumerableMap__NonExistentKey',
182158
);
183-
expect(await instance['length()']()).to.eq(2);
184-
185-
await instance['remove(address)'](addressTwo);
186-
await expect(
187-
instance['get(address)'](addressTwo),
188-
).to.be.revertedWithCustomError(
189-
instance,
190-
'EnumerableMap__NonExistentKey',
191-
);
192-
expect(await instance['length()']()).to.eq(1);
193-
194-
await instance['remove(address)'](addressThree);
195-
await expect(
196-
instance['get(address)'](addressThree),
197-
).to.be.revertedWithCustomError(
198-
instance,
199-
'EnumerableMap__NonExistentKey',
200-
);
201-
expect(await instance['length()']()).to.eq(0);
159+
expect(await instance.callStatic['length()']()).to.eq(0);
202160
});
203161

204162
it('returns true if address key removed', async () => {
@@ -226,37 +184,27 @@ describe('EnumerableMap', () => {
226184
});
227185

228186
describe('__internal', () => {
229-
const addressOne = bnToAddress(BigNumber.from('100'));
230-
const addressTwo = bnToAddress(BigNumber.from('200'));
231-
const addressThree = bnToAddress(BigNumber.from('300'));
232187
const uintOne = BigNumber.from('1');
233188
const uintTwo = BigNumber.from('2');
234189
const uintThree = BigNumber.from('3');
190+
const addressOne = bnToAddress(BigNumber.from('100'));
191+
const addressTwo = bnToAddress(BigNumber.from('200'));
192+
const addressThree = bnToAddress(BigNumber.from('300'));
235193

236194
describe('#at(uint256)', () => {
237195
it('returns value coresponding to index provided', async () => {
238196
await instance['set(uint256,address)'](uintOne, addressOne);
239-
await instance['set(uint256,address)'](uintTwo, addressTwo);
240-
await instance['set(uint256,address)'](uintThree, addressThree);
241197

242-
expect(await instance['at(uint256)'](0)).to.deep.equal([
243-
uintOne,
244-
addressOne,
245-
]);
246-
expect(await instance['at(uint256)'](1)).to.deep.equal([
247-
uintTwo,
248-
addressTwo,
249-
]);
250-
expect(await instance['at(uint256)'](2)).to.deep.equal([
251-
uintThree,
252-
addressThree,
253-
]);
198+
const [key, value] = await instance.callStatic['at(uint256)'](0);
199+
200+
expect(key).to.equal(uintOne);
201+
expect(value).to.equal(addressOne);
254202
});
255203

256204
describe('reverts if', () => {
257205
it('index is out of bounds', async () => {
258206
await expect(
259-
instance['at(uint256)'](0),
207+
instance.callStatic['at(uint256)'](0),
260208
).to.be.revertedWithCustomError(
261209
instance,
262210
'EnumerableMap__IndexOutOfBounds',
@@ -268,58 +216,54 @@ describe('EnumerableMap', () => {
268216
describe('#contains(uint256)', () => {
269217
it('returns true if value has been added', async () => {
270218
await instance['set(uint256,address)'](uintOne, addressOne);
271-
await instance['set(uint256,address)'](uintTwo, addressTwo);
272-
await instance['set(uint256,address)'](uintThree, addressThree);
273219

274-
expect(await instance['contains(uint256)'](uintOne)).to.be.true;
275-
expect(await instance['contains(uint256)'](uintTwo)).to.be.true;
276-
expect(await instance['contains(uint256)'](uintThree)).to.be.true;
220+
expect(await instance.callStatic['contains(uint256)'](uintOne)).to.be
221+
.true;
277222
});
278223

279224
it('returns false if value has not been added', async () => {
280-
expect(await instance['contains(uint256)'](uintOne)).to.be.false;
225+
expect(await instance.callStatic['contains(uint256)'](uintOne)).to.be
226+
.false;
281227
});
282228
});
283229

284230
describe('#length()', () => {
285231
it('returns length of enumerable map', async () => {
286-
expect(await instance['length()']()).to.equal(0);
232+
expect(await instance.callStatic['length()']()).to.equal(0);
287233

288234
await instance['set(uint256,address)'](uintOne, addressOne);
289-
expect(await instance['length()']()).to.equal(1);
235+
expect(await instance.callStatic['length()']()).to.equal(1);
290236

291237
await instance['set(uint256,address)'](uintTwo, addressTwo);
292-
expect(await instance['length()']()).to.equal(2);
238+
expect(await instance.callStatic['length()']()).to.equal(2);
293239

294240
await instance['set(uint256,address)'](uintThree, addressThree);
295-
expect(await instance['length()']()).to.equal(3);
241+
expect(await instance.callStatic['length()']()).to.equal(3);
296242

297243
await instance['remove(uint256)'](uintOne);
298-
expect(await instance['length()']()).to.equal(2);
244+
expect(await instance.callStatic['length()']()).to.equal(2);
299245

300246
await instance['remove(uint256)'](uintTwo);
301-
expect(await instance['length()']()).to.equal(1);
247+
expect(await instance.callStatic['length()']()).to.equal(1);
302248

303249
await instance['remove(uint256)'](uintThree);
304-
expect(await instance['length()']()).to.equal(0);
250+
expect(await instance.callStatic['length()']()).to.equal(0);
305251
});
306252
});
307253

308254
describe('#get(uint256)', () => {
309255
it('returns address stored at key', async () => {
310256
await instance['set(uint256,address)'](uintOne, addressOne);
311-
await instance['set(uint256,address)'](uintTwo, addressTwo);
312-
await instance['set(uint256,address)'](uintThree, addressThree);
313257

314-
expect(await instance['get(uint256)'](uintOne)).to.eq(addressOne);
315-
expect(await instance['get(uint256)'](uintTwo)).to.eq(addressTwo);
316-
expect(await instance['get(uint256)'](uintThree)).to.eq(addressThree);
258+
expect(await instance.callStatic['get(uint256)'](uintOne)).to.eq(
259+
addressOne,
260+
);
317261
});
318262

319263
describe('reverts if', () => {
320264
it('key does not exist', async () => {
321265
await expect(
322-
instance['get(uint256)'](uintOne),
266+
instance.callStatic['get(uint256)'](uintOne),
323267
).to.be.revertedWithCustomError(
324268
instance,
325269
'EnumerableMap__NonExistentKey',
@@ -331,16 +275,12 @@ describe('EnumerableMap', () => {
331275
describe('#set(uint256,address)', () => {
332276
it('sets the address value at uint256 key', async () => {
333277
await instance['set(uint256,address)'](uintOne, addressOne);
334-
await instance['set(uint256,address)'](uintTwo, addressTwo);
335-
await instance['set(uint256,address)'](uintThree, addressThree);
336-
337-
expect(await instance['contains(uint256)'](uintOne)).to.be.true;
338-
expect(await instance['contains(uint256)'](uintTwo)).to.be.true;
339-
expect(await instance['contains(uint256)'](uintThree)).to.be.true;
340278

341-
expect(await instance['get(uint256)'](uintOne)).to.eq(addressOne);
342-
expect(await instance['get(uint256)'](uintTwo)).to.eq(addressTwo);
343-
expect(await instance['get(uint256)'](uintThree)).to.eq(addressThree);
279+
expect(await instance.callStatic['contains(uint256)'](uintOne)).to.be
280+
.true;
281+
expect(await instance.callStatic['get(uint256)'](uintOne)).to.eq(
282+
addressOne,
283+
);
344284
});
345285

346286
it('returns true if address value is added at uint256 key', async () => {
@@ -367,37 +307,16 @@ describe('EnumerableMap', () => {
367307
describe('#remove(uint256)', () => {
368308
it('removes the address value at given uint256 key', async () => {
369309
await instance['set(uint256,address)'](uintOne, addressOne);
370-
await instance['set(uint256,address)'](uintTwo, addressTwo);
371-
await instance['set(uint256,address)'](uintThree, addressThree);
372310

373-
expect(await instance['length()']()).to.eq(3);
311+
expect(await instance.callStatic['length()']()).to.eq(1);
374312

375313
await instance['remove(uint256)'](uintOne);
376314
await expect(
377-
instance['get(uint256)'](uintOne),
378-
).to.be.revertedWithCustomError(
379-
instance,
380-
'EnumerableMap__NonExistentKey',
381-
);
382-
expect(await instance['length()']()).to.eq(2);
383-
384-
await instance['remove(uint256)'](uintTwo);
385-
await expect(
386-
instance['get(uint256)'](uintTwo),
387-
).to.be.revertedWithCustomError(
388-
instance,
389-
'EnumerableMap__NonExistentKey',
390-
);
391-
expect(await instance['length()']()).to.eq(1);
392-
393-
await instance['remove(uint256)'](uintThree);
394-
await expect(
395-
instance['get(uint256)'](uintThree),
315+
instance.callStatic['get(uint256)'](uintOne),
396316
).to.be.revertedWithCustomError(
397317
instance,
398318
'EnumerableMap__NonExistentKey',
399319
);
400-
expect(await instance['length()']()).to.eq(0);
401320
});
402321

403322
it('returns true if uint256 key removed', async () => {

0 commit comments

Comments
 (0)