Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 75c056f

Browse files
authored
Merge pull request #10 from fanatid/fix/Script
fix Script.set
2 parents 2b451cc + 4a62cb4 commit 75c056f

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/script/script.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ var Script = function Script(from) {
3636
return Script.fromAddress(from);
3737
} else if (from instanceof Script) {
3838
return Script.fromBuffer(from.toBuffer());
39-
} else if (typeof from === 'string') {
39+
} else if (_.isString(from)) {
4040
return Script.fromString(from);
41-
} else if (typeof from !== 'undefined') {
41+
} else if (_.isObject(from) && _.isArray(from.chunks)) {
4242
this.set(from);
4343
}
4444
};
4545

4646
Script.prototype.set = function(obj) {
47-
this.chunks = obj.chunks || this.chunks;
47+
$.checkArgument(_.isObject(obj));
48+
$.checkArgument(_.isArray(obj.chunks));
49+
this.chunks = obj.chunks;
4850
return this;
4951
};
5052

test/script/script.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,35 @@ describe('Script', function() {
1515

1616
it('should make a new script', function() {
1717
var script = new Script();
18-
should.exist(script);
18+
expect(script).to.be.instanceof(Script);
19+
expect(script.chunks).to.deep.equal([]);
20+
});
21+
22+
it('should make a new script when from is null', function() {
23+
var script = new Script(null);
24+
expect(script).to.be.instanceof(Script);
25+
expect(script.chunks).to.deep.equal([]);
26+
});
27+
28+
describe('#set', function() {
29+
var script = new Script();
30+
31+
it('should be object', function() {
32+
expect(function() {
33+
script.set(null);
34+
}).to.throw(/^Invalid Argument$/)
35+
});
36+
37+
it('chunks should be array', function() {
38+
expect(function() {
39+
script.set({chunks: 1});
40+
}).to.throw(/^Invalid Argument$/);
41+
});
42+
43+
it('set chunks', function() {
44+
script.set({chunks: [1]});
45+
expect(script.chunks).to.deep.equal([1]);
46+
});
1947
});
2048

2149
describe('#fromBuffer', function() {

0 commit comments

Comments
 (0)