Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: test/type_test.ts

Issue 2394683003: JS Interop Facade generation polish.
Patch Set: more cleanup Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/test_support.ts ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /// <reference path="../typings/mocha/mocha.d.ts"/> 1 /// <reference path="../typings/mocha/mocha.d.ts"/>
2 import {expectTranslate} from './test_support'; 2 import {expectTranslate} from './test_support';
3 3
4 describe('types', () => { 4 describe('types', () => {
5 it('supports qualified names', () => { 5 it('supports qualified names', () => {
6 expectTranslate('var x: foo.Bar;').to.equal(`@JS() 6 expectTranslate('var x: foo.Bar;').to.equal(`@JS()
7 external foo.Bar get x; 7 external foo.Bar get x;
8 @JS() 8 @JS()
9 external set x(foo.Bar v);`); 9 external set x(foo.Bar v);`);
10 }); 10 });
11
12 it('supports null types', () => {
13 expectTranslate('export function attr(name: string, value: null);').to.equal (`@JS()
14 external attr(String name, Null value);`);
15
16 expectTranslate(`export function style(name: string, priority?: 'regular' | 'important');`)
17 .to.equal(`@JS()
18 external style(String name, [String /*'regular'|'important'*/ priority]);`);
19
20 expectTranslate(`export function style(name: string, priority?: null | 'impo rtant');`)
21 .to.equal(`@JS()
22 external style(String name, [String /*Null|'important'*/ priority]);`);
23 expectTranslate('var foo: null;').to.equal(`@JS()
24 external Null get foo;
25 @JS()
26 external set foo(Null v);`);
27 });
28 it('supports this return type', () => {
29 expectTranslate('export interface Foo { bar() : this; }').to.equal(`@anonymo us
30 @JS()
31 abstract class Foo {
32 external Foo bar();
33 }`);
34 });
35
11 it('comment type literals', () => { 36 it('comment type literals', () => {
12 expectTranslate('var x: {x: string, y: number};').to.equal(`@JS() 37 expectTranslate('var x: {x: string, y: number};').to.equal(`@JS()
13 external dynamic /*{x: string, y: number}*/ get x; 38 external dynamic /*{x: string, y: number}*/ get x;
14 @JS() 39 @JS()
15 external set x(dynamic /*{x: string, y: number}*/ v);`); 40 external set x(dynamic /*{x: string, y: number}*/ v);`);
16 }); 41 });
17 it('do not translates string index signatures to dartisms', () => { 42 it('do not translates string index signatures to dartisms', () => {
18 // We wish these could be just Map<String, dynamic> but sadly can't support 43 // We wish these could be just Map<String, dynamic> but sadly can't support
19 // that yet. 44 // that yet.
20 expectTranslate('var x: {[k: string]: any[]};').to.equal(`@JS() 45 expectTranslate('var x: {[k: string]: any[]};').to.equal(`@JS()
21 external dynamic /*JSMap of <String,List<dynamic>>*/ get x; 46 external dynamic /*JSMap of <String,List<dynamic>>*/ get x;
22 @JS() 47 @JS()
23 external set x(dynamic /*JSMap of <String,List<dynamic>>*/ v);`); 48 external set x(dynamic /*JSMap of <String,List<dynamic>>*/ v);`);
24 expectTranslate('var x: {[k: number]: number};').to.equal(`@JS() 49 expectTranslate('var x: {[k: number]: number};').to.equal(`@JS()
25 external dynamic /*JSMap of <num,num>*/ get x; 50 external dynamic /*JSMap of <num,num>*/ get x;
26 @JS() 51 @JS()
27 external set x(dynamic /*JSMap of <num,num>*/ v);`); 52 external set x(dynamic /*JSMap of <num,num>*/ v);`);
28 }); 53 });
29 it('drops type literals with index signatures and other properties', () => { 54 it('drops type literals with index signatures and other properties', () => {
30 expectTranslate('var x: {a: number, [k: string]: number};').to.equal(`@JS() 55 expectTranslate('var x: {a: number, [k: string]: number};').to.equal(`@JS()
31 external dynamic /*{a: number, [k: string]: number}*/ get x; 56 external dynamic /*{a: number, [k: string]: number}*/ get x;
32 @JS() 57 @JS()
33 external set x(dynamic /*{a: number, [k: string]: number}*/ v);`); 58 external set x(dynamic /*{a: number, [k: string]: number}*/ v);`);
34 }); 59 });
35 it('does not mangle prototype names', () => { 60 it('does not mangle prototype names', () => {
36 expectTranslate('import toString = require("./somewhere");') 61 expectTranslate('import toString = require("./somewhere");')
37 .to.equal('import "somewhere.dart" as toString;'); 62 .to.equal('import "somewhere.dart" as toString;');
38 }); 63 });
64
39 it('should support union types', () => { 65 it('should support union types', () => {
40
41 expectTranslate('function foo() : number | number[];').to.equal(`@JS() 66 expectTranslate('function foo() : number | number[];').to.equal(`@JS()
42 external dynamic /*num|List<num>*/ foo();`); 67 external dynamic /*num|List<num>*/ foo();`);
43 expectTranslate('var x: number|List<string>;').to.equal(`@JS() 68 expectTranslate('var x: number|List<string>;').to.equal(`@JS()
44 external dynamic /*num|List<String>*/ get x; 69 external dynamic /*num|List<String>*/ get x;
45 @JS() 70 @JS()
46 external set x(dynamic /*num|List<String>*/ v);`); 71 external set x(dynamic /*num|List<String>*/ v);`);
47 expectTranslate('function x(): number|List<{[k: string]: any}> {};').to.equa l(`@JS() 72 expectTranslate('function x(): number|List<{[k: string]: any}> {};').to.equa l(`@JS()
48 external dynamic /*num|List<JSMap of <String,dynamic>>*/ x();`); 73 external dynamic /*num|List<JSMap of <String,dynamic>>*/ x();`);
49 }); 74 });
75
76 it('should support parenthesized types', () => {
77 expectTranslate('function foo() : (number | number[]);').to.equal(`@JS()
78 external dynamic /*num|List<num>*/ foo();`);
79 expectTranslate('var x: (number|List<string>);').to.equal(`@JS()
80 external dynamic /*num|List<String>*/ get x;
81 @JS()
82 external set x(dynamic /*num|List<String>*/ v);`);
83 expectTranslate('function x(): number|(List<{[k: string]: any}>) {};').to.eq ual(`@JS()
84 external dynamic /*num|List<JSMap of <String,dynamic>>*/ x();`);
85 });
86
50 it('should support array types', () => { 87 it('should support array types', () => {
51 expectTranslate('var x: string[] = [];').to.equal(`@JS() 88 expectTranslate('var x: string[] = [];').to.equal(`@JS()
52 external List<String> get x; 89 external List<String> get x;
53 @JS() 90 @JS()
54 external set x(List<String> v);`); 91 external set x(List<String> v);`);
55 }); 92 });
56 it('should support function types', () => { 93 it('should support function types', () => {
57 expectTranslate('var x: (a: string) => string;').to.equal(`import "package:f unc/func.dart"; 94 expectTranslate('var x: (a: string) => string;').to.equal(`import "package:f unc/func.dart";
58 95
59 @JS() 96 @JS()
(...skipping 27 matching lines...) Expand all
87 X.fakeConstructor$(); 124 X.fakeConstructor$();
88 }`); 125 }`);
89 }); 126 });
90 it('should support use', () => { 127 it('should support use', () => {
91 expectTranslate('class X extends Y<A, B> { }').to.equal(`@JS() 128 expectTranslate('class X extends Y<A, B> { }').to.equal(`@JS()
92 class X extends Y<A, B> { 129 class X extends Y<A, B> {
93 // @Ignore 130 // @Ignore
94 X.fakeConstructor$() : super.fakeConstructor$(); 131 X.fakeConstructor$() : super.fakeConstructor$();
95 }`); 132 }`);
96 }); 133 });
97 it('should remove single <void> generic argument', () => { 134 it('should hanlde single <void> generic argument', () => {
98 expectTranslate('var x: X<number>;').to.equal(`@JS() 135 expectTranslate('var x: X<number>;').to.equal(`@JS()
99 external X<num> get x; 136 external X<num> get x;
100 @JS() 137 @JS()
101 external set x(X<num> v);`); 138 external set x(X<num> v);`);
102 expectTranslate('class X extends Y<void> { }').to.equal(`@JS() 139 expectTranslate('class X extends Y<void> { }').to.equal(`@JS()
103 class X extends Y { 140 class X extends Y<Null> {
104 // @Ignore 141 // @Ignore
105 X.fakeConstructor$() : super.fakeConstructor$(); 142 X.fakeConstructor$() : super.fakeConstructor$();
106 }`); 143 }`);
107 }); 144 });
145
146 it('should replace void generic argument with Object', () => {
147 expectTranslate('class X extends Y<void, string> { }').to.equal(`@JS()
148 class X extends Y<Null, String> {
149 // @Ignore
150 X.fakeConstructor$() : super.fakeConstructor$();
151 }`);
152 expectTranslate('var z : Y<Null, string>;').to.equal(`@JS()
153 external Y<Null, String> get z;
154 @JS()
155 external set z(Y<Null, String> v);`);
156 expectTranslate('var z : Y<void, string, void>;').to.equal(`@JS()
157 external Y<Null, String, Null> get z;
158 @JS()
159 external set z(Y<Null, String, Null> v);`);
160
161 });
162
163 it('should create class for type alias literals', () => {
164 expectTranslate(`/**
165 * Event Parameters.
166 */
167 export type EventParameters = {
168 bubbles: boolean;
169 /**
170 * Is cancelable.
171 */
172 cancelable: boolean;
173 };
174
175 export function dispatch(parameters: EventParameters): void;`)
176 .to.equal(`/// Event Parameters.
177 @anonymous
178 @JS()
179 abstract class EventParameters {
180 external bool get bubbles;
181 external set bubbles(bool v);
182
183 /// Is cancelable.
184 external bool get cancelable;
185 external set cancelable(bool v);
186 external factory EventParameters({bool bubbles, bool cancelable});
187 }
188
189 @JS()
190 external void dispatch(EventParameters parameters);`);
191
192 expectTranslate(`/**
193 * Event Parameters.
194 */
195 export type EventParameters<T> = {
196 bubbles: T;
197 /**
198 * Is cancelable.
199 */
200 cancelable: T;
201 };
202
203 export function dispatch(parameters: EventParameters<string>): void;`)
204 .to.equal(`/// Event Parameters.
205 @anonymous
206 @JS()
207 abstract class EventParameters<T> {
208 external T get bubbles;
209 external set bubbles(T v);
210
211 /// Is cancelable.
212 external T get cancelable;
213 external set cancelable(T v);
214 external factory EventParameters({T bubbles, T cancelable});
215 }
216
217 @JS()
218 external void dispatch(EventParameters<String> parameters);`);
219
220 });
221
222 it('should create typedef for type alias function literals', () => {
223 expectTranslate(`
224 export type ValueFn<A, B, T> = (this: T, a: A, b: B) => A;
225
226 export type SimpleValueFn<A, B> = (a: A, b: B) => A;
227
228 export function dispatch(callback: ValueFn<string, number, Element>): void;
229 export function dispatchSimple(callback: SimpleValueFn<string, number>): void;`)
230 .to.equal(`import "dart:html";
231
232 typedef A ValueFn<A, B, T>(/*T this*/ A a, B b);
233 typedef A SimpleValueFn<A, B>(A a, B b);
234 @JS()
235 external void dispatch(ValueFn<String, num, Element> callback);
236 @JS()
237 external void dispatchSimple(SimpleValueFn<String, num> callback);`);
238 });
239
240 it('should handle generic parameters on non dart compatible type aliases', () => {
241 expectTranslate(`
242 export type Triangle<G> = [G, G, G];
243 export type ListOfLists<G> = [G[]];
244
245 export function triangles<T>(): Triangle<T>[];
246 `).to.equal(`/*export type Triangle<G> = [G, G, G];*/
247 /*export type ListOfLists<G> = [G[]];*/
248 @JS()
249 external List<List<dynamic/*=T*/ > /*Tuple of <T,T,T>*/ > triangles/*<T>*/();`);
250
251 });
108 }); 252 });
OLDNEW
« no previous file with comments | « test/test_support.ts ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698