parent
05fce5c14b
commit
ba2d67f5e9
@ -0,0 +1,56 @@ |
||||
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; |
||||
} |
||||
return compilationResult; |
||||
} |
||||
|
||||
function compile (source, optimization) { |
||||
var missingInputs = []; |
||||
var result = compiler.compile(source, optimization, function (path) { |
||||
missingInputs.push(path); |
||||
}); |
||||
var key = optimization.toString(); |
||||
for (var k in source.sources) { |
||||
key += k + source.sources[k]; |
||||
} |
||||
key = key.replace(/(\t)|(\n)|( )/g, ''); |
||||
return { |
||||
key: key, |
||||
source: source, |
||||
optimization: optimization, |
||||
missingInputs: missingInputs, |
||||
result: result |
||||
}; |
||||
} |
||||
|
||||
function replaceSolCompiler (results) { |
||||
fs.readFile('./test-browser/mockcompiler/compiler.js', 'utf8', function (error, data) { |
||||
if (error) { |
||||
console.log(error); |
||||
process.exit(1); |
||||
return; |
||||
} |
||||
data = data + '\n\nvar mockData = ' + JSON.stringify(results) + ';\n'; |
||||
fs.writeFile('./soljson.js', data, 'utf8', function (error) { |
||||
if (error) { |
||||
console.log(error); |
||||
process.exit(1); |
||||
return; |
||||
} |
||||
}); |
||||
}); |
||||
} |
@ -0,0 +1,23 @@ |
||||
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]); |
||||
}); |
||||
callback(); |
||||
}); |
||||
} |
||||
}; |
@ -0,0 +1,41 @@ |
||||
var Module = { // eslint-disable-line
|
||||
cwrap: function () { return arguments[0] === 'version' ? version : compile; }, |
||||
writeStringToMemory: function () {}, |
||||
setValue: function () {}, |
||||
Pointer_stringify: function () {}, |
||||
Runtime: { |
||||
addFunction: function () {}, |
||||
removeFunction: function () {} |
||||
}, |
||||
_compileJSONMulti: {}, |
||||
_compileJSONCallback: {}, |
||||
_compileJSON: {} |
||||
}; |
||||
|
||||
function compile (source, optimization, missingInputs) { |
||||
if (typeof source === 'string') { |
||||
source = JSON.parse(source); |
||||
} |
||||
var key = optimization.toString(); |
||||
for (var k in source.sources) { |
||||
key += k + source.sources[k]; |
||||
} |
||||
key = key.replace(/(\t)|(\n)|( )/g, ''); |
||||
var data = mockData[key]; // eslint-disable-line
|
||||
if (data === undefined) { |
||||
return JSON.stringify({ |
||||
errors: ['mock compiler: source not found'] |
||||
}); |
||||
} else { |
||||
data.missingInputs.map(function (item, i) { |
||||
if (missingInputs) { |
||||
missingInputs(item); |
||||
} |
||||
}); |
||||
} |
||||
return JSON.stringify(data.result); |
||||
} |
||||
|
||||
function version () { |
||||
return 'mock compiler'; |
||||
} |
@ -0,0 +1,77 @@ |
||||
module.exports = { |
||||
'testSimpleContract': { |
||||
'sources': { |
||||
'Untitled1': 'contract test1 {} contract test2 {}' |
||||
} |
||||
}, |
||||
'ballot': { |
||||
'sources': { |
||||
'Untitled1': `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; |
||||
} |
||||
} |
||||
} |
||||
` |
||||
} |
||||
} |
||||
}; |
@ -0,0 +1,31 @@ |
||||
'use strict'; |
||||
var testData = require('../mockcompiler/requests'); |
||||
// var contractHelper = require('../helpers/contracts');
|
||||
|
||||
module.exports = { |
||||
'Ballot': function (browser) { |
||||
runTests(browser, testData); |
||||
} |
||||
}; |
||||
|
||||
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.Untitled1, 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); |
||||
}); |
||||
}*/ |
@ -0,0 +1,28 @@ |
||||
'use strict'; |
||||
var testData = require('../mockcompiler/requests'); |
||||
var contractHelper = require('../helpers/contracts'); |
||||
|
||||
module.exports = { |
||||
'Simple Contract': function (browser) { |
||||
runTests(browser, testData); |
||||
} |
||||
}; |
||||
|
||||
function runTests (browser, testData) { |
||||
browser |
||||
.url('http://127.0.0.1:8080/#version=builtin') |
||||
.waitForElementVisible('.newFile', 10000); |
||||
browser.assert.notEqual(testData, null); |
||||
testSimpleContract(browser, testData.testSimpleContract.sources.Untitled1, 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); |
||||
} |
Loading…
Reference in new issue