custom transaction context support for special functions

pull/7/head
aniket-engg 5 years ago committed by Aniket
parent 29c4f59cc0
commit 742b91bd8f
  1. 35
      remix-tests/src/testRunner.ts
  2. 4
      remix-tests/tests/testRunner.ts
  3. 29
      remix-tests/tests/various_sender/sender_and_value_test.sol

@ -101,6 +101,23 @@ function getTestFunctionsInterface (jsonInterface: FunctionDescription[], funcLi
return functionsInterface
}
/**
* @dev returns ABI of special functions from passed interface
* @param jsonInterface Json Interface
*/
function getSpecialFunctionsInterface (jsonInterface: FunctionDescription[]): Record<string, FunctionDescription> {
const specialFunctionsInterface: Record<string, FunctionDescription> = {}
const funcList: string[] = ['beforeAll', 'beforeEach', 'afterAll', 'afterEach']
for(const func of funcList){
const funcInterface: FunctionDescription | undefined = jsonInterface.find(node => node.type === 'function' && node.name === func)
if(funcInterface) {
specialFunctionsInterface[func] = funcInterface
}
}
return specialFunctionsInterface
}
/**
* @dev Prepare a list of tests to run using test contract file ABI, AST & contract name
* @param jsonInterface File JSON interface
@ -111,25 +128,29 @@ function getTestFunctionsInterface (jsonInterface: FunctionDescription[], funcLi
function createRunList (jsonInterface: FunctionDescription[], fileAST: AstNode, testContractName: string): RunListInterface[] {
const availableFunctions: string[] = getAvailableFunctions(fileAST, testContractName)
const testFunctionsInterface: FunctionDescription[] = getTestFunctionsInterface(jsonInterface, availableFunctions)
const specialFunctionsInterface: Record<string, FunctionDescription> = getSpecialFunctionsInterface(jsonInterface)
let runList: RunListInterface[] = []
if (availableFunctions.indexOf('beforeAll') >= 0) {
runList.push({ name: 'beforeAll', type: 'internal', constant: false, payable: false })
if (availableFunctions.includes('beforeAll')) {
let func = specialFunctionsInterface['beforeAll']
runList.push({ name: 'beforeAll', inputs: func.inputs, signature: func.signature, type: 'internal', constant: isConstant(func), payable: isPayable(func) })
}
for (const func of testFunctionsInterface) {
if (availableFunctions.indexOf('beforeEach') >= 0) {
runList.push({ name: 'beforeEach', type: 'internal', constant: false, payable: false })
if (availableFunctions.includes('beforeEach')) {
let func = specialFunctionsInterface['beforeEach']
runList.push({ name: 'beforeEach', inputs: func.inputs, signature: func.signature, type: 'internal', constant: isConstant(func), payable: isPayable(func) })
}
if(func.name && func.inputs) runList.push({ name: func.name, inputs: func.inputs, signature: func.signature, type: 'test', constant: isConstant(func), payable: isPayable(func) })
if (availableFunctions.indexOf('afterEach') >= 0) {
runList.push({ name: 'afterEach', type: 'internal', constant: false, payable: false })
let func = specialFunctionsInterface['afterEach']
runList.push({ name: 'afterEach', inputs: func.inputs, signature: func.signature, type: 'internal', constant: isConstant(func), payable: isPayable(func) })
}
}
if (availableFunctions.indexOf('afterAll') >= 0) {
runList.push({ name: 'afterAll', type: 'internal', constant: false, payable: false })
let func = specialFunctionsInterface['afterAll']
runList.push({ name: 'afterAll', inputs: func.inputs, signature: func.signature, type: 'internal', constant: isConstant(func), payable: isPayable(func) })
}
return runList

@ -246,8 +246,8 @@ describe('testRunner', () => {
after(() => { tests = [] })
it('should have 5 passing tests', function () {
assert.equal(results.passingNum, 5)
it('should have 17 passing tests', function () {
assert.equal(results.passingNum, 17)
})
it('should have 0 failing tests', function () {
assert.equal(results.failureNum, 0)

@ -2,7 +2,20 @@ import "remix_tests.sol"; // this import is automatically injected by Remix.
import "remix_accounts.sol";
contract SenderAndValueTest {
function beforeAll () public {}
/// #sender: account-2
/// #value: 200
function beforeAll () public payable {
Assert.equal(msg.sender, TestsAccounts.getAccount(2), "wrong sender in beforeAll");
Assert.equal(msg.value, 200, "wrong value in beforeAll");
}
/// #sender: account-3
/// #value: 300
function beforeEach () public payable {
Assert.equal(msg.sender, TestsAccounts.getAccount(3), "wrong sender in beforeEach");
Assert.equal(msg.value, 300, "wrong value in beforeEach");
}
/// #sender: account-1
function checkSenderIs1 () public {
@ -28,4 +41,18 @@ contract SenderAndValueTest {
function checkValueIsnt10 () public payable{
Assert.notEqual(msg.value, 10, "wrong value in checkValueIsnt10");
}
/// #sender: account-4
/// #value: 400
function afterEach () public payable {
Assert.equal(msg.sender, TestsAccounts.getAccount(4), "wrong sender in afterEach");
Assert.equal(msg.value, 400, "wrong value in afterEach");
}
/// #sender: account-5
/// #value: 500
function afterAll () public payable {
Assert.equal(msg.sender, TestsAccounts.getAccount(5), "wrong sender in afterAll");
Assert.equal(msg.value, 500, "wrong value in afterAll");
}
}

Loading…
Cancel
Save