Test your smart contract in Solidity.
-
Click on "Generate" to generate a sample test file.
+
Click on "Generate" to generate a sample test file in.
+ ${availablePaths}
diff --git a/src/app/tabs/testTab/testTab.js b/src/app/tabs/testTab/testTab.js
index ce95bf52f5..3988de6b8d 100644
--- a/src/app/tabs/testTab/testTab.js
+++ b/src/app/tabs/testTab/testTab.js
@@ -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 + '_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;
}