add beforeEach; update tests

pull/7/head
Iuri Matias 7 years ago
parent 349080c98f
commit 490db5a7d7
  1. 4
      src/testRunner.js
  2. 17
      tests/examples_1/simple_storage.sol
  3. 23
      tests/examples_1/simple_storage_test.sol
  4. 17
      tests/examples_2/simple_storage.sol
  5. 28
      tests/examples_2/simple_storage_test.sol
  6. 58
      tests/testRunner.js

@ -3,8 +3,8 @@ var changeCase = require('change-case');
function runTest(testName, testObject, testCallback, resultsCallback) { function runTest(testName, testObject, testCallback, resultsCallback) {
let runList = []; let runList = [];
let specialFunctions = ['beforeAll']; let specialFunctions = ['beforeAll', 'beforeEach'];
let availableFunctions = testObject._jsonInterface.filter((x) => x.type === 'function').map((x) => x.name); let availableFunctions = testObject._jsonInterface.reverse().filter((x) => x.type === 'function').map((x) => x.name);
let testFunctions = testObject._jsonInterface.filter((x) => specialFunctions.indexOf(x.name) < 0 && x.type === 'function'); let testFunctions = testObject._jsonInterface.filter((x) => specialFunctions.indexOf(x.name) < 0 && x.type === 'function');
if (availableFunctions.indexOf("beforeAll") >= 0) { if (availableFunctions.indexOf("beforeAll") >= 0) {

@ -0,0 +1,17 @@
pragma solidity ^0.4.7;
contract SimpleStorage {
uint public storedData;
function SimpleStorage() public {
storedData = 100;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}

@ -0,0 +1,23 @@
pragma solidity ^0.4.7;
import "./tests.sol";
import "./simple_storage.sol";
contract MyTest {
SimpleStorage foo;
function beforeAll() {
foo = new SimpleStorage();
}
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;
}
}

@ -0,0 +1,17 @@
pragma solidity ^0.4.7;
contract SimpleStorage {
uint public storedData;
function SimpleStorage() public {
storedData = 100;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}

@ -0,0 +1,28 @@
pragma solidity ^0.4.7;
import "./tests.sol";
import "./simple_storage.sol";
contract MyTest {
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;
}
}

@ -23,20 +23,16 @@ function compileAndDeploy(filename, callback) {
} }
describe('testRunner', function() { describe('testRunner', function() {
describe("simple test", function() { describe("test with beforeAll", function() {
let filename = 'examples/simple_storage_test.sol' let filename = 'tests/examples_1/simple_storage_test.sol'
let tests = [], results = {}; let tests = [], results = {};
before(function(done) { before(function(done) {
compileAndDeploy(filename, function(_err, contracts) { compileAndDeploy(filename, function(_err, contracts) {
var testCallback = function() { var testCallback = function(test) {
console.log("testCallback"); tests.push(test);
console.dir(arguments);
tests.push(arguments);
} }
var resultsCallback = function(_err, _results) { var resultsCallback = function(_err, _results) {
console.log("resultsCallback");
console.dir(_results);
results = _results; results = _results;
done(); done();
} }
@ -48,9 +44,53 @@ describe('testRunner', function() {
assert.equal(results.passingNum, 1) assert.equal(results.passingNum, 1)
}); });
it('should 1 passing test', function() { it('should 1 failing test', function() {
assert.equal(results.failureNum, 1) assert.equal(results.failureNum, 1)
}); });
it('should returns 3 messages', function() {
assert.deepEqual(tests, [
{ type: 'contract', value: 'tests/examples_1/simple_storage_test.sol' },
{ type: 'testPass', value: 'Initial value should be100' },
{ type: 'testFailure', value: 'Initial value should be200' }
]);
});
}); });
describe("test with beforeEach", function() {
let filename = 'tests/examples_2/simple_storage_test.sol'
let tests = [], results = {};
before(function(done) {
compileAndDeploy(filename, function(_err, contracts) {
var testCallback = function(test) {
tests.push(test);
}
var resultsCallback = function(_err, _results) {
results = _results;
done();
}
TestRunner.runTest(filename, contracts.MyTest, testCallback, resultsCallback);
});
});
it('should 2 passing tests', function() {
assert.equal(results.passingNum, 2)
});
it('should 0 failing tests', function() {
assert.equal(results.failureNum, 0)
});
it('should returns 3 messages', function() {
assert.deepEqual(tests, [
{ type: 'contract', value: 'tests/examples_2/simple_storage_test.sol' },
{ type: 'testPass', value: 'Initial value should be100' },
{ type: 'testPass', value: 'Initial value should be200' }
]);
});
});
}); });

Loading…
Cancel
Save