OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.test.uri_retention_test; | 5 library dart2js.test.uri_retention_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
11 | 11 |
12 import 'memory_compiler.dart' show | 12 import 'memory_compiler.dart' show runCompiler, OutputCollector; |
13 runCompiler, OutputCollector; | |
14 | 13 |
15 Future<String> compileSources(sources, {bool minify, bool preserveUri}) async { | 14 Future<String> compileSources(sources, {bool minify, bool preserveUri}) async { |
16 var options = []; | 15 var options = []; |
17 if (minify) options.add("--minify"); | 16 if (minify) options.add("--minify"); |
18 if (preserveUri) options.add("--preserve-uris"); | 17 if (preserveUri) options.add("--preserve-uris"); |
19 OutputCollector outputCollector = new OutputCollector(); | 18 OutputCollector outputCollector = new OutputCollector(); |
20 await runCompiler( | 19 await runCompiler( |
21 memorySourceFiles: sources, | 20 memorySourceFiles: sources, |
22 options: options, | 21 options: options, |
23 outputProvider: outputCollector); | 22 outputProvider: outputCollector); |
24 return outputCollector.getOutput('', 'js'); | 23 return outputCollector.getOutput('', 'js'); |
25 } | 24 } |
26 | 25 |
27 Future test(sources, { bool libName, bool fileName }) { | 26 Future test(sources, {bool libName, bool fileName}) { |
28 return | 27 return compileSources(sources, minify: false, preserveUri: false) |
29 compileSources(sources, minify: false, preserveUri: false).then((output) { | 28 .then((output) { |
30 // Unminified the sources should always contain the library name and the | 29 // Unminified the sources should always contain the library name and the |
31 // file name. | 30 // file name. |
32 Expect.isTrue(output.contains("main_lib")); | 31 Expect.isTrue(output.contains("main_lib")); |
33 Expect.isTrue(output.contains("main.dart")); | 32 Expect.isTrue(output.contains("main.dart")); |
34 }).then((_) { | 33 }).then((_) { |
35 compileSources(sources, minify: true, preserveUri: false).then((output) { | 34 compileSources(sources, minify: true, preserveUri: false).then((output) { |
36 Expect.equals(libName, output.contains("main_lib")); | 35 Expect.equals(libName, output.contains("main_lib")); |
37 Expect.isFalse(output.contains("main.dart")); | 36 Expect.isFalse(output.contains("main.dart")); |
38 }); | 37 }); |
39 }).then((_) { | 38 }).then((_) { |
40 compileSources(sources, minify: true, preserveUri: true).then((output) { | 39 compileSources(sources, minify: true, preserveUri: true).then((output) { |
41 Expect.equals(libName, output.contains("main_lib")); | 40 Expect.equals(libName, output.contains("main_lib")); |
42 Expect.equals(fileName, output.contains("main.dart")); | 41 Expect.equals(fileName, output.contains("main.dart")); |
43 }); | 42 }); |
44 }); | 43 }); |
45 } | 44 } |
46 | 45 |
47 void main() { | 46 void main() { |
48 asyncTest(() { | 47 asyncTest(() { |
49 return new Future.value() | 48 return new Future.value() |
50 .then((_) => test(MEMORY_SOURCE_FILES1, libName: false, fileName: false)) | 49 .then( |
51 .then((_) => test(MEMORY_SOURCE_FILES2, libName: true, fileName: false)) | 50 (_) => test(MEMORY_SOURCE_FILES1, libName: false, fileName: false)) |
52 .then((_) => test(MEMORY_SOURCE_FILES3, libName: true, fileName: true)); | 51 .then((_) => test(MEMORY_SOURCE_FILES2, libName: true, fileName: false)) |
| 52 .then((_) => test(MEMORY_SOURCE_FILES3, libName: true, fileName: true)); |
53 }); | 53 }); |
54 } | 54 } |
55 | 55 |
56 const MEMORY_SOURCE_FILES1 = const <String, String> { | 56 const MEMORY_SOURCE_FILES1 = const <String, String>{ |
57 'main.dart': """ | 57 'main.dart': """ |
58 library main_lib; | 58 library main_lib; |
59 | 59 |
60 class A { | 60 class A { |
61 final uri = "foo"; | 61 final uri = "foo"; |
62 } | 62 } |
63 | 63 |
64 main() { | 64 main() { |
65 print(Uri.base); | 65 print(Uri.base); |
66 print(new A().uri); | 66 print(new A().uri); |
67 } | 67 } |
68 """, | 68 """, |
69 }; | 69 }; |
70 | 70 |
71 | |
72 // Requires the library name, but not the URIs. | 71 // Requires the library name, but not the URIs. |
73 const MEMORY_SOURCE_FILES2 = const <String, String> { | 72 const MEMORY_SOURCE_FILES2 = const <String, String>{ |
74 'main.dart': """ | 73 'main.dart': """ |
75 library main_lib; | 74 library main_lib; |
76 | 75 |
77 @MirrorsUsed(targets: 'main_lib') | 76 @MirrorsUsed(targets: 'main_lib') |
78 import 'dart:mirrors'; | 77 import 'dart:mirrors'; |
79 import 'file2.dart'; | 78 import 'file2.dart'; |
80 | 79 |
81 class A { | 80 class A { |
82 } | 81 } |
83 | 82 |
84 main() { | 83 main() { |
85 print(Uri.base); | 84 print(Uri.base); |
86 // Unfortunately we can't use new B().uri yet, because that would require | 85 // Unfortunately we can't use new B().uri yet, because that would require |
87 // some type-feedback to know that the '.uri' is not the one from the library. | 86 // some type-feedback to know that the '.uri' is not the one from the library. |
88 print(new B()); | 87 print(new B()); |
89 print(reflectClass(A).declarations.length); | 88 print(reflectClass(A).declarations.length); |
90 } | 89 } |
91 """, | 90 """, |
92 'file2.dart': """ | 91 'file2.dart': """ |
93 library other_lib; | 92 library other_lib; |
94 | 93 |
95 class B { | 94 class B { |
96 final uri = "xyz"; | 95 final uri = "xyz"; |
97 } | 96 } |
98 """, | 97 """, |
99 }; | 98 }; |
100 | 99 |
101 // Requires the uri (and will contain the library-name, too). | 100 // Requires the uri (and will contain the library-name, too). |
102 const MEMORY_SOURCE_FILES3 = const <String, String> { | 101 const MEMORY_SOURCE_FILES3 = const <String, String>{ |
103 'main.dart': """ | 102 'main.dart': """ |
104 library main_lib; | 103 library main_lib; |
105 | 104 |
106 @MirrorsUsed(targets: 'main_lib') | 105 @MirrorsUsed(targets: 'main_lib') |
107 import 'dart:mirrors'; | 106 import 'dart:mirrors'; |
108 | 107 |
109 main() { | 108 main() { |
110 print(currentMirrorSystem().findLibrary(#main_lib).uri); | 109 print(currentMirrorSystem().findLibrary(#main_lib).uri); |
111 } | 110 } |
112 """, | 111 """, |
113 }; | 112 }; |
OLD | NEW |