-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtype-handling.spec.ts
257 lines (205 loc) · 9.11 KB
/
type-handling.spec.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { FakeContract, smock } from '@src';
import { convertStructToPojo } from '@src/utils';
import { Delegator__factory, Returner } from '@typechained';
import chai, { expect } from 'chai';
import { BigNumber, utils } from 'ethers';
import { ethers } from 'hardhat';
import { toPlainObject } from 'lodash';
import { BYTES32_EXAMPLE, BYTES_EXAMPLE, STRUCT_DYNAMIC_SIZE_EXAMPLE, STRUCT_FIXED_SIZE_EXAMPLE } from 'test/utils';
chai.should();
chai.use(smock.matchers);
describe('ProgrammableFunctionLogic: Type Handling', () => {
let fake: FakeContract<Returner>;
beforeEach(async () => {
fake = await smock.fake<Returner>('Returner');
});
context('fixed data types', () => {
describe('default behaviors', () => {
it('should return false for a boolean', async () => {
expect(await fake.callStatic.getBoolean()).to.equal(false);
});
it('should return zero for a uint256', async () => {
expect(await fake.callStatic.getUint256()).to.equal(BigNumber.from('0'));
});
it('should return 32 zero bytes for a bytes32', async () => {
const expected = '0x0000000000000000000000000000000000000000000000000000000000000000';
expect(await fake.callStatic.getBytes32()).to.equal(expected);
});
});
describe('from a specified value', () => {
it('should be able to return a boolean', async () => {
const expected = true;
fake.getBoolean.returns(expected);
expect(await fake.callStatic.getBoolean()).to.equal(expected);
});
it('should be able to return a uint256', async () => {
const expected = utils.parseUnits('1');
fake.getUint256.returns(expected);
expect(await fake.callStatic.getUint256()).to.equal(expected);
});
it('should be able to return a bytes32', async () => {
const expected = BYTES32_EXAMPLE;
fake.getBytes32.returns(expected);
expect(await fake.callStatic.getBytes32()).to.equal(expected);
});
it('should be able to return a boolean through a delegatecall', async () => {
const delegatorFactory = (await ethers.getContractFactory('Delegator')) as Delegator__factory;
const delegator = await delegatorFactory.deploy();
const expected = true;
fake.getBoolean.returns(expected);
const result = await delegator.callStatic.delegateGetBoolean(fake.address);
expect(result).to.equal(expected);
expect(fake.getBoolean).to.be.calledOnce;
expect(fake.getBoolean).to.be.delegatedFrom(delegator.address);
});
});
});
context('dynamic data types', () => {
describe('from a specified value', () => {
it('should be able to return a bytes value', async () => {
const expected = BYTES_EXAMPLE;
fake.getBytes.returns(expected);
expect(await fake.callStatic.getBytes()).to.equal(expected);
});
it('should be able to return a string value', async () => {
const expected = 'this is an expected return string';
fake.getString.returns(expected);
expect(await fake.callStatic.getString()).to.equal(expected);
});
it('should be able to return a struct with fixed size values', async () => {
const expected = STRUCT_FIXED_SIZE_EXAMPLE;
fake.getStructFixedSize.returns(expected);
const result = toPlainObject(await fake.callStatic.getStructFixedSize());
expect(result.valBoolean).to.equal(expected.valBoolean);
expect(result.valUint256).to.deep.equal(expected.valUint256);
expect(result.valBytes32).to.equal(expected.valBytes32);
});
it('should be able to return a struct inside a mapping', async () => {
const expected = STRUCT_FIXED_SIZE_EXAMPLE;
fake.structMap.returns(expected);
const result = convertStructToPojo(await fake.callStatic.structMap(BigNumber.from(1)));
expect(result).to.deep.equal(expected);
});
it('should be able to return a nested struct inside a mapping', async () => {
const expected = {
valRootString: 'Random',
valStructFixedSize: STRUCT_FIXED_SIZE_EXAMPLE,
valStructDynamicSize: STRUCT_DYNAMIC_SIZE_EXAMPLE,
valRootBoolean: true,
};
fake.nestedStructMap.returns(expected);
const result = convertStructToPojo(await fake.callStatic.nestedStructMap(BigNumber.from(1)));
expect(result).to.deep.equal(expected);
});
it('should be able to return a struct with dynamic size values', async () => {
const expected = STRUCT_DYNAMIC_SIZE_EXAMPLE;
fake.getStructDynamicSize.returns(expected);
const result = toPlainObject(await fake.callStatic.getStructDynamicSize());
expect(result.valBytes).to.equal(expected.valBytes);
expect(result.valString).to.equal(expected.valString);
});
it('should be able to return a struct with both fixed and dynamic size values', async () => {
const expected = {
...STRUCT_FIXED_SIZE_EXAMPLE,
...STRUCT_DYNAMIC_SIZE_EXAMPLE,
};
fake.getStructMixedSize.returns(expected);
const result = toPlainObject(await fake.callStatic.getStructMixedSize());
expect(result.valBoolean).to.equal(expected.valBoolean);
expect(result.valUint256).to.deep.equal(expected.valUint256);
expect(result.valBytes32).to.equal(expected.valBytes32);
expect(result.valBytes).to.equal(expected.valBytes);
expect(result.valString).to.equal(expected.valString);
});
it('should be able to return a nested struct', async () => {
const expected = {
valStructFixedSize: STRUCT_FIXED_SIZE_EXAMPLE,
valStructDynamicSize: STRUCT_DYNAMIC_SIZE_EXAMPLE,
};
fake.getStructNested.returns(expected);
const result = toPlainObject(await fake.callStatic.getStructNested());
expect(result.valStructFixedSize[0]).to.deep.equal(expected.valStructFixedSize.valBoolean);
expect(result.valStructFixedSize[1]).to.deep.equal(expected.valStructFixedSize.valUint256);
expect(result.valStructFixedSize[2]).to.deep.equal(expected.valStructFixedSize.valBytes32);
expect(result.valStructDynamicSize[0]).to.deep.equal(expected.valStructDynamicSize.valBytes);
expect(result.valStructDynamicSize[1]).to.deep.equal(expected.valStructDynamicSize.valString);
});
it('should be able to return an array of uint256 values', async () => {
const expected = [1234, 2345, 3456, 4567, 5678, 6789].map((n) => {
return BigNumber.from(n);
});
fake.getArrayUint256.returns(expected);
const result = await fake.callStatic.getArrayUint256();
for (let i = 0; i < result.length; i++) {
expect(result[i]).to.deep.equal(expected[i]);
}
});
it('should be able to return multiple arrays of uint256 values', async () => {
const expected = [
[1234, 2345, 3456, 4567, 5678, 6789].map((n) => {
return BigNumber.from(n);
}),
[1234, 2345, 3456, 4567, 5678, 6789].map((n) => {
return BigNumber.from(n);
}),
];
fake.getMultipleUint256Arrays.returns(expected);
const result = await fake.callStatic.getMultipleUint256Arrays();
for (let i = 0; i < result.length; i++) {
for (let j = 0; j < result[i].length; j++) {
expect(result[i][j]).to.deep.equal(expected[i][j]);
}
}
});
});
});
context('fallback function', () => {
const EMPTY_ANSWER = '0x' + '00'.repeat(2048);
it('should return with no data by default', async () => {
expect(await ethers.provider.call({ to: fake.address })).to.equal(EMPTY_ANSWER);
});
it('should be able to return with empty data', async () => {
fake.fallback.returns();
expect(
await ethers.provider.call({
to: fake.address,
})
).to.equal(EMPTY_ANSWER);
});
it('should be able to return simple data', async () => {
const expected = '0x1234';
fake.fallback.returns(expected);
expect(
await ethers.provider.call({
to: fake.address,
})
).to.equal(expected);
});
it('should be able to return complex data as hex', async () => {
fake.fallback.returns([1, 2, 3]);
expect(
await ethers.provider.call({
to: fake.address,
})
).to.equal('0x010203');
});
it('should be able to return string data as hex', async () => {
fake.fallback.returns('hello');
expect(
await ethers.provider.call({
to: fake.address,
})
).to.equal('0x68656c6c6f');
});
});
context('function value', () => {
it('should pass the argument to the function and return the result', async () => {
fake.getInputtedUint256.returns((number: number) => number * 10);
expect(await fake.callStatic.getInputtedUint256(10)).to.equal(100);
});
it('should wait for result as promise', async () => {
fake.getInputtedUint256.returns(async (number: number) => number * 10);
expect(await fake.callStatic.getInputtedUint256(10)).to.equal(100);
});
});
});