Skip to content

Commit

Permalink
Adding tests for inheritance
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 17, 2023
1 parent 6000257 commit 2bc1040
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
13 changes: 13 additions & 0 deletions contracts/solidity/inhetitance/Base.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

contract Base {
receive() external payable {}
function classIdentifier() public pure virtual returns (string memory) {
return "Base";
}

function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
23 changes: 23 additions & 0 deletions contracts/solidity/inhetitance/Main.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import { Base } from "./Base.sol";

contract Main is Base {
function classIdentifier() public pure override(Base) returns (string memory) {
return "Main";
}

function returnThis() public view returns (Main) {
return this;
}

function returnSuper() public view virtual returns (string memory) {
return super.classIdentifier();
}

function destroyContract(address recipient) public returns (bool) {
selfdestruct(payable(recipient));

return true;
}
}
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',
Inheritance: 'Inheritance',
}

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

describe('Crypto Inheritance tests', function () {
let signers, contractMain, contractMainAddr, contractBase, contractBaseAddr, wallet
const TOP_UP_AMOUNT = ethers.utils.parseEther('0.000001');

before(async function () {
signers = await ethers.getSigners()
wallet = signers[0];

const factoryMain = await ethers.getContractFactory('Main')
contractMain = await factoryMain.deploy()
await contractMain.deployed()
contractMainAddr = contractMain.address

const factoryBase = await ethers.getContractFactory('Base')
contractBase = await factoryBase.deploy()
await contractBase.deployed()
contractBaseAddr = contractBase.address

//top up the test contract with some funds
const tx = {
to: contractMainAddr,
value: TOP_UP_AMOUNT
}
const topUpRes = await wallet.sendTransaction(tx)
await topUpRes.wait();
})

it("this (current contract's type)", async function () {
const mainThis = await contractMain.returnThis()

expect(mainThis).to.equal(contractMainAddr)
})

it('super', async function () {
const res = await contractMain.classIdentifier()

expect(res).to.equal("Main")
})

it('selfdestruct(address payable recipient)', async function () {
const balanceBaseInitial = await contractBase.getBalance()
expect(balanceBaseInitial).to.be.equal(0)

const tx = await contractMain.destroyContract(contractBaseAddr)
tx.wait()
const balanceBaseFinal = await contractBase.getBalance()

expect(balanceBaseFinal.gt(balanceBaseInitial)).to.be.true
})

})

0 comments on commit 2bc1040

Please sign in to comment.