Add tests for nested struct. (#59)

Co-authored-by: nikugogoi <95nikass@gmail.com>
This commit is contained in:
Ashwin Phatak 2021-06-14 19:57:29 +05:30 committed by GitHub
parent eb20708faf
commit 3815853f7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 117 additions and 18 deletions

View File

@ -59,6 +59,10 @@ $ yarn test
* [x] Value Types
* [x] Get value of a single member in struct
* [ ] Reference Types
* [x] Struct type members (nested)
* [ ] Array type members
* [ ] Bytes and string type members
* [ ] Mapping type members
* [ ] Mapping Types
* [x] Value Type keys
* [ ] Fixed-size byte array keys

View File

@ -596,13 +596,11 @@ describe('Get value from storage', () => {
describe('structs with reference type members', () => {
let testReferenceStructs: Contract, storageLayout: StorageLayout;
let fixedArrayStruct: {[key: string]: any};
const stringStruct = {
string1: 'string1',
uint1: BigInt(123),
string2: 'string2'
};
let fixedArrayStruct: {[key: string]: any},
bytesStruct: {[key: string]: any},
stringStruct: {[key: string]: any},
nestedStruct: {[key: string]: any};
before(async () => {
const TestReferenceStructs = await ethers.getContractFactory('TestReferenceStructs');
@ -617,6 +615,26 @@ describe('Get value from storage', () => {
uintArray: [1, 2, 3, 4].map(el => BigInt(el)),
addressArray: signers.slice(0, 3).map(signer => signer.address.toLowerCase())
};
bytesStruct = {
byteArray: ethers.utils.hexlify(ethers.utils.randomBytes(40)),
address1: signers[1].address.toLowerCase(),
uint1: BigInt(1234)
};
stringStruct = {
string1: 'string1',
int1: BigInt(123),
uint1: BigInt(456),
string2: 'string2',
address1: signers[2].address.toLowerCase(),
bool1: false
};
nestedStruct = {
bytesStruct,
address1: signers[3].address.toLowerCase()
};
});
// Get all members of a struct.
@ -627,13 +645,27 @@ describe('Get value from storage', () => {
expect(value).to.eql(fixedArrayStruct);
});
it('get value for struct with dynamically sized byte members', async () => {
await testReferenceStructs.setBytesStruct(bytesStruct);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'bytesStruct');
expect(value).to.eql(bytesStruct);
});
it('get value for struct with string type members', async () => {
await testReferenceStructs.setStringStruct(stringStruct.string1, stringStruct.uint1, stringStruct.string2);
await testReferenceStructs.setStringStruct(stringStruct);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'stringStruct');
expect(value).to.eql(stringStruct);
});
it('get value for nested struct with struct type members', async () => {
await testReferenceStructs.setNestedStruct(nestedStruct);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct');
expect(value).to.eql(nestedStruct);
});
// Get value of a member in a struct
it('get value of fixed-size array member in a struct', async () => {
const member = 'uintArray';
@ -643,9 +675,17 @@ describe('Get value from storage', () => {
expect(value).to.eql(fixedArrayStruct[member]);
});
it('get value of bytes member in a struct', async () => {
const member = 'byteArray';
await testReferenceStructs.setBytesStruct(bytesStruct);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'bytesStruct', member);
expect(value).to.equal(bytesStruct[member]);
});
it('get value of string member in a struct', async () => {
const member = 'string2';
await testReferenceStructs.setStringStruct(stringStruct.string1, stringStruct.uint1, stringStruct.string2);
await testReferenceStructs.setStringStruct(stringStruct);
const blockHash = await getBlockHash();
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'stringStruct', member);
expect(value).to.eql(stringStruct[member]);
@ -686,6 +726,23 @@ describe('Get value from storage', () => {
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'referenceMappingStruct', member, stringKey));
expect(value).to.equal(referenceMappingStruct[member].get(stringKey));
});
it('get value of nested struct member', async () => {
await testReferenceStructs.setNestedStruct(nestedStruct);
const blockHash = await getBlockHash();
const member = 'bytesStruct';
let { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct', member);
expect(value).to.eql(nestedStruct[member]);
// Get value inside the nested struct member.
let nestedMember = 'address1';
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct', member, nestedMember));
expect(value).to.eql(nestedStruct[member][nestedMember]);
nestedMember = 'byteArray';
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct', member, nestedMember));
expect(value).to.eql(nestedStruct[member][nestedMember]);
});
});
describe('basic mapping type', () => {
@ -774,10 +831,13 @@ describe('Get value from storage', () => {
// Tests for reference type values.
it('get value for mapping with struct type values', async () => {
const expectedValue = {
const [signer1] = await ethers.getSigners();
const expectedValue: {[key: string]: any} = {
uint1: BigInt(123),
int1: BigInt(456),
bool1: true
bool1: true,
address1: signer1.address.toLowerCase()
};
const mapKey = 123;
@ -787,16 +847,23 @@ describe('Get value from storage', () => {
expect(value).to.eql(expectedValue);
// Get value of specified struct member in mapping.
const structMember = 'bool1';
let structMember = 'bool1';
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testMappingTypes.address, 'intStructMap', mapKey, structMember));
expect(value).to.equal(expectedValue[structMember]);
structMember = 'address1';
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testMappingTypes.address, 'intStructMap', mapKey, structMember));
expect(value).to.equal(expectedValue[structMember]);
});
it('get value for mapping of fixed size bytes keys and struct type values', async () => {
const [signer1] = await ethers.getSigners();
const expectedValue = {
uint1: BigInt(123),
int1: BigInt(456),
bool1: true
bool1: true,
address1: signer1.address.toLowerCase()
};
const mapKey = ethers.utils.hexlify(ethers.utils.randomBytes(32));

View File

@ -35,6 +35,7 @@ contract TestBasicMapping {
uint128 uint1;
int56 int1;
bool bool1;
address address1;
}
// Mapping with signed integer as keys and struct type values.

View File

@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma solidity ^0.7.6;
pragma abicoder v2;
contract TestReferenceStructs {
struct FixedArrayStruct {
@ -10,10 +11,21 @@ contract TestReferenceStructs {
FixedArrayStruct fixedArrayStruct;
struct BytesStruct {
bytes byteArray;
address address1;
uint256 uint1;
}
BytesStruct bytesStruct;
struct StringStruct {
string string1;
uint8 uint1;
uint24 uint1;
string string2;
address address1;
bool bool1;
int24 int1;
}
StringStruct stringStruct;
@ -33,6 +45,13 @@ contract TestReferenceStructs {
ReferenceMappingStruct referenceMappingStruct;
struct NestedStruct {
BytesStruct bytesStruct;
address address1;
}
NestedStruct nestedStruct;
// Set variable fixedArrayStruct.
function setFixedArrayStruct(int8 int1Value, uint16[4] calldata uintArrayValue, address[3] calldata addressArrayValue) external {
fixedArrayStruct.int1 = int1Value;
@ -40,11 +59,14 @@ contract TestReferenceStructs {
fixedArrayStruct.addressArray = addressArrayValue;
}
// Set variable bytesStruct.
function setBytesStruct(BytesStruct calldata value) external {
bytesStruct = value;
}
// Set variable stringStruct.
function setStringStruct(string calldata string1Value, uint8 uint1Value, string calldata string2Value) external {
stringStruct.string1 = string1Value;
stringStruct.uint1 = uint1Value;
stringStruct.string2 = string2Value;
function setStringStruct(StringStruct calldata value) external {
stringStruct = value;
}
// Set variable valueMappingStruct.
@ -59,4 +81,9 @@ contract TestReferenceStructs {
referenceMappingStruct.bytesAddressMap[bytesAddressKey] = bytesAddressValue;
referenceMappingStruct.stringUintMap[stringUintKey] = stringUintValue;
}
// Set variable nestedStruct.
function setNestedStruct(NestedStruct calldata value) external {
nestedStruct = value;
}
}