Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get struct in map and add coverage #189

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/contracts/mock/StorageGetter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ struct PackedStruct {
address packedE;
}

struct OtherPackedStruct {
address packedA;
bytes12 packedB;
}

contract StorageGetter {
uint16 _packedUintA;
uint16 _packedUintB;
Expand All @@ -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;
Expand Down Expand Up @@ -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];
}
}
51 changes: 51 additions & 0 deletions test/unit/mock/readable-storage-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Loading