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