-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathweb3.eth.utils.tests.pas
288 lines (270 loc) · 7.94 KB
/
web3.eth.utils.tests.pas
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under GNU AGPL v3.0 with Commons Clause }
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as published }
{ by the Free Software Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/> }
{ }
{******************************************************************************}
unit web3.eth.utils.tests;
{$I web3.inc}
interface
uses
// DUnitX
DUnitX.TestFramework;
type
[TestFixture]
TTests = class
public
[Test]
procedure FromWei;
[Test]
procedure ToWei1;
[Test]
procedure ToWei2;
[Test]
procedure ToWei3;
[Test]
procedure ToWei4;
[Test]
procedure ToWei5;
[Test]
procedure ToWei6;
[Test]
procedure WeiToWei;
[Test]
procedure ToChecksum;
end;
implementation
uses
// Delphi
System.SysUtils,
// web3
web3,
web3.eth.types,
web3.eth.utils;
procedure TTests.FromWei;
begin
Assert.IsTrue(
(web3.eth.utils.fromWei(1000000000000000000, wei) = '1000000000000000000')
and (web3.eth.utils.fromWei(1000000000000000000, kwei) = '1000000000000000')
and (web3.eth.utils.fromWei(1000000000000000000, mwei) = '1000000000000')
and (web3.eth.utils.fromWei(1000000000000000000, gwei) = '1000000000')
and (web3.eth.utils.fromWei(1000000000000000000, szabo) = '1000000')
and (web3.eth.utils.fromWei(1000000000000000000, finney) = '1000')
and (web3.eth.utils.fromWei(1000000000000000000, ether) = '1')
and (web3.eth.utils.fromWei(1000000000000000000, kether) = '0.001')
and (web3.eth.utils.fromWei(1000000000000000000, grand) = '0.001')
and (web3.eth.utils.fromWei(1000000000000000000, mether) = '0.000001')
and (web3.eth.utils.fromWei(1000000000000000000, gether) = '0.000000001')
and (web3.eth.utils.fromWei(1000000000000000000, tether) = '0.000000000001')
);
end;
procedure TTests.ToWei1;
type
TTestCase = record
&to : TDenomination;
output: string;
end;
const
TEST_CASES: array[0..14] of TTestCase = (
(&to: wei; output: '1'),
(&to: kwei; output: '1000'),
(&to: babbage; output: '1000'),
(&to: mwei; output: '1000000'),
(&to: lovelace; output: '1000000'),
(&to: gwei; output: '1000000000'),
(&to: shannon; output: '1000000000'),
(&to: szabo; output: '1000000000000'),
(&to: finney; output: '1000000000000000'),
(&to: ether; output: '1000000000000000000'),
(&to: kether; output: '1000000000000000000000'),
(&to: grand; output: '1000000000000000000000'),
(&to: mether; output: '1000000000000000000000000'),
(&to: gether; output: '1000000000000000000000000000'),
(&to: tether; output: '1000000000000000000000000000000')
);
begin
for var TEST_CASE in TEST_CASES do
begin
web3.eth.utils.toWei('1', TEST_CASE.&to)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei: TWei)
begin
Assert.IsTrue(wei = TWei.Create(TEST_CASE.output))
end);
end;
end;
procedure TTests.ToWei2;
begin
web3.eth.utils.toWei('1', kwei)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei1: TWei)
begin
web3.eth.utils.toWei('1', femtoether)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei2: TWei)
begin
Assert.IsTrue(wei1 = wei2)
end);
end);
end;
procedure TTests.ToWei3;
begin
web3.eth.utils.toWei('1', szabo)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei1: TWei)
begin
web3.eth.utils.toWei('1', microether)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei2: TWei)
begin
Assert.IsTrue(wei1 = wei2)
end);
end);
end;
procedure TTests.ToWei4;
begin
web3.eth.utils.toWei('1', finney)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei1: TWei)
begin
web3.eth.utils.toWei('1', milliether)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei2: TWei)
begin
Assert.IsTrue(wei1 = wei2)
end);
end);
end;
procedure TTests.ToWei5;
begin
web3.eth.utils.toWei('1', milli)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei1: TWei)
begin
web3.eth.utils.toWei('1', milliether)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei2: TWei)
begin
Assert.IsTrue(wei1 = wei2)
end);
end);
end;
procedure TTests.ToWei6;
begin
web3.eth.utils.toWei('1', milli)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei1: TWei)
begin
web3.eth.utils.toWei('1000', micro)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei2: TWei)
begin
Assert.IsTrue(wei1 = wei2)
end);
end);
end;
procedure TTests.WeiToWei;
const
TEST_CASES: array[0..17] of string = (
'0',
'0.1',
'0.01',
'0.001',
'0.0001',
'0.00001',
'0.000001',
'0.0000001',
'0.00000001',
'1',
'1.1',
'1.01',
'1.001',
'1.0001',
'1.00001',
'1.000001',
'1.0000001',
'1.00000001');
begin
for var TEST_CASE in TEST_CASES do
begin
web3.eth.utils.toWei(TEST_CASE, ether)
.ifErr(procedure(err: IError)
begin
Assert.Fail(err.Message)
end)
.&else(procedure(wei: TWei)
begin
Assert.AreEqual(web3.eth.utils.fromWei(wei, ether), TEST_CASE)
end);
end;
end;
procedure TTests.ToChecksum;
const
TEST_CASES: array[0..3] of string = (
'0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
'0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
'0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
'0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb'
);
begin
for var TEST_CASE in TEST_CASES do
begin
Assert.AreEqual(
string(TAddress.Create(TEST_CASE.ToUpper).ToChecksum), TEST_CASE, False);
Assert.AreEqual(
string(TAddress.Create(TEST_CASE.ToLower).ToChecksum), TEST_CASE, False);
end;
end;
initialization
TDUnitX.RegisterTestFixture(TTests);
end.