Skip to content

Commit a4deb4f

Browse files
authored
StorageReadable unit tests (#154)
Foundry unit tests for StorageReadable contract. Closes #146.
1 parent 6f1a7e9 commit a4deb4f

File tree

2 files changed

+59
-84
lines changed

2 files changed

+59
-84
lines changed

test/reader/StorageReadable.spec.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

test/reader/StorageReadable.t.sol

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
pragma solidity >=0.7.6 <0.9.0;
3+
pragma abicoder v2;
4+
5+
import {Test} from "forge-std/Test.sol";
6+
import {StorageAccessibleWrapper} from "src/contracts/test/vendor/StorageAccessibleWrapper.sol";
7+
8+
contract StorageReadableTest is Test {
9+
StorageAccessibleWrapper instance;
10+
11+
function setUp() public {
12+
instance = new StorageAccessibleWrapper();
13+
}
14+
15+
function test_can_read_statically_sized_words() public {
16+
instance.setFoo(42);
17+
bytes memory actualBytes = instance.getStorageAt(instance.SLOT_FOO(), 1);
18+
bytes memory expectedBytes = abi.encode(42);
19+
assertEq(actualBytes, expectedBytes);
20+
}
21+
22+
function test_can_read_fields_that_are_packed_into_single_storage_slot() public {
23+
instance.setBar(7);
24+
instance.setBam(13);
25+
bytes memory actualBytes = instance.getStorageAt(instance.SLOT_BAR(), 1);
26+
bytes memory expectedBytes = abi.encodePacked(new bytes(8), uint64(13), uint128(7));
27+
assertEq(actualBytes, expectedBytes);
28+
}
29+
30+
function test_can_read_arrays_in_one_go() public {
31+
uint8 slot = instance.SLOT_BAZ();
32+
uint256[] memory arr = new uint256[](2);
33+
arr[0] = 42;
34+
arr[1] = 1337;
35+
instance.setBaz(arr);
36+
bytes memory data = instance.getStorageAt(slot, 1);
37+
uint256 length = abi.decode(data, (uint256));
38+
assertEq(length, 2);
39+
bytes memory packed = instance.getStorageAt(uint256(keccak256(abi.encode(slot))), length);
40+
(uint256 firstValue, uint256 secondValue) = abi.decode(packed, (uint256, uint256));
41+
assertEq(firstValue, 42);
42+
assertEq(secondValue, 1337);
43+
}
44+
45+
function test_can_read_mappings() public {
46+
instance.setQuxKeyValue(42, 69);
47+
bytes memory data = instance.getStorageAt(uint256(keccak256(abi.encode([42, instance.SLOT_QUX()]))), 1);
48+
uint256 value = abi.decode(data, (uint256));
49+
assertEq(value, 69);
50+
}
51+
52+
function test_can_read_structs() public {
53+
instance.setFoobar(19, 21);
54+
bytes memory packed = instance.getStorageAt(instance.SLOT_FOOBAR(), 10);
55+
(uint256 firstValue, uint256 secondValue) = abi.decode(packed, (uint256, uint256));
56+
assertEq(firstValue, 19);
57+
assertEq(secondValue, 21);
58+
}
59+
}

0 commit comments

Comments
 (0)