You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
370 lines
16 KiB
370 lines
16 KiB
4 years ago
|
'use strict'
|
||
|
|
||
4 years ago
|
import { NightwatchBrowser } from 'nightwatch'
|
||
4 years ago
|
import init from '../helpers/init'
|
||
|
|
||
|
module.exports = {
|
||
|
|
||
|
before: function (browser: NightwatchBrowser, done) {
|
||
|
init(browser, done)
|
||
|
},
|
||
|
|
||
|
'@sources': function () {
|
||
|
return sources
|
||
|
},
|
||
|
|
||
|
'Should launch solidity unit test plugin': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.clickLaunchIcon('fileExplorers')
|
||
4 years ago
|
.addFile('simple_storage.sol', sources[0]['simple_storage.sol'])
|
||
|
.addFile('ks2a.sol', sources[0]['ks2a.sol'])
|
||
4 years ago
|
.clickLaunchIcon('pluginManager')
|
||
|
.scrollAndClick('*[data-id="pluginManagerComponentActivateButtonsolidityUnitTesting"]')
|
||
|
.click('*[data-id="verticalIconsKindsolidityUnitTesting"]')
|
||
|
.waitForElementPresent('*[data-id="sidePanelSwapitTitle"]')
|
||
|
.assert.containsText('*[data-id="sidePanelSwapitTitle"]', 'SOLIDITY UNIT TESTING')
|
||
4 years ago
|
},
|
||
|
|
||
|
'Should generate test file': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.clickLaunchIcon('fileExplorers')
|
||
4 years ago
|
.openFile('simple_storage.sol')
|
||
4 years ago
|
.click('*[data-id="verticalIconsKindsolidityUnitTesting"]')
|
||
|
.waitForElementPresent('*[data-id="testTabGenerateTestFile"]')
|
||
|
.click('*[data-id="testTabGenerateTestFile"]')
|
||
4 years ago
|
.waitForElementPresent('*[title="tests/simple_storage_test.sol"]')
|
||
4 years ago
|
.clickLaunchIcon('fileExplorers')
|
||
|
.pause(10000)
|
||
4 years ago
|
.openFile('tests/simple_storage_test.sol')
|
||
4 years ago
|
.removeFile('tests/simple_storage_test.sol', 'default_workspace')
|
||
4 years ago
|
},
|
||
|
|
||
|
'Should run simple unit test `simple_storage_test.sol` ': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.addFile('tests/simple_storage_test.sol', sources[0]['tests/simple_storage_test.sol'])
|
||
4 years ago
|
.click('*[data-id="verticalIconsKindsolidityUnitTesting"]')
|
||
|
.waitForElementPresent('*[data-id="testTabCheckAllTests"]')
|
||
|
.click('*[data-id="testTabCheckAllTests"]')
|
||
|
.clickElementAtPosition('.singleTestLabel', 1)
|
||
|
.scrollAndClick('*[data-id="testTabRunTestsTabRunAction"]')
|
||
4 years ago
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'MyTest (/tests/simple_storage_test.sol)', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✓ Initial value should be100', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✓ Value is set200', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✘ Should fail for wrong value200', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'Passing: 2', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'Failing: 1', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'FAIL MyTest (/tests/simple_storage_test.sol)', 120000)
|
||
4 years ago
|
},
|
||
|
|
||
|
'Should run advance unit test using natspec and experimental ABIEncoderV2 `ks2b_test.sol` ': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.clickLaunchIcon('fileExplorers')
|
||
4 years ago
|
.addFile('tests/ks2b_test.sol', sources[0]['tests/ks2b_test.sol'])
|
||
4 years ago
|
.click('*[data-id="verticalIconsKindsolidityUnitTesting"]')
|
||
|
.waitForElementPresent('*[data-id="testTabCheckAllTests"]')
|
||
|
.click('*[data-id="testTabCheckAllTests"]')
|
||
|
.clickElementAtPosition('.singleTestLabel', 2)
|
||
|
.scrollAndClick('*[data-id="testTabRunTestsTabRunAction"]')
|
||
4 years ago
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '/tests/ks2b_test.sol', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✓ Check project exists', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✘ Check wrong project owner', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✘ Check wrong sender', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✘ Check wrong value', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '✓ Check project is fundable', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'owner is incorrect', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'wrong sender', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'wrong value', 120000)
|
||
4 years ago
|
},
|
||
|
|
||
|
'Should stop unit tests during test execution` ': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.waitForElementPresent('*[data-id="testTabRunTestsTabRunAction"]')
|
||
|
.clickElementAtPosition('.singleTestLabel', 0)
|
||
|
.clickElementAtPosition('.singleTestLabel', 1)
|
||
|
.scrollAndClick('*[data-id="testTabRunTestsTabRunAction"]')
|
||
|
.pause(5000)
|
||
|
.click('*[data-id="testTabRunTestsTabStopAction"]')
|
||
4 years ago
|
.waitForElementContainsText('*[data-id="testTabRunTestsTabStopAction"]', 'Stopping', 60000)
|
||
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '/tests/ks2b_test.sol', 120000)
|
||
4 years ago
|
.notContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '/tests/4_Ballot_test.sol')
|
||
|
.notContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', '/tests/simple_storage_test.sol')
|
||
4 years ago
|
.waitForElementContainsText('*[data-id="testTabTestsExecutionStopped"]', 'The test execution has been stopped', 60000)
|
||
4 years ago
|
},
|
||
|
|
||
|
'Should fail on compilation': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.addFile('tests/compilationError_test.sol', sources[0]['compilationError_test.sol'])
|
||
4 years ago
|
.clickLaunchIcon('fileExplorers')
|
||
4 years ago
|
.openFile('tests/compilationError_test.sol')
|
||
4 years ago
|
.clickLaunchIcon('solidityUnitTesting')
|
||
|
.click('*[data-id="testTabCheckAllTests"]')
|
||
|
.clickElementAtPosition('.singleTestLabel', 3)
|
||
|
.scrollAndClick('*[data-id="testTabRunTestsTabRunAction"]')
|
||
4 years ago
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'SyntaxError: No visibility specified', 120000)
|
||
|
.waitForElementContainsText('*[data-id="testTabTestsExecutionStoppedError"]', 'The test execution has been stopped because of error(s) in your test file', 120000)
|
||
4 years ago
|
},
|
||
|
|
||
|
'Should fail on deploy': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.addFile('tests/deployError_test.sol', sources[0]['tests/deployError_test.sol'])
|
||
4 years ago
|
.clickLaunchIcon('fileExplorers')
|
||
4 years ago
|
.openFile('tests/deployError_test.sol')
|
||
4 years ago
|
.clickLaunchIcon('solidityUnitTesting')
|
||
|
.click('*[data-id="testTabCheckAllTests"]')
|
||
|
.clickElementAtPosition('.singleTestLabel', 4)
|
||
|
.scrollAndClick('*[data-id="testTabRunTestsTabRunAction"]')
|
||
4 years ago
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'contract deployment failed after trying twice', 120000)
|
||
4 years ago
|
},
|
||
|
|
||
|
'Should fail when parameters are to method in test contract': function (browser: NightwatchBrowser) {
|
||
|
browser.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.addFile('tests/methodFailure_test.sol', sources[0]['tests/methodFailure_test.sol'])
|
||
4 years ago
|
.clickLaunchIcon('fileExplorers')
|
||
4 years ago
|
.openFile('tests/methodFailure_test.sol')
|
||
4 years ago
|
.clickLaunchIcon('solidityUnitTesting')
|
||
|
.click('*[data-id="testTabCheckAllTests"]')
|
||
|
.clickElementAtPosition('.singleTestLabel', 5)
|
||
|
.scrollAndClick('*[data-id="testTabRunTestsTabRunAction"]')
|
||
4 years ago
|
.waitForElementContainsText('*[data-id="testTabSolidityUnitTestsOutput"]', 'Method \'add\' can not have parameters inside a test contract', 120000)
|
||
4 years ago
|
},
|
||
|
|
||
|
'Changing current path': function (browser: NightwatchBrowser) {
|
||
4 years ago
|
browser
|
||
4 years ago
|
.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
4 years ago
|
.addFile('myTests/simple_storage_test.sol', sources[0]['tests/simple_storage_test.sol'])
|
||
4 years ago
|
.clickLaunchIcon('solidityUnitTesting')
|
||
4 years ago
|
.setValue('*[data-id="uiPathInput"]', 'myTests')
|
||
4 years ago
|
.click('*[data-id="testTabGenerateTestFolder"]')
|
||
4 years ago
|
.clickElementAtPosition('.singleTestLabel', 0)
|
||
|
.scrollAndClick('*[data-id="testTabRunTestsTabRunAction"]')
|
||
4 years ago
|
.waitForElementPresent('*[data-id="testTabSolidityUnitTestsOutputheader"]', 60000)
|
||
4 years ago
|
.waitForElementPresent('*[data-id="testTabSolidityUnitTestsOutput"]')
|
||
|
.clearValue('*[data-id="uiPathInput"]')
|
||
4 years ago
|
.setValue('*[data-id="uiPathInput"]', 'tests')
|
||
4 years ago
|
.click('*[data-id="testTabGenerateTestFolder"]')
|
||
4 years ago
|
},
|
||
|
|
||
|
'Solidity Unittests': function (browser: NightwatchBrowser) {
|
||
|
runTests(browser)
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
function runTests (browser: NightwatchBrowser) {
|
||
|
browser
|
||
|
.waitForElementPresent('*[data-id="verticalIconsKindfileExplorers"]')
|
||
|
.clickLaunchIcon('fileExplorers')
|
||
4 years ago
|
.click('*[data-id="treeViewLitreeViewItemcontracts"]')
|
||
|
.openFile('contracts/3_Ballot.sol')
|
||
4 years ago
|
.clickLaunchIcon('solidityUnitTesting')
|
||
|
.pause(500)
|
||
|
.scrollAndClick('#runTestsTabRunAction')
|
||
4 years ago
|
.waitForElementVisible('*[data-id="testTabSolidityUnitTestsOutputheader"]', 120000)
|
||
|
.waitForElementPresent('#solidityUnittestsOutput div[class^="testPass"]', 60000)
|
||
|
.waitForElementContainsText('#solidityUnittestsOutput', '/tests/4_Ballot_test.sol', 60000)
|
||
|
.waitForElementContainsText('#solidityUnittestsOutput', '✓ Check winning proposal', 60000)
|
||
|
.waitForElementContainsText('#solidityUnittestsOutput', '✓ Check winnin proposal with return value', 60000)
|
||
4 years ago
|
.end()
|
||
|
}
|
||
|
|
||
|
const sources = [
|
||
|
{
|
||
4 years ago
|
'simple_storage.sol': {
|
||
4 years ago
|
content: `
|
||
4 years ago
|
pragma solidity >=0.4.22 <0.9.0;
|
||
4 years ago
|
|
||
|
contract SimpleStorage {
|
||
|
uint public storedData;
|
||
|
|
||
4 years ago
|
constructor() {
|
||
4 years ago
|
storedData = 100;
|
||
|
}
|
||
|
|
||
|
function set(uint x) public {
|
||
|
storedData = x;
|
||
|
}
|
||
|
|
||
|
function get() public view returns (uint retVal) {
|
||
|
return storedData;
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
},
|
||
4 years ago
|
'tests/simple_storage_test.sol': {
|
||
4 years ago
|
content: `
|
||
4 years ago
|
pragma solidity >=0.4.22 <0.9.0;
|
||
4 years ago
|
import "remix_tests.sol";
|
||
|
import "../simple_storage.sol";
|
||
|
|
||
|
contract MyTest {
|
||
|
SimpleStorage foo;
|
||
|
|
||
|
function beforeEach() public {
|
||
|
foo = new SimpleStorage();
|
||
|
}
|
||
|
|
||
|
function initialValueShouldBe100() public returns (bool) {
|
||
|
return Assert.equal(foo.get(), 100, "initial value is not correct");
|
||
|
}
|
||
|
|
||
|
function valueIsSet200() public returns (bool) {
|
||
|
foo.set(200);
|
||
|
return Assert.equal(foo.get(), 200, "value is not 200");
|
||
|
}
|
||
|
|
||
|
function shouldFailForWrongValue200() public returns (bool) {
|
||
|
foo.set(300);
|
||
|
return Assert.equal(foo.get(), 200, "value is not 200");
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
},
|
||
4 years ago
|
'ks2a.sol': {
|
||
4 years ago
|
content: `
|
||
4 years ago
|
pragma solidity >=0.4.22 <0.9.0;
|
||
4 years ago
|
contract Kickstarter {
|
||
|
enum State { Started, Completed }
|
||
|
|
||
|
struct Project {
|
||
|
address owner;
|
||
|
string name;
|
||
|
uint goal;
|
||
|
uint fundsAvailable; // added
|
||
|
uint amountContributed; // added
|
||
|
State state;
|
||
|
mapping(address => uint) funders; // added
|
||
|
}
|
||
4 years ago
|
uint numProjects;
|
||
4 years ago
|
Project[] public projects;
|
||
|
|
||
4 years ago
|
constructor() {
|
||
4 years ago
|
}
|
||
|
|
||
|
function createProject(string memory name, uint goal) public {
|
||
4 years ago
|
projects.push(); // new line
|
||
4 years ago
|
Project storage project = projects[projects.length - 1];
|
||
|
project.name = name;
|
||
|
project.goal = goal;
|
||
|
project.owner = msg.sender;
|
||
|
project.state = State.Started;
|
||
|
}
|
||
|
|
||
|
function fundProject(uint projectId) payable public {
|
||
|
Project storage project = projects[projectId];
|
||
|
// require project exists
|
||
|
// PLEASE CHECK / or erase
|
||
|
// not this: require(projects[projectId].exists, "the project must exist to be funded");
|
||
|
|
||
|
// require for... underflow/overflow protection
|
||
|
project.funders[msg.sender] += msg.value;
|
||
|
project.amountContributed += msg.value;
|
||
|
project.fundsAvailable += msg.value;
|
||
|
|
||
|
if (project.amountContributed >= project.goal) {
|
||
|
project.state = State.Completed;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// this function is here because we can't use web3 when using the VM
|
||
|
function getContractBalance() public view returns(uint balance) {
|
||
|
return address(this).balance;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
`
|
||
|
},
|
||
4 years ago
|
'tests/ks2b_test.sol': {
|
||
4 years ago
|
content: `
|
||
4 years ago
|
pragma solidity >=0.4.22 <0.9.0;
|
||
4 years ago
|
pragma experimental ABIEncoderV2;
|
||
|
|
||
|
import "remix_tests.sol"; // this import is automatically injected by Remix.
|
||
|
import "remix_accounts.sol";
|
||
|
import "../ks2a.sol";
|
||
|
|
||
|
contract kickstarterTest {
|
||
|
enum State { Started, Completed }
|
||
|
|
||
|
Kickstarter kickstarter;
|
||
|
|
||
|
function beforeAll () public {
|
||
|
kickstarter = new Kickstarter();
|
||
|
kickstarter.createProject("ProjectA", 123000);
|
||
|
kickstarter.createProject("ProjectB", 100);
|
||
|
}
|
||
|
|
||
|
/// #sender: account-1
|
||
|
/// #value: 10000000
|
||
|
function checkProjectExists () public payable {
|
||
|
(address owner, string memory name, uint goal, uint fundsAvailable, uint amountContributed, Kickstarter.State state) = kickstarter.projects(0);
|
||
|
Assert.equal(name, "ProjectA", "project name is incorrect");
|
||
|
Assert.equal(goal, 123000, "funding goal is incorrect");
|
||
|
Assert.equal(owner, address(this), "owner is incorrect");
|
||
|
Assert.equal(msg.sender, TestsAccounts.getAccount(1), "wrong sender");
|
||
|
Assert.equal(msg.value, 10000000, "wrong value");
|
||
|
}
|
||
|
|
||
|
/// #sender: account-1
|
||
|
/// #value: 10000000
|
||
|
function checkWrongProjectOwner () public payable {
|
||
|
(address owner,,,,,) = kickstarter.projects(0);
|
||
|
Assert.equal(owner, TestsAccounts.getAccount(0), "owner is incorrect"); //failing case
|
||
|
}
|
||
|
|
||
|
/// #sender: account-1
|
||
|
/// #value: 10000000
|
||
|
function checkWrongSender () public payable {
|
||
|
Assert.equal(msg.sender, TestsAccounts.getAccount(0), "wrong sender"); //failing case
|
||
|
}
|
||
|
|
||
|
/// #sender: account-1
|
||
|
/// #value: 10000000
|
||
|
function checkWrongValue () public payable {
|
||
|
Assert.equal(msg.value, 5000000, "wrong value"); //failing case
|
||
|
}
|
||
|
|
||
|
function checkProjectIsFundable () public {
|
||
4 years ago
|
kickstarter.fundProject{value:120000}(0);
|
||
4 years ago
|
(address owner, string memory name, uint goal, uint fundsAvailable, uint amountContributed, Kickstarter.State state) = kickstarter.projects(0);
|
||
|
Assert.equal(amountContributed, 120000, "contributed amount is incorrect");
|
||
|
}
|
||
|
|
||
|
}
|
||
|
`
|
||
|
},
|
||
4 years ago
|
'compilationError_test.sol': {
|
||
4 years ago
|
content: `
|
||
4 years ago
|
pragma solidity ^0.7.0;
|
||
4 years ago
|
|
||
|
contract failOnCompilation {
|
||
|
fallback() {
|
||
|
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
},
|
||
4 years ago
|
'tests/deployError_test.sol': {
|
||
4 years ago
|
content: `
|
||
4 years ago
|
pragma solidity ^0.7.0;
|
||
4 years ago
|
|
||
|
contract failingDeploy {
|
||
4 years ago
|
constructor() {
|
||
4 years ago
|
revert('Deploy Failed');
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
},
|
||
4 years ago
|
'tests/methodFailure_test.sol': {
|
||
4 years ago
|
content: `
|
||
4 years ago
|
pragma solidity ^0.7.0;
|
||
4 years ago
|
|
||
|
contract methodfailure {
|
||
|
function add(uint a, uint b) public {
|
||
|
uint c = a+b;
|
||
|
Assert.equal(a+b, c, "wrong value");
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
}
|
||
|
}
|
||
|
]
|