diff --git a/ci/makeMockCompiler.js b/ci/makeMockCompiler.js index e1a9014b21..3ced9b3084 100644 --- a/ci/makeMockCompiler.js +++ b/ci/makeMockCompiler.js @@ -4,20 +4,37 @@ var fs = require('fs') var solc = require('solc/wrapper') var soljson = require('../soljson') var compiler = solc(soljson) -var inputs = require('../test-browser/mockcompiler/requests.js') -var compilationResult = gatherCompilationResults(inputs) -replaceSolCompiler(compilationResult) -function gatherCompilationResults (sol) { - var compilationResult = {} - for (var k in sol) { - var item = sol[k] - var result = compile(item, 1) - compilationResult[result.key] = result - result = compile(item, 0) - compilationResult[result.key] = result +gatherCompilationResults(function (error, data) { + if (error) { + console.log(error) + process.exit(1) + } else { + replaceSolCompiler(data) } - return compilationResult +}) + +function gatherCompilationResults (callback) { + var compilationResult = {} + fs.readdir('./test-browser/tests', 'utf8', function (error, filenames) { + if (error) { + console.log(error) + process.exit(1) + } else { + filenames.map(function (item, i) { + var testDef = require('../test-browser/tests/' + item) + if ('@sources' in testDef) { + var source = testDef['@sources']() + var result = compile(source, 1) + compilationResult[result.key] = result + result = compile(source, 0) + compilationResult[result.key] = result + } + }) + + callback(null, compilationResult) + } + }) } function compile (source, optimization) { diff --git a/src/app/editor.js b/src/app/editor.js index 32309a2c5d..97e1a99af0 100644 --- a/src/app/editor.js +++ b/src/app/editor.js @@ -12,6 +12,7 @@ function Editor (loadingFromGist, storage) { var SOL_CACHE_FILE = null var editor = ace.edit('input') + document.getElementById('input').editor = editor // required to access the editor during tests var sessions = {} var sourceAnnotations = [] diff --git a/src/app/example-contracts.js b/src/app/example-contracts.js index ac5a9dbf06..23458924a9 100644 --- a/src/app/example-contracts.js +++ b/src/app/example-contracts.js @@ -1,67 +1,67 @@ 'use strict' -var ballot = `pragma solidity ^0.4.0 +var ballot = `pragma solidity ^0.4.0; contract Ballot { struct Voter { - uint weight - bool voted - uint8 vote - address delegate + uint weight; + bool voted; + uint8 vote; + address delegate; } struct Proposal { - uint voteCount + uint voteCount; } - address chairperson - mapping(address => Voter) voters - Proposal[] proposals + address chairperson; + mapping(address => Voter) voters; + Proposal[] proposals; /// Create a new ballot with $(_numProposals) different proposals. function Ballot(uint8 _numProposals) { - chairperson = msg.sender - voters[chairperson].weight = 1 - proposals.length = _numProposals + chairperson = msg.sender; + voters[chairperson].weight = 1; + proposals.length = _numProposals; } /// Give $(voter) the right to vote on this ballot. /// May only be called by $(chairperson). function giveRightToVote(address voter) { - if (msg.sender != chairperson || voters[voter].voted) return - voters[voter].weight = 1 + if (msg.sender != chairperson || voters[voter].voted) return; + voters[voter].weight = 1; } /// Delegate your vote to the voter $(to). function delegate(address to) { - Voter sender = voters[msg.sender] // assigns reference - if (sender.voted) return + Voter sender = voters[msg.sender]; // assigns reference + if (sender.voted) return; while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) - to = voters[to].delegate - if (to == msg.sender) return - sender.voted = true - sender.delegate = to - Voter delegate = voters[to] + to = voters[to].delegate; + if (to == msg.sender) return; + sender.voted = true; + sender.delegate = to; + Voter delegate = voters[to]; if (delegate.voted) - proposals[delegate.vote].voteCount += sender.weight + proposals[delegate.vote].voteCount += sender.weight; else - delegate.weight += sender.weight + delegate.weight += sender.weight; } /// Give a single vote to proposal $(proposal). function vote(uint8 proposal) { - Voter sender = voters[msg.sender] - if (sender.voted || proposal >= proposals.length) return - sender.voted = true - sender.vote = proposal - proposals[proposal].voteCount += sender.weight + Voter sender = voters[msg.sender]; + if (sender.voted || proposal >= proposals.length) return; + sender.voted = true; + sender.vote = proposal; + proposals[proposal].voteCount += sender.weight; } function winningProposal() constant returns (uint8 winningProposal) { - uint256 winningVoteCount = 0 + uint256 winningVoteCount = 0; for (uint8 proposal = 0; proposal < proposals.length; proposal++) if (proposals[proposal].voteCount > winningVoteCount) { - winningVoteCount = proposals[proposal].voteCount - winningProposal = proposal + winningVoteCount = proposals[proposal].voteCount; + winningProposal = proposal; } } }` diff --git a/test-browser/helpers/applytestmode.js b/test-browser/helpers/applytestmode.js new file mode 100644 index 0000000000..89366fd7d5 --- /dev/null +++ b/test-browser/helpers/applytestmode.js @@ -0,0 +1,7 @@ +/** + * This script is injected by NightWatch just before starting test + * + */ +console.log('applying test mode') +document.getElementById('input').editor.setBehavioursEnabled(false) // disable bracket auto-match (i.e. automatic injection of closing brackets and other things), so we can enter raw source code. +console.log('test mode applied') diff --git a/test-browser/helpers/contracts.js b/test-browser/helpers/contracts.js index fea2d475d2..e4a71bdc69 100644 --- a/test-browser/helpers/contracts.js +++ b/test-browser/helpers/contracts.js @@ -1,25 +1,26 @@ 'use strict' module.exports = { - checkCompiledContracts: function (browser, compiled, callback) { - browser.execute(function () { - var contracts = document.querySelectorAll('.udapp .contract') - var ret = [] - for (var k in contracts) { - var el = contracts[k] - if (el.querySelector) { - ret.push({ - name: el.querySelector('.title').innerText.replace(el.querySelector('.size').innerText, '').replace(/(\t)|(\r)|(\n)/g, '') // IE/firefox add \r\n - }) - } - } - return ret - }, [''], function (result) { - browser.assert.equal(result.value.length, compiled.length) - result.value.map(function (item, i) { - browser.assert.equal(item.name, compiled[i]) + checkCompiledContracts: checkCompiledContracts, + testContracts: testContracts +} + +function checkCompiledContracts (browser, compiled, callback) { + browser.elements('css selector', '.udapp .contract .title', function (elements) { + elements.value.map(function (item, i) { + browser.elementIdText(item.ELEMENT, function (text) { + browser.assert.equal(text.value.split('\n')[0], compiled[i]) }) - callback() }) - } + callback() + }) +} + +function testContracts (browser, contractCode, compiledContractNames, callback) { + browser + .clearValue('#input textarea') + .click('.newFile') + .setValue('#input textarea', contractCode, function () {}) + .waitForElementPresent('.contract .create', 2000) + checkCompiledContracts(browser, compiledContractNames, callback) } diff --git a/test-browser/helpers/init.js b/test-browser/helpers/init.js new file mode 100644 index 0000000000..314daa61e8 --- /dev/null +++ b/test-browser/helpers/init.js @@ -0,0 +1,7 @@ +module.exports = function (browser, callback) { + browser + .url('http://127.0.0.1:8080/#version=builtin') + .injectScript('test-browser/helpers/applytestmode.js', function () { + callback() + }) +} diff --git a/test-browser/mockcompiler/requests.js b/test-browser/mockcompiler/requests.js deleted file mode 100644 index 40997e5e3a..0000000000 --- a/test-browser/mockcompiler/requests.js +++ /dev/null @@ -1,79 +0,0 @@ -'use strict' - -module.exports = { - 'testSimpleContract': { - 'sources': { - 'Untitled': 'contract test1 {} contract test2 {}' - } - }, - 'ballot': { - 'sources': { - 'Untitled': `pragma solidity ^0.4.0 -contract Ballot { - - struct Voter { - uint weight - bool voted - uint8 vote - address delegate - } - struct Proposal { - uint voteCount - } - - address chairperson - mapping(address => Voter) voters - Proposal[] proposals - - /// Create a new ballot with $(_numProposals) different proposals. - function Ballot(uint8 _numProposals) { - chairperson = msg.sender - voters[chairperson].weight = 1 - proposals.length = _numProposals - } - - /// Give $(voter) the right to vote on this ballot. - /// May only be called by $(chairperson). - function giveRightToVote(address voter) { - if (msg.sender != chairperson || voters[voter].voted) return - voters[voter].weight = 1 - } - - /// Delegate your vote to the voter $(to). - function delegate(address to) { - Voter sender = voters[msg.sender] // assigns reference - if (sender.voted) return - while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) - to = voters[to].delegate - if (to == msg.sender) return - sender.voted = true - sender.delegate = to - Voter delegate = voters[to] - if (delegate.voted) - proposals[delegate.vote].voteCount += sender.weight - else - delegate.weight += sender.weight - } - - /// Give a single vote to proposal $(proposal). - function vote(uint8 proposal) { - Voter sender = voters[msg.sender] - if (sender.voted || proposal >= proposals.length) return - sender.voted = true - sender.vote = proposal - proposals[proposal].voteCount += sender.weight - } - - function winningProposal() constant returns (uint8 winningProposal) { - uint256 winningVoteCount = 0 - for (uint8 proposal = 0; proposal < proposals.length; proposal++) - if (proposals[proposal].voteCount > winningVoteCount) { - winningVoteCount = proposals[proposal].voteCount - winningProposal = proposal - } - } -} -` - } - } -} diff --git a/test-browser/tests/ballot.js b/test-browser/tests/ballot.js index 3985b367e2..b6ca5b0154 100644 --- a/test-browser/tests/ballot.js +++ b/test-browser/tests/ballot.js @@ -1,35 +1,30 @@ 'use strict' +var contractHelper = require('../helpers/contracts') +var examples = require('../../src/app/example-contracts') +var init = require('../helpers/init') -var testData = require('../mockcompiler/requests') -// var contractHelper = require('../helpers/contracts') +var sources = { + 'sources': { + 'Untitled': examples.ballot.content + } +} module.exports = { + before: function (browser, done) { + init(browser, done) + }, + '@sources': function () { + return sources + }, 'Ballot': function (browser) { - runTests(browser, testData) + runTests(browser) } } function runTests (browser, testData) { browser - .url('http://127.0.0.1:8080/#version=builtin') .waitForElementVisible('.newFile', 10000) - browser.assert.notEqual(testData, null) -// TODO add Ballot tests. -> setValue('#input textarea', ... ) is not working properly with that contract. -/* - testBallot(browser, testData.ballot.sources.Untitled, function () { + contractHelper.testContracts(browser, sources.sources.Untitled, ['Ballot'], function () { browser.end() - }); -*/ -} - -/* -function testBallot (browser, contract, callback) { - browser - .click('.newFile') - .clearValue('#input textarea') - .setValue('#input textarea', contract, function () { - browser.pause('10000') - contractHelper.checkCompiledContracts(browser, ['Ballot'], callback) - }) + }) } -*/ diff --git a/test-browser/tests/debugger.js b/test-browser/tests/debugger.js index 80daaaf3b6..280f0de69e 100644 --- a/test-browser/tests/debugger.js +++ b/test-browser/tests/debugger.js @@ -1,9 +1,12 @@ 'use strict' +var init = require('../helpers/init') module.exports = { + before: function (browser, done) { + init(browser, done) + }, 'Debugger Render': function (browser) { browser - .url('http://127.0.0.1:8080/#version=builtin') .waitForElementPresent('#debugger', 10000) .waitForElementPresent('#debugger #slider', 10000) .end() diff --git a/test-browser/tests/new-file-test.js b/test-browser/tests/new-file-test.js index 5438adfaa3..6d33faa57b 100644 --- a/test-browser/tests/new-file-test.js +++ b/test-browser/tests/new-file-test.js @@ -1,9 +1,12 @@ 'use strict' +var init = require('../helpers/init') module.exports = { + before: function (browser, done) { + init(browser, done) + }, 'New file test': function (browser) { browser - .url('http://127.0.0.1:8080/#version=builtin') .waitForElementVisible('.newFile', 10000) .click('.newFile') .pause('10000') diff --git a/test-browser/tests/simpleContract.js b/test-browser/tests/simpleContract.js index cb7d126b81..d9c089938d 100644 --- a/test-browser/tests/simpleContract.js +++ b/test-browser/tests/simpleContract.js @@ -1,29 +1,29 @@ 'use strict' - -var testData = require('../mockcompiler/requests') var contractHelper = require('../helpers/contracts') +var init = require('../helpers/init') + +var sources = { + 'sources': { + 'Untitled': 'contract test1 {} contract test2 {}' + } +} module.exports = { + before: function (browser, done) { + init(browser, done) + }, + '@sources': function () { + return sources + }, 'Simple Contract': function (browser) { - runTests(browser, testData) + runTests(browser) } } -function runTests (browser, testData) { +function runTests (browser) { browser - .url('http://127.0.0.1:8080/#version=builtin') .waitForElementVisible('.newFile', 10000) - browser.assert.notEqual(testData, null) - testSimpleContract(browser, testData.testSimpleContract.sources.Untitled, function () { + contractHelper.testContracts(browser, sources.sources.Untitled, ['test1', 'test2'], function () { browser.end() }) } - -function testSimpleContract (browser, contract, callback) { - browser - .click('.newFile') - .clearValue('#input textarea') - .setValue('#input textarea', contract) - .pause('5000') - contractHelper.checkCompiledContracts(browser, ['test1', 'test2'], callback) -} diff --git a/test-browser/tests/smoke-test.js b/test-browser/tests/smoke-test.js index d56f8d0cd6..6b75bf2736 100644 --- a/test-browser/tests/smoke-test.js +++ b/test-browser/tests/smoke-test.js @@ -1,9 +1,13 @@ 'use strict' +var init = require('../helpers/init') module.exports = { + before: function (browser, done) { + browser + init(browser, done) + }, 'Smoke test': function (browser) { browser - .url('http://127.0.0.1:8080/#version=builtin') .waitForElementVisible('#righthand-panel', 10000) .pause('10000') .assert.containsText('#righthand-panel', 'Solidity version')