adding custom path to Unit Testing

pull/5370/head
LianaHus 5 years ago committed by yann300
parent 26cf43c451
commit 66ffc8f704
  1. 50
      src/app/tabs/test-tab.js
  2. 39
      src/app/tabs/testTab/testTab.js

@ -281,7 +281,7 @@ module.exports = class TestTab extends ViewPlugin {
const stopBtnLabel = document.getElementById('runTestsTabStopActionLabel')
stopBtnLabel.innerText = 'Stop'
if (this.data.selectedTests.length !== 0) {
const runBtn = document.getElementById('runTestsTabRunAction')
const runBtn = document.getElemenstById('runTestsTabRunAction')
runBtn.removeAttribute('disabled')
}
this.areTestsRunning = false
@ -351,6 +351,11 @@ module.exports = class TestTab extends ViewPlugin {
})
}
updateCurrentPath(e) {
this.testTabLogic.setCurrentPath(e.target.value)
// todo check if path exists if not create one.
}
runTests () {
this.areTestsRunning = true
this.hasBeenStopped = false
@ -386,20 +391,22 @@ module.exports = class TestTab extends ViewPlugin {
}
updateGenerateFileAction (currentFile) {
let el = yo`<button
class="btn border w-50"
data-id="testTabGenerateTestFile"
title="Generate sample test file."
onclick="${this.testTabLogic.generateTestFile.bind(this.testTabLogic)}"
>
Generate
</button>`
let el = yo`
<button
class="btn border w-50"
data-id="testTabGenerateTestFile"
title="Generate sample test file."
onclick="${this.testTabLogic.generateTestFile.bind(this.testTabLogic)}"
>
Generate
</button>
`
if (
!currentFile ||
(currentFile && currentFile.split('.').pop().toLowerCase() !== 'sol')
) {
el.setAttribute('disabled', 'disabled')
el.setAttribute('title', 'No solidity file selected')
//el.setAttribute('disabled', 'disabled')
//el.setAttribute('title', 'No solidity file selected')
}
if (!this.generateFileActionElement) {
this.generateFileActionElement = el
@ -487,6 +494,24 @@ module.exports = class TestTab extends ViewPlugin {
this.testsOutput = yo`<div class="mx-3 mb-2 pb-4 border-top border-primary" hidden='true' id="solidityUnittestsOutput" data-id="testTabSolidityUnitTestsOutput"></a>`
this.testsExecutionStopped = yo`<label class="text-warning h6" data-id="testTabTestsExecutionStopped">The test execution has been stopped</label>`
this.testsExecutionStoppedError = yo`<label class="text-danger h6" data-id="testTabTestsExecutionStoppedError">The test execution has been stopped because of error(s) in your test file</label>`
this.uiPathList = yo`<datalist id="utPathList"></datalist>`
const availablePaths = yo`
<div>
<input
placeholder="browser/tests"
list="utPathList"
class="custom-select"
id="utPath"
name="utPath"
onchange=${(e)=>this.updateCurrentPath(e)}/>
${this.uiPathList}
</div>
`
let options = ['browser', 'browser/ll/test']
options.forEach((path) => {
this.uiPathList.appendChild(yo`<option>${path}</option>`)
})
this.testsExecutionStopped.hidden = true
this.testsExecutionStoppedError.hidden = true
this.resultStatistics = this.createResultLabel()
@ -495,7 +520,8 @@ module.exports = class TestTab extends ViewPlugin {
<div class="${css.testTabView} px-2" id="testView">
<div class="${css.infoBox}">
<p class="text-lg"> Test your smart contract in Solidity.</p>
<p> Click on "Generate" to generate a sample test file.</p>
<p> Click on "Generate" to generate a sample test file in.</p>
${availablePaths}
</div>
<div class="${css.tests}">
<div class="d-flex p-2">

@ -5,48 +5,53 @@ class TestTabLogic {
constructor (fileManager) {
this.fileManager = fileManager
this.currentPath = 'browser/tests'
}
setCurrentPath(path) {
this.currentPath = path
}
generateTestFile () {
const path = this.fileManager.currentPath()
const fileName = this.fileManager.currentFile()
const fileProvider = this.fileManager.fileProviderOf(path)
let fileName = this.fileManager.currentFile()
const hasCurrent = !!fileName
if (!fileName) fileName = this.currentPath + '/newFile.sol'
const fileProvider = this.fileManager.fileProviderOf(this.currentPath)
if (!fileProvider) return
helper.createNonClashingNameWithPrefix(fileName, fileProvider, '_test', (error, newFile) => {
const splittedFileName = fileName.split('/')
const fileNameToImport = (!hasCurrent) ? fileName : this.currentPath + '/' + splittedFileName[splittedFileName.length - 1]
//const fileNameToImport = (!fileName) ? fileName : splittedFileName[splittedFileName.length - 1]
helper.createNonClashingNameWithPrefix(fileNameToImport, fileProvider, '_test', (error, newFile) => {
if (error) return modalDialogCustom.alert('Failed to create file. ' + newFile + ' ' + error)
const splittedFileName = fileName.split('/')
// This is fine for now because test file is created on same path where file to be tested is.
// This should be updated to pass complete path, if test file comes from different directory/path
const fileNameToImport = splittedFileName[splittedFileName.length - 1]
if (!fileProvider.set(newFile, this.generateTestContractSample(fileNameToImport))) return modalDialogCustom.alert('Failed to create test file ' + newFile)
if (!fileProvider.set(newFile, this.generateTestContractSample(hasCurrent, fileNameToImport))) return modalDialogCustom.alert('Failed to create test file ' + newFile)
this.fileManager.open(newFile)
})
}
async getTests (cb) {
const path = this.fileManager.currentPath()
if (!path) return cb(null, [])
const provider = this.fileManager.fileProviderOf(path)
if (!this.currentPath) return cb(null, [])
const provider = this.fileManager.fileProviderOf(this.currentPath)
if (!provider) return cb(null, [])
const tests = []
let files
try {
files = await this.fileManager.readdir(path)
files = await this.fileManager.readdir(this.currentPath)
} catch (e) {
cb(e.message)
}
for (var file in files) {
if (/.(_test.sol)$/.exec(file)) tests.push(provider.type + '/' + file)
}
cb(null, tests, path)
cb(null, tests, this.currentPath)
}
// @todo(#2758): If currently selected file is compiled and compilation result is available,
// 'contractName' should be <compiledContractName> + '_testSuite'
generateTestContractSample (fileToImport, contractName = 'testSuite') {
generateTestContractSample (hasCurrent, fileToImport, contractName = 'testSuite') {
const comment = hasCurrent ? `import "${fileToImport}";` : '// Import here the file to test.'
return `pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./${fileToImport}";
${comment}
// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract ${contractName} {
@ -65,7 +70,7 @@ contract ${contractName} {
Assert.notEqual(uint(2), uint(3), "2 should not be equal to 3");
}
function checkSuccess2() public view returns (bool) {
function checkSuccess2() public pure returns (bool) {
// Use the return value (true or false) to test the contract
return true;
}

Loading…
Cancel
Save