OLD | NEW |
1 /// <reference path="../typings/mocha/mocha.d.ts"/> | 1 /// <reference path="../typings/mocha/mocha.d.ts"/> |
2 import {expectTranslate, expectErroneousCode} from './test_support'; | 2 import {expectTranslate} from './test_support'; |
3 | 3 |
4 describe('decorators', () => { | 4 |
| 5 // We want to ignore decorators on JS interop for now. |
| 6 // These tests make sure we haven't accidentally left in historic code from |
| 7 // ts2dart that export decorators. |
| 8 describe('ignore decorators', () => { |
5 it('translates plain decorators', () => { | 9 it('translates plain decorators', () => { |
6 expectTranslate('@A class X {}').to.equal(`@A | 10 expectTranslate('@A class X {}').to.equal(`@JS() |
7 class X {}`); | 11 class X { |
| 12 // @Ignore |
| 13 X.fakeConstructor$(); |
| 14 }`); |
8 }); | 15 }); |
9 it('translates plain decorators when applied to abstract classes', () => { | 16 it('ignore plain decorators applied to abstract classes', () => { |
10 expectTranslate('@A abstract class X {}').to.equal(`@A | 17 expectTranslate('@A abstract class X {}').to.equal(`@JS() |
11 abstract class X {}`); | 18 abstract class X { |
| 19 // @Ignore |
| 20 X.fakeConstructor$(); |
| 21 }`); |
12 }); | 22 }); |
13 it('translates arguments', () => { | 23 it('translates arguments', () => { |
14 expectTranslate('@A(a, b) class X {}').to.equal(`@A(a, b) | 24 expectTranslate('@A(a, b) class X {}').to.equal(`@JS() |
15 class X {}`); | 25 class X { |
| 26 // @Ignore |
| 27 X.fakeConstructor$(); |
| 28 }`); |
16 }); | 29 }); |
17 it('translates const arguments', () => { | 30 it('translates const arguments', () => { |
18 expectTranslate('@A([1]) class X {}').to.equal(`@A(const [1]) | 31 expectTranslate('@A([1]) class X {}').to.equal(`@JS() |
19 class X {}`); | 32 class X { |
20 expectTranslate('@A({"a": 1}) class X {}').to.equal(`@A(const {"a": 1}) | 33 // @Ignore |
21 class X {}`); | 34 X.fakeConstructor$(); |
22 expectTranslate('@A(new B()) class X {}').to.equal(`@A(const B()) | 35 }`); |
23 class X {}`); | 36 expectTranslate('@A({"a": 1}) class X {}').to.equal(`@JS() |
| 37 class X { |
| 38 // @Ignore |
| 39 X.fakeConstructor$(); |
| 40 }`); |
| 41 expectTranslate('@A(new B()) class X {}').to.equal(`@JS() |
| 42 class X { |
| 43 // @Ignore |
| 44 X.fakeConstructor$(); |
| 45 }`); |
24 }); | 46 }); |
25 it('translates on functions', () => { | 47 it('translates on functions', () => { |
26 expectTranslate('@A function f() {}').to.equal(`@A | 48 expectTranslate('@A function f() {}').to.equal(`@JS() |
27 f() {}`); | 49 external f();`); |
28 }); | 50 }); |
29 it('translates on properties', () => { | 51 it('translates on properties', () => { |
30 expectTranslate('class X { @A p; }').to.equal(`class X { | 52 expectTranslate('class X { @A p; }').to.equal(`@JS() |
31 @A | 53 class X { |
32 var p; | 54 // @Ignore |
| 55 X.fakeConstructor$(); |
| 56 external get p; |
| 57 external set p(v); |
33 }`); | 58 }`); |
34 }); | 59 }); |
35 it('translates on parameters', | 60 it('translates on parameters', () => { |
36 () => { expectTranslate('function f (@A p) {}').to.equal('f(@A p) {}'); }); | 61 expectTranslate('function f (@A p) {}').to.equal(`@JS() |
37 it('special cases @CONST', () => { | 62 external f(p);`); |
38 expectTranslate('@CONST class X {}').to.equal(`class X { | 63 }); |
39 const X(); | 64 it('ignore special cases @CONST', () => { |
| 65 expectTranslate('@CONST class X {}').to.equal(`@JS() |
| 66 class X { |
| 67 // @Ignore |
| 68 X.fakeConstructor$(); |
40 }`); | 69 }`); |
41 expectTranslate('@CONST() class X {}').to.equal(`class X { | 70 expectTranslate('@CONST() class X {}').to.equal(`@JS() |
42 const X(); | 71 class X { |
| 72 // @Ignore |
| 73 X.fakeConstructor$(); |
43 }`); | 74 }`); |
44 expectTranslate(`@CONST class X { | 75 expectTranslate(`@CONST class X { |
45 x: number; | 76 x: number; |
46 y; | 77 y; |
47 constructor() { super(3); this.x = 1; this.y = 2; } | 78 constructor() { super(3); this.x = 1; this.y = 2; } |
48 }`) | 79 }`) |
49 .to.equal(`class X { | 80 .to.equal(`@JS() |
50 final num x; | 81 class X { |
51 final y; | 82 // @Ignore |
52 const X() | 83 X.fakeConstructor$(); |
53 : x = 1, | 84 external num get x; |
54 y = 2, | 85 external set x(num v); |
55 super(3); | 86 external get y; |
| 87 external set y(v); |
| 88 external factory X(); |
56 }`); | 89 }`); |
57 | 90 |
58 // @CONST constructors. | 91 // @CONST constructors. |
59 expectTranslate('@CONST class X { constructor() {} }').to.equal(`class X { | 92 expectTranslate('@CONST class X { constructor() {} }').to.equal(`@JS() |
60 const X(); | 93 class X { |
| 94 // @Ignore |
| 95 X.fakeConstructor$(); |
| 96 external factory X(); |
61 }`); | 97 }`); |
62 // For backwards-compatibility for traceur inputs (not valid TS input) | 98 // For backwards-compatibility for traceur inputs (not valid TS input) |
63 expectTranslate('class X { @CONST constructor() {} }').to.equal(`class X { | 99 expectTranslate('class X { @CONST constructor() {} }').to.equal(`@JS() |
64 const X(); | 100 class X { |
| 101 // @Ignore |
| 102 X.fakeConstructor$(); |
| 103 external factory X(); |
65 }`); | 104 }`); |
66 expectErroneousCode('@CONST class X { constructor() { if (1); } }') | |
67 .to.throw('const constructors can only contain assignments and super cal
ls'); | |
68 expectErroneousCode('@CONST class X { constructor() { f(); } }') | |
69 .to.throw('const constructors can only contain assignments and super cal
ls'); | |
70 expectErroneousCode('@CONST class X { constructor() { "string literal"; } }'
) | |
71 .to.throw('const constructors can only contain assignments and super cal
ls'); | |
72 expectErroneousCode('class X { @CONST constructor() { x = 1; } }') | |
73 .to.throw('assignments in const constructors must assign into this.'); | |
74 expectErroneousCode('class X { @CONST constructor() { thax = 1; } }') | |
75 .to.throw('assignments in const constructors must assign into this.'); | |
76 | 105 |
77 // @CONST properties. | 106 // @CONST properties. |
78 expectTranslate('class Foo { @CONST() static foo = 1; }').to.equal(`class Fo
o { | 107 expectTranslate('class Foo { @CONST() static foo = 1; }').to.equal(`@JS() |
79 static const foo = 1; | 108 class Foo { |
| 109 // @Ignore |
| 110 Foo.fakeConstructor$(); |
| 111 external static get foo; |
| 112 external static set foo(v); |
80 }`); | 113 }`); |
81 }); | 114 }); |
82 }); | 115 }); |
OLD | NEW |