|
| 1 | +import {ddescribe, describe, it, iit, expect, beforeEach, IS_DARTIUM} from 'angular2/test_lib'; |
| 2 | +import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities'; |
| 3 | +import {isPresent, global, CONST} from 'angular2/src/facade/lang'; |
| 4 | + |
| 5 | +export function main() { |
| 6 | + var rc; |
| 7 | + beforeEach(() => { |
| 8 | + rc = new ReflectionCapabilities(); |
| 9 | + }); |
| 10 | + |
| 11 | + function mockReflect(mockData, cls) { |
| 12 | + // This only makes sense for JS, but Dart passes trivially too. |
| 13 | + if (!IS_DARTIUM) { |
| 14 | + global.Reflect = { |
| 15 | + 'getMetadata': (key, targetCls) => { |
| 16 | + return (targetCls == cls) ? mockData[key] : null; |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + function assertTestClassAnnotations(annotations) { |
| 23 | + expect(annotations[0]).toBeAnInstanceOf(ClassDec1); |
| 24 | + expect(annotations[1]).toBeAnInstanceOf(ClassDec2); |
| 25 | + } |
| 26 | + |
| 27 | + function assertTestClassParameters(parameters) { |
| 28 | + expect(parameters[0].length).toBe(2); |
| 29 | + expect(parameters[0][0]).toEqual(P1); |
| 30 | + expect(parameters[0][1]).toBeAnInstanceOf(ParamDec); |
| 31 | + |
| 32 | + expect(parameters[1].length).toBe(1); |
| 33 | + expect(parameters[1][0]).toEqual(P2); |
| 34 | + |
| 35 | + |
| 36 | + expect(parameters[2].length).toBe(1); |
| 37 | + expect(parameters[2][0]).toBeAnInstanceOf(ParamDec); |
| 38 | + |
| 39 | + expect(parameters[3].length).toBe(0); |
| 40 | + } |
| 41 | + |
| 42 | + describe('reflection capabilities', () => { |
| 43 | + it('can read out class annotations through annotations key', () => { |
| 44 | + assertTestClassAnnotations(rc.annotations(TestClass)); |
| 45 | + }); |
| 46 | + |
| 47 | + it('can read out parameter annotations through parameters key', () => { |
| 48 | + assertTestClassParameters(rc.parameters(TestClass)); |
| 49 | + }); |
| 50 | + |
| 51 | + // Mocking in the tests below is needed because the test runs through Traceur. |
| 52 | + // After the switch to TS the setup will have to change, where the direct key |
| 53 | + // access will be mocked, and the tests below will be direct. |
| 54 | + it('can read out class annotations though Reflect APIs', () => { |
| 55 | + if (IS_DARTIUM) return; |
| 56 | + mockReflect(mockDataForTestClassDec, TestClassDec); |
| 57 | + assertTestClassAnnotations(rc.annotations(TestClassDec)); |
| 58 | + }); |
| 59 | + |
| 60 | + it('can read out parameter annotations though Reflect APIs', () => { |
| 61 | + if (IS_DARTIUM) return; |
| 62 | + mockReflect(mockDataForTestClassDec, TestClassDec); |
| 63 | + assertTestClassParameters(rc.parameters(TestClassDec)); |
| 64 | + }); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +class ClassDec1 { |
| 69 | + @CONST() |
| 70 | + constructor() {} |
| 71 | +} |
| 72 | + |
| 73 | +class ClassDec2 { |
| 74 | + @CONST() |
| 75 | + constructor() {} |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +class ParamDec { |
| 80 | + @CONST() |
| 81 | + constructor() {} |
| 82 | +} |
| 83 | + |
| 84 | +class P1 {} |
| 85 | +class P2 {} |
| 86 | + |
| 87 | +@ClassDec1() |
| 88 | +@ClassDec2() |
| 89 | +class TestClass { |
| 90 | + constructor(@ParamDec() a: P1, b: P2, @ParamDec() c, d) {} |
| 91 | +} |
| 92 | + |
| 93 | +// Mocking the data stored in global.Reflect as if TS was compiling TestClass above. |
| 94 | +var mockDataForTestClassDec = { |
| 95 | + 'annotations': [new ClassDec1(), new ClassDec2()], |
| 96 | + 'parameters': [new ParamDec(), null, new ParamDec()], |
| 97 | + 'design:paramtypes': [P1, P2, Object, Object] |
| 98 | +}; |
| 99 | +class TestClassDec {} |
0 commit comments