From 5ab92ce8440927b302f79444d76eae26863f9b52 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 11 Jun 2015 11:25:40 -0700 Subject: [PATCH] bug: increase number of supported contstructor args to 20 Closes #2487 --- .../reflection/reflection_capabilities.dart | 34 ++- .../src/reflection/reflection_capabilities.ts | 65 ++++-- .../test/reflection/reflector_spec.ts | 193 +++++++++++++++++- 3 files changed, 267 insertions(+), 25 deletions(-) diff --git a/modules/angular2/src/reflection/reflection_capabilities.dart b/modules/angular2/src/reflection/reflection_capabilities.dart index 14a51da509..ab82be182f 100644 --- a/modules/angular2/src/reflection/reflection_capabilities.dart +++ b/modules/angular2/src/reflection/reflection_capabilities.dart @@ -44,9 +44,41 @@ class ReflectionCapabilities implements PlatformReflectionCapabilities { case 10: return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) => create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]).reflectee; + case 11: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11]).reflectee; + case 12: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12]).reflectee; + case 13: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13]).reflectee; + case 14: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14]).reflectee; + case 15: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15]).reflectee; + case 16: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16]).reflectee; + case 17: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17]).reflectee; + case 18: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18]).reflectee; + case 19: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19]). + reflectee; + case 20: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) => + create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20]). + reflectee; } - throw "Cannot create a factory for '${stringify(type)}' because its constructor has more than 10 arguments"; + throw "Cannot create a factory for '${stringify(type)}' because its constructor has more than 20 arguments"; } List parameters(typeOrFunc) { diff --git a/modules/angular2/src/reflection/reflection_capabilities.ts b/modules/angular2/src/reflection/reflection_capabilities.ts index 25f7abf84a..5e3d1de9bb 100644 --- a/modules/angular2/src/reflection/reflection_capabilities.ts +++ b/modules/angular2/src/reflection/reflection_capabilities.ts @@ -11,37 +11,66 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities { factory(t: Type): Function { switch (t.length) { case 0: - return function() { return new t(); }; + return () => new t(); case 1: - return function(a1) { return new t(a1); }; + return (a1) => new t(a1); case 2: - return function(a1, a2) { return new t(a1, a2); }; + return (a1, a2) => new t(a1, a2); case 3: - return function(a1, a2, a3) { return new t(a1, a2, a3); }; + return (a1, a2, a3) => new t(a1, a2, a3); case 4: - return function(a1, a2, a3, a4) { return new t(a1, a2, a3, a4); }; + return (a1, a2, a3, a4) => new t(a1, a2, a3, a4); case 5: - return function(a1, a2, a3, a4, a5) { return new t(a1, a2, a3, a4, a5); }; + return (a1, a2, a3, a4, a5) => new t(a1, a2, a3, a4, a5); case 6: - return function(a1, a2, a3, a4, a5, a6) { return new t(a1, a2, a3, a4, a5, a6); }; + return (a1, a2, a3, a4, a5, a6) => new t(a1, a2, a3, a4, a5, a6); case 7: - return function(a1, a2, a3, a4, a5, a6, a7) { return new t(a1, a2, a3, a4, a5, a6, a7); }; + return (a1, a2, a3, a4, a5, a6, a7) => new t(a1, a2, a3, a4, a5, a6, a7); case 8: - return function(a1, a2, a3, a4, a5, a6, a7, a8) { - return new t(a1, a2, a3, a4, a5, a6, a7, a8); - }; + return (a1, a2, a3, a4, a5, a6, a7, a8) => new t(a1, a2, a3, a4, a5, a6, a7, a8); case 9: - return function(a1, a2, a3, a4, a5, a6, a7, a8, a9) { - return new t(a1, a2, a3, a4, a5, a6, a7, a8, a9); - }; + return (a1, a2, a3, a4, a5, a6, a7, a8, a9) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9); case 10: - return function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) { - return new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); - }; + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); + case 11: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11); + case 12: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12); + case 13: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13); + case 14: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); + case 15: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15); + case 16: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16); + case 17: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, + a17); + case 18: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) => + new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, + a18); + case 19: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, + a19) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, + a17, a18, a19); + case 20: + return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, + a19, a20) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, + a16, a17, a18, a19, a20); }; throw new Error( - `Cannot create a factory for '${stringify(t)}' because its constructor has more than 10 arguments`); + `Cannot create a factory for '${stringify(t)}' because its constructor has more than 20 arguments`); } _zipTypesAndAnnotaions(paramTypes, paramAnnotations): List> { diff --git a/modules/angular2/test/reflection/reflector_spec.ts b/modules/angular2/test/reflection/reflector_spec.ts index 91a1540b48..3c7f256e2e 100644 --- a/modules/angular2/test/reflection/reflector_spec.ts +++ b/modules/angular2/test/reflection/reflector_spec.ts @@ -2,6 +2,7 @@ import {describe, it, iit, ddescribe, expect, beforeEach, IS_DARTIUM} from 'angu import {Reflector} from 'angular2/src/reflection/reflection'; import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities'; import {ClassDecorator, ParamDecorator, classDecorator, paramDecorator} from './reflector_common'; +import {List} from 'angular2/src/facade/collection'; class AType { value; @@ -24,10 +25,6 @@ class ClassWithoutDecorators { constructor(a, b) {} } -class TestObjWith11Args { - constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) {} -} - class TestObj { a; b; @@ -58,8 +55,37 @@ export function main() { expect(obj.b).toEqual(2); }); - it("should throw when more than 10 arguments", () => { - expect(() => reflector.factory(TestObjWith11Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)) + it("should check args from no to max", () => { + var f = t => reflector.factory(t); + var checkArgs = (obj, args) => expect(obj.args).toEqual(args); + + // clang-format off + checkArgs(f(TestObjWith00Args)(), []); + checkArgs(f(TestObjWith01Args)(1), [1]); + checkArgs(f(TestObjWith02Args)(1, 2), [1, 2]); + checkArgs(f(TestObjWith03Args)(1, 2, 3), [1, 2, 3]); + checkArgs(f(TestObjWith04Args)(1, 2, 3, 4), [1, 2, 3, 4]); + checkArgs(f(TestObjWith05Args)(1, 2, 3, 4, 5), [1, 2, 3, 4, 5]); + checkArgs(f(TestObjWith06Args)(1, 2, 3, 4, 5, 6), [1, 2, 3, 4, 5, 6]); + checkArgs(f(TestObjWith07Args)(1, 2, 3, 4, 5, 6, 7), [1, 2, 3, 4, 5, 6, 7]); + checkArgs(f(TestObjWith08Args)(1, 2, 3, 4, 5, 6, 7, 8), [1, 2, 3, 4, 5, 6, 7, 8]); + checkArgs(f(TestObjWith09Args)(1, 2, 3, 4, 5, 6, 7, 8, 9), [1, 2, 3, 4, 5, 6, 7, 8, 9]); + checkArgs(f(TestObjWith10Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + checkArgs(f(TestObjWith11Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + checkArgs(f(TestObjWith12Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); + checkArgs(f(TestObjWith13Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); + checkArgs(f(TestObjWith14Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); + checkArgs(f(TestObjWith15Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); + checkArgs(f(TestObjWith16Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + checkArgs(f(TestObjWith17Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]); + checkArgs(f(TestObjWith18Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]); + checkArgs(f(TestObjWith19Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]); + checkArgs(f(TestObjWith20Args)(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]); + // clang-format on + }); + + it("should throw when more than 20 arguments", () => { + expect(() => reflector.factory(TestObjWith21Args)) .toThrowError(); }); @@ -165,3 +191,158 @@ export function main() { }); }); } + + +class TestObjWith00Args { + args: List; + constructor() { + this.args = []; + } +} + +class TestObjWith01Args { + args: List; + constructor(a1) { + this.args = [a1]; + } +} + +class TestObjWith02Args { + args: List; + constructor(a1, a2) { + this.args = [a1, a2]; + } +} + +class TestObjWith03Args { + args: List; + constructor(a1, a2, a3) { + this.args = [a1, a2, a3]; + } +} + +class TestObjWith04Args { + args: List; + constructor(a1, a2, a3, a4) { + this.args = [a1, a2, a3, a4]; + } +} + +class TestObjWith05Args { + args: List; + constructor(a1, a2, a3, a4, a5) { + this.args = [a1, a2, a3, a4, a5]; + } +} + +class TestObjWith06Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6) { + this.args = [a1, a2, a3, a4, a5, a6]; + } +} + +class TestObjWith07Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7) { + this.args = [a1, a2, a3, a4, a5, a6, a7]; + } +} + +class TestObjWith08Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8]; + } +} + +class TestObjWith09Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9]; + } +} + +class TestObjWith10Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]; + } +} + +class TestObjWith11Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11]; + } +} + +class TestObjWith12Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12]; + } +} + +class TestObjWith13Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13]; + } +} + +class TestObjWith14Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14]; + } +} + +class TestObjWith15Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15]; + } +} + +class TestObjWith16Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16]; + } +} + +class TestObjWith17Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17]; + } +} + +class TestObjWith18Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18]; + } +} + +class TestObjWith19Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19]; + } +} + +class TestObjWith20Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20]; + } +} + +class TestObjWith21Args { + args: List; + constructor(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21) { + this.args = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21]; + } +}