add more typings to src/testRunner.ts

pull/7/head
Omkara 6 years ago
parent ee384be395
commit dcf8b12417
  1. 2
      remix-tests/package.json
  2. 81
      remix-tests/src/testRunner.ts
  3. 55
      remix-tests/tests/testRunner.ts

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

@ -2,6 +2,26 @@ import async from 'async'
import * as changeCase from 'change-case'
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) {
for (var method in methodIdentifiers) {
if (signature.replace('0x', '') === methodIdentifiers[method].replace('0x', '')) {
@ -53,12 +73,12 @@ function createRunList (jsonInterface) {
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 passingNum = 0
let failureNum = 0
let timePassed = 0
let passingNum: number = 0
let failureNum: number = 0
let timePassed: number = 0
let web3 = new Web3()
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('TestsAccounts.getAccount(' + opts.accounts.length + ')')
}
testCallback({type: 'contract', value: testName, filename: testObject.filename})
const resp: CbReturnInterface = {
type: 'contract',
value: testName,
filename: testObject.filename
}
testCallback(undefined, resp)
async.eachOfLimit(runList, 1, function (func, index, next) {
let sender
if (func.signature) {
@ -89,29 +113,48 @@ export default function runTest (testName, testObject: any, contractDetails: any
method.call(sendParams).then((result) => {
let time = Math.ceil((Date.now() - startTime) / 1000.0)
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
timePassed += time
} 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
}
next()
})
} else {
method.send(sendParams).on('receipt', function (receipt) {
method.send(sendParams).on('receipt', (receipt) => {
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 testPassed = false
let testPassed: boolean = false
for (let i in receipt.events) {
let event = receipt.events[i]
if (event.raw.topics.indexOf(topic) >= 0) {
var testEvent = web3.eth.abi.decodeParameters(['bool', 'string'], event.raw.data)
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
return next()
}
@ -120,7 +163,13 @@ export default function runTest (testName, testObject: any, contractDetails: any
}
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
}
@ -130,12 +179,12 @@ export default function runTest (testName, testObject: any, contractDetails: any
console.dir(err)
return next(err)
}
}).on('error', function (err) {
}).on('error', function (err: Error | null | undefined) {
console.error(err)
next(err)
})
}
}, function (error) {
}, function(error) {
resultsCallback(error, {
passingNum: passingNum,
failureNum: failureNum,

@ -5,12 +5,7 @@ import { Provider } from 'remix-simulator'
let Compiler = require('../src/compiler.js')
let Deployer = require('../src/deployer.js')
import runTest from '../dist/testRunner.js'
interface Results {
passingNum: number,
failureNum: number,
}
import runTest, { ResultsInterface, TestCbInterface, ResultCbInterface } from '../dist/testRunner.js'
function compileAndDeploy (filename: string, callback: Function) {
let web3: Web3 = new Web3()
@ -19,7 +14,7 @@ function compileAndDeploy (filename: string, callback: Function) {
let accounts: object
async.waterfall([
function getAccountList(next: Function): void {
web3.eth.getAccounts((_err: Error, _accounts: object) => {
web3.eth.getAccounts((_err: Error | null | undefined, _accounts: object) => {
accounts = _accounts
next(_err)
})
@ -45,14 +40,16 @@ function compileAndDeploy (filename: string, callback: Function) {
describe('#runTest', () => {
describe('test with beforeAll', () => {
let filename = 'tests/examples_1/simple_storage_test.sol'
let tests:any[] = [], results:Results;
let tests:any[] = [], results:ResultsInterface;
before((done) => {
compileAndDeploy(filename, (_err: Error, compilationData: object, contracts: any, accounts: object) => {
var testCallback = (test: object) => {
compileAndDeploy(filename, (_err: Error | null | undefined, compilationData: object, contracts: any, accounts: object) => {
var testCallback: TestCbInterface = (err, test) => {
if (err) { throw err }
tests.push(test)
}
var resultsCallback = (_err: Error, _results: Results) => {
var resultsCallback: ResultCbInterface = (err, _results) => {
if (err) { throw err }
results = _results
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