Commit 742b91bd authored by aniket-engg's avatar aniket-engg Committed by Aniket

custom transaction context support for special functions

parent 29c4f59c
...@@ -102,6 +102,23 @@ function getTestFunctionsInterface (jsonInterface: FunctionDescription[], funcLi ...@@ -102,6 +102,23 @@ function getTestFunctionsInterface (jsonInterface: FunctionDescription[], funcLi
} }
/** /**
* @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 * @dev Prepare a list of tests to run using test contract file ABI, AST & contract name
* @param jsonInterface File JSON interface * @param jsonInterface File JSON interface
* @param fileAST File AST * @param fileAST File AST
...@@ -111,25 +128,29 @@ function getTestFunctionsInterface (jsonInterface: FunctionDescription[], funcLi ...@@ -111,25 +128,29 @@ function getTestFunctionsInterface (jsonInterface: FunctionDescription[], funcLi
function createRunList (jsonInterface: FunctionDescription[], fileAST: AstNode, testContractName: string): RunListInterface[] { function createRunList (jsonInterface: FunctionDescription[], fileAST: AstNode, testContractName: string): RunListInterface[] {
const availableFunctions: string[] = getAvailableFunctions(fileAST, testContractName) const availableFunctions: string[] = getAvailableFunctions(fileAST, testContractName)
const testFunctionsInterface: FunctionDescription[] = getTestFunctionsInterface(jsonInterface, availableFunctions) const testFunctionsInterface: FunctionDescription[] = getTestFunctionsInterface(jsonInterface, availableFunctions)
const specialFunctionsInterface: Record<string, FunctionDescription> = getSpecialFunctionsInterface(jsonInterface)
let runList: RunListInterface[] = [] let runList: RunListInterface[] = []
if (availableFunctions.indexOf('beforeAll') >= 0) { if (availableFunctions.includes('beforeAll')) {
runList.push({ name: 'beforeAll', type: 'internal', constant: false, payable: false }) 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) { for (const func of testFunctionsInterface) {
if (availableFunctions.indexOf('beforeEach') >= 0) { if (availableFunctions.includes('beforeEach')) {
runList.push({ name: 'beforeEach', type: 'internal', constant: false, payable: false }) 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(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) { 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) { 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 return runList
......
...@@ -246,8 +246,8 @@ describe('testRunner', () => { ...@@ -246,8 +246,8 @@ describe('testRunner', () => {
after(() => { tests = [] }) after(() => { tests = [] })
it('should have 5 passing tests', function () { it('should have 17 passing tests', function () {
assert.equal(results.passingNum, 5) assert.equal(results.passingNum, 17)
}) })
it('should have 0 failing tests', function () { it('should have 0 failing tests', function () {
assert.equal(results.failureNum, 0) assert.equal(results.failureNum, 0)
......
...@@ -2,7 +2,20 @@ import "remix_tests.sol"; // this import is automatically injected by Remix. ...@@ -2,7 +2,20 @@ import "remix_tests.sol"; // this import is automatically injected by Remix.
import "remix_accounts.sol"; import "remix_accounts.sol";
contract SenderAndValueTest { 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 /// #sender: account-1
function checkSenderIs1 () public { function checkSenderIs1 () public {
...@@ -28,4 +41,18 @@ contract SenderAndValueTest { ...@@ -28,4 +41,18 @@ contract SenderAndValueTest {
function checkValueIsnt10 () public payable{ function checkValueIsnt10 () public payable{
Assert.notEqual(msg.value, 10, "wrong value in checkValueIsnt10"); 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");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment