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

Adds tests for parameter restructuring and assignment #498

Merged
merged 5 commits into from
Nov 6, 2023
Merged
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
24 changes: 24 additions & 0 deletions contracts/solidity/assignments/AssignmentReferenceTypes.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.20;

contract AssignmentReferenceTypes {
uint[5] private someArray = [1, 2, 3, 4, 5];

function testAssignmentOfReferenceTypes() external {
testChangeCopy(someArray);
testChangeReference(someArray);
}

function testChangeCopy(uint[5] memory y) internal pure {
y[2] = 8;
}

function testChangeReference(uint[5] storage y) internal {
natanasow marked this conversation as resolved.
Show resolved Hide resolved
y[3] = 10;
}

function getSomeArray() external view returns(uint[5] memory) {
return someArray;
}
}
natanasow marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions contracts/solidity/assignments/DestructuringReturns.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.20;

contract DestructuringReturns {
function f() public pure returns (uint, bool, uint) {
return (7, true, 2);
}

function testDestructuredReturnParams() external pure returns(uint, bool, uint) {
(uint x, bool y, uint z) = f();

return (x, y, z);
}
}
natanasow marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ const Contract = {
Inheritance: 'Inheritance',
ControlStructures: 'ControlStructures',
Defaults: 'Defaults',
AssignmentReferenceTypes: 'AssignmentReferenceTypes',
DestructuringReturns: 'DestructuringReturns',
}

const CALL_EXCEPTION = 'CALL_EXCEPTION'
Expand Down
49 changes: 49 additions & 0 deletions test/solidity/assignments/assignments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

const { expect } = require('chai')
konstantinabl marked this conversation as resolved.
Show resolved Hide resolved
const { ethers } = require('hardhat')
const Constants = require('../../constants')

describe('@solidityequiv1 Test assignments', function () {

before(async function () {
const factoryDestructuring = await ethers.getContractFactory(Constants.Contract.DestructuringReturns)
contractDesctructuring = await factoryDestructuring.deploy()

const factoryReferenceTypes = await ethers.getContractFactory(Constants.Contract.AssignmentReferenceTypes)
contractReferenceTypes = await factoryReferenceTypes.deploy()
})

it('should verify destructuring works', async function () {
const result = await contractDesctructuring.testDestructuredReturnParams();
expect(result).to.deep.equal([ethers.BigNumber.from(7), true, ethers.BigNumber.from(2)])
})

it('should verify assignment of reference types', async function () {
// here we are testing that if a parameter is assigned to memory a copy will be created
// and the original object wont be changed
// while if it is in storage and only referenced we expect it to change
await contractReferenceTypes.testAssignmentOfReferenceTypes();
const result = await contractReferenceTypes.getSomeArray();
expect(result).to.deep.equal([ethers.BigNumber.from(1), ethers.BigNumber.from(2), ethers.BigNumber.from(3),
konstantinabl marked this conversation as resolved.
Show resolved Hide resolved
ethers.BigNumber.from(10), ethers.BigNumber.from(5)])
})
})