add tests to test_app

This commit is contained in:
Iuri Matias 2017-02-09 21:05:45 -05:00
parent af908bc5f9
commit 98f0bd45e9
10 changed files with 227 additions and 18 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ test_app/dist/
test_app/.embark/development/ test_app/.embark/development/
test_app/config/production/password test_app/config/production/password
test_app/node_modules/ test_app/node_modules/
test_app/chains.json

View File

@ -0,0 +1,65 @@
// https://github.com/nexusdev/erc20/blob/master/contracts/base.sol
pragma solidity ^0.4.2;
contract Token {
event Transfer(address indexed from, address indexed to, uint value);
event Approval( address indexed owner, address indexed spender, uint value);
mapping( address => uint ) _balances;
mapping( address => mapping( address => uint ) ) _approvals;
uint public _supply;
function Token( uint initial_balance ) {
_balances[msg.sender] = initial_balance;
_supply = initial_balance;
}
function totalSupply() constant returns (uint supply) {
return _supply;
}
function balanceOf( address who ) constant returns (uint value) {
return _balances[who];
}
function transfer( address to, uint value) returns (bool ok) {
if( _balances[msg.sender] < value ) {
throw;
}
if( !safeToAdd(_balances[to], value) ) {
throw;
}
_balances[msg.sender] -= value;
_balances[to] += value;
Transfer( msg.sender, to, value );
return true;
}
function transferFrom( address from, address to, uint value) returns (bool ok) {
// if you don't have enough balance, throw
if( _balances[from] < value ) {
throw;
}
// if you don't have approval, throw
if( _approvals[from][msg.sender] < value ) {
throw;
}
if( !safeToAdd(_balances[to], value) ) {
throw;
}
// transfer and return true
_approvals[from][msg.sender] -= value;
_balances[from] -= value;
_balances[to] += value;
Transfer( from, to, value );
return true;
}
function approve(address spender, uint value) returns (bool ok) {
// TODO: should increase instead
_approvals[msg.sender][spender] = value;
Approval( msg.sender, spender, value );
return true;
}
function allowance(address owner, address spender) constant returns (uint _allowance) {
return _approvals[owner][spender];
}
function safeToAdd(uint a, uint b) internal returns (bool) {
return (a + b >= a);
}
}

2
test_app/app/js/_vendor/async.min.js vendored Normal file

File diff suppressed because one or more lines are too long

53
test_app/app/js/test.js Normal file
View File

@ -0,0 +1,53 @@
$(document).ready(function() {
async.waterfall([
function test1(callback) {
AnotherStorage.simpleStorageAddress().then(function(simpleStorageAddress) {
$("#tests").append("<br>test 1: " + (simpleStorageAddress === SimpleStorage.address));
callback();
});
},
function test2(callback) {
SimpleStorage.storedData().then(function(result) {
$("#tests").append("<br>test 2 (true first time): " + (result.toNumber() === 100));
$("#tests").append("<br>test 2 (true after): " + (result.toNumber() === 150));
callback();
});
},
function test3(callback) {
SimpleStorage.set(150).then(function() {
SimpleStorage.get().then(function(result) {
$("#tests").append("<br>test 3: " + (result.toNumber() === 150));
callback();
});
});
},
function test4(callback) {
$("#tests").append("<br>test 4: " + (Token.address === "undefined"));
$("#tests").append("<br>test 4: " + (MyToken.address !== undefined));
$("#tests").append("<br>test 4: " + (MyToken2.address !== undefined));
callback();
},
function test5(callback) {
MyToken._supply().then(function(result) {
$("#tests").append("<br>test 5: " + (result.toNumber() === 1000));
callback();
});
},
function test6(callback) {
MyToken2._supply().then(function(result) {
$("#tests").append("<br>test 6: " + (result.toNumber() === 2000));
callback();
});
},
function test7(callback) {
$("#tests").append("<br>test 7: " + (AlreadyDeployedToken.address === "0x123"));
callback();
}
], function (err, result) {
$("#tests").append("<br>done");
});
});

12
test_app/app/test.html Normal file
View File

@ -0,0 +1,12 @@
<html>
<head>
<title>Embark - TestApp</title>
<link rel="stylesheet" href="css/app.css">
<script src="js/app.js"></script>
</head>
<body class="container">
<div id="tests"></div>
</body>
</html>

View File

@ -1,15 +0,0 @@
{
"0xb6cfeab83614da04c03db0fb8a6787a45d0be8d576fcc6f8f457a5a816d22ab3": {
"name": "development",
"contracts": {
"0x54e0e43bdd12bfce76988ea8f9e936bcdbfff7d5ba494a2522666c4cd57c1490": {
"address": "0x5520587bfafb95e2ef4ed26bb56571050fd3e1b2",
"name": "SimpleStorage"
},
"0x5bfc346ae2758ec398e301cd18b5b42e31817ef2c0eba0de8d921eee73310477": {
"address": "0x042cc0d1ea786d15ad62a025c63fbd47ebdf3237",
"name": "AnotherStorage"
}
}
}
}

View File

@ -11,6 +11,21 @@
"args": [ "args": [
"$SimpleStorage" "$SimpleStorage"
] ]
},
"Token": {
"deploy": false,
"args": [1000]
},
"MyToken": {
"instanceOf": "Token"
},
"MyToken2": {
"instanceOf": "Token",
"args": [2000]
},
"AlreadyDeployedToken": {
"address": "0x123",
"instanceOf": "Token"
} }
} }
} }

View File

@ -4,11 +4,12 @@
"css/app.css": ["app/css/**"], "css/app.css": ["app/css/**"],
"images/": ["app/images/**"], "images/": ["app/images/**"],
"js/app.js": ["embark.js", "app/js/_vendor/jquery.min.js", "app/js/_vendor/bootstrap.min.js", "app/js/**"], "js/app.js": ["embark.js", "app/js/_vendor/jquery.min.js", "app/js/_vendor/bootstrap.min.js", "app/js/**"],
"index.html": "app/index.html" "index.html": "app/index.html",
"test.html": "app/test.html"
}, },
"buildDir": "dist/", "buildDir": "dist/",
"config": "config/", "config": "config/",
"plugins": { "plugins": {
"embark-babel": {} "embark-babel": {"files": ["**/*.js", "**/*.jsx", "!**/_vendor/*.js"]}
} }
} }

View File

@ -11,12 +11,23 @@ describe("AnotherStorage", function() {
}, },
"AnotherStorage": { "AnotherStorage": {
args: ["$SimpleStorage"] args: ["$SimpleStorage"]
},
"Token": {
deploy: false,
args: [1000]
},
"MyToken": {
instanceOf: "Token"
},
"MyToken2": {
instanceOf: "Token",
args: [2000]
} }
}; };
EmbarkSpec.deployAll(contractsConfig, done); EmbarkSpec.deployAll(contractsConfig, done);
}); });
it("should set constructor value", function(done) { it("set SimpleStorage address", function(done) {
AnotherStorage.simpleStorageAddress(function(err, result) { AnotherStorage.simpleStorageAddress(function(err, result) {
assert.equal(result.toString(), SimpleStorage.address); assert.equal(result.toString(), SimpleStorage.address);
done(); done();

View File

@ -0,0 +1,64 @@
var assert = require('assert');
var Embark = require('embark');
var EmbarkSpec = Embark.initTests();
var web3 = EmbarkSpec.web3;
describe("Token", function() {
before(function(done) {
var contractsConfig = {
"SimpleStorage": {
args: [100]
},
"AnotherStorage": {
args: ["$SimpleStorage"]
},
"Token": {
deploy: false,
args: [1000]
},
"MyToken": {
instanceOf: "Token"
},
"MyToken2": {
instanceOf: "Token",
args: [2000]
},
"AlreadyDeployedToken": {
"address": "0x123",
instanceOf: "Token"
}
};
EmbarkSpec.deployAll(contractsConfig, done);
});
it("not deploy Token", function(done) {
assert.equal(Token.address, "undefined");
done();
});
it("not deploy MyToken and MyToken2", function(done) {
assert.notEqual(MyToken.address, "undefined");
assert.notEqual(MyToken2.address, "undefined");
done();
});
it("set MyToken Balance correctly", function(done) {
MyToken._supply(function(err, result) {
assert.equal(result.toNumber(), 1000);
done();
});
});
it("set MyToken2 Balance correctly", function(done) {
MyToken2._supply(function(err, result) {
assert.equal(result.toNumber(), 2000);
done();
});
});
it("get right address", function(done) {
assert.equal(AlreadyDeployedToken.address, "0x123");
done();
});
});