diff --git a/test/contracts/mock/StorageGetter.sol b/test/contracts/mock/StorageGetter.sol index f7456f7..d1ce36f 100644 --- a/test/contracts/mock/StorageGetter.sol +++ b/test/contracts/mock/StorageGetter.sol @@ -14,6 +14,11 @@ struct PackedStruct { address packedE; } +struct OtherPackedStruct { + address packedA; + bytes12 packedB; +} + contract StorageGetter { uint16 _packedUintA; uint16 _packedUintB; @@ -34,6 +39,9 @@ contract StorageGetter { mapping(address => address) _addressToAddressMap; uint256[] internal _uint256Array; int16[][] internal _int2DArray; + mapping(bytes32 => SimpleStruct) _bytes32ToSimpleStructMap; + mapping(bytes32 => PackedStruct) _bytes32ToPackedStructMap; + mapping(bytes32 => OtherPackedStruct) _bytes32ToOtherPackedStructMap; // Testing storage slot packing. bool internal _packedA; @@ -142,4 +150,16 @@ contract StorageGetter { function getPackedAddress() public view returns (address) { return _packedB; } + + function getBytes32ToSimpleStructMapValue(bytes32 _key) public view returns (SimpleStruct memory _out) { + return _bytes32ToSimpleStructMap[_key]; + } + + function getBytes32ToPackedStructMapValue(bytes32 _key) public view returns (PackedStruct memory _out) { + return _bytes32ToPackedStructMap[_key]; + } + + function getBytes32ToOtherPackedStructMapValue(bytes32 _key) public view returns (OtherPackedStruct memory _out) { + return _bytes32ToOtherPackedStructMap[_key]; + } } diff --git a/test/unit/mock/readable-storage-logic.spec.ts b/test/unit/mock/readable-storage-logic.spec.ts index 535d827..eca921f 100644 --- a/test/unit/mock/readable-storage-logic.spec.ts +++ b/test/unit/mock/readable-storage-logic.spec.ts @@ -150,5 +150,56 @@ describe('Mock: Readable storage logic', () => { const getValue = await mock.getVariable('_uint256Array'); expect(getValue).to.deep.equal(await mock.getUint256Array()); }); + + it('should be able to get values in a bytes32 => SimpleStruct mapping', async () => { + const mapKey = BYTES32_EXAMPLE; + const struct = { + valueA: BigNumber.from(1234), + valueB: true, + }; + await mock.setVariable('_bytes32ToSimpleStructMap', { [mapKey]: struct }); + + const getMockVariable = await mock.getVariable('_bytes32ToSimpleStructMap', [mapKey]); + const getStructView = await mock.getBytes32ToSimpleStructMapValue(mapKey); + expect(getMockVariable).to.deep.equal(struct); + expect(getStructView.valueA).to.deep.equal(struct.valueA); + expect(getStructView.valueB).to.deep.equal(struct.valueB); + }); + + it('should be able to get values in a bytes32 => PackedStruct mapping', async () => { + const mapKey = BYTES32_EXAMPLE; + const struct = { + packedA: BigNumber.from(2), + packedB: BigNumber.from(1), + packedC: BigNumber.from(2), + packedD: BigNumber.from(1), + packedE: ADDRESS_EXAMPLE, + }; + await mock.setVariable('_bytes32ToPackedStructMap', { [mapKey]: struct }); + + const getMockVariable = await mock.getVariable('_bytes32ToPackedStructMap', [mapKey]); + const getStructView = await mock.getBytes32ToPackedStructMapValue(mapKey); + expect(getMockVariable).to.deep.equal(struct); + expect(getStructView.packedA).to.deep.equal(struct.packedA); + expect(getStructView.packedB).to.deep.equal(struct.packedB); + expect(getStructView.packedC).to.deep.equal(struct.packedC); + expect(getStructView.packedD).to.deep.equal(struct.packedD); + expect(getStructView.packedE).to.deep.equal(struct.packedE); + }); + + it('should be able to get values in a bytes32 => OtherPackedStruct mapping', async () => { + const mapKey = BYTES32_EXAMPLE; + const struct = { + packedA: ADDRESS_EXAMPLE, + packedB: '0x000000000000000000000001', + }; + await mock.setVariable('_bytes32ToOtherPackedStructMap', { [mapKey]: struct }); + + const getMockVariable = await mock.getVariable('_bytes32ToOtherPackedStructMap', [mapKey]); + const getStructView = await mock.getBytes32ToOtherPackedStructMapValue(mapKey); + expect(getMockVariable).to.deep.equal(struct); + expect(getStructView.packedA).to.deep.equal(struct.packedA); + expect(getStructView.packedB).to.deep.equal(struct.packedB); + }); }); });