From d2b790fef93c998f90085aefa83156a7bbfd6e10 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sun, 9 Mar 2014 13:43:36 +0800 Subject: [PATCH] throw error when input to script constructor is not an array [#58] --- src/script.js | 3 +++ test/script.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 test/script.js diff --git a/src/script.js b/src/script.js index 1b48500..aa99322 100644 --- a/src/script.js +++ b/src/script.js @@ -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(); }; diff --git a/test/script.js b/test/script.js new file mode 100644 index 0000000..2850fc7 --- /dev/null +++ b/test/script.js @@ -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({}) + }) + }) + }) + +})