From 7eeebe32d973d6f936e75dbea845b7291bad5441 Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Fri, 10 Dec 2021 12:28:40 +0530 Subject: [PATCH] refactoring --- .../src/lib/solidity-unit-testing.tsx | 339 +++++++++++++----- 1 file changed, 251 insertions(+), 88 deletions(-) diff --git a/libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx b/libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx index 934cb8dba3..9c5d64d658 100644 --- a/libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx +++ b/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 testSuites: any let runningTestFileName: any + + + + + let [testsResultByFilename, setTestsResultByFilename] = useState>({}) const trimTestDirInput = (input:string) => { 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) => { - console.log('result---in testCallback->', result) - let debugBtn - if ((result.type === 'testPass' || result.type === 'testFailure') && result.debugTxHash) { - const { web3, debugTxHash } = result - debugBtn = ( -
startDebug(debugTxHash, web3)}> - -
- ) - } - if (result.type === 'contract') { - var testSuite = result.value - if (testSuites) { - testSuites.push(testSuite) - } else { - testSuites = [testSuite] + const showTestsResult = () => { + setTestsOutput([]) + let filenames = Object.keys(testsResultByFilename) + for(const filename of filenames) { + const fileTestsResult = testsResultByFilename[filename] + const contracts = Object.keys(fileTestsResult) + for(const contract of contracts) { + if(contract && contract !== 'summary') { + runningTestFileName = cleanFileName(filename, contract) + // show contract and file name + const ContractCard: any = ( +
+ {contract} ({filename}) +
+ ) + setTestsOutput(prevCards => ([...prevCards, ContractCard])) + // 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 = ( +
discardHighlight()} + > +
+ ✓ {test.value} + {/* {debugBtn} */} +
+
+ ) + setTestsOutput(prevCards => ([...prevCards, testPassCard])) + } + } + } + } } - runningTestFileName = cleanFileName(result.filename, testSuite) - const ContractCard: any = ( -
- {testSuite} ({result.filename}) -
- ) - setTestsOutput(prevCards => ([...prevCards, ContractCard])) - } else if (result.type === 'testPass') { - if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value) - const testPassCard: any = ( -
discardHighlight()} - > -
- ✓ {result.value} - {debugBtn} -
-
- ) - 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 = (
highlightLocation(result.location, runningTests, result.filename)} - > -
- ✘ {result.value} - {debugBtn} -
- Error Message: - "{result.errMsg}" -
) - 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 = (
highlightLocation(result.location, runningTests, result.filename)} - > -
- ✘ {result.value} - {debugBtn} -
- Error Message: - "{result.errMsg}" - Assertion: -
- Expected value should be -
{method}
-
{preposition} {expected}
-
- Received value: - {result.returned} - Skipping the remaining tests of the function. -
) - setTestsOutput(prevCards => ([...prevCards, testFailCard2])) + // show testsSummary + } + // let debugBtn + // if ((result.type === 'testPass' || result.type === 'testFailure') && result.debugTxHash) { + // const { web3, debugTxHash } = result + // debugBtn = ( + //
startDebug(debugTxHash, web3)}> + // + //
+ // ) + // } + // if (result.type === 'contract') { + // var testSuite = result.value + // if (testSuites) { + // testSuites.push(testSuite) + // } else { + // testSuites = [testSuite] + // } + // runningTestFileName = cleanFileName(result.filename, testSuite) + // const ContractCard: any = ( + //
+ // {testSuite} ({result.filename}) + //
+ // ) + // setTestsOutput(prevCards => ([...prevCards, ContractCard])) + // } else if (result.type === 'testPass') { + // if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value) + // const testPassCard: any = ( + //
discardHighlight()} + // > + //
+ // ✓ {result.value} + // {debugBtn} + //
+ //
+ // ) + // 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 = (
highlightLocation(result.location, runningTests, result.filename)} + // > + //
+ // ✘ {result.value} + // {debugBtn} + //
+ // Error Message: + // "{result.errMsg}" + //
) + // 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 = (
highlightLocation(result.location, runningTests, result.filename)} + // > + //
+ // ✘ {result.value} + // {debugBtn} + //
+ // Error Message: + // "{result.errMsg}" + // Assertion: + //
+ // Expected value should be + //
{method}
+ //
{preposition} {expected}
+ //
+ // Received value: + // {result.returned} + // Skipping the remaining tests of the function. + //
) + // 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.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value) + if(result.type === 'contract') { + 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 = ( + //
startDebug(debugTxHash, web3)}> + // + //
+ // ) + // } + // if (result.type === 'contract') { + // var testSuite = result.value + // if (testSuites) { + // testSuites.push(testSuite) + // } else { + // testSuites = [testSuite] + // } + // runningTestFileName = cleanFileName(result.filename, testSuite) + // const ContractCard: any = ( + //
+ // {testSuite} ({result.filename}) + //
+ // ) + // setTestsOutput(prevCards => ([...prevCards, ContractCard])) + // } else if (result.type === 'testPass') { + // if (result.hhLogs && result.hhLogs.length) printHHLogs(result.hhLogs, result.value) + // const testPassCard: any = ( + //
discardHighlight()} + // > + //
+ // ✓ {result.value} + // {debugBtn} + //
+ //
+ // ) + // 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 = (
highlightLocation(result.location, runningTests, result.filename)} + // > + //
+ // ✘ {result.value} + // {debugBtn} + //
+ // Error Message: + // "{result.errMsg}" + //
) + // 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 = (
highlightLocation(result.location, runningTests, result.filename)} + // > + //
+ // ✘ {result.value} + // {debugBtn} + //
+ // Error Message: + // "{result.errMsg}" + // Assertion: + //
+ // Expected value should be + //
{method}
+ //
{preposition} {expected}
+ //
+ // Received value: + // {result.returned} + // Skipping the remaining tests of the function. + //
) + // 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) => { @@ -343,7 +503,8 @@ export const SolidityUnitTesting = (props: 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 setTestsSummaryHidden('visible') // 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) { const totalTime = parseFloat(result.totalTime).toFixed(2) 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 // this.testSuites.forEach(testSuite => { @@ -459,6 +621,7 @@ export const SolidityUnitTesting = (props: any) => { clearResults() // yo.update(this.resultStatistics, this.createResultLabel()) const tests = selectedTests + console.log('tests--in runTests----------------->', tests) if (!tests) return // this.resultStatistics.hidden = tests.length === 0 // _paq.push(['trackEvent', 'solidityUnitTesting', 'runTests']) @@ -641,12 +804,12 @@ export const SolidityUnitTesting = (props: any) => {
{testsOutput}
-
+ {/*
{testsSummary && testsSummary.filename ? `Result for ${testsSummary.filename}` : ''} {testsSummary && testsSummary.passed >= 0 ? `Passed: ${testsSummary.passed}` : ''} {testsSummary && testsSummary.failed >= 0 ? `Failed: ${testsSummary.failed}` : ''} {testsSummary && testsSummary.timeTaken ? `Time Taken: ${testsSummary.timeTaken}` : ''} -
+
*/} )