| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library dart2js.serialization_analysis_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:expect/expect.dart'; |
| 10 import 'package:compiler/src/elements/elements.dart'; |
| 11 import 'package:compiler/src/serialization/serialization.dart'; |
| 12 import 'package:compiler/src/serialization/json_serializer.dart'; |
| 13 import 'package:compiler/src/serialization/task.dart'; |
| 14 import 'package:compiler/src/dart2jslib.dart'; |
| 15 import 'package:compiler/src/filenames.dart'; |
| 16 import 'memory_compiler.dart'; |
| 17 |
| 18 const List<Test> TESTS = const <Test>[ |
| 19 const Test(const { |
| 20 'main.dart': 'main() => print("Hello World");' |
| 21 }), |
| 22 |
| 23 const Test(const { |
| 24 'main.dart': 'main() => print("Hello World", 0);' |
| 25 }, |
| 26 expectedWarningCount: 1, |
| 27 expectedInfoCount: 1), |
| 28 |
| 29 const Test(const { |
| 30 'main.dart': r''' |
| 31 main() { |
| 32 String text = "Hello World"; |
| 33 print('$text'); |
| 34 }''' |
| 35 }), |
| 36 |
| 37 const Test(const { |
| 38 'main.dart': r''' |
| 39 main() { |
| 40 String text = "Hello World"; |
| 41 print('$text', text); |
| 42 }''' |
| 43 }, |
| 44 expectedWarningCount: 1, |
| 45 expectedInfoCount: 1), |
| 46 |
| 47 const Test(const { |
| 48 'main.dart': r''' |
| 49 main(List<String> arguments) { |
| 50 print(arguments); |
| 51 }''' |
| 52 }), |
| 53 |
| 54 const Test(const { |
| 55 'main.dart': r''' |
| 56 main(List<String> arguments) { |
| 57 for (int i = 0; i < arguments.length; i++) { |
| 58 print(arguments[i]); |
| 59 } |
| 60 }''' |
| 61 }), |
| 62 |
| 63 const Test(const { |
| 64 'main.dart': r''' |
| 65 main(List<String> arguments) { |
| 66 for (String argument in arguments) { |
| 67 print(argument); |
| 68 } |
| 69 }''' |
| 70 }), |
| 71 |
| 72 const Test(const { |
| 73 'main.dart': r''' |
| 74 class Class {} |
| 75 main() { |
| 76 print(new Class()); |
| 77 }''' |
| 78 }), |
| 79 |
| 80 const Test(const { |
| 81 'main.dart': r''' |
| 82 class Class implements Function {} |
| 83 main() { |
| 84 print(new Class()); |
| 85 }''' |
| 86 }, |
| 87 expectedWarningCount: 1), |
| 88 |
| 89 const Test(const { |
| 90 'main.dart': r''' |
| 91 class Class implements Function { |
| 92 call() {} |
| 93 } |
| 94 main() { |
| 95 print(new Class()()); |
| 96 }''' |
| 97 }), |
| 98 |
| 99 const Test(const { |
| 100 'main.dart': r''' |
| 101 class Class implements Comparable<Class> { |
| 102 int compareTo(Class other) => 0; |
| 103 } |
| 104 main() { |
| 105 print(new Class()); |
| 106 }''' |
| 107 }), |
| 108 |
| 109 const Test(const { |
| 110 'main.dart': r''' |
| 111 class Class implements Comparable<Class, Class> { |
| 112 int compareTo(other) => 0; |
| 113 } |
| 114 main() { |
| 115 print(new Class()); |
| 116 }''' |
| 117 }, |
| 118 expectedWarningCount: 1), |
| 119 |
| 120 const Test(const { |
| 121 'main.dart': r''' |
| 122 class Class implements Comparable<Class> { |
| 123 int compareTo(String other) => 0; |
| 124 } |
| 125 main() { |
| 126 print(new Class().compareTo(null)); |
| 127 }''' |
| 128 }, |
| 129 expectedWarningCount: 1, |
| 130 expectedInfoCount: 1), |
| 131 |
| 132 const Test(const { |
| 133 'main.dart': r''' |
| 134 class Class implements Comparable { |
| 135 bool compareTo(a, b) => true; |
| 136 } |
| 137 main() { |
| 138 print(new Class().compareTo(null, null)); |
| 139 }''' |
| 140 }, |
| 141 expectedWarningCount: 1, |
| 142 expectedInfoCount: 1), |
| 143 ]; |
| 144 |
| 145 main(List<String> arguments) { |
| 146 asyncTest(() async { |
| 147 String serializedData = await serializeDartCore(); |
| 148 |
| 149 if (arguments.isNotEmpty) { |
| 150 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last)); |
| 151 await analyze(serializedData, entryPoint, null); |
| 152 } else { |
| 153 Uri entryPoint = Uri.parse('memory:main.dart'); |
| 154 for (Test test in TESTS) { |
| 155 await analyze(serializedData, entryPoint, test); |
| 156 } |
| 157 } |
| 158 }); |
| 159 } |
| 160 |
| 161 class Test { |
| 162 final Map sourceFiles; |
| 163 final int expectedErrorCount; |
| 164 final int expectedWarningCount; |
| 165 final int expectedHintCount; |
| 166 final int expectedInfoCount; |
| 167 |
| 168 const Test(this.sourceFiles, { |
| 169 this.expectedErrorCount: 0, |
| 170 this.expectedWarningCount: 0, |
| 171 this.expectedHintCount: 0, |
| 172 this.expectedInfoCount: 0}); |
| 173 } |
| 174 |
| 175 Future analyze(String serializedData, Uri entryPoint, Test test) async { |
| 176 Deserializer deserializer = new Deserializer.fromText( |
| 177 serializedData, const JsonSerializationDecoder()); |
| 178 DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); |
| 179 Compiler compiler = compilerFor( |
| 180 test != null ? test.sourceFiles : const {}, |
| 181 options: ['--analyze-only', '--output-type=dart'], |
| 182 diagnosticHandler: diagnosticCollector); |
| 183 compiler.serialization.deserializer = new _DeserializerSystem(deserializer); |
| 184 await compiler.runCompiler(entryPoint); |
| 185 if (test != null) { |
| 186 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length, |
| 187 "Unexpected error count."); |
| 188 Expect.equals( |
| 189 test.expectedWarningCount, |
| 190 diagnosticCollector.warnings.length, |
| 191 "Unexpected warning count."); |
| 192 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length, |
| 193 "Unexpected hint count."); |
| 194 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length, |
| 195 "Unexpected info count."); |
| 196 } |
| 197 } |
| 198 |
| 199 Future<String> serializeDartCore() async { |
| 200 Compiler compiler = compilerFor({}, |
| 201 options: ['--analyze-all', '--output-type=dart']); |
| 202 await compiler.runCompiler(Uri.parse('dart:core')); |
| 203 return serialize(compiler.libraryLoader.libraries); |
| 204 } |
| 205 |
| 206 String serialize(Iterable<LibraryElement> libraries) { |
| 207 Serializer serializer = new Serializer(const JsonSerializationEncoder()); |
| 208 for (LibraryElement library in libraries) { |
| 209 serializer.serialize(library); |
| 210 } |
| 211 return serializer.toText(); |
| 212 } |
| 213 |
| 214 class _DeserializerSystem extends DeserializerSystem { |
| 215 final Deserializer _deserializer; |
| 216 final List<LibraryElement> deserializedLibraries = <LibraryElement>[]; |
| 217 |
| 218 _DeserializerSystem(this._deserializer); |
| 219 |
| 220 LibraryElement readLibrary(Uri resolvedUri) { |
| 221 LibraryElement library = _deserializer.lookupLibrary(resolvedUri); |
| 222 if (library != null) { |
| 223 deserializedLibraries.add(library); |
| 224 } |
| 225 return library; |
| 226 } |
| 227 |
| 228 @override |
| 229 WorldImpact computeWorldImpact(Element element) { |
| 230 return const WorldImpact(); |
| 231 } |
| 232 |
| 233 @override |
| 234 bool isDeserialized(Element element) { |
| 235 return deserializedLibraries.contains(element.library); |
| 236 } |
| 237 } |
| OLD | NEW |