pass value in test methods

pull/5370/head
aniket-engg 5 years ago committed by Aniket
parent 9c40be1317
commit 4f32d060aa
  1. 3
      remix-tests/package.json
  2. 33
      remix-tests/src/testRunner.ts
  3. 1
      remix-tests/src/types.ts
  4. 15
      remix-tests/tests/various_sender/sender_test.sol

@ -33,7 +33,8 @@
}, },
"standard": { "standard": {
"ignore": [ "ignore": [
"tests/" "tests/",
"dist/"
] ]
}, },
"homepage": "https://github.com/ethereum/remix/tree/master/remix-tests#readme", "homepage": "https://github.com/ethereum/remix/tree/master/remix-tests#readme",

@ -14,16 +14,27 @@ function getFunctionFullName (signature: string, methodIdentifiers: Record <stri
} }
function isConstant(funcABI: FunctionDescription): boolean { function isConstant(funcABI: FunctionDescription): boolean {
return (funcABI.stateMutability === 'view' || funcABI.stateMutability === 'pure') return (funcABI.constant || funcABI.stateMutability === 'view' || funcABI.stateMutability === 'pure')
}
function isPayable(funcABI: FunctionDescription): boolean {
return (funcABI.payable || funcABI.stateMutability === 'payable')
} }
function getOverridedSender (userdoc: UserDocumentation, signature: string, methodIdentifiers: Record <string, string>) { function getOverridedSender (userdoc: UserDocumentation, signature: string, methodIdentifiers: Record <string, string>) {
let fullName: any = getFunctionFullName(signature, methodIdentifiers) let fullName: any = getFunctionFullName(signature, methodIdentifiers)
let match: RegExp = /sender: account-+(\d)/g let senderRegex: RegExp = /#sender: account-+(\d)/g
let accountIndex: any = userdoc.methods[fullName] ? match.exec(userdoc.methods[fullName].notice) : null let accountIndex: RegExpExecArray | null = userdoc.methods[fullName] ? senderRegex.exec(userdoc.methods[fullName].notice) : null
return fullName && accountIndex ? accountIndex[1] : null return fullName && accountIndex ? accountIndex[1] : null
} }
function getProvidedValue (userdoc: UserDocumentation, signature: string, methodIdentifiers: Record <string, string>) {
let fullName: any = getFunctionFullName(signature, methodIdentifiers)
let valueRegex: RegExp = /#value: (\d+)/g
let value: RegExpExecArray | null = userdoc.methods[fullName] ? valueRegex.exec(userdoc.methods[fullName].notice) : null
return fullName && value ? value[1] : null
}
/** /**
* @dev returns functions of a test contract file in same sequence they appear in file (using passed AST) * @dev returns functions of a test contract file in same sequence they appear in file (using passed AST)
* @param fileAST AST of test contract file source * @param fileAST AST of test contract file source
@ -74,21 +85,21 @@ function createRunList (jsonInterface: FunctionDescription[], fileAST: AstNode,
let runList: RunListInterface[] = [] let runList: RunListInterface[] = []
if (availableFunctions.indexOf('beforeAll') >= 0) { if (availableFunctions.indexOf('beforeAll') >= 0) {
runList.push({ name: 'beforeAll', type: 'internal', constant: false }) runList.push({ name: 'beforeAll', type: 'internal', constant: false, payable: false })
} }
for (const func of testFunctionsInterface) { for (const func of testFunctionsInterface) {
if (availableFunctions.indexOf('beforeEach') >= 0) { if (availableFunctions.indexOf('beforeEach') >= 0) {
runList.push({ name: 'beforeEach', type: 'internal', constant: false }) runList.push({ name: 'beforeEach', type: 'internal', constant: false, payable: false })
} }
runList.push({ name: func.name, signature: func.signature, type: 'test', constant: isConstant(func) }) runList.push({ name: func.name, 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 }) runList.push({ name: 'afterEach', type: 'internal', constant: false, payable: false })
} }
} }
if (availableFunctions.indexOf('afterAll') >= 0) { if (availableFunctions.indexOf('afterAll') >= 0) {
runList.push({ name: 'afterAll', type: 'internal', constant: false }) runList.push({ name: 'afterAll', type: 'internal', constant: false, payable: false })
} }
return runList return runList
@ -125,7 +136,6 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
} }
let sendParams let sendParams
if (sender) sendParams = { from: sender } if (sender) sendParams = { from: sender }
const method = testObject.methods[func.name].apply(testObject.methods[func.name], []) const method = testObject.methods[func.name].apply(testObject.methods[func.name], [])
const startTime = Date.now() const startTime = Date.now()
if (func.constant) { if (func.constant) {
@ -155,6 +165,11 @@ export function runTest (testName: string, testObject: any, contractDetails: Com
next() next()
}) })
} else { } else {
if(func.payable) {
const value = getProvidedValue(contractDetails.userdoc, func.signature, contractDetails.evm.methodIdentifiers)
if(sendParams) sendParams.value = value
else sendParams = { value }
}
method.send(sendParams).on('receipt', (receipt) => { method.send(sendParams).on('receipt', (receipt) => {
try { try {
const time: number = (Date.now() - startTime) / 1000.0 const time: number = (Date.now() - startTime) / 1000.0

@ -16,6 +16,7 @@ export interface RunListInterface {
name: string, name: string,
type: string, type: string,
constant: boolean, constant: boolean,
payable: boolean,
signature?: any signature?: any
} }
export interface ResultsInterface { export interface ResultsInterface {

@ -2,25 +2,24 @@ import "remix_tests.sol"; // this import is automatically injected by Remix.
import "remix_accounts.sol"; import "remix_accounts.sol";
contract SenderTest { contract SenderTest {
function beforeAll () public {} function beforeAll () public {}
/// sender: account-1 /// #sender: account-1
function checkSenderIs1 () public { function checkSenderIs1 () public {
Assert.equal(msg.sender, TestsAccounts.getAccount(1), "wrong sender in checkSenderIs1"); Assert.equal(msg.sender, TestsAccounts.getAccount(1), "wrong sender in checkSenderIs1");
} }
/// sender: account-0 /// #sender: account-0
function checkSenderIs0 () public { /// #value: 10
function checkSenderIs0 () public payable{
Assert.equal(msg.sender, TestsAccounts.getAccount(0), "wrong sender in checkSenderIs0"); Assert.equal(msg.sender, TestsAccounts.getAccount(0), "wrong sender in checkSenderIs0");
} }
/// sender: account-1 /// #value: 100
function checkSenderIsNt0 () public { function checkSenderIsNt0 () public payable{
Assert.notEqual(msg.sender, TestsAccounts.getAccount(0), "wrong sender in checkSenderIsNot0"); Assert.equal(msg.sender, TestsAccounts.getAccount(0), "wrong sender in checkSenderIsNot0");
} }
/// sender: account-2
function checkSenderIsnt2 () public { function checkSenderIsnt2 () public {
Assert.notEqual(msg.sender, TestsAccounts.getAccount(1), "wrong sender in checkSenderIsnt2"); Assert.notEqual(msg.sender, TestsAccounts.getAccount(1), "wrong sender in checkSenderIsnt2");
} }

Loading…
Cancel
Save