web3.js/test/web3.eth.contract.js

208 lines
4.9 KiB
JavaScript
Raw Normal View History

2015-01-28 05:20:36 -08:00
var assert = require('assert');
2015-03-08 10:18:52 -07:00
var contract = require('../lib/web3/contract.js');
2015-01-28 05:20:36 -08:00
2015-03-25 15:50:39 -07:00
describe('web3.eth.contract', function() {
2015-01-28 05:20:36 -08:00
it('should create simple contract with one method from abi with explicit type name', function () {
// given
var description = [{
"name": "test(uint256)",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
2015-03-06 07:09:21 -08:00
var Con = contract(description);
var myCon = new Con(null);
2015-01-28 05:20:36 -08:00
// then
2015-03-06 07:09:21 -08:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
2015-01-28 05:20:36 -08:00
});
it('should create simple contract with one method from abi with implicit type name', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
2015-03-06 07:09:21 -08:00
var Con = contract(description);
var myCon = new Con(null);
2015-01-28 05:20:36 -08:00
// then
2015-03-06 07:09:21 -08:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
2015-01-28 05:20:36 -08:00
});
it('should create contract with multiple methods', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
],
}, {
"name": "test2",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
2015-03-06 07:09:21 -08:00
var Con = contract(description);
var myCon = new Con(null);
2015-01-28 05:20:36 -08:00
// then
2015-03-06 07:09:21 -08:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
assert.equal('function', typeof myCon.test2);
assert.equal('function', typeof myCon.test2['uint256']);
2015-01-28 05:20:36 -08:00
});
it('should create contract with overloaded methods', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
],
}, {
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "string"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
2015-03-06 07:09:21 -08:00
var Con = contract(description);
var myCon = new Con(null);
2015-01-28 05:20:36 -08:00
// then
2015-03-06 07:09:21 -08:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
assert.equal('function', typeof myCon.test['string']);
2015-01-28 05:20:36 -08:00
});
it('should create contract with no methods', function () {
// given
var description = [{
"name": "test(uint256)",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
2015-03-06 07:09:21 -08:00
var Con = contract(description);
var myCon = new Con(null);
2015-01-28 05:20:36 -08:00
// then
2015-03-06 07:09:21 -08:00
assert.equal('undefined', typeof myCon.test);
2015-01-28 05:20:36 -08:00
});
2015-01-29 03:35:21 -08:00
it('should create contract with one event', function () {
// given
var description = [{
"name": "test",
"type": "event",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
2015-03-06 07:09:21 -08:00
var Con = contract(description);
var myCon = new Con(null);
2015-01-29 03:35:21 -08:00
// then
2015-03-06 07:09:21 -08:00
assert.equal('function', typeof myCon.test);
assert.equal('function', typeof myCon.test['uint256']);
2015-01-29 03:35:21 -08:00
});
2015-01-28 05:20:36 -08:00
});