| OLD | NEW |
| 1 /// <reference path="../typings/mocha/mocha.d.ts"/> | 1 /// <reference path="../typings/mocha/mocha.d.ts"/> |
| 2 import chai = require('chai'); | 2 import chai = require('chai'); |
| 3 import main = require('../lib/main'); | 3 import main = require('../lib/main'); |
| 4 import ModuleTranspiler from '../lib/module'; | 4 import ModuleTranspiler from '../lib/module'; |
| 5 import {FacadeConverter, NameRewriter} from '../lib/facade_converter'; | 5 import {FacadeConverter, NameRewriter} from '../lib/facade_converter'; |
| 6 | 6 |
| 7 import {expectTranslate, expectErroneousCode, translateSources} from './test_sup
port'; | 7 import {expectTranslate, expectErroneousCode, translateSources} from './test_sup
port'; |
| 8 | 8 |
| 9 describe('imports', () => { | 9 describe('imports', () => { |
| 10 it('translates import equals statements', () => { | 10 it('translates import equals statements', () => { |
| 11 expectTranslate('import x = require("y");').to.equal('import "package:y.dart
" as x;'); | 11 expectTranslate('import x = require("y");').to.equal('import "package:y.dart
" as x;'); |
| 12 }); | 12 }); |
| 13 it('translates import from statements', () => { | 13 it('translates import from statements', () => { |
| 14 expectTranslate('import {x,y} from "z";').to.equal('import "package:z.dart"
show x, y;'); | 14 expectTranslate('import {x,y} from "z";').to.equal('import "package:z.dart"
show x, y;'); |
| 15 }); | 15 }); |
| 16 it('translates import star', () => { | 16 it('translates import star', () => { |
| 17 expectTranslate('import * as foo from "z";').to.equal('import "package:z.dar
t" as foo;'); | 17 expectTranslate('import * as foo from "z";').to.equal('import "package:z.dar
t" as foo;'); |
| 18 }); | 18 }); |
| 19 it('allows import dart file from relative path', () => { | 19 it('simplify import dart file imports with relative paths', () => { |
| 20 expectTranslate('import x = require("./y")').to.equal('import "y.dart" as x;
'); | 20 expectTranslate('import x = require("./y")').to.equal('import "y.dart" as x;
'); |
| 21 expectTranslate('import {x} from "./y"').to.equal('import "y.dart" show x;')
; | 21 expectTranslate('import {x} from "./y"').to.equal('import "y.dart" show x;')
; |
| 22 expectTranslate('import {x} from "../y"').to.equal('import "../y.dart" show
x;'); | 22 expectTranslate('import {x} from "../y"').to.equal('import "y.dart" show x;'
); |
| 23 }); | 23 }); |
| 24 it('fails for renamed imports', () => { | 24 it('fails for renamed imports', () => { |
| 25 expectErroneousCode('import {Foo as Bar} from "baz";') | 25 expectErroneousCode('import {Foo as Bar} from "baz";') |
| 26 .to.throw(/import\/export renames are unsupported in Dart/); | 26 .to.throw(/import\/export renames are unsupported in Dart/); |
| 27 }); | 27 }); |
| 28 it('empty import spec generates safe Dart code', | 28 it('empty import spec generates safe Dart code', |
| 29 () => { expectTranslate('import {} from "baz";').to.equal(''); }); | 29 () => { expectTranslate('import {} from "baz";').to.equal(''); }); |
| 30 }); | 30 }); |
| 31 | 31 |
| 32 describe('exports', () => { | 32 describe('exports', () => { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 44 X.fakeConstructor$(); | 44 X.fakeConstructor$(); |
| 45 }`); | 45 }`); |
| 46 }); | 46 }); |
| 47 it('allows export declarations', | 47 it('allows export declarations', |
| 48 () => { expectTranslate('export * from "X";').to.equal('export "package:X.d
art";'); }); | 48 () => { expectTranslate('export * from "X";').to.equal('export "package:X.d
art";'); }); |
| 49 it('allows export declarations', | 49 it('allows export declarations', |
| 50 () => { expectTranslate('export * from "./X";').to.equal('export "X.dart";'
); }); | 50 () => { expectTranslate('export * from "./X";').to.equal('export "X.dart";'
); }); |
| 51 it('allows named export declarations', () => { | 51 it('allows named export declarations', () => { |
| 52 expectTranslate('export {a, b} from "X";').to.equal('export "package:X.dart"
show a, b;'); | 52 expectTranslate('export {a, b} from "X";').to.equal('export "package:X.dart"
show a, b;'); |
| 53 }); | 53 }); |
| 54 it('ignores named export declarations', () => { |
| 55 expectTranslate(`declare module '../some_other_module' { |
| 56 interface Foo { } |
| 57 }`) |
| 58 .to.equal( |
| 59 '// Library augmentation not allowed by Dart. Ignoring augmentation
of ../some_other_module'); |
| 60 }); |
| 61 |
| 54 it('fails for renamed exports', () => { | 62 it('fails for renamed exports', () => { |
| 55 expectErroneousCode('export {Foo as Bar} from "baz";') | 63 expectErroneousCode('export {Foo as Bar} from "baz";') |
| 56 .to.throw(/import\/export renames are unsupported in Dart/); | 64 .to.throw(/import\/export renames are unsupported in Dart/); |
| 57 }); | 65 }); |
| 58 it('fails for exports without URLs', () => { | 66 it('fails for exports without URLs', () => { |
| 59 expectErroneousCode('export {a as b};').to.throw('re-exports must have a mod
ule URL'); | 67 expectErroneousCode('export {a as b};').to.throw('re-exports must have a mod
ule URL'); |
| 60 }); | 68 }); |
| 61 it('fails for empty export specs', | 69 it('fails for empty export specs', |
| 62 () => { expectErroneousCode('export {} from "baz";').to.throw(/empty export
list/); }); | 70 () => { expectErroneousCode('export {} from "baz";').to.throw(/empty export
list/); }); |
| 63 }); | 71 }); |
| 64 | 72 |
| 65 describe('library name', () => { | 73 describe('module name', () => { |
| 66 let transpiler: main.Transpiler; | 74 let transpiler: main.Transpiler; |
| 67 let modTranspiler: ModuleTranspiler; | 75 let modTranspiler: ModuleTranspiler; |
| 68 beforeEach(() => { | 76 beforeEach(() => { |
| 69 transpiler = new main.Transpiler({failFast: true, generateLibraryName: true,
basePath: '/a'}); | 77 transpiler = new main.Transpiler({failFast: true, moduleName: 'sample_module
', basePath: '/a'}); |
| 70 modTranspiler = new ModuleTranspiler( | 78 modTranspiler = new ModuleTranspiler( |
| 71 transpiler, new FacadeConverter(transpiler, '', new NameRewriter(), fals
e), true); | 79 transpiler, new FacadeConverter(transpiler, '', new NameRewriter()), 'sa
mple_module'); |
| 72 }); | 80 }); |
| 73 it('adds a library name', () => { | 81 it('adds module name', () => { |
| 74 let results = translateSources( | 82 let results = translateSources( |
| 75 {'/a/b/c.ts': 'var x;'}, {failFast: true, generateLibraryName: true, bas
ePath: '/a'}); | 83 {'/a/b/c.ts': 'var x;'}, {failFast: true, moduleName: 'sample_module', b
asePath: '/a'}); |
| 76 chai.expect(results['/a/b/c.ts']).to.equal(`@JS() | 84 chai.expect(results['/a/b/c.ts']).to.equal(`@JS("sample_module") |
| 77 library b.c; | 85 library b.c; |
| 78 | 86 |
| 79 import "package:js/js.dart"; | 87 import "package:js/js.dart"; |
| 80 | 88 |
| 81 @JS() | 89 @JS() |
| 82 external get x; | 90 external get x; |
| 83 @JS() | 91 @JS() |
| 84 external set x(v); | 92 external set x(v); |
| 85 `); | 93 `); |
| 86 }); | 94 }); |
| 87 it('leaves relative paths alone', | 95 it('leaves relative paths alone', |
| 88 () => { chai.expect(modTranspiler.getLibraryName('a/b')).to.equal('a.b'); }
); | 96 () => { chai.expect(modTranspiler.getLibraryName('a/b')).to.equal('a.b'); }
); |
| 89 it('handles reserved words', () => { | 97 it('handles reserved words', () => { |
| 90 chai.expect(modTranspiler.getLibraryName('/a/for/in/do/x')).to.equal('_for._
in._do.x'); | 98 chai.expect(modTranspiler.getLibraryName('/a/for/in/do/x')).to.equal('_for._
in._do.x'); |
| 91 }); | 99 }); |
| 92 it('handles built-in and limited keywords', () => { | 100 it('handles built-in and limited keywords', () => { |
| 93 chai.expect(modTranspiler.getLibraryName('/a/as/if/sync/x')).to.equal('as._i
f.sync.x'); | 101 chai.expect(modTranspiler.getLibraryName('/a/as/if/sync/x')).to.equal('as._i
f.sync.x'); |
| 94 }); | 102 }); |
| 95 it('handles file extensions', () => { | 103 it('handles file extensions', () => { |
| 96 chai.expect(modTranspiler.getLibraryName('a/x.ts')).to.equal('a.x'); | 104 chai.expect(modTranspiler.getLibraryName('a/x.ts')).to.equal('a.x'); |
| 97 chai.expect(modTranspiler.getLibraryName('a/x.js')).to.equal('a.x'); | 105 chai.expect(modTranspiler.getLibraryName('a/x.js')).to.equal('a.x'); |
| 98 }); | 106 }); |
| 99 it('handles non word characters', | 107 it('handles non word characters', |
| 100 () => { chai.expect(modTranspiler.getLibraryName('a/%x.ts')).to.equal('a._x
'); }); | 108 () => { chai.expect(modTranspiler.getLibraryName('a/%x.ts')).to.equal('a._x
'); }); |
| 101 }); | 109 }); |
| OLD | NEW |