@@ -5,7 +5,81 @@ import "ds-test/test.sol";
5
5
import "cheats/Vm.sol " ;
6
6
import "../logs/console.sol " ;
7
7
8
+ library TomlStructs {
9
+ address constant HEVM_ADDRESS = address (bytes20 (uint160 (uint256 (keccak256 ("hevm cheat code " )))));
10
+ Vm constant vm = Vm (HEVM_ADDRESS);
11
+
12
+ // forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^FlatToml
13
+ string constant schema_FlatToml =
14
+ "FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes) " ;
15
+
16
+ // forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^NestedToml
17
+ string constant schema_NestedToml =
18
+ "NestedToml(FlatToml[] members,AnotherFlatToml inner,string name)AnotherFlatToml(bytes4 fixedBytes)FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes) " ;
19
+
20
+ function deserializeFlatToml (string memory toml ) internal pure returns (ParseTomlTest.FlatToml memory ) {
21
+ return abi.decode (vm.parseTomlType (toml, schema_FlatToml), (ParseTomlTest.FlatToml));
22
+ }
23
+
24
+ function deserializeFlatToml (string memory toml , string memory path )
25
+ internal
26
+ pure
27
+ returns (ParseTomlTest.FlatToml memory )
28
+ {
29
+ return abi.decode (vm.parseTomlType (toml, path, schema_FlatToml), (ParseTomlTest.FlatToml));
30
+ }
31
+
32
+ function deserializeFlatTomlArray (string memory toml , string memory path )
33
+ internal
34
+ pure
35
+ returns (ParseTomlTest.FlatToml[] memory )
36
+ {
37
+ return abi.decode (vm.parseTomlTypeArray (toml, path, schema_FlatToml), (ParseTomlTest.FlatToml[]));
38
+ }
39
+
40
+ function deserializeNestedToml (string memory toml ) internal pure returns (ParseTomlTest.NestedToml memory ) {
41
+ return abi.decode (vm.parseTomlType (toml, schema_NestedToml), (ParseTomlTest.NestedToml));
42
+ }
43
+
44
+ function deserializeNestedToml (string memory toml , string memory path )
45
+ internal
46
+ pure
47
+ returns (ParseTomlTest.NestedToml memory )
48
+ {
49
+ return abi.decode (vm.parseTomlType (toml, path, schema_NestedToml), (ParseTomlTest.NestedToml));
50
+ }
51
+
52
+ function deserializeNestedTomlArray (string memory toml , string memory path )
53
+ internal
54
+ pure
55
+ returns (ParseTomlTest.NestedToml[] memory )
56
+ {
57
+ return abi.decode (vm.parseTomlType (toml, path, schema_NestedToml), (ParseTomlTest.NestedToml[]));
58
+ }
59
+ }
60
+
8
61
contract ParseTomlTest is DSTest {
62
+ using TomlStructs for * ;
63
+
64
+ struct FlatToml {
65
+ uint256 a;
66
+ int24 [][] arr;
67
+ string str;
68
+ bytes b;
69
+ address addr;
70
+ bytes32 fixedBytes;
71
+ }
72
+
73
+ struct AnotherFlatToml {
74
+ bytes4 fixedBytes;
75
+ }
76
+
77
+ struct NestedToml {
78
+ FlatToml[] members;
79
+ AnotherFlatToml inner;
80
+ string name;
81
+ }
82
+
9
83
Vm constant vm = Vm (HEVM_ADDRESS);
10
84
string toml;
11
85
@@ -169,20 +243,20 @@ contract ParseTomlTest is DSTest {
169
243
assertEq (bytesArray[1 ], hex "02 " );
170
244
}
171
245
172
- struct Nested {
246
+ struct NestedStruct {
173
247
uint256 number;
174
248
string str;
175
249
}
176
250
177
251
function test_nestedObject () public {
178
252
bytes memory data = vm.parseToml (toml, ".nestedObject " );
179
- Nested memory nested = abi.decode (data, (Nested ));
253
+ NestedStruct memory nested = abi.decode (data, (NestedStruct ));
180
254
assertEq (nested.number, 9223372036854775807 ); // TOML is limited to 64-bit integers
181
255
assertEq (nested.str, "NEST " );
182
256
}
183
257
184
- function test_advancedJsonPath () public {
185
- bytes memory data = vm.parseToml (toml, ".advancedJsonPath [*].id " );
258
+ function test_advancedTomlPath () public {
259
+ bytes memory data = vm.parseToml (toml, ".advancedTomlPath [*].id " );
186
260
uint256 [] memory numbers = abi.decode (data, (uint256 []));
187
261
assertEq (numbers[0 ], 1 );
188
262
assertEq (numbers[1 ], 2 );
@@ -225,6 +299,36 @@ contract ParseTomlTest is DSTest {
225
299
vm._expectCheatcodeRevert ("key \" .* \" must return exactly one JSON object " );
226
300
vm.parseTomlKeys (tomlString, ".* " );
227
301
}
302
+
303
+ // forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^FlatToml
304
+ string constant schema_FlatToml =
305
+ "FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes) " ;
306
+
307
+ // forge eip712 testdata/default/cheats/Toml.t.sol -R 'cheats=testdata/cheats' -R 'ds-test=testdata/lib/ds-test/src' | grep ^NestedToml
308
+ string constant schema_NestedToml =
309
+ "NestedToml(FlatToml[] members,AnotherFlatToml inner,string name)AnotherFlatToml(bytes4 fixedBytes)FlatToml(uint256 a,int24[][] arr,string str,bytes b,address addr,bytes32 fixedBytes) " ;
310
+
311
+ function test_parseTomlType () public {
312
+ string memory readToml = vm.readFile ("fixtures/Toml/nested_toml_struct.toml " );
313
+ NestedToml memory data = readToml.deserializeNestedToml ();
314
+ assertEq (data.members.length , 2 );
315
+
316
+ FlatToml memory expected = FlatToml ({
317
+ a: 200 ,
318
+ arr: new int24 [][](0 ),
319
+ str: "some other string " ,
320
+ b: hex "0000000000000000000000000000000000000000 " ,
321
+ addr: 0x167D91deaEEE3021161502873d3bcc6291081648 ,
322
+ fixedBytes: 0xed1c7beb1f00feaaaec5636950d6edb25a8d4fedc8deb2711287b64c4d27719d
323
+ });
324
+
325
+ assertEq (keccak256 (abi.encode (data.members[1 ])), keccak256 (abi.encode (expected)));
326
+ assertEq (bytes32 (data.inner.fixedBytes), bytes32 (bytes4 (0x12345678 )));
327
+
328
+ FlatToml[] memory members = TomlStructs.deserializeFlatTomlArray (readToml, ".members " );
329
+
330
+ assertEq (keccak256 (abi.encode (members)), keccak256 (abi.encode (data.members)));
331
+ }
228
332
}
229
333
230
334
contract WriteTomlTest is DSTest {
@@ -238,18 +342,18 @@ contract WriteTomlTest is DSTest {
238
342
json2 = "example2 " ;
239
343
}
240
344
241
- struct simpleJson {
345
+ struct simpleStruct {
242
346
uint256 a;
243
347
string b;
244
348
}
245
349
246
- struct notSimpleJson {
350
+ struct nestedStruct {
247
351
uint256 a;
248
352
string b;
249
- simpleJson c;
353
+ simpleStruct c;
250
354
}
251
355
252
- function test_serializeNotSimpleToml () public {
356
+ function test_serializeNestedStructToml () public {
253
357
string memory json3 = "json3 " ;
254
358
string memory path = "fixtures/Toml/write_complex_test.toml " ;
255
359
vm.serializeUint (json3, "a " , uint256 (123 ));
@@ -259,14 +363,16 @@ contract WriteTomlTest is DSTest {
259
363
vm.writeToml (finalJson, path);
260
364
string memory toml = vm.readFile (path);
261
365
bytes memory data = vm.parseToml (toml);
262
- notSimpleJson memory decodedData = abi.decode (data, (notSimpleJson));
366
+ nestedStruct memory decodedData = abi.decode (data, (nestedStruct));
367
+ console.log (decodedData.a);
368
+ assertEq (decodedData.a, 123 );
263
369
}
264
370
265
371
function test_retrieveEntireToml () public {
266
372
string memory path = "fixtures/Toml/write_complex_test.toml " ;
267
373
string memory toml = vm.readFile (path);
268
374
bytes memory data = vm.parseToml (toml, ". " );
269
- notSimpleJson memory decodedData = abi.decode (data, (notSimpleJson ));
375
+ nestedStruct memory decodedData = abi.decode (data, (nestedStruct ));
270
376
console.log (decodedData.a);
271
377
assertEq (decodedData.a, 123 );
272
378
}
@@ -294,7 +400,7 @@ contract WriteTomlTest is DSTest {
294
400
295
401
string memory toml = vm.readFile (path);
296
402
bytes memory data = vm.parseToml (toml);
297
- simpleJson memory decodedData = abi.decode (data, (simpleJson ));
403
+ simpleStruct memory decodedData = abi.decode (data, (simpleStruct ));
298
404
assertEq (decodedData.a, 123 );
299
405
assertEq (decodedData.b, "test " );
300
406
@@ -303,7 +409,7 @@ contract WriteTomlTest is DSTest {
303
409
// read again
304
410
toml = vm.readFile (path);
305
411
data = vm.parseToml (toml, ".b " );
306
- decodedData = abi.decode (data, (simpleJson ));
412
+ decodedData = abi.decode (data, (simpleStruct ));
307
413
assertEq (decodedData.a, 123 );
308
414
assertEq (decodedData.b, "test " );
309
415
0 commit comments