forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathToml.t.sol
424 lines (356 loc) · 15.6 KB
/
Toml.t.sol
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
import "../logs/console.sol";
library TomlStructs {
address constant HEVM_ADDRESS = address(bytes20(uint160(uint256(keccak256("hevm cheat code")))));
Vm constant vm = Vm(HEVM_ADDRESS);
// forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^FlatToml
string constant schema_FlatToml =
"FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes)";
// forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^NestedToml
string constant schema_NestedToml =
"NestedToml(FlatToml[] members,AnotherFlatToml inner,string name)AnotherFlatToml(bytes4 fixedBytes)FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes)";
function deserializeFlatToml(string memory toml) internal pure returns (ParseTomlTest.FlatToml memory) {
return abi.decode(vm.parseTomlType(toml, schema_FlatToml), (ParseTomlTest.FlatToml));
}
function deserializeFlatToml(string memory toml, string memory path)
internal
pure
returns (ParseTomlTest.FlatToml memory)
{
return abi.decode(vm.parseTomlType(toml, path, schema_FlatToml), (ParseTomlTest.FlatToml));
}
function deserializeFlatTomlArray(string memory toml, string memory path)
internal
pure
returns (ParseTomlTest.FlatToml[] memory)
{
return abi.decode(vm.parseTomlTypeArray(toml, path, schema_FlatToml), (ParseTomlTest.FlatToml[]));
}
function deserializeNestedToml(string memory toml) internal pure returns (ParseTomlTest.NestedToml memory) {
return abi.decode(vm.parseTomlType(toml, schema_NestedToml), (ParseTomlTest.NestedToml));
}
function deserializeNestedToml(string memory toml, string memory path)
internal
pure
returns (ParseTomlTest.NestedToml memory)
{
return abi.decode(vm.parseTomlType(toml, path, schema_NestedToml), (ParseTomlTest.NestedToml));
}
function deserializeNestedTomlArray(string memory toml, string memory path)
internal
pure
returns (ParseTomlTest.NestedToml[] memory)
{
return abi.decode(vm.parseTomlType(toml, path, schema_NestedToml), (ParseTomlTest.NestedToml[]));
}
}
contract ParseTomlTest is DSTest {
using TomlStructs for *;
struct FlatToml {
uint256 a;
int24[][] arr;
string str;
bytes b;
address addr;
bytes32 fixedBytes;
}
struct AnotherFlatToml {
bytes4 fixedBytes;
}
struct NestedToml {
FlatToml[] members;
AnotherFlatToml inner;
string name;
}
Vm constant vm = Vm(HEVM_ADDRESS);
string toml;
function setUp() public {
string memory path = "fixtures/Toml/test.toml";
toml = vm.readFile(path);
}
function test_basicString() public {
bytes memory data = vm.parseToml(toml, ".basicString");
string memory decodedData = abi.decode(data, (string));
assertEq("hai", decodedData);
}
function test_nullString() public {
bytes memory data = vm.parseToml(toml, ".nullString");
string memory decodedData = abi.decode(data, (string));
assertEq("", decodedData);
}
function test_stringMultiline() public {
bytes memory data = vm.parseToml(toml, ".multilineString");
string memory decodedData = abi.decode(data, (string));
assertEq("hai\nthere\n", decodedData);
}
function test_stringArray() public {
bytes memory data = vm.parseToml(toml, ".stringArray");
string[] memory decodedData = abi.decode(data, (string[]));
assertEq("hai", decodedData[0]);
assertEq("there", decodedData[1]);
}
function test_address() public {
bytes memory data = vm.parseToml(toml, ".address");
address decodedData = abi.decode(data, (address));
assertEq(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, decodedData);
}
function test_addressArray() public {
bytes memory data = vm.parseToml(toml, ".addressArray");
address[] memory decodedData = abi.decode(data, (address[]));
assertEq(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, decodedData[0]);
assertEq(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D, decodedData[1]);
}
function test_H160ButNotaddress() public {
string memory data = abi.decode(vm.parseToml(toml, ".H160NotAddress"), (string));
assertEq("0000000000000000000000000000000000001337", data);
}
function test_bool() public {
bytes memory data = vm.parseToml(toml, ".boolTrue");
bool decodedData = abi.decode(data, (bool));
assertTrue(decodedData);
data = vm.parseToml(toml, ".boolFalse");
decodedData = abi.decode(data, (bool));
assertTrue(!decodedData);
}
function test_boolArray() public {
bytes memory data = vm.parseToml(toml, ".boolArray");
bool[] memory decodedData = abi.decode(data, (bool[]));
assertTrue(decodedData[0]);
assertTrue(!decodedData[1]);
}
function test_dateTime() public {
bytes memory data = vm.parseToml(toml, ".datetime");
string memory decodedData = abi.decode(data, (string));
assertEq(decodedData, "2021-08-10T14:48:00Z");
}
function test_dateTimeArray() public {
bytes memory data = vm.parseToml(toml, ".datetimeArray");
string[] memory decodedData = abi.decode(data, (string[]));
assertEq(decodedData[0], "2021-08-10T14:48:00Z");
assertEq(decodedData[1], "2021-08-10T14:48:00Z");
}
function test_uintArray() public {
bytes memory data = vm.parseToml(toml, ".uintArray");
uint256[] memory decodedData = abi.decode(data, (uint256[]));
assertEq(42, decodedData[0]);
assertEq(43, decodedData[1]);
}
// Object keys are sorted alphabetically, regardless of input.
struct Whole {
string str;
string[] strArray;
uint256[] uintArray;
}
function test_wholeToml() public {
// we need to make the path relative to the crate that's running tests for it (forge crate)
string memory path = "fixtures/Toml/whole_toml.toml";
console.log(path);
toml = vm.readFile(path);
bytes memory data = vm.parseToml(toml);
Whole memory whole = abi.decode(data, (Whole));
assertEq(whole.str, "hai");
assertEq(whole.strArray[0], "hai");
assertEq(whole.strArray[1], "there");
assertEq(whole.uintArray[0], 42);
assertEq(whole.uintArray[1], 43);
}
function test_coercionRevert() public {
vm._expectCheatcodeRevert("expected uint256, found JSON object");
vm.parseTomlUint(toml, ".nestedObject");
}
function test_coercionUint() public {
uint256 number = vm.parseTomlUint(toml, ".uintNumber");
assertEq(number, 9223372036854775807); // TOML is limited to 64-bit integers
number = vm.parseTomlUint(toml, ".uintString");
assertEq(number, 115792089237316195423570985008687907853269984665640564039457584007913129639935);
number = vm.parseTomlUint(toml, ".uintHex");
assertEq(number, 1231232);
uint256[] memory numbers = vm.parseTomlUintArray(toml, ".uintArray");
assertEq(numbers[0], 42);
assertEq(numbers[1], 43);
numbers = vm.parseTomlUintArray(toml, ".uintStringArray");
assertEq(numbers[0], 1231232);
assertEq(numbers[1], 1231232);
assertEq(numbers[2], 1231232);
}
function test_coercionInt() public {
int256 number = vm.parseTomlInt(toml, ".intNumber");
assertEq(number, -12);
number = vm.parseTomlInt(toml, ".intString");
assertEq(number, -12);
number = vm.parseTomlInt(toml, ".intHex");
assertEq(number, -12);
}
function test_coercionBool() public {
bool boolean = vm.parseTomlBool(toml, ".boolTrue");
assertTrue(boolean);
bool boolFalse = vm.parseTomlBool(toml, ".boolFalse");
assertTrue(!boolFalse);
boolean = vm.parseTomlBool(toml, ".boolString");
assertEq(boolean, true);
bool[] memory booleans = vm.parseTomlBoolArray(toml, ".boolArray");
assertTrue(booleans[0]);
assertTrue(!booleans[1]);
booleans = vm.parseTomlBoolArray(toml, ".boolStringArray");
assertTrue(booleans[0]);
assertTrue(!booleans[1]);
}
function test_coercionBytes() public {
bytes memory bytes_ = vm.parseTomlBytes(toml, ".bytesString");
assertEq(bytes_, hex"01");
bytes[] memory bytesArray = vm.parseTomlBytesArray(toml, ".bytesStringArray");
assertEq(bytesArray[0], hex"01");
assertEq(bytesArray[1], hex"02");
}
struct NestedStruct {
uint256 number;
string str;
}
function test_nestedObject() public {
bytes memory data = vm.parseToml(toml, ".nestedObject");
NestedStruct memory nested = abi.decode(data, (NestedStruct));
assertEq(nested.number, 9223372036854775807); // TOML is limited to 64-bit integers
assertEq(nested.str, "NEST");
}
function test_advancedTomlPath() public {
bytes memory data = vm.parseToml(toml, ".advancedTomlPath[*].id");
uint256[] memory numbers = abi.decode(data, (uint256[]));
assertEq(numbers[0], 1);
assertEq(numbers[1], 2);
}
function test_canonicalizePath() public {
bytes memory data = vm.parseToml(toml, "$.basicString");
string memory decodedData = abi.decode(data, (string));
assertEq("hai", decodedData);
}
function test_nonExistentKey() public {
bytes memory data = vm.parseToml(toml, ".thisKeyDoesNotExist");
assertEq(0, data.length);
}
function test_parseTomlKeys() public {
string memory tomlString =
"some_key_to_value = \"some_value\"\n some_key_to_array = [1, 2, 3]\n [some_key_to_object]\n key1 = \"value1\"\n key2 = 2";
string[] memory keys = vm.parseTomlKeys(tomlString, "$");
string[] memory expected = new string[](3);
expected[0] = "some_key_to_value";
expected[1] = "some_key_to_array";
expected[2] = "some_key_to_object";
assertEq(abi.encode(keys), abi.encode(expected));
keys = vm.parseTomlKeys(tomlString, ".some_key_to_object");
expected = new string[](2);
expected[0] = "key1";
expected[1] = "key2";
assertEq(abi.encode(keys), abi.encode(expected));
vm._expectCheatcodeRevert("JSON value at \".some_key_to_array\" is not an object");
vm.parseTomlKeys(tomlString, ".some_key_to_array");
vm._expectCheatcodeRevert("JSON value at \".some_key_to_value\" is not an object");
vm.parseTomlKeys(tomlString, ".some_key_to_value");
vm._expectCheatcodeRevert("key \".*\" must return exactly one JSON object");
vm.parseTomlKeys(tomlString, ".*");
}
// forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^FlatToml
string constant schema_FlatToml =
"FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes)";
// forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^NestedToml
string constant schema_NestedToml =
"NestedToml(FlatToml[] members,AnotherFlatToml inner,string name)AnotherFlatToml(bytes4 fixedBytes)FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes)";
function test_parseTomlType() public {
string memory readToml = vm.readFile("fixtures/Toml/nested_toml_struct.toml");
NestedToml memory data = readToml.deserializeNestedToml();
assertEq(data.members.length, 2);
FlatToml memory expected = FlatToml({
a: 200,
arr: new int24[][](0),
str: "some other string",
b: hex"0000000000000000000000000000000000000000",
addr: 0x167D91deaEEE3021161502873d3bcc6291081648,
fixedBytes: 0xed1c7beb1f00feaaaec5636950d6edb25a8d4fedc8deb2711287b64c4d27719d
});
assertEq(keccak256(abi.encode(data.members[1])), keccak256(abi.encode(expected)));
assertEq(bytes32(data.inner.fixedBytes), bytes32(bytes4(0x12345678)));
FlatToml[] memory members = TomlStructs.deserializeFlatTomlArray(readToml, ".members");
assertEq(keccak256(abi.encode(members)), keccak256(abi.encode(data.members)));
}
}
contract WriteTomlTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
string json1;
string json2;
function setUp() public {
json1 = "example";
json2 = "example2";
}
struct simpleStruct {
uint256 a;
string b;
}
struct nestedStruct {
uint256 a;
string b;
simpleStruct c;
}
function test_serializeNestedStructToml() public {
string memory json3 = "json3";
string memory path = "fixtures/Toml/write_complex_test.toml";
vm.serializeUint(json3, "a", uint256(123));
string memory semiFinal = vm.serializeString(json3, "b", "test");
string memory finalJson = vm.serializeString(json3, "c", semiFinal);
console.log(finalJson);
vm.writeToml(finalJson, path);
string memory toml = vm.readFile(path);
bytes memory data = vm.parseToml(toml);
nestedStruct memory decodedData = abi.decode(data, (nestedStruct));
console.log(decodedData.a);
assertEq(decodedData.a, 123);
}
function test_retrieveEntireToml() public {
string memory path = "fixtures/Toml/write_complex_test.toml";
string memory toml = vm.readFile(path);
bytes memory data = vm.parseToml(toml, ".");
nestedStruct memory decodedData = abi.decode(data, (nestedStruct));
console.log(decodedData.a);
assertEq(decodedData.a, 123);
}
function test_checkKeyExists() public {
string memory path = "fixtures/Toml/write_complex_test.toml";
string memory toml = vm.readFile(path);
bool exists = vm.keyExistsToml(toml, ".a");
assertTrue(exists);
}
function test_checkKeyDoesNotExist() public {
string memory path = "fixtures/Toml/write_complex_test.toml";
string memory toml = vm.readFile(path);
bool exists = vm.keyExistsToml(toml, ".d");
assertTrue(!exists);
}
function test_writeToml() public {
string memory json3 = "json3";
string memory path = "fixtures/Toml/write_test.toml";
vm.serializeUint(json3, "a", uint256(123));
string memory finalJson = vm.serializeString(json3, "b", "test");
vm.writeToml(finalJson, path);
string memory toml = vm.readFile(path);
bytes memory data = vm.parseToml(toml);
simpleStruct memory decodedData = abi.decode(data, (simpleStruct));
assertEq(decodedData.a, 123);
assertEq(decodedData.b, "test");
// write json3 to key b
vm.writeToml(finalJson, path, ".b");
// read again
toml = vm.readFile(path);
data = vm.parseToml(toml, ".b");
decodedData = abi.decode(data, (simpleStruct));
assertEq(decodedData.a, 123);
assertEq(decodedData.b, "test");
// replace a single value to key b
address ex = address(0xBEEF);
vm.writeToml(vm.toString(ex), path, ".b");
toml = vm.readFile(path);
data = vm.parseToml(toml, ".b");
address decodedAddress = abi.decode(data, (address));
assertEq(decodedAddress, ex);
}
}