add test for the new toTopic filter

This commit is contained in:
Fabian Vogelsteller 2015-04-22 15:47:29 +02:00
parent a6ac6ed36a
commit c9cd31b4ab
9 changed files with 57 additions and 73 deletions

26
dist/web3-light.js vendored
View File

@ -1134,9 +1134,6 @@ var fromDecimal = function (value) {
var toHex = function (val) {
/*jshint maxcomplexity:7 */
if(val === null || typeof val === 'undefined')
return val;
if (isBoolean(val))
return fromDecimal(+val);
@ -2309,6 +2306,25 @@ var RequestManager = require('./requestmanager');
var formatters = require('./formatters');
var utils = require('../utils/utils');
/**
* Converts a given topic to a hex string, but also allows null values.
*
* @param {Mixed} value
* @return {String}
*/
var toTopic = function(value){
if(value === null || typeof value === 'undefined')
return null;
value = String(value);
if(value.indexOf('0x') === 0)
return value;
else
return utils.fromAscii(value);
};
/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones
/// @param should be string or object
/// @returns options string or object
@ -2323,9 +2339,7 @@ var getOptions = function (options) {
// make sure topics, get converted to hex
options.topics = options.topics || [];
options.topics = options.topics.map(function(topic){
return (utils.isArray(topic))
? topic.map(utils.toHex)
: utils.toHex(topic);
return (utils.isArray(topic)) ? topic.map(toTopic) : toTopic(topic);
});
// lazy load

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

26
dist/web3.js vendored
View File

@ -1134,9 +1134,6 @@ var fromDecimal = function (value) {
var toHex = function (val) {
/*jshint maxcomplexity:7 */
if(val === null || typeof val === 'undefined')
return val;
if (isBoolean(val))
return fromDecimal(+val);
@ -2309,6 +2306,25 @@ var RequestManager = require('./requestmanager');
var formatters = require('./formatters');
var utils = require('../utils/utils');
/**
* Converts a given topic to a hex string, but also allows null values.
*
* @param {Mixed} value
* @return {String}
*/
var toTopic = function(value){
if(value === null || typeof value === 'undefined')
return null;
value = String(value);
if(value.indexOf('0x') === 0)
return value;
else
return utils.fromAscii(value);
};
/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones
/// @param should be string or object
/// @returns options string or object
@ -2323,9 +2339,7 @@ var getOptions = function (options) {
// make sure topics, get converted to hex
options.topics = options.topics || [];
options.topics = options.topics.map(function(topic){
return (utils.isArray(topic))
? topic.map(utils.toHex)
: utils.toHex(topic);
return (utils.isArray(topic)) ? topic.map(toTopic) : toTopic(topic);
});
// lazy load

6
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

4
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -37,11 +37,14 @@ var utils = require('../utils/utils');
var toTopic = function(value){
if(value === null || typeof value === 'undefined')
return value;
else if(value.indexOf('0x') === 0)
return null;
value = String(value);
if(value.indexOf('0x') === 0)
return value;
else
return utils.fromAscii(String(value));
return utils.fromAscii(value);
};
/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones
@ -58,9 +61,7 @@ var getOptions = function (options) {
// make sure topics, get converted to hex
options.topics = options.topics || [];
options.topics = options.topics.map(function(topic){
return (utils.isArray(topic))
? topic.map(toTopic)
: toTopic(topic);
return (utils.isArray(topic)) ? topic.map(toTopic) : toTopic(topic);
});
// lazy load

View File

@ -63,7 +63,7 @@ describe('web3.eth.contract', function () {
assert.deepEqual(payload.params[0], {
topics: [
sha3,
'0x1234567890123456789012345678901234567890'
'0x0000000000000000000000001234567890123456789012345678901234567890'
],
address: '0x1234567890123456789012345678901234567890'
});

View File

@ -1,45 +0,0 @@
var chai = require('chai');
var web3 = require('../index');
var assert = chai.assert;
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var method = 'filter';
var tests = [{
args: [{
to: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855',
topics: ['0x324f5435', '0x564b4566f3453']
}],
formattedArgs: [{
to: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855',
topics: ['0x324f5435', '0x564b4566f3453']
}],
result: '0xf',
formattedResult: '0xf',
call: 'shh_newFilter'
}];
describe('shh', function () {
describe(method, function () {
tests.forEach(function (test, index) {
it('property test: ' + index, function () {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
provider.injectResult(test.result);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.call);
assert.deepEqual(payload.params, test.formattedArgs);
});
// call
web3.shh[method].apply(null, test.args);
});
});
});
});