From 101796f7e95cc32f902627e84b6d2fb4b67ce2ca Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Apr 2015 11:11:06 -0400 Subject: [PATCH] Improved inventory precondition checks to handle objects. --- lib/messages/utils.js | 10 +++++----- test/messages/commands/index.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/messages/utils.js b/lib/messages/utils.js index d1bd29e..eb3a975 100644 --- a/lib/messages/utils.js +++ b/lib/messages/utils.js @@ -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) { diff --git a/test/messages/commands/index.js b/test/messages/commands/index.js index ed28719..f4df5e6 100644 --- a/test/messages/commands/index.js +++ b/test/messages/commands/index.js @@ -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) {