From 4a62cb43f00928437c2847c1a80f9d1f26ee0496 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Tue, 27 Oct 2015 18:52:28 +0300 Subject: [PATCH] fix Script.set --- lib/script/script.js | 8 +++++--- test/script/script.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/script/script.js b/lib/script/script.js index f6b8edbe2..38fa2d673 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -36,15 +36,17 @@ var Script = function Script(from) { return Script.fromAddress(from); } else if (from instanceof Script) { return Script.fromBuffer(from.toBuffer()); - } else if (typeof from === 'string') { + } else if (_.isString(from)) { return Script.fromString(from); - } else if (typeof from !== 'undefined') { + } else if (_.isObject(from) && _.isArray(from.chunks)) { this.set(from); } }; Script.prototype.set = function(obj) { - this.chunks = obj.chunks || this.chunks; + $.checkArgument(_.isObject(obj)); + $.checkArgument(_.isArray(obj.chunks)); + this.chunks = obj.chunks; return this; }; diff --git a/test/script/script.js b/test/script/script.js index 2434b7b63..a6926ae85 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -15,7 +15,35 @@ describe('Script', function() { it('should make a new script', function() { var script = new Script(); - should.exist(script); + expect(script).to.be.instanceof(Script); + expect(script.chunks).to.deep.equal([]); + }); + + it('should make a new script when from is null', function() { + var script = new Script(null); + expect(script).to.be.instanceof(Script); + expect(script.chunks).to.deep.equal([]); + }); + + describe('#set', function() { + var script = new Script(); + + it('should be object', function() { + expect(function() { + script.set(null); + }).to.throw(/^Invalid Argument$/) + }); + + it('chunks should be array', function() { + expect(function() { + script.set({chunks: 1}); + }).to.throw(/^Invalid Argument$/); + }); + + it('set chunks', function() { + script.set({chunks: [1]}); + expect(script.chunks).to.deep.equal([1]); + }); }); describe('#fromBuffer', function() {