generate file working

pull/1862/head
Aniket-Engg 3 years ago committed by Aniket
parent 93af567fa9
commit 3c0ce77009
  1. 80
      libs/remix-ui/solidity-unit-testing/src/lib/logic/testTabLogic.ts

@ -49,7 +49,9 @@ export class TestTabLogic {
const fileNameToImport = (!hasCurrent) ? fileName : this.currentPath + '/' + splittedFileName[splittedFileName.length - 1] const fileNameToImport = (!hasCurrent) ? fileName : this.currentPath + '/' + splittedFileName[splittedFileName.length - 1]
this.helper.createNonClashingNameWithPrefix(fileNameToImport, fileProvider, '_test', (error: any, newFile: any) => { this.helper.createNonClashingNameWithPrefix(fileNameToImport, fileProvider, '_test', (error: any, newFile: any) => {
// if (error) return modalDialogCustom.alert('Failed to create file. ' + newFile + ' ' + error) // if (error) return modalDialogCustom.alert('Failed to create file. ' + newFile + ' ' + error)
// if (!fileProvider.set(newFile, this.generateTestContractSample(hasCurrent, fileName))) return modalDialogCustom.alert('Failed to create test file ' + newFile) const isFileCreated = fileProvider.set(newFile, this.generateTestContractSample(hasCurrent, fileName))
console.log('isFileCreated---->', isFileCreated)
// if (!isFileCreated) return modalDialogCustom.alert('Failed to create test file ' + newFile)
this.fileManager.open(newFile) this.fileManager.open(newFile)
this.fileManager.syncEditor(newFile) this.fileManager.syncEditor(newFile)
}) })
@ -89,52 +91,52 @@ export class TestTabLogic {
const comment = hasCurrent ? `import "${relative}/${remixPath.basename(fileToImport)}";` : '// <import file to test>' const comment = hasCurrent ? `import "${relative}/${remixPath.basename(fileToImport)}";` : '// <import file to test>'
return `// SPDX-License-Identifier: GPL-3.0 return `// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; pragma solidity >=0.4.22 <0.9.0;
// This import is automatically injected by Remix // This import is automatically injected by Remix
import "remix_tests.sol"; import "remix_tests.sol";
// This import is required to use custom transaction context // This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin // Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin // But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol"; import "remix_accounts.sol";
${comment} ${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} {
/// 'beforeAll' runs before all other tests /// 'beforeAll' runs before all other tests
/// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll' /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
function beforeAll() public { function beforeAll() public {
// <instantiate contract> // <instantiate contract>
Assert.equal(uint(1), uint(1), "1 should be equal to 1"); Assert.equal(uint(1), uint(1), "1 should be equal to 1");
} }
function checkSuccess() public { function checkSuccess() public {
// Use 'Assert' methods: https://remix-ide.readthedocs.io/en/latest/assert_library.html // Use 'Assert' methods: https://remix-ide.readthedocs.io/en/latest/assert_library.html
Assert.ok(2 == 2, 'should be true'); Assert.ok(2 == 2, 'should be true');
Assert.greaterThan(uint(2), uint(1), "2 should be greater than to 1"); Assert.greaterThan(uint(2), uint(1), "2 should be greater than to 1");
Assert.lesserThan(uint(2), uint(3), "2 should be lesser than to 3"); Assert.lesserThan(uint(2), uint(3), "2 should be lesser than to 3");
} }
function checkSuccess2() public pure 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;
} }
function checkFailure() public { function checkFailure() public {
Assert.notEqual(uint(1), uint(1), "1 should not be equal to 1"); Assert.notEqual(uint(1), uint(1), "1 should not be equal to 1");
} }
/// Custom Transaction Context: https://remix-ide.readthedocs.io/en/latest/unittesting.html#customization /// Custom Transaction Context: https://remix-ide.readthedocs.io/en/latest/unittesting.html#customization
/// #sender: account-1 /// #sender: account-1
/// #value: 100 /// #value: 100
function checkSenderAndValue() public payable { function checkSenderAndValue() public payable {
// account index varies 0-9, value is in wei // account index varies 0-9, value is in wei
Assert.equal(msg.sender, TestsAccounts.getAccount(1), "Invalid sender"); Assert.equal(msg.sender, TestsAccounts.getAccount(1), "Invalid sender");
Assert.equal(msg.value, 100, "Invalid value"); Assert.equal(msg.value, 100, "Invalid value");
}
} }
}
` `
} }
} }

Loading…
Cancel
Save