Improved inventory precondition checks to handle objects.

This commit is contained in:
Braydon Fuller 2015-04-01 11:11:06 -04:00
parent 3921c46507
commit 101796f7e9
2 changed files with 21 additions and 5 deletions

View File

@ -7,12 +7,12 @@ var _ = bitcore.deps._;
var utils;
module.exports = utils = {
checkInventory: function(inventory) {
checkInventory: function(arg) {
$.checkArgument(
_.isUndefined(inventory) ||
inventory.length === 0 ||
(!_.isUndefined(inventory[0].type) && !_.isUndefined(inventory[0].hash)),
'Inventory must be an array of inventory objects'
_.isUndefined(arg) ||
(Array.isArray(arg) && arg.length === 0) ||
(Array.isArray(arg) && !_.isUndefined(arg[0].type) && !_.isUndefined(arg[0].hash)),
'Argument is expected to be an array of inventory objects'
);
},
checkFinished: function checkFinished(parser) {

View File

@ -70,6 +70,22 @@ describe('Command Messages', function() {
});
describe('Inventory', function() {
it('should error if arg is not an array', function() {
(function() {
var message = messages.Inventory({});
}).should.throw('Argument is expected to be an array of inventory objects');
});
it('should not error if arg is an empty array', function() {
var message = messages.Inventory([]);
});
it('should error if arg is not an array of inventory objects', function() {
(function() {
var message = messages.Inventory([Number(0)]);
}).should.throw('Argument is expected to be an array of inventory objects');
});
});
describe('Transaction', function() {
it('should be able to pass a custom Transaction', function(done) {