diff --git a/lib/patterns/constructor.js b/lib/patterns/constructor.js index 123f080..6c5caf1 100644 --- a/lib/patterns/constructor.js +++ b/lib/patterns/constructor.js @@ -1,7 +1,6 @@ // Load modules. var Component = require('../component') - , util = require('util') - , ComponentCreateError = require('../errors/componentcreate'); + , util = require('util'); /** @@ -32,20 +31,7 @@ util.inherits(ConstructorComponent, Component); ConstructorComponent.prototype.instantiate = function() { var args = [].slice.call(arguments) , ctor = this._ctor; - switch (args.length) { - case 0: return new ctor(); - case 1: return new ctor(args[0]); - case 2: return new ctor(args[0], args[1]); - case 3: return new ctor(args[0], args[1], args[2]); - case 4: return new ctor(args[0], args[1], args[2], args[3]); - case 5: return new ctor(args[0], args[1], args[2], args[3], args[4]); - case 6: return new ctor(args[0], args[1], args[2], args[3], args[4], args[5]); - case 7: return new ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); - case 8: return new ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]); - case 9: return new ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]); - case 10: return new ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]); - } - throw new ComponentCreateError("Constructor for object '" + this.id + "' requires too many arguments"); + return new ctor(...args); } diff --git a/test/patterns/constructor.test.js b/test/patterns/constructor.test.js index 2d7d814..c258c19 100644 --- a/test/patterns/constructor.test.js +++ b/test/patterns/constructor.test.js @@ -1,11 +1,10 @@ /* global describe, it, expect */ -var ConstructorComponent = require('../../lib/patterns/constructor') - , ComponentCreateError = require('../../lib/errors/componentcreate') +var ConstructorComponent = require('../../lib/patterns/constructor'); -describe('ConstructorComponent', function() { - function Animal(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) { +describe('ConstructorComponent', function () { + function Animal(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) { this._a0 = a0; this._a1 = a1; this._a2 = a2; @@ -16,14 +15,16 @@ describe('ConstructorComponent', function() { this._a7 = a7; this._a8 = a8; this._a9 = a9; + this._a10 = a10; + this._a11 = a11; } - + var ctor = new ConstructorComponent('animal', Animal); - - - it('should instantiate with 1 argument', function() { + + + it('should instantiate with 1 argument', function () { var inst = ctor.instantiate('0') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.be.undefined; @@ -36,10 +37,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 2 arguments', function() { + + it('should instantiate with 2 arguments', function () { var inst = ctor.instantiate('0', '1') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -52,10 +53,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 3 arguments', function() { + + it('should instantiate with 3 arguments', function () { var inst = ctor.instantiate('0', '1', '2') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -68,10 +69,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 4 arguments', function() { + + it('should instantiate with 4 arguments', function () { var inst = ctor.instantiate('0', '1', '2', '3') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -84,10 +85,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 5 arguments', function() { + + it('should instantiate with 5 arguments', function () { var inst = ctor.instantiate('0', '1', '2', '3', '4') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -100,10 +101,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 6 arguments', function() { + + it('should instantiate with 6 arguments', function () { var inst = ctor.instantiate('0', '1', '2', '3', '4', '5') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -116,10 +117,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 7 arguments', function() { + + it('should instantiate with 7 arguments', function () { var inst = ctor.instantiate('0', '1', '2', '3', '4', '5', '6') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -132,10 +133,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 8 arguments', function() { + + it('should instantiate with 8 arguments', function () { var inst = ctor.instantiate('0', '1', '2', '3', '4', '5', '6', '7') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -148,10 +149,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.be.undefined; expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 9 arguments', function() { + + it('should instantiate with 9 arguments', function () { var inst = ctor.instantiate('0', '1', '2', '3', '4', '5', '6', '7', '8') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -164,10 +165,10 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.equal('8'); expect(inst._a9).to.be.undefined; }); - - it('should instantiate with 10 arguments', function() { + + it('should instantiate with 10 arguments', function () { var inst = ctor.instantiate('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') - + expect(inst).to.be.an('object'); expect(inst._a0).to.equal('0'); expect(inst._a1).to.equal('1'); @@ -180,10 +181,22 @@ describe('ConstructorComponent', function() { expect(inst._a8).to.equal('8'); expect(inst._a9).to.equal('9'); }); - - it('should throw an error when instantiated with too many arguments', function() { - expect(function() { - ctor.instantiate('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'); - }).to.throw(ComponentCreateError, "Constructor for object 'animal' requires too many arguments"); + + it('should instantiate with more than 10 arguments', function () { + var inst = ctor.instantiate('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11') + + expect(inst).to.be.an('object'); + expect(inst._a0).to.equal('0'); + expect(inst._a1).to.equal('1'); + expect(inst._a2).to.equal('2'); + expect(inst._a3).to.equal('3'); + expect(inst._a4).to.equal('4'); + expect(inst._a5).to.equal('5'); + expect(inst._a6).to.equal('6'); + expect(inst._a7).to.equal('7'); + expect(inst._a8).to.equal('8'); + expect(inst._a9).to.equal('9'); + expect(inst._a10).to.equal('10'); + expect(inst._a11).to.equal('11'); }); });