throw error when input to script constructor is not an array

[#58]
This commit is contained in:
Wei Lu 2014-03-09 13:43:36 +08:00
parent 8a2a9cb4e7
commit d2b790fef9
2 changed files with 20 additions and 0 deletions

View file

@ -6,6 +6,9 @@ var network = require('./network');
var Script = function(data) {
this.buffer = data || [];
if(!Array.isArray(data)) {
throw new Error('expect Script to be initialized with Array, but got ' + data)
}
this.parse();
};

17
test/script.js Normal file
View file

@ -0,0 +1,17 @@
var Script = require('../src/script.js')
var assert = require('assert')
describe('Script', function() {
describe('constructor', function() {
it('works for a byte array', function() {
assert.ok(new Script([]))
})
it('throws an error when input is not an array', function() {
assert.throws(function(){
new Script({})
})
})
})
})