refactoring

pull/5370/head
Aniket-Engg 3 years ago committed by Aniket
parent f46d948a08
commit 7eeebe32d9
  1. 339
      libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx

@ -62,6 +62,11 @@ export const SolidityUnitTesting = (props: any) => {
let testSuite: any let testSuite: any
let testSuites: any let testSuites: any
let runningTestFileName: any let runningTestFileName: any
let [testsResultByFilename, setTestsResultByFilename] = useState<Record<string, any>>({})
const trimTestDirInput = (input:string) => { const trimTestDirInput = (input:string) => {
if (input.includes('/')) return input.split('/').map(e => e.trim()).join('/') if (input.includes('/')) return input.split('/').map(e => e.trim()).join('/')
@ -245,93 +250,248 @@ export const SolidityUnitTesting = (props: any) => {
} }
} }
const testCallback = (result: any, runningTests: any) => { const showTestsResult = () => {
console.log('result---in testCallback->', result) setTestsOutput([])
let debugBtn let filenames = Object.keys(testsResultByFilename)
if ((result.type === 'testPass' || result.type === 'testFailure') && result.debugTxHash) { for(const filename of filenames) {
const { web3, debugTxHash } = result const fileTestsResult = testsResultByFilename[filename]
debugBtn = ( const contracts = Object.keys(fileTestsResult)
<div id={result.value.replaceAll(' ', '_')} className="btn border btn btn-sm ml-1" style={{ cursor: 'pointer' }} title="Start debugging" onClick={() => startDebug(debugTxHash, web3)}> for(const contract of contracts) {
<i className="fas fa-bug"></i> if(contract && contract !== 'summary') {
</div> runningTestFileName = cleanFileName(filename, contract)
) // show contract and file name
} const ContractCard: any = (
if (result.type === 'contract') { <div id={runningTestFileName} data-id="testTabSolidityUnitTestsOutputheader" className="pt-1">
var testSuite = result.value <span className="font-weight-bold">{contract} ({filename})</span>
if (testSuites) { </div>
testSuites.push(testSuite) )
} else { setTestsOutput(prevCards => ([...prevCards, ContractCard]))
testSuites = [testSuite] // show tests
const tests = fileTestsResult[contract]
if (tests?.length) {
for(const test of tests) {
if (test.type === 'testPass') {
if (test.hhLogs && test.hhLogs.length) printHHLogs(test.hhLogs, test.value)
const testPassCard: any = (
<div
id={runningTestFileName}
data-id="testTabSolidityUnitTestsOutputheader"
className="testPass testLog bg-light mb-2 px-2 text-success border-0"
onClick={() => discardHighlight()}
>
<div className="d-flex my-1 align-items-start justify-content-between">
<span > {test.value}</span>
{/* {debugBtn} */}
</div>
</div>
)
setTestsOutput(prevCards => ([...prevCards, testPassCard]))
}
}
}
}
} }
runningTestFileName = cleanFileName(result.filename, testSuite) // show testsSummary
const ContractCard: any = ( }
<div id={runningTestFileName} data-id="testTabSolidityUnitTestsOutputheader" className="pt-1"> // let debugBtn
<span className="font-weight-bold">{testSuite} ({result.filename})</span> // if ((result.type === 'testPass' || result.type === 'testFailure') && result.debugTxHash) {
</div> // const { web3, debugTxHash } = result
) // debugBtn = (
setTestsOutput(prevCards => ([...prevCards, ContractCard])) // <div id={result.value.replaceAll(' ', '_')} className="btn border btn btn-sm ml-1" style={{ cursor: 'pointer' }} title="Start debugging" onClick={() => startDebug(debugTxHash, web3)}>
} else if (result.type === 'testPass') { // <i className="fas fa-bug"></i>
if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value) // </div>
const testPassCard: any = ( // )
<div // }
id={runningTestFileName} // if (result.type === 'contract') {
data-id="testTabSolidityUnitTestsOutputheader" // var testSuite = result.value
className="testPass testLog bg-light mb-2 px-2 text-success border-0" // if (testSuites) {
onClick={() => discardHighlight()} // testSuites.push(testSuite)
> // } else {
<div className="d-flex my-1 align-items-start justify-content-between"> // testSuites = [testSuite]
<span > {result.value}</span> // }
{debugBtn} // runningTestFileName = cleanFileName(result.filename, testSuite)
</div> // const ContractCard: any = (
</div> // <div id={runningTestFileName} data-id="testTabSolidityUnitTestsOutputheader" className="pt-1">
) // <span className="font-weight-bold">{testSuite} ({result.filename})</span>
setTestsOutput(prevCards => ([...prevCards, testPassCard])) // </div>
} else if (result.type === 'testFailure') { // )
if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value) // setTestsOutput(prevCards => ([...prevCards, ContractCard]))
if (!result.assertMethod) { // } else if (result.type === 'testPass') {
const testFailCard1: any = (<div // if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value)
className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0" // const testPassCard: any = (
id={"UTContext" + result.context} // <div
onClick={() => highlightLocation(result.location, runningTests, result.filename)} // id={runningTestFileName}
> // data-id="testTabSolidityUnitTestsOutputheader"
<div className="d-flex my-1 align-items-start justify-content-between"> // className="testPass testLog bg-light mb-2 px-2 text-success border-0"
<span> {result.value}</span> // onClick={() => discardHighlight()}
{debugBtn} // >
</div> // <div className="d-flex my-1 align-items-start justify-content-between">
<span className="text-dark">Error Message:</span> // <span > ✓ {result.value}</span>
<span className="pb-2 text-break">"{result.errMsg}"</span> // {debugBtn}
</div>) // </div>
setTestsOutput(prevCards => ([...prevCards, testFailCard1])) // </div>
} else { // )
const preposition = result.assertMethod === 'equal' || result.assertMethod === 'notEqual' ? 'to' : '' // setTestsOutput(prevCards => ([...prevCards, testPassCard]))
const method = result.assertMethod === 'ok' ? '' : result.assertMethod // } else if (result.type === 'testFailure') {
const expected = result.assertMethod === 'ok' ? '\'true\'' : result.expected // if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value)
const testFailCard2: any = (<div // if (!result.assertMethod) {
className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0" // const testFailCard1: any = (<div
id="UTContext${result.context}" // className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0"
onClick={() => highlightLocation(result.location, runningTests, result.filename)} // id={"UTContext" + result.context}
> // onClick={() => highlightLocation(result.location, runningTests, result.filename)}
<div className="d-flex my-1 align-items-start justify-content-between"> // >
<span> {result.value}</span> // <div className="d-flex my-1 align-items-start justify-content-between">
{debugBtn} // <span> ✘ {result.value}</span>
</div> // {debugBtn}
<span className="text-dark">Error Message:</span> // </div>
<span className="pb-2 text-break">"{result.errMsg}"</span> // <span className="text-dark">Error Message:</span>
<span className="text-dark">Assertion:</span> // <span className="pb-2 text-break">"{result.errMsg}"</span>
<div className="d-flex flex-wrap"> // </div>)
<span>Expected value should be</span> // setTestsOutput(prevCards => ([...prevCards, testFailCard1]))
<div className="mx-1 font-weight-bold">{method}</div> // } else {
<div>{preposition} {expected}</div> // const preposition = result.assertMethod === 'equal' || result.assertMethod === 'notEqual' ? 'to' : ''
</div> // const method = result.assertMethod === 'ok' ? '' : result.assertMethod
<span className="text-dark">Received value:</span> // const expected = result.assertMethod === 'ok' ? '\'true\'' : result.expected
<span>{result.returned}</span> // const testFailCard2: any = (<div
<span className="text-dark text-sm pb-2">Skipping the remaining tests of the function.</span> // className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0"
</div>) // id="UTContext${result.context}"
setTestsOutput(prevCards => ([...prevCards, testFailCard2])) // onClick={() => highlightLocation(result.location, runningTests, result.filename)}
// >
// <div className="d-flex my-1 align-items-start justify-content-between">
// <span> ✘ {result.value}</span>
// {debugBtn}
// </div>
// <span className="text-dark">Error Message:</span>
// <span className="pb-2 text-break">"{result.errMsg}"</span>
// <span className="text-dark">Assertion:</span>
// <div className="d-flex flex-wrap">
// <span>Expected value should be</span>
// <div className="mx-1 font-weight-bold">{method}</div>
// <div>{preposition} {expected}</div>
// </div>
// <span className="text-dark">Received value:</span>
// <span>{result.returned}</span>
// <span className="text-dark text-sm pb-2">Skipping the remaining tests of the function.</span>
// </div>)
// setTestsOutput(prevCards => ([...prevCards, testFailCard2]))
// }
// } else if (result.type === 'logOnly') {
// if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value)
// }
}
// const testCallback = (result: any, runningTests: any) => {
// console.log('result--------------in testCallback->', result)
// console.log('testsResultByFilename--------============------in testCallback->', testsResultByFilename)
// if(result.filename) {
// if(!testsResultByFilename[result.filename]) {
// testsResultByFilename[result.filename] = {}
// testsResultByFilename[result.filename][contract]['tests'] = []
// testsResultByFilename[result.filename]['summary'] = {}
// }
// testsResultByFilename[result.filename]['tests'].push(result)
// }
// }
const testCallback = (result: any, runningTests: any) => {
console.log('result--------------in testCallback->', result)
console.log('testsResultByFilename--------============------in testCallback->', testsResultByFilename)
if(result.filename) {
if(!testsResultByFilename[result.filename]) {
testsResultByFilename[result.filename] = {}
testsResultByFilename[result.filename]['summary'] = {}
} }
} else if (result.type === 'logOnly') { if(result.type === 'contract') {
if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value) testsResultByFilename[result.filename][result.value] = []
} else
testsResultByFilename[result.filename][result.context].push(result)
showTestsResult()
} }
// let debugBtn
// if ((result.type === 'testPass' || result.type === 'testFailure') && result.debugTxHash) {
// const { web3, debugTxHash } = result
// debugBtn = (
// <div id={result.value.replaceAll(' ', '_')} className="btn border btn btn-sm ml-1" style={{ cursor: 'pointer' }} title="Start debugging" onClick={() => startDebug(debugTxHash, web3)}>
// <i className="fas fa-bug"></i>
// </div>
// )
// }
// if (result.type === 'contract') {
// var testSuite = result.value
// if (testSuites) {
// testSuites.push(testSuite)
// } else {
// testSuites = [testSuite]
// }
// runningTestFileName = cleanFileName(result.filename, testSuite)
// const ContractCard: any = (
// <div id={runningTestFileName} data-id="testTabSolidityUnitTestsOutputheader" className="pt-1">
// <span className="font-weight-bold">{testSuite} ({result.filename})</span>
// </div>
// )
// setTestsOutput(prevCards => ([...prevCards, ContractCard]))
// } else if (result.type === 'testPass') {
// if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value)
// const testPassCard: any = (
// <div
// id={runningTestFileName}
// data-id="testTabSolidityUnitTestsOutputheader"
// className="testPass testLog bg-light mb-2 px-2 text-success border-0"
// onClick={() => discardHighlight()}
// >
// <div className="d-flex my-1 align-items-start justify-content-between">
// <span > ✓ {result.value}</span>
// {debugBtn}
// </div>
// </div>
// )
// setTestsOutput(prevCards => ([...prevCards, testPassCard]))
// } else if (result.type === 'testFailure') {
// if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value)
// if (!result.assertMethod) {
// const testFailCard1: any = (<div
// className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0"
// id={"UTContext" + result.context}
// onClick={() => highlightLocation(result.location, runningTests, result.filename)}
// >
// <div className="d-flex my-1 align-items-start justify-content-between">
// <span> ✘ {result.value}</span>
// {debugBtn}
// </div>
// <span className="text-dark">Error Message:</span>
// <span className="pb-2 text-break">"{result.errMsg}"</span>
// </div>)
// setTestsOutput(prevCards => ([...prevCards, testFailCard1]))
// } else {
// const preposition = result.assertMethod === 'equal' || result.assertMethod === 'notEqual' ? 'to' : ''
// const method = result.assertMethod === 'ok' ? '' : result.assertMethod
// const expected = result.assertMethod === 'ok' ? '\'true\'' : result.expected
// const testFailCard2: any = (<div
// className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0"
// id="UTContext${result.context}"
// onClick={() => highlightLocation(result.location, runningTests, result.filename)}
// >
// <div className="d-flex my-1 align-items-start justify-content-between">
// <span> ✘ {result.value}</span>
// {debugBtn}
// </div>
// <span className="text-dark">Error Message:</span>
// <span className="pb-2 text-break">"{result.errMsg}"</span>
// <span className="text-dark">Assertion:</span>
// <div className="d-flex flex-wrap">
// <span>Expected value should be</span>
// <div className="mx-1 font-weight-bold">{method}</div>
// <div>{preposition} {expected}</div>
// </div>
// <span className="text-dark">Received value:</span>
// <span>{result.returned}</span>
// <span className="text-dark text-sm pb-2">Skipping the remaining tests of the function.</span>
// </div>)
// setTestsOutput(prevCards => ([...prevCards, testFailCard2]))
// }
// } else if (result.type === 'logOnly') {
// if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value)
// }
} }
const resultsCallback = (_err: any, result: any, cb: any) => { const resultsCallback = (_err: any, result: any, cb: any) => {
@ -343,7 +503,8 @@ export const SolidityUnitTesting = (props: any) => {
} }
const updateFinalResult = (_errors: any, result: any, filename: any) => { const updateFinalResult = (_errors: any, result: any, filename: any) => {
console.log('result---in updateFinalResult->', result, filename) console.log('result---------------------------in updateFinalResult->', result, filename)
console.log('testsResultByFilename---------------------------in updateFinalResult->', testsResultByFilename)
++readyTestsNumber ++readyTestsNumber
setTestsSummaryHidden('visible') setTestsSummaryHidden('visible')
// if (!result && (_errors && (_errors.errors || (Array.isArray(_errors) && (_errors[0].message || _errors[0].formattedMessage))))) { // if (!result && (_errors && (_errors.errors || (Array.isArray(_errors) && (_errors[0].message || _errors[0].formattedMessage))))) {
@ -363,7 +524,8 @@ export const SolidityUnitTesting = (props: any) => {
if (result) { if (result) {
const totalTime = parseFloat(result.totalTime).toFixed(2) const totalTime = parseFloat(result.totalTime).toFixed(2)
testsSummary = { filename, passed: result.totalPassing, failed: result.totalFailing, timeTaken: totalTime } testsSummary = { filename, passed: result.totalPassing, failed: result.totalFailing, timeTaken: totalTime }
setTestsSummary(testsSummary) testsResultByFilename[filename]['summary']= testsSummary
// setTestsSummary(testsSummary)
} }
// fix for displaying right label for multiple tests (testsuites) in a single file // fix for displaying right label for multiple tests (testsuites) in a single file
// this.testSuites.forEach(testSuite => { // this.testSuites.forEach(testSuite => {
@ -459,6 +621,7 @@ export const SolidityUnitTesting = (props: any) => {
clearResults() clearResults()
// yo.update(this.resultStatistics, this.createResultLabel()) // yo.update(this.resultStatistics, this.createResultLabel())
const tests = selectedTests const tests = selectedTests
console.log('tests--in runTests----------------->', tests)
if (!tests) return if (!tests) return
// this.resultStatistics.hidden = tests.length === 0 // this.resultStatistics.hidden = tests.length === 0
// _paq.push(['trackEvent', 'solidityUnitTesting', 'runTests']) // _paq.push(['trackEvent', 'solidityUnitTesting', 'runTests'])
@ -641,12 +804,12 @@ export const SolidityUnitTesting = (props: any) => {
<label className="text-danger h6" data-id="testTabTestsExecutionStoppedError" hidden={testsExecutionStoppedErrorHidden}>The test execution has been stopped because of error(s) in your test file</label> <label className="text-danger h6" data-id="testTabTestsExecutionStoppedError" hidden={testsExecutionStoppedErrorHidden}>The test execution has been stopped because of error(s) in your test file</label>
</div> </div>
<div>{testsOutput}</div> <div>{testsOutput}</div>
<div className="d-flex alert-secondary mb-3 p-3 flex-column" style= {{visibility: testsSummaryHidden} as CSSProperties }> {/* <div className="d-flex alert-secondary mb-3 p-3 flex-column" style= {{visibility: testsSummaryHidden} as CSSProperties }>
<span className="font-weight-bold">{testsSummary && testsSummary.filename ? `Result for ${testsSummary.filename}` : ''}</span> <span className="font-weight-bold">{testsSummary && testsSummary.filename ? `Result for ${testsSummary.filename}` : ''}</span>
<span className="text-success">{testsSummary && testsSummary.passed >= 0 ? `Passed: ${testsSummary.passed}` : ''}</span> <span className="text-success">{testsSummary && testsSummary.passed >= 0 ? `Passed: ${testsSummary.passed}` : ''}</span>
<span className="text-danger">{testsSummary && testsSummary.failed >= 0 ? `Failed: ${testsSummary.failed}` : ''}</span> <span className="text-danger">{testsSummary && testsSummary.failed >= 0 ? `Failed: ${testsSummary.failed}` : ''}</span>
<span>{testsSummary && testsSummary.timeTaken ? `Time Taken: ${testsSummary.timeTaken}` : ''}</span> <span>{testsSummary && testsSummary.timeTaken ? `Time Taken: ${testsSummary.timeTaken}` : ''}</span>
</div> </div> */}
</div> </div>
</div> </div>
) )

Loading…
Cancel
Save