-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathJson.t.sol
267 lines (228 loc) · 9.44 KB
/
Json.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
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;
import "ds-test/test.sol";
import "./Cheats.sol";
import "../logs/console.sol";
contract ParseJson is DSTest {
Cheats constant cheats = Cheats(HEVM_ADDRESS);
string json;
function setUp() public {
string memory path = "../testdata/fixtures/Json/test.json";
json = cheats.readFile(path);
}
function test_uintArray() public {
bytes memory data = cheats.parseJson(json, ".uintArray");
uint256[] memory decodedData = abi.decode(data, (uint256[]));
assertEq(42, decodedData[0]);
assertEq(43, decodedData[1]);
}
function test_str() public {
bytes memory data = cheats.parseJson(json, ".str");
string memory decodedData = abi.decode(data, (string));
assertEq("hai", decodedData);
}
function test_strArray() public {
bytes memory data = cheats.parseJson(json, ".strArray");
string[] memory decodedData = abi.decode(data, (string[]));
assertEq("hai", decodedData[0]);
assertEq("there", decodedData[1]);
}
function test_bool() public {
bytes memory data = cheats.parseJson(json, ".bool");
bool decodedData = abi.decode(data, (bool));
assertTrue(decodedData);
}
function test_boolArray() public {
bytes memory data = cheats.parseJson(json, ".boolArray");
bool[] memory decodedData = abi.decode(data, (bool[]));
assertTrue(decodedData[0]);
assertTrue(!decodedData[1]);
}
function test_address() public {
bytes memory data = cheats.parseJson(json, ".address");
address decodedData = abi.decode(data, (address));
assertEq(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, decodedData);
}
function test_addressArray() public {
bytes memory data = cheats.parseJson(json, ".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(cheats.parseJson(json, ".H160NotAddress"), (string));
assertEq("0000000000000000000000000000000000001337", data);
}
struct Nested {
uint256 number;
string str;
}
function test_nestedObject() public {
bytes memory data = cheats.parseJson(json, ".nestedObject");
Nested memory nested = abi.decode(data, (Nested));
assertEq(nested.number, 115792089237316195423570985008687907853269984665640564039457584007913129639935);
assertEq(nested.str, "NEST");
}
struct Whole {
string str;
string[] strArray;
uint256[] uintArray;
}
function test_wholeObject() public {
// we need to make the path relative to the crate that's running tests for it (forge crate)
string memory path = "../testdata/fixtures/Json/wholeJson.json";
console.log(path);
json = cheats.readFile(path);
bytes memory data = cheats.parseJson(json);
Whole memory whole = abi.decode(data, (Whole));
assertEq(whole.str, "hai");
assertEq(whole.uintArray[0], 42);
assertEq(whole.uintArray[1], 43);
assertEq(whole.strArray[0], "hai");
assertEq(whole.strArray[1], "there");
}
function test_coercionRevert() public {
cheats.expectRevert(
"You can only coerce values or arrays, not JSON objects. The key '.nestedObject' returns an object"
);
uint256 number = cheats.parseJsonUint(json, ".nestedObject");
}
function test_coercionUint() public {
uint256 number = cheats.parseJsonUint(json, ".hexUint");
assertEq(number, 1231232);
number = cheats.parseJsonUint(json, ".stringUint");
assertEq(number, 115792089237316195423570985008687907853269984665640564039457584007913129639935);
number = cheats.parseJsonUint(json, ".numberUint");
assertEq(number, 115792089237316195423570985008687907853269984665640564039457584007913129639935);
uint256[] memory numbers = cheats.parseJsonUintArray(json, ".arrayUint");
assertEq(numbers[0], 1231232);
assertEq(numbers[1], 1231232);
assertEq(numbers[2], 1231232);
}
function test_coercionInt() public {
int256 number = cheats.parseJsonInt(json, ".hexInt");
assertEq(number, -12);
number = cheats.parseJsonInt(json, ".stringInt");
assertEq(number, -12);
}
function test_coercionBool() public {
bool boolean = cheats.parseJsonBool(json, ".booleanString");
assertEq(boolean, true);
bool[] memory booleans = cheats.parseJsonBoolArray(json, ".booleanArray");
assert(booleans[0]);
assert(!booleans[1]);
}
function test_advancedJsonPath() public {
bytes memory data = cheats.parseJson(json, ".advancedJsonPath[*].id");
uint256[] memory numbers = abi.decode(data, (uint256[]));
assertEq(numbers[0], 1);
assertEq(numbers[1], 2);
}
function test_canonicalizePath() public {
bytes memory data = cheats.parseJson(json, "$.str");
string memory decodedData = abi.decode(data, (string));
assertEq("hai", decodedData);
}
}
contract WriteJson is DSTest {
Cheats constant vm = Cheats(HEVM_ADDRESS);
string json1;
string json2;
function setUp() public {
json1 = "example";
json2 = "example2";
}
function test_serializeJson() public {
vm.serializeUint(json1, "key1", uint256(254));
vm.serializeBool(json1, "boolean", true);
vm.serializeInt(json2, "key2", -234);
vm.serializeUint(json2, "deploy", uint256(254));
string memory data = vm.serializeBool(json2, "boolean", true);
vm.serializeString(json2, "json1", data);
emit log(data);
}
function test_serializeArray() public {
bool[] memory data1 = new bool[](3);
data1[0] = true;
data1[2] = false;
vm.serializeBool(json1, "array1", data1);
address[] memory data2 = new address[](3);
data2[0] = address(0xBEEEF);
data2[2] = vm.addr(123);
vm.serializeAddress(json1, "array2", data2);
bytes[] memory data3 = new bytes[](3);
data3[0] = bytes("123");
data3[2] = bytes("fpovhpgjaiosfjhapiufpsdf");
string memory finalJson = vm.serializeBytes(json1, "array3", data3);
string memory path = "../testdata/fixtures/Json/write_test_array.json";
vm.writeJson(finalJson, path);
string memory json = vm.readFile(path);
bytes memory rawData = vm.parseJson(json, ".array1");
bool[] memory parsedData1 = new bool[](3);
parsedData1 = abi.decode(rawData, (bool[]));
assertEq(parsedData1[0], data1[0]);
assertEq(parsedData1[1], data1[1]);
assertEq(parsedData1[2], data1[2]);
rawData = vm.parseJson(json, ".array2");
address[] memory parsedData2 = new address[](3);
parsedData2 = abi.decode(rawData, (address[]));
assertEq(parsedData2[0], data2[0]);
assertEq(parsedData2[1], data2[1]);
assertEq(parsedData2[2], data2[2]);
rawData = vm.parseJson(json, ".array3");
bytes[] memory parsedData3 = new bytes[](3);
parsedData3 = abi.decode(rawData, (bytes[]));
assertEq(parsedData3[0], data3[0]);
assertEq(parsedData3[1], data3[1]);
assertEq(parsedData3[2], data3[2]);
vm.removeFile(path);
}
struct simpleJson {
uint256 a;
string b;
}
struct notSimpleJson {
uint256 a;
string b;
simpleJson c;
}
function test_serializeNotSimpleJson() public {
string memory json3 = "json3";
string memory path = "../testdata/fixtures/Json/write_complex_test.json";
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.writeJson(finalJson, path);
string memory json = vm.readFile(path);
bytes memory data = vm.parseJson(json);
notSimpleJson memory decodedData = abi.decode(data, (notSimpleJson));
}
function test_writeJson() public {
string memory json3 = "json3";
string memory path = "../testdata/fixtures/Json/write_test.json";
vm.serializeUint(json3, "a", uint256(123));
string memory finalJson = vm.serializeString(json3, "b", "test");
vm.writeJson(finalJson, path);
string memory json = vm.readFile(path);
bytes memory data = vm.parseJson(json);
simpleJson memory decodedData = abi.decode(data, (simpleJson));
assertEq(decodedData.a, 123);
assertEq(decodedData.b, "test");
// write json3 to key b
vm.writeJson(finalJson, path, ".b");
// read again
json = vm.readFile(path);
data = vm.parseJson(json, ".b");
decodedData = abi.decode(data, (simpleJson));
assertEq(decodedData.a, 123);
assertEq(decodedData.b, "test");
// replace a single value to key b
address ex = address(0xBEEF);
vm.writeJson(vm.toString(ex), path, ".b");
json = vm.readFile(path);
data = vm.parseJson(json, ".b");
address decodedAddress = abi.decode(data, (address));
assertEq(decodedAddress, ex);
}
}