This repository was archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBytes32Sets.test.ts
More file actions
102 lines (85 loc) · 2.74 KB
/
Copy pathBytes32Sets.test.ts
File metadata and controls
102 lines (85 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { ethers } from 'hardhat'
import { expect } from 'chai'
import {
Bytes32SetsTest,
Bytes32SetsTest__factory,
} from '../../typechain-types/ethers-v6'
describe('Bytes32Sets', () => {
let bytes32SetsTest: Bytes32SetsTest
const key1 = ethers.encodeBytes32String('key1')
const key2 = ethers.encodeBytes32String('key2')
beforeEach(async () => {
const [admin] = await ethers.getSigners()
const Bytes32SetsTest = await new Bytes32SetsTest__factory(admin).deploy()
bytes32SetsTest = await Bytes32SetsTest.waitForDeployment()
await bytes32SetsTest.waitForDeployment()
})
describe('insert', () => {
it('reverts if the key is already in the set', async () => {
await insertTestKey(key1)
await expect(insertTestKey(key1)).to.be.revertedWith(
'Bytes32Set: key already exists in the set.'
)
})
it('inserts a key', async () => {
await insertTestKey(key1)
expect(await keyExists(key1)).to.be.true
})
})
describe('remove', () => {
it('reverts if the key is not in the set', async () => {
await expect(removeTestKey(key1)).to.be.revertedWith(
'Bytes32Set: key does not exist in the set.'
)
})
it('removes a key', async () => {
await insertTestKey(key1)
await removeTestKey(key1)
expect(await keyExists(key1)).to.be.false
})
it('removes the key before the last key', async () => {
await insertTestKey(key1)
await insertTestKey(key2)
await removeTestKey(key1)
expect(await keyExists(key1)).to.be.false
expect(await keyExists(key2)).to.be.true
})
})
describe('count', () => {
it('returns the number of keys in the set', async () => {
expect(await getCount()).to.equal(0)
await insertTestKey(key1)
expect(await getCount()).to.equal(1)
await removeTestKey(key1)
expect(await getCount()).to.equal(0)
})
})
describe('exists', () => {
it('returns true if the key exists in the set', async () => {
expect(await keyExists(key1)).to.be.false
await insertTestKey(key1)
expect(await keyExists(key1)).to.be.true
})
})
describe('keyAtIndex', () => {
it('returns the key at the given index', async () => {
await insertTestKey(key1)
expect(await getKeyAtIndex(0)).to.equal(key1)
})
})
async function insertTestKey(key: string) {
await bytes32SetsTest.insert(key)
}
async function removeTestKey(key: string) {
await bytes32SetsTest.remove(key)
}
async function getCount() {
return await bytes32SetsTest.count()
}
async function keyExists(key: string) {
return await bytes32SetsTest.exists(key)
}
async function getKeyAtIndex(index: number) {
return await bytes32SetsTest.keyAtIndex(index)
}
})