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

Side by Side Diff: test/js_interop_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/declaration_test.ts ('k') | test/module_test.ts » ('j') | 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 // TODO(jacobr): merge these tests back in with the other tests. These tests are 4 // TODO(jacobr): merge these tests back in with the other tests. These tests are
5 // only separate because we expected at one point to integrate with TS2Dart 5 // only separate because we expected at one point to integrate with TS2Dart
6 // instead of refactoring TS2Dart to only output facades. 6 // instead of refactoring TS2Dart to only output facades.
7 describe('variables', () => { 7 describe('variables', () => {
8 it('should print variable declaration', () => { 8 it('should print variable declaration', () => {
9 expectTranslate('var a:number;').to.equal(`@JS() 9 expectTranslate('var a:number;').to.equal(`@JS()
10 external num get a; 10 external num get a;
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 expectTranslate('class X<T> { Z(): X<T> {} }').to.equal(`@JS() 272 expectTranslate('class X<T> { Z(): X<T> {} }').to.equal(`@JS()
273 class X<T> { 273 class X<T> {
274 // @Ignore 274 // @Ignore
275 X.fakeConstructor$(); 275 X.fakeConstructor$();
276 external X<T> Z(); 276 external X<T> Z();
277 }`); 277 }`);
278 }); 278 });
279 it('merge overrides', () => { 279 it('merge overrides', () => {
280 expectTranslate(` 280 expectTranslate(`
281 class X { 281 class X {
282 createElement(tagName: "img"): HTMLImageElement; 282 createElement<T>(tagName: "img"): T;
283 createElement(tagName: "video"): HTMLVideoElement; 283 createElement<T>(tagName: "video"): T;
284 createElement(tagName: string): HTMLElement; 284 createElement<T>(tagName: string): T;
285 }`).to.equal(`@JS()
286 class X {
287 // @Ignore
288 X.fakeConstructor$();
289 /*external T createElement<T>('img' tagName);*/
290 /*external T createElement<T>('video' tagName);*/
291 /*external T createElement<T>(String tagName);*/
292 external dynamic/*=T*/ createElement/*<T>*/(
293 String /*'img'|'video'|String*/ tagName);
294 }`);
295
296 expectTranslate(`
297 class X {
298 createElement<T>(tagName: "img"): T;
299 createElement<T>(tagName: "video"): T;
300 createElement<V>(tagName: string): V;
301 }`).to.equal(`@JS()
302 class X {
303 // @Ignore
304 X.fakeConstructor$();
305 /*external T createElement<T>('img' tagName);*/
306 /*external T createElement<T>('video' tagName);*/
307 /*external V createElement<V>(String tagName);*/
308 external dynamic /*T|V*/ createElement/*<T, V>*/(
309 String /*'img'|'video'|String*/ tagName);
310 }`);
311
312 expectTranslate(`
313 class X {
314 createElement<T extends HTMLImageElement>(tagName: "img"): T;
315 createElement<T extends HTMLVideoElement>(tagName: "video"): T;
316 createElement<T extends Element>(tagName: string): T;
285 }`).to.equal(`import "dart:html"; 317 }`).to.equal(`import "dart:html";
286 318
287 @JS() 319 @JS()
288 class X { 320 class X {
289 // @Ignore 321 // @Ignore
290 X.fakeConstructor$(); 322 X.fakeConstructor$();
291 /*external ImageElement createElement("img" tagName);*/ 323 /*external T createElement<T extends ImageElement>('img' tagName);*/
292 /*external VideoElement createElement("video" tagName);*/ 324 /*external T createElement<T extends VideoElement>('video' tagName);*/
293 /*external HtmlElement createElement(String tagName);*/ 325 /*external T createElement<T extends Element>(String tagName);*/
294 external dynamic /*ImageElement|VideoElement|HtmlElement*/ createElement( 326 external dynamic/*=T*/ createElement/*<T>*/(
295 String tagName); 327 String /*'img'|'video'|String*/ tagName);
296 }`); 328 }`);
329
330 expectTranslate(`export interface ScaleLinear<O> {
331 (value: number): Output;
332 domain(): Array<O>;
333 }
334
335 export function scaleLinear(): ScaleLinear<number>;
336 export function scaleLinear<O>(): ScaleLinear<O>;`)
337 .to.equal(`@anonymous
338 @JS()
339 abstract class ScaleLinear<O> {
340 external Output call(num value);
341 external List<O> domain();
342 }
343
344 /*external ScaleLinear<num> scaleLinear();*/
345 /*external ScaleLinear<O> scaleLinear<O>();*/
346 @JS()
347 external ScaleLinear /*ScaleLinear<num>|ScaleLinear<O>*/ scaleLinear/*<O>*/();`) ;
348
297 expectTranslate(` 349 expectTranslate(`
298 class X { 350 class X {
299 F(a:string):num; 351 F(a:string):num;
300 F(a:string, b: string|num):string; 352 F(a:string, b: string|num):string;
301 F(a2:string, b: string, c: num):string; 353 F(a2:string, b: string, c: num):string;
302 }`).to.equal(`@JS() 354 }`).to.equal(`@JS()
303 class X { 355 class X {
304 // @Ignore 356 // @Ignore
305 X.fakeConstructor$(); 357 X.fakeConstructor$();
306 /*external num F(String a);*/ 358 /*external num F(String a);*/
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 expectTranslate(` 402 expectTranslate(`
351 interface SampleAudioNode { 403 interface SampleAudioNode {
352 addEventListener(type: "ended", listener: (ev: Event) => any, useCapture?: boo lean): void; 404 addEventListener(type: "ended", listener: (ev: Event) => any, useCapture?: boo lean): void;
353 addEventListener(type: string, listener: EventListenerOrEventListenerObject, u seCapture?: boolean): void; 405 addEventListener(type: string, listener: EventListenerOrEventListenerObject, u seCapture?: boolean): void;
354 }`).to.equal(`import "dart:html"; 406 }`).to.equal(`import "dart:html";
355 import "package:func/func.dart"; 407 import "package:func/func.dart";
356 408
357 @anonymous 409 @anonymous
358 @JS() 410 @JS()
359 abstract class SampleAudioNode { 411 abstract class SampleAudioNode {
360 /*external void addEventListener("ended" type, dynamic listener(Event ev), [bo ol useCapture]);*/ 412 /*external void addEventListener('ended' type, dynamic listener(Event ev), [bo ol useCapture]);*/
361 /*external void addEventListener(String type, EventListener|EventListenerObjec t listener, [bool useCapture]);*/ 413 /*external void addEventListener(String type, EventListener|EventListenerObjec t listener, [bool useCapture]);*/
362 external void addEventListener(String type, 414 external void addEventListener(String /*'ended'|String*/ type,
363 dynamic /*Func1<Event, dynamic>|EventListener|EventListenerObject*/ listen er, 415 dynamic /*Func1<Event, dynamic>|EventListener|EventListenerObject*/ listen er,
364 [bool useCapture]); 416 [bool useCapture]);
365 }`); 417 }`);
366 418
367 expectTranslate(` 419 expectTranslate(`
368 interface ListenObject { 420 interface ListenObject {
369 someDummyMethod(evt: string): void; 421 someDummyMethod(evt: string): void;
370 } 422 }
371 423
372 interface ExampleListener { 424 interface ExampleListener {
373 (evt: string): void; 425 (evt: string): void;
374 } 426 }
375 427
376 interface DummySample { 428 interface DummySample {
377 addEventListener(type: "ended", listener: ListenObject): void; 429 addEventListener(type: 'ended', listener: ListenObject): void;
378 addEventListener(type: string, listener: ExampleListener): void; 430 addEventListener(type: string, listener: ExampleListener): void;
379 }`).to.equal(`@anonymous 431 }`).to.equal(`@anonymous
380 @JS() 432 @JS()
381 abstract class ListenObject { 433 abstract class ListenObject {
382 external void someDummyMethod(String evt); 434 external void someDummyMethod(String evt);
383 } 435 }
384 436
385 typedef void ExampleListener(String evt); 437 typedef void ExampleListener(String evt);
386 438
387 @anonymous 439 @anonymous
388 @JS() 440 @JS()
389 abstract class DummySample { 441 abstract class DummySample {
390 /*external void addEventListener("ended" type, ListenObject listener);*/ 442 /*external void addEventListener('ended' type, ListenObject listener);*/
391 /*external void addEventListener(String type, ExampleListener listener);*/ 443 /*external void addEventListener(String type, ExampleListener listener);*/
392 external void addEventListener( 444 external void addEventListener(String /*'ended'|String*/ type,
393 String type, dynamic /*ListenObject|ExampleListener*/ listener); 445 dynamic /*ListenObject|ExampleListener*/ listener);
394 }`); 446 }`);
395 447
396 expectTranslate(` 448 expectTranslate(`
397 interface ListenAny { 449 interface ListenAny {
398 (evt: any): void; 450 (evt: any): void;
399 } 451 }
400 452
401 interface ExampleListener { 453 interface ExampleListener {
402 (evt: string): void; 454 (evt: string): void;
403 } 455 }
404 456
405 interface DummySample { 457 interface DummySample {
406 addEventListener(type: "ended", listener: ListenAny): void; 458 addEventListener(type: 'ended', listener: ListenAny): void;
407 addEventListener(type: string, listener: ExampleListener): void; 459 addEventListener(type: string, listener: ExampleListener): void;
408 }`).to.equal(`typedef void ListenAny(dynamic evt); 460 }`).to.equal(`typedef void ListenAny(dynamic evt);
409 typedef void ExampleListener(String evt); 461 typedef void ExampleListener(String evt);
410 462
411 @anonymous 463 @anonymous
412 @JS() 464 @JS()
413 abstract class DummySample { 465 abstract class DummySample {
414 /*external void addEventListener("ended" type, ListenAny listener);*/ 466 /*external void addEventListener('ended' type, ListenAny listener);*/
415 /*external void addEventListener(String type, ExampleListener listener);*/ 467 /*external void addEventListener(String type, ExampleListener listener);*/
416 external void addEventListener( 468 external void addEventListener(String /*'ended'|String*/ type,
417 String type, Function /*ListenAny|ExampleListener*/ listener); 469 Function /*ListenAny|ExampleListener*/ listener);
418 }`); 470 }`);
419 471
420 }); 472 });
421 it('dot dot dot', () => { 473 it('dot dot dot', () => {
422 expectTranslate(` 474 expectTranslate(`
423 function buildName(firstName: string, ...restOfName: string[]): string; 475 function buildName(firstName: string, ...restOfName: string[]): string;
424 `).to.equal(`@JS() 476 `).to.equal(`@JS()
425 external String buildName(String firstName, 477 external String buildName(String firstName,
426 [String restOfName1, 478 [String restOfName1,
427 String restOfName2, 479 String restOfName2,
428 String restOfName3, 480 String restOfName3,
429 String restOfName4, 481 String restOfName4,
430 String restOfName5]);`); 482 String restOfName5]);`);
431 expectTranslate(` 483 expectTranslate(`
432 function log(...args);`) 484 function log(...args);`)
433 .to.equal(`@JS() 485 .to.equal(`@JS()
434 external log([args1, args2, args3, args4, args5]);`); 486 external log([args1, args2, args3, args4, args5]);`);
435 }); 487 });
436 it('interface constructors', () => { 488 it('interface constructors', () => {
437 expectTranslate(` 489 expectTranslate(`
438 interface C { 490 interface C {
439 oncached: (ev: Event) => any; 491 oncached: (ev: Event) => any;
440 } 492 }
441 declare var C: {new(): C; CHECKING: number; }`) 493 declare var C: {new(): C; CHECKING: number; }`)
442 .to.equal(`import "package:func/func.dart"; 494 .to.equal(`import "package:func/func.dart";
443 import "dart:html"; 495 import "dart:html";
444 496
445 @JS() 497 @JS("C")
446 abstract class C { 498 abstract class C {
447 external Func1<Event, dynamic> get oncached; 499 external Func1<Event, dynamic> get oncached;
448 external set oncached(Func1<Event, dynamic> v); 500 external set oncached(Func1<Event, dynamic> v);
449 external factory C(); 501 external factory C();
450 external static num get CHECKING; 502 external static num get CHECKING;
451 external static set CHECKING(num v); 503 external static set CHECKING(num v);
452 }`); 504 }`);
453 }); 505 });
454 it('property bag interfaces', () => { 506 it('property bag interfaces', () => {
455 expectTranslate(` 507 expectTranslate(`
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 external B get d; 557 external B get d;
506 external set d(B v); 558 external set d(B v);
507 external dynamic get e; 559 external dynamic get e;
508 external set e(dynamic v); 560 external set e(dynamic v);
509 external factory Y({B d, dynamic e, A a, num b, X c}); 561 external factory Y({B d, dynamic e, A a, num b, X c});
510 }`); 562 }`);
511 }); 563 });
512 it('callable', () => { 564 it('callable', () => {
513 expectTranslate('interface X<T> { (a:T):T; Y():T; }').to.equal(`@anonymous 565 expectTranslate('interface X<T> { (a:T):T; Y():T; }').to.equal(`@anonymous
514 @JS() 566 @JS()
515 abstract class X<T> implements Function { 567 abstract class X<T> {
516 external T call(T a); 568 external T call(T a);
517 external T Y(); 569 external T Y();
518 }`); 570 }`);
519 }); 571 });
520 572
521 it('supports constructors', () => { 573 it('supports constructors', () => {
522 expectTranslate('class X { constructor() { } }').to.equal(`@JS() 574 expectTranslate('class X { constructor() { } }').to.equal(`@JS()
523 class X { 575 class X {
524 // @Ignore 576 // @Ignore
525 X.fakeConstructor$(); 577 X.fakeConstructor$();
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
828 // End module m2 880 // End module m2
829 @JS() 881 @JS()
830 external register(A x);`); 882 external register(A x);`);
831 }); 883 });
832 884
833 describe('type alias', () => { 885 describe('type alias', () => {
834 it('replace with simple type', () => { 886 it('replace with simple type', () => {
835 expectTranslate(` 887 expectTranslate(`
836 type MyNumber = number; 888 type MyNumber = number;
837 export function add(x: MyNumber, y: MyNumber): MyNumber; 889 export function add(x: MyNumber, y: MyNumber): MyNumber;
838 `).to.equal(`@JS() 890 `).to.equal(`/*type MyNumber = number;*/
891 @JS()
839 external num add(num x, num y);`); 892 external num add(num x, num y);`);
840 }); 893 });
841 }); 894 });
842 895
843 it('union types', () => { 896 it('union types', () => {
897 // TODO(jacobr): we should resolve that listener1 and listener2 are both fun ctions.
898
844 // TODO(jacobr): ideally the draw method should specify that arg el has type 899 // TODO(jacobr): ideally the draw method should specify that arg el has type
845 // HtmlElement instead of dynamic. 900 // HtmlElement instead of dynamic.
846 expectTranslate(` 901 expectTranslate(`
847 type listener1 = ()=>boolean; 902 type listener1 = ()=>boolean;
848 type listener2 = (e:string)=>boolean; 903 type listener2 = (e:string)=>boolean;
849 function addEventListener(listener: listener1|listener2);`) 904 function addEventListener(listener: listener1|listener2);`)
850 .to.equal(`import "package:func/func.dart"; 905 .to.equal(`typedef bool listener1();
851 906 typedef bool listener2(String e);
852 @JS() 907 @JS()
853 external addEventListener( 908 external addEventListener(dynamic /*listener1|listener2*/ listener);`);
854 Function /*Func0<bool>|Func1<String, bool>*/ listener);`);
855 909
856 expectTranslate('function draw(el: HTMLCanvasElement|HTMLImageElement):void; ') 910 expectTranslate('function draw(el: HTMLCanvasElement|HTMLImageElement):void; ')
857 .to.equal(`import "dart:html"; 911 .to.equal(`import "dart:html";
858 912
859 @JS() 913 @JS()
860 external void draw(dynamic /*CanvasElement|ImageElement*/ el);`); 914 external void draw(dynamic /*CanvasElement|ImageElement*/ el);`);
861 }); 915 });
916
917 it('callback this type', () => {
918 expectTranslate(`
919 function addEventListener(type: string, listener: (this: Element, event: Event) => void);`)
920 .to.equal(`import "dart:html";
921
922 @JS()
923 external addEventListener(
924 String type, void listener(/*Element this*/ Event event));`);
925 expectTranslate(`
926 function addEventListener(type: 'load', listener: (this: HTMLImageElement, event : Event) => void);
927 function addEventListener(type: string, listener: (this: Element, event: Event) => void);
928 `).to.equal(`import "dart:html";
929 import "package:func/func.dart";
930
931 /*external addEventListener('load' type, void listener(ImageElement JS$this, Eve nt event));*/
932 /*external addEventListener(
933 String type, void listener(Element JS$this, Event event));*/
934 @JS()
935 external addEventListener(
936 String /*'load'|String*/ type, void listener(/*Element this*/ Event event)); `);
937 });
862 }); 938 });
OLDNEW
« no previous file with comments | « test/declaration_test.ts ('k') | test/module_test.ts » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698