tests: refactor solidity test cases (#249)

* Refactor

* add script to run all tests

* Spawn ethermintd in node script

* Update README

* kill process when test finished

* add new test case

* add yarn.lock inside tests to be tracked
This commit is contained in:
Yijia Su
2021-07-12 05:22:20 -04:00
committed by GitHub
parent 0020e4f2cd
commit fa77bae105
16 changed files with 15318 additions and 184 deletions
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
*/
contract EventTest {
uint256 number;
event ValueStored1(
uint value1
);
event ValueStored2(
string msg,
uint value1
);
event ValueStored3(
string msg,
uint indexed value1,
uint value2
);
function store(uint256 num) public {
number = num;
}
function storeWithEvent(uint256 num) public {
number = num;
emit ValueStored1(num);
emit ValueStored2("TestMsg", num);
emit ValueStored3("TestMsg", num, num);
}
}
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
*/
contract Storage {
uint256 number;
/**
* @dev Store value in variable
* @param num value to store
*/
function store(uint256 num) public {
number = num;
}
/**
* @dev Return value
* @return value of 'number'
*/
function retrieve() public view returns (uint256){
return number;
}
function shouldRevert() public {
require(false, 'This must REVERT');
}
}
@@ -0,0 +1,17 @@
{
"name": "eth-call-test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@truffle/hdwallet-provider": "^1.4.1",
"concurrently": "^6.2.0",
"ganache-cli": "^6.12.2",
"truffle": "^5.3.3",
"truffle-assertions": "^0.9.2"
},
"scripts": {
"test-ganache": "yarn truffle test",
"test-ethermint": "yarn truffle test --network ethermint"
}
}
@@ -0,0 +1,30 @@
const Storage = artifacts.require('Storage');
contract('Test Storage Contract', async function (accounts) {
let storageInstance;
before(function () {
console.log(`Using Accounts (${accounts.length}): \n${accounts.join('\n')}`);
console.log('==========================\n');
})
it('should deploy Stroage contract', async function () {
storageInstance = await Storage.new();
console.log(`Deployed Storage at: ${storageInstance.address}`);
expect(storageInstance.address).not.to.be.undefined;
});
it('should succesfully stored a value', async function () {
const tx = await storageInstance.store(888);
console.log(`Stored value 888 by tx: ${tx.tx}`);
expect(tx.tx).not.to.be.undefined;
});
it('should succesfully retrieve a value', async function () {
const value = await storageInstance.retrieve();
expect(value.toString()).to.equal('888');
});
})
@@ -0,0 +1,34 @@
const Storage = artifacts.require('Storage');
async function expectRevert(promise) {
try {
await promise;
} catch (error) {
if (error.message.indexOf('revert') === -1) {
expect('revert').to.equal(error.message, 'Wrong kind of exception received');
}
return;
}
expect.fail('Expected an exception but none was received');
}
contract('Test EVM Revert', async function (accounts) {
before(function () {
console.log(`Using Accounts (${accounts.length}): \n${accounts.join('\n')}`);
console.log('==========================\n');
})
let storageInstance;
it('should deploy Stroage contract', async function () {
storageInstance = await Storage.new();
console.log(`Deployed Storage at: ${storageInstance.address}`);
expect(storageInstance.address).not.to.be.undefined;
});
it('should revert when call `shouldRevert()`', async function () {
await expectRevert(storageInstance.shouldRevert());
});
})
@@ -0,0 +1,33 @@
const EventTest = artifacts.require('EventTest');
const truffleAssert = require('truffle-assertions');
contract('Test EventTest Contract', async function (accounts) {
let eventInstance;
before(function () {
console.log(`Using Accounts (${accounts.length}): \n${accounts.join('\n')}`);
console.log('==========================\n');
})
it('should deploy EventTest contract', async function () {
eventInstance = await EventTest.new();
console.log(`Deployed EventTest at: ${eventInstance.address}`);
expect(eventInstance.address).not.to.be.undefined;
});
it('should emit events', async function () {
const tx = await eventInstance.storeWithEvent(888);
truffleAssert.eventEmitted(tx, 'ValueStored1', events => {
return events['0'].toString() === '888';
});
truffleAssert.eventEmitted(tx, 'ValueStored2', events => {
return events['0'].toString() === 'TestMsg' && events['1'].toString() === '888';
});
truffleAssert.eventEmitted(tx, 'ValueStored3', events => {
return events['0'].toString() === 'TestMsg' && events['1'].toString() === '888' && events['2'].toString() === '888';
});
});
})
@@ -0,0 +1,17 @@
module.exports = {
networks: {
// Development network is just left as truffle's default settings
ethermint: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
gas: 5000000, // Gas sent with each transaction
gasPrice: 1000000000, // 1 gwei (in wei)
},
},
compilers: {
solc: {
version: "0.8.3",
},
},
}