Skip to content

Commit

Permalink
Adding tests for the default values of all types in Solidity
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov committed Oct 18, 2023
1 parent 6000257 commit 9593407
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
104 changes: 104 additions & 0 deletions contracts/solidity/defaults/Defaults.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

contract Defaults {
struct UintDefaults {
uint uInt;
uint8 uInt8;
uint16 uInt16;
uint32 uInt32;
uint64 uInt64;
uint128 uInt128;
uint256 uInt256;
}

struct IntDefaults {
int intDef;
int8 intDef8;
int16 intDef16;
int32 intDef32;
int64 intDef64;
int128 intDef128;
int256 intDef256;
}

// struct FixedDefaults {
// fixed fixedVar;
// fixed8x18 fixed8x18Var;
// fixed16x12 fixed16x12Var;
// fixed32x10 fixed32x10Var;
// fixed64x8 fixed64x8Var;
// fixed128x6 fixed128x6Var;
// fixed256x4 fixed256x4Var;
// }

// struct UFixedDefaults {
// ufixed ufixedVar;
// ufixed8x18 ufixed8x18Var;
// ufixed16x12 ufixed16x12Var;
// ufixed32x10 ufixed32x10Var;
// ufixed64x8 ufixed64x8Var;
// ufixed128x6 ufixed128x6Var;
// ufixed256x4 ufixed256x4Var;
// }

struct BytesDefaults {
bytes3 bytesDef3;
bytes10 bytesDef10;
bytes15 bytesDef15;
bytes20 bytesDef20;
bytes25 bytesDef25;
bytes30 bytesDef30;
bytes32 bytesDef32;
}

struct ArrayDefaults {
string[] strArr;
uint[] uintArr;
bool[] boolArr;
bytes[] bytesArr;
}

function getUintDefaults() external pure returns (UintDefaults memory) {
UintDefaults memory defaults;
return defaults;
}

function getIntDefaults() external pure returns (UintDefaults memory) {
UintDefaults memory defaults;
return defaults;
}

// Not supported by solidity yet
// function getFixedDefaults() external pure returns (FixedDefaults memory) {
// FixedDefaults memory defaults;
// return defaults;
// }

// Not supported by solidity yet
// function getUFixedDefaults() external pure returns (UFixedDefaults memory) {
// UFixedDefaults memory defaults;
// return defaults;
// }

function getBytesDefaults() external pure returns (BytesDefaults memory) {
BytesDefaults memory defaults;
return defaults;
}

function getStringDefaults() external pure returns (string memory) {
string memory defaults;
return defaults;
}

function getArrayDefaults() external pure returns (ArrayDefaults memory) {
ArrayDefaults memory defaults;
return defaults;
}

function getAddressDefaults() external pure returns (address) {
address defaults;
return defaults;
}

}
1 change: 1 addition & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const Contract = {
Transaction: 'Transaction',
MessageFrameAddresses: 'MessageFrameAddresses',
New: 'New',
Defaults: "Defaults",
}

const CALL_EXCEPTION = 'CALL_EXCEPTION'
Expand Down
81 changes: 81 additions & 0 deletions test/solidity/defaults/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { expect } = require('chai')
const { ethers } = require('hardhat')
const Constants = require('../../constants')

describe('Solidity Defaults', function () {
let signers
let contract

before(async function () {
signers = await ethers.getSigners()

const factory = await ethers.getContractFactory(Constants.Contract.Defaults)
contract = await factory.deploy()
})

it('confirm solidity functionality: uint defaults', async function () {
const res = await contract.getUintDefaults()
expect(res.uInt8).to.equal(0)
expect(res.uInt16).to.equal(0)
expect(res.uInt32).to.equal(0)
expect(res.uInt64).to.equal(ethers.BigNumber.from(0))
expect(res.uInt128).to.equal(ethers.BigNumber.from(0))
expect(res.uInt256).to.equal(ethers.BigNumber.from(0))
expect(res.uInt).to.equal(ethers.BigNumber.from(0))
})

it('confirm solidity functionality: int defaults', async function () {
const res = await contract.getIntDefaults()
expect(res.uInt8).to.equal(0)
expect(res.uInt16).to.equal(0)
expect(res.uInt32).to.equal(0)
expect(res.uInt64).to.equal(ethers.BigNumber.from(0))
expect(res.uInt128).to.equal(ethers.BigNumber.from(0))
expect(res.uInt256).to.equal(ethers.BigNumber.from(0))
expect(res.uInt).to.equal(ethers.BigNumber.from(0))
})

// Not supported by solidity yet
xit('confirm solidity functionality: fixed defaults', async function () {
const res = await contract.getFixedDefaults()
})

// Not supported by solidity yet
xit('confirm solidity functionality: ufixed defaults', async function () {
const res = await contract.getUFixedDefaults()
})

it('confirm solidity functionality: bytes defaults', async function () {
const res = await contract.getBytesDefaults()
expect(res.bytesDef3).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 3))
expect(res.bytesDef10).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 10))
expect(res.bytesDef15).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 15))
expect(res.bytesDef20).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 20))
expect(res.bytesDef25).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 25))
expect(res.bytesDef30).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 30))
expect(res.bytesDef32).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 32))
})

it('confirm solidity functionality: string defaults', async function () {
const res = await contract.getStringDefaults()
expect(res).to.equal('')
})

it('confirm solidity functionality: array defaults', async function () {
const res = await contract.getArrayDefaults()
expect(Array.isArray(res.strArr)).to.be.true
expect(Array.isArray(res.uintArr)).to.be.true
expect(Array.isArray(res.boolArr)).to.be.true
expect(Array.isArray(res.bytesArr)).to.be.true
})

it('confirm solidity functionality: address defaults', async function () {
const res = await contract.getAddressDefaults()
expect(res).to.equal(ethers.utils.hexZeroPad(ethers.utils.hexlify(0), 20))
})

// mapping cannot be returned from solidity
xit('confirm solidity functionality: mapping', async function () {

})
})

0 comments on commit 9593407

Please sign in to comment.