@ -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 ) => {
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 )
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 )
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 ( p ath)
files = await this . fileManager . readdir ( this . currentP ath)
} catch ( e ) {
cb ( e . message )
}
for ( var file in files ) {
if ( /.(_test.sol)$/ . exec ( file ) ) tests . push ( provider . type + '/' + file )
}
cb ( null , tests , p ath)
cb ( null , tests , this . currentP ath)
}
// @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 ;
}