look directory for tests and run each one; update log output

pull/7/head
Iuri Matias 7 years ago
parent 25d308438c
commit 5c64c6c5b6
  1. 29
      examples/simple_storage2_test.sol
  2. 4
      examples/simple_storage_test.sol
  3. 61
      index.js
  4. 6
      run.js
  5. 40
      src/compiler.js

@ -0,0 +1,29 @@
pragma solidity ^0.4.7;
import "./tests.sol";
import "./simple_storage.sol";
contract MyTest2 {
SimpleStorage foo;
uint i = 0;
function beforeEach() {
foo = new SimpleStorage();
if (i == 1) {
foo.set(200);
}
i += 1;
}
function initialValueShouldBe100() public constant returns (bool) {
//return Assert.equal(foo.get(), 100, "initial value is not correct");
return foo.get() == 100;
}
function initialValueShouldBe200() public constant returns (bool) {
//return Assert.equal(foo.get(), 200, "initial value is not correct");
return foo.get() == 200;
}
}

@ -11,12 +11,12 @@ contract MyTest {
function initialValueShouldBe100() public constant returns (bool) {
//return Assert.equal(foo.get(), 100, "initial value is not correct");
return true;
return foo.get() == 100;
}
function initialValueShouldBe200() public constant returns (bool) {
//return Assert.equal(foo.get(), 200, "initial value is not correct");
return false;
return foo.get() == 200;
}
}

@ -1,5 +1,6 @@
const async = require('async');
const path = require('path');
const fs = require('fs');
require('colors');
let Compiler = require('./src/compiler.js');
@ -28,7 +29,7 @@ var runTestFile = function(filename, web3) {
function runTests(contractsToTest, contracts, next) {
var testCallback = function(result) {
if (result.type === 'contract') {
console.log(("#" + result.value).green);
console.log(("\t " + result.value).green);
} else if (result.type === 'testPass') {
console.log("\t✓ ".green.bold + result.value.grey);
} else if (result.type === 'testFailure') {
@ -55,7 +56,65 @@ var runTestFile = function(filename, web3) {
});
}
var runTestFiles = function(directory, web3) {
async.waterfall([
function compile(next) {
Compiler.compileFiles(directory, next);
},
function deployAllContracts(compilationResult, next) {
Deployer.deployAll(compilationResult, web3, function(err, contracts) {
if (err) {
next(err);
}
let contractsToTest = [];
fs.readdirSync(directory).forEach(filename => {
if (filename.indexOf('_test.sol') < 0) {
return;
}
Object.keys(compilationResult[path.basename(filename)]).forEach(contractName => {
contractsToTest.push(contractName)
})
})
next(null, contractsToTest, contracts);
});
},
function runTests(contractsToTest, contracts, next) {
var testCallback = function(result) {
if (result.type === 'contract') {
console.log("\n " + result.value);
} else if (result.type === 'testPass') {
console.log("\t✓ ".green.bold + result.value.grey);
} else if (result.type === 'testFailure') {
console.log("\t✘ ".bold.red + result.value.red);
}
}
var resultsCallback = function(_err, result, cb) {
if (result.passingNum > 0) {
console.log((result.passingNum + " passing").green);
}
if (result.failureNum > 0) {
console.log((result.failureNum + " failing").red);
}
cb();
}
async.eachOfLimit(contractsToTest, 1, (contractName, index, cb) => {
runTest(contractName, contracts[contractName], testCallback, (err, result) => {
if (err) {
return cb(err);
}
resultsCallback(null, result, cb);
});
}, next);
}
], function() {
});
}
module.exports = {
runTestFile: runTestFile,
runTestFiles: runTestFiles,
runTest: runTest
};

@ -1,11 +1,17 @@
const commander = require('commander');
const Web3 = require('web3');
const RemixTests = require('./index.js');
const fs = require('fs');
commander.action(function (filename) {
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
if (fs.lstatSync(filename).isDirectory()) {
RemixTests.runTestFiles(filename, web3);
} else {
RemixTests.runTestFile(filename, web3);
}
});
if (!process.argv.slice(2).length) {

@ -7,6 +7,7 @@ let compilerInput = remixLib.helpers.compiler;
let RemixCompiler = require('remix-solidity').Compiler;
// TODO: replace this with remix's own compiler code
function compileFile(filename, cb) {
let compiler;
const sources = {
@ -32,7 +33,7 @@ function compileFile(filename, cb) {
compiler.event.register('compilationFinished', this, function (success, data, source) {
next(null, data);
});
compiler.compile(sources, "examples/");
compiler.compile(sources, filepath);
}
], function(err, result) {
cb(err, result.contracts);
@ -40,7 +41,42 @@ function compileFile(filename, cb) {
}
function compileFiles(directory, cb) {
let compiler;
const sources = {
"tests.sol": {content: fs.readFileSync("sol/tests.sol").toString()},
}
// TODO: for now assumes filepath dir contains all tests, later all this
// should be replaced with remix's & browser solidity compiler code
fs.readdirSync(directory).forEach(file => {
sources[file] = {content: fs.readFileSync(path.join(directory, file)).toString()}
});
async.waterfall([
function loadCompiler(next) {
compiler = new RemixCompiler();
compiler.onInternalCompilerLoaded();
//compiler.event.register('compilerLoaded', this, function (version) {
next();
//});
},
function doCompilation(next) {
compiler.event.register('compilationFinished', this, function (success, data, source) {
next(null, data);
});
compiler.compile(sources, directory);
}
], function(err, result) {
cb(err, result.contracts);
});
}
module.exports = {
compileFile: compileFile
compileFile: compileFile,
compileFiles: compileFiles
}

Loading…
Cancel
Save