From 742b91bd8fc8681d5f44ae32e81cf84b939dbe05 Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Mon, 4 May 2020 16:22:08 +0530 Subject: [PATCH] custom transaction context support for special functions --- remix-tests/src/testRunner.ts | 35 +++++++++++++++---- remix-tests/tests/testRunner.ts | 4 +-- .../various_sender/sender_and_value_test.sol | 29 ++++++++++++++- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/remix-tests/src/testRunner.ts b/remix-tests/src/testRunner.ts index 8e022c8854..316284cd96 100644 --- a/remix-tests/src/testRunner.ts +++ b/remix-tests/src/testRunner.ts @@ -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 { + const specialFunctionsInterface: Record = {} + 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 = 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 diff --git a/remix-tests/tests/testRunner.ts b/remix-tests/tests/testRunner.ts index 5f5c17a9e5..7a8728c92a 100644 --- a/remix-tests/tests/testRunner.ts +++ b/remix-tests/tests/testRunner.ts @@ -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) diff --git a/remix-tests/tests/various_sender/sender_and_value_test.sol b/remix-tests/tests/various_sender/sender_and_value_test.sol index e3a4cb465b..1ed50021fd 100644 --- a/remix-tests/tests/various_sender/sender_and_value_test.sol +++ b/remix-tests/tests/various_sender/sender_and_value_test.sol @@ -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"); + } }