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('functions', () => { | 4 describe('functions', () => { |
5 it('supports declarations', () => { expectTranslate('function x() {}').to.equa
l('x() {}'); }); | 5 it('supports declarations', () => { |
6 it('supports param default values', () => { | 6 expectTranslate('function x() {}').to.equal(`@JS() |
7 expectTranslate('function x(a = 42, b = 1) { return 42; }').to.equal(`x([a =
42, b = 1]) { | 7 external x();`); |
8 return 42; | 8 }); |
9 }`); | 9 it('hide param default values', () => { |
10 expectTranslate('function x(p1, a = 42, b = 1, p2) { return 42; }') | 10 expectTranslate('function x(a = 42, b = 1) { return 42; }').to.equal(`@JS() |
11 .to.equal(`x(p1, [a = 42, b = 1, p2]) { | 11 external x([a, b]);`); |
12 return 42; | 12 expectTranslate('function x(p1, a = 42, b = 1, p2) { return 42; }').to.equal
(`@JS() |
13 }`); | 13 external x(p1, [a, b, p2]);`); |
14 }); | 14 }); |
15 it('translates optional parameters', () => { | 15 it('translates optional parameters', () => { |
16 expectTranslate('function x(a?: number, b?: number) { return 42; }') | 16 expectTranslate('function x(a?: number, b?: number) { return 42; }').to.equa
l(`@JS() |
17 .to.equal(`x([num a, num b]) { | 17 external x([num a, num b]);`); |
18 return 42; | 18 expectTranslate('function x(p1, a?: number, b?: number, p2) { return 42; }')
.to.equal(`@JS() |
19 }`); | 19 external x(p1, [num a, num b, p2]);`); |
20 expectTranslate('function x(p1, a?: number, b?: number, p2) { return 42; }') | |
21 .to.equal(`x(p1, [num a, num b, p2]) { | |
22 return 42; | |
23 }`); | |
24 }); | 20 }); |
25 it('supports empty returns', () => { | 21 it('supports empty returns', () => { |
26 expectTranslate('function x() { return; }').to.equal(`x() { | 22 expectTranslate('function x() { return; }').to.equal(`@JS() |
27 return; | 23 external x();`); |
28 }`); | |
29 }); | 24 }); |
30 | 25 |
31 it('does not support var args', () => { | 26 it('polyfill var args', () => { |
32 expectErroneousCode('function x(...a: number) { return 42; }') | 27 expectTranslate('function x(...a: number[]) { return 42; }').to.equal(`@JS() |
33 .to.throw('rest parameters are unsupported'); | 28 external x([num a1, num a2, num a3, num a4, num a5]);`); |
34 }); | |
35 it('translates function expressions', | |
36 () => { expectTranslate('var a = function() {}').to.equal('var a = () {};')
; }); | |
37 it('translates fat arrow operator', () => { | |
38 expectTranslate('var a = () => {}').to.equal('var a = () {};'); | |
39 expectTranslate('var a = (): string => {}').to.equal('var a = /* String */ (
) {};'); | |
40 expectTranslate('var a = (p) => isBlank(p)').to.equal('var a = (p) => isBlan
k(p);'); | |
41 expectTranslate('var a = (p = null) => isBlank(p)') | |
42 .to.equal('var a = ([p = null]) => isBlank(p);'); | |
43 }); | |
44 it('translates types on function expressions', () => { | |
45 expectTranslate('let a = function(p: string): string { return p; };') | |
46 .to.equal(`var a = /* String */ (String p) { | |
47 return p; | |
48 };`); | |
49 }); | 29 }); |
50 it('supports function parameters', () => { | 30 it('supports function parameters', () => { |
51 expectTranslate('function f(fn: (a: A, b: B) => C) {}').to.equal('f(C fn(A a
, B b)) {}'); | 31 expectTranslate('function f(fn: (a: A, b: B) => C) {}').to.equal(`@JS() |
| 32 external f(C fn(A a, B b));`); |
52 }); | 33 }); |
53 it('supports recursive function parameters', () => { | 34 it('supports recursive function parameters', () => { |
54 expectTranslate('function f(fn: (a: (b: B) => C) => D) {}').to.equal('f(D fn
(C a(B b))) {}'); | 35 expectTranslate('function f(fn: (a: (b: B) => C) => D) {}').to.equal(`@JS() |
| 36 external f(D fn(C a(B b)));`); |
55 }); | 37 }); |
56 it('supports generic-typed function parameters', () => { | 38 it('supports generic-typed function parameters', () => { |
57 expectTranslate('function f<T, U>(fn: (a: T, b: U) => T) {}', { | 39 expectTranslate('function f<T, U>(fn: (a: T, b: U) => T) {}').to.equal(`@JS(
) |
58 translateBuiltins: true | 40 external f/*<T, U>*/(dynamic/*=T*/ fn(dynamic/*=T*/ a, dynamic/*=U*/ b));`); |
59 }).to.equal('f/*< T, U >*/(dynamic/*= T */ fn(dynamic/*= T */ a, dynamic/*=
U */ b)) {}'); | |
60 }); | 41 }); |
61 it('translates functions taking rest parameters to untyped Function', () => { | 42 it('translates functions taking rest parameters to untyped Function', () => { |
62 expectTranslate('function f(fn: (...a: string[]) => number) {}').to.equal('f
(Function fn) {}'); | 43 expectTranslate('function f(fn: (...a: string[]) => number) {}').to.equal(`@
JS() |
| 44 external f(Function /*(...a: string[]) => number*/ fn);`); |
63 }); | 45 }); |
64 }); | 46 }); |
65 | 47 |
| 48 /* TODO(jacobr): support named parameters. |
66 describe('named parameters', () => { | 49 describe('named parameters', () => { |
67 it('supports named parameters', () => { | 50 it('supports named parameters', () => { |
68 expectTranslate('function x({a = "x", b}) { return a + b; }', { | 51 expectTranslate('function x({a = "x", b}) { return a + b; }').to.equal(`x({a
: "x", b}) { |
69 translateBuiltins: true | |
70 }).to.equal(`x({a: "x", b}) { | |
71 return a + b; | 52 return a + b; |
72 }`); | 53 }`); |
73 }); | 54 }); |
74 it('supports types on named parameters', () => { | 55 it('supports types on named parameters', () => { |
75 expectTranslate('function x({a = 1, b = 2}: {a: number, b: number} = {}) { r
eturn a + b; }', { | 56 expectTranslate('function x({a = 1, b = 2}: {a: number, b: number} = {}) { r
eturn a + b; |
76 translateBuiltins: true | 57 }').to.equal(`x({num a: 1, num b: 2}) { |
77 }).to.equal(`x({num a: 1, num b: 2}) { | |
78 return a + b; | 58 return a + b; |
79 }`); | 59 }`); |
80 }); | 60 }); |
81 it('supports reference types on named parameters', () => { | 61 it('supports reference types on named parameters', () => { |
82 expectTranslate( | 62 expectTranslate( |
83 'interface Args { a: string; b: number }\n' + | 63 'interface Args { a: string; b: number }\n' + |
84 'function x({a, b, c}: Args) { return a + b; }', | 64 'function x({a, b, c}: Args) { return a + b; }') |
85 {translateBuiltins: true}) | |
86 .to.equal(`abstract class Args { | 65 .to.equal(`abstract class Args { |
87 String a; | 66 String a; |
88 num b; | 67 num b; |
89 } | 68 } |
90 | 69 |
91 x({String a, num b, c}) { | 70 x({String a, num b, c}) { |
92 return a + b; | 71 return a + b; |
93 }`); | 72 }`); |
94 }); | 73 }); |
95 it('supports declared, untyped named parameters', () => { | 74 it('supports declared, untyped named parameters', () => { |
96 expectTranslate('function x({a, b}: {a: number, b}) { return a + b; }', { | 75 expectTranslate('function x({a, b}: {a: number, b}) { return a + b; }').to.e
qual(`x({num a, b}) |
97 translateBuiltins: true | 76 { |
98 }).to.equal(`x({num a, b}) { | |
99 return a + b; | 77 return a + b; |
100 }`); | 78 }`); |
101 }); | 79 }); |
102 it('fails for non-property types on named parameters', () => { | 80 it('fails for non-property types on named parameters', () => { |
103 expectErroneousCode( | 81 expectErroneousCode( |
104 'interface X { a(a: number); }\n' + | 82 'interface X { a(a: number); }\n' + |
105 'function x({a}: X) { return a + b; }', | 83 'function x({a}: X) { return a + b; }') |
106 {translateBuiltins: true}) | |
107 .to.throw('X.a used for named parameter definition must be a property'); | 84 .to.throw('X.a used for named parameter definition must be a property'); |
108 }); | 85 }); |
109 }); | 86 }); |
| 87 */ |
110 | 88 |
111 describe('generic functions', () => { | 89 describe('generic functions', () => { |
112 it('supports generic types', () => { | 90 it('supports generic types', () => { |
113 expectTranslate('function sort<T, U>(xs: T[]): T[] { return xs; }', { | 91 expectTranslate('function sort<T, U>(xs: T[]): T[] { return xs; }').to.equal
(`@JS() |
114 translateBuiltins: true | 92 external List<dynamic/*=T*/ > sort/*<T, U>*/(List<dynamic/*=T*/ > xs);`); |
115 }).to.equal(`List<dynamic/*= T */ > sort/*< T, U >*/(List<dynamic/*= T */ >
xs) { | |
116 return xs; | |
117 }`); | |
118 }); | 93 }); |
119 it('replaces type usage sites, but not idents', () => { | 94 it('replaces type usage sites, but not idents', () => { |
120 expectTranslate( | 95 expectTranslate(`function wobble<T, U>(u: U): T { }`).to.equal(`@JS() |
121 `function wobble<T, U>(u: U): T { | 96 external dynamic/*=T*/ wobble/*<T, U>*/(dynamic/*=U*/ u);`); |
122 let t: T = <T>u; | |
123 for (let T of [1, 2]) {} | |
124 return t; | |
125 }`, | |
126 {translateBuiltins: true}) | |
127 .to.equal(`dynamic/*= T */ wobble/*< T, U >*/(dynamic/*= U */ u) { | |
128 dynamic/*= T */ t = (u as dynamic/*= T */); | |
129 for (var T in [1, 2]) {} | |
130 return t; | |
131 }`); | |
132 }); | 97 }); |
133 it('translates generic calls', () => { | 98 it('translates generic calls', () => { |
134 expectTranslate( | 99 expectTranslate(`function wobble<T>(foo: T): T { }`).to.equal(`@JS() |
135 `function wobble<T>(foo: T): T { return foo; } | 100 external dynamic/*=T*/ wobble/*<T>*/(dynamic/*=T*/ foo);`); |
136 let f = foo<string>('hello');`, | |
137 {translateBuiltins: true}) | |
138 .to.equal(`dynamic/*= T */ wobble/*< T >*/(dynamic/*= T */ foo) { | |
139 return foo; | |
140 } | |
141 | |
142 var f = foo/*< String >*/("hello");`); | |
143 }); | 101 }); |
144 }); | 102 }); |
OLD | NEW |