add more typings to src/testRunner.ts

pull/5370/head
Omkara 6 years ago committed by 0mkar
parent 0820fdf0a3
commit 0e2e6ac6f1
  1. 2
      remix-tests/package.json
  2. 79
      remix-tests/src/testRunner.ts
  3. 55
      remix-tests/tests/testRunner.ts

@ -19,7 +19,7 @@
}, },
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"test": "mocha --require ts-node/register tests/*.ts -t 300000" "test": "tsc && mocha --require ts-node/register tests/*.ts -t 300000"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

@ -2,6 +2,26 @@ import async from 'async'
import * as changeCase from 'change-case' import * as changeCase from 'change-case'
import Web3 from 'web3' import Web3 from 'web3'
interface CbReturnInterface {
type: string,
value: any,
time?: number,
context?: string,
errMsg?: string
filename?: string
}
export interface ResultsInterface {
passingNum: number,
failureNum: number,
timePassed: number
}
export interface TestCbInterface {
(error: Error | null | undefined, result?: CbReturnInterface) : void;
}
export interface ResultCbInterface {
(error: Error | null | undefined, result: ResultsInterface) : void;
}
function getFunctionFullName (signature, methodIdentifiers) { function getFunctionFullName (signature, methodIdentifiers) {
for (var method in methodIdentifiers) { for (var method in methodIdentifiers) {
if (signature.replace('0x', '') === methodIdentifiers[method].replace('0x', '')) { if (signature.replace('0x', '') === methodIdentifiers[method].replace('0x', '')) {
@ -53,12 +73,12 @@ function createRunList (jsonInterface) {
return runList return runList
} }
export default function runTest (testName, testObject: any, contractDetails: any, opts: any, testCallback: Function, resultsCallback: Function) { export default function runTest (testName, testObject: any, contractDetails: any, opts: any, testCallback: TestCbInterface, resultsCallback: ResultCbInterface) {
let runList = createRunList(testObject._jsonInterface) let runList = createRunList(testObject._jsonInterface)
let passingNum = 0 let passingNum: number = 0
let failureNum = 0 let failureNum: number = 0
let timePassed = 0 let timePassed: number = 0
let web3 = new Web3() let web3 = new Web3()
var userAgent = (typeof (navigator) !== 'undefined') && navigator.userAgent ? navigator.userAgent.toLowerCase() : '-' var userAgent = (typeof (navigator) !== 'undefined') && navigator.userAgent ? navigator.userAgent.toLowerCase() : '-'
@ -70,8 +90,12 @@ export default function runTest (testName, testObject: any, contractDetails: any
signale.warn('e.g: the following code won\'t work in the current context:') signale.warn('e.g: the following code won\'t work in the current context:')
signale.warn('TestsAccounts.getAccount(' + opts.accounts.length + ')') signale.warn('TestsAccounts.getAccount(' + opts.accounts.length + ')')
} }
const resp: CbReturnInterface = {
testCallback({type: 'contract', value: testName, filename: testObject.filename}) type: 'contract',
value: testName,
filename: testObject.filename
}
testCallback(undefined, resp)
async.eachOfLimit(runList, 1, function (func, index, next) { async.eachOfLimit(runList, 1, function (func, index, next) {
let sender let sender
if (func.signature) { if (func.signature) {
@ -89,29 +113,48 @@ export default function runTest (testName, testObject: any, contractDetails: any
method.call(sendParams).then((result) => { method.call(sendParams).then((result) => {
let time = Math.ceil((Date.now() - startTime) / 1000.0) let time = Math.ceil((Date.now() - startTime) / 1000.0)
if (result) { if (result) {
testCallback({type: 'testPass', value: changeCase.sentenceCase(func.name), time: time, context: testName}) const resp: CbReturnInterface = {
type: 'testPass',
value: changeCase.sentenceCase(func.name),
time: time,
context: testName
}
testCallback(undefined, resp)
passingNum += 1 passingNum += 1
timePassed += time timePassed += time
} else { } else {
testCallback({type: 'testFailure', value: changeCase.sentenceCase(func.name), time: time, errMsg: 'function returned false', context: testName}) const resp: CbReturnInterface = {
type: 'testFailure',
value: changeCase.sentenceCase(func.name),
time: time,
errMsg: 'function returned false',
context: testName
}
testCallback(undefined, resp)
failureNum += 1 failureNum += 1
} }
next() next()
}) })
} else { } else {
method.send(sendParams).on('receipt', function (receipt) { method.send(sendParams).on('receipt', (receipt) => {
try { try {
let time = Math.ceil((Date.now() - startTime) / 1000.0) let time: number = Math.ceil((Date.now() - startTime) / 1000.0)
let topic = Web3.utils.sha3('AssertionEvent(bool,string)') let topic = Web3.utils.sha3('AssertionEvent(bool,string)')
let testPassed: boolean = false
let testPassed = false
for (let i in receipt.events) { for (let i in receipt.events) {
let event = receipt.events[i] let event = receipt.events[i]
if (event.raw.topics.indexOf(topic) >= 0) { if (event.raw.topics.indexOf(topic) >= 0) {
var testEvent = web3.eth.abi.decodeParameters(['bool', 'string'], event.raw.data) var testEvent = web3.eth.abi.decodeParameters(['bool', 'string'], event.raw.data)
if (!testEvent[0]) { if (!testEvent[0]) {
testCallback({type: 'testFailure', value: changeCase.sentenceCase(func.name), time: time, errMsg: testEvent[1], context: testName}) const resp: CbReturnInterface = {
type: 'testFailure',
value: changeCase.sentenceCase(func.name),
time: time,
errMsg: testEvent[1],
context: testName
};
testCallback(undefined, resp)
failureNum += 1 failureNum += 1
return next() return next()
} }
@ -120,7 +163,13 @@ export default function runTest (testName, testObject: any, contractDetails: any
} }
if (testPassed) { if (testPassed) {
testCallback({type: 'testPass', value: changeCase.sentenceCase(func.name), time: time, context: testName}) const resp: CbReturnInterface = {
type: 'testPass',
value: changeCase.sentenceCase(func.name),
time: time,
context: testName
}
testCallback(undefined, resp)
passingNum += 1 passingNum += 1
} }
@ -130,7 +179,7 @@ export default function runTest (testName, testObject: any, contractDetails: any
console.dir(err) console.dir(err)
return next(err) return next(err)
} }
}).on('error', function (err) { }).on('error', function (err: Error | null | undefined) {
console.error(err) console.error(err)
next(err) next(err)
}) })

@ -5,12 +5,7 @@ import { Provider } from 'remix-simulator'
let Compiler = require('../src/compiler.js') let Compiler = require('../src/compiler.js')
let Deployer = require('../src/deployer.js') let Deployer = require('../src/deployer.js')
import runTest from '../dist/testRunner.js' import runTest, { ResultsInterface, TestCbInterface, ResultCbInterface } from '../dist/testRunner.js'
interface Results {
passingNum: number,
failureNum: number,
}
function compileAndDeploy (filename: string, callback: Function) { function compileAndDeploy (filename: string, callback: Function) {
let web3: Web3 = new Web3() let web3: Web3 = new Web3()
@ -19,7 +14,7 @@ function compileAndDeploy (filename: string, callback: Function) {
let accounts: object let accounts: object
async.waterfall([ async.waterfall([
function getAccountList(next: Function): void { function getAccountList(next: Function): void {
web3.eth.getAccounts((_err: Error, _accounts: object) => { web3.eth.getAccounts((_err: Error | null | undefined, _accounts: object) => {
accounts = _accounts accounts = _accounts
next(_err) next(_err)
}) })
@ -45,14 +40,16 @@ function compileAndDeploy (filename: string, callback: Function) {
describe('#runTest', () => { describe('#runTest', () => {
describe('test with beforeAll', () => { describe('test with beforeAll', () => {
let filename = 'tests/examples_1/simple_storage_test.sol' let filename = 'tests/examples_1/simple_storage_test.sol'
let tests:any[] = [], results:Results; let tests:any[] = [], results:ResultsInterface;
before((done) => { before((done) => {
compileAndDeploy(filename, (_err: Error, compilationData: object, contracts: any, accounts: object) => { compileAndDeploy(filename, (_err: Error | null | undefined, compilationData: object, contracts: any, accounts: object) => {
var testCallback = (test: object) => { var testCallback: TestCbInterface = (err, test) => {
if (err) { throw err }
tests.push(test) tests.push(test)
} }
var resultsCallback = (_err: Error, _results: Results) => { var resultsCallback: ResultCbInterface = (err, _results) => {
if (err) { throw err }
results = _results results = _results
done() done()
} }
@ -78,5 +75,41 @@ function compileAndDeploy (filename: string, callback: Function) {
]) ])
}) })
}) })
describe('test with beforeEach', function () {
let filename = 'tests/examples_2/simple_storage_test.sol'
let tests:any[] = [], results:ResultsInterface;
before(function (done) {
compileAndDeploy(filename, function (_err: Error | null | undefined, compilationData: object, contracts: any, accounts: object) {
var testCallback: TestCbInterface = (err, test) => {
if (err) { throw err }
tests.push(test)
}
var resultsCallback: ResultCbInterface = (err, _results) => {
if (err) { throw err }
results = _results
done()
}
runTest('MyTest', contracts.MyTest, compilationData[filename]['MyTest'], { accounts }, testCallback, resultsCallback)
})
})
it('should 2 passing tests', function () {
assert.equal(results.passingNum, 2)
})
it('should 0 failing tests', function () {
assert.equal(results.failureNum, 0)
})
it('should returns 3 messages', function () {
assert.deepEqual(tests, [
{ type: 'contract', value: 'MyTest', filename: 'tests/examples_2/simple_storage_test.sol' },
{ type: 'testPass', value: 'Initial value should be100', time: 1, context: 'MyTest' },
{ type: 'testPass', value: 'Initial value should be200', time: 1, context: 'MyTest' }
])
})
})
}) })
}) })

Loading…
Cancel
Save