diff --git a/lib/script/script.js b/lib/script/script.js index eded4d1..1489920 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 4430ce2..8b3924e 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() {