| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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.serialization_analysis_test; | 5 library dart2js.serialization_analysis_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
| 10 import 'package:compiler/src/commandline_options.dart'; | 10 import 'package:compiler/src/commandline_options.dart'; |
| 11 import 'package:compiler/src/common/backend_api.dart'; | 11 import 'package:compiler/src/common/backend_api.dart'; |
| 12 import 'package:compiler/src/common/names.dart'; | 12 import 'package:compiler/src/common/names.dart'; |
| 13 import 'package:compiler/src/compiler.dart'; | 13 import 'package:compiler/src/compiler.dart'; |
| 14 import 'package:compiler/src/filenames.dart'; | 14 import 'package:compiler/src/filenames.dart'; |
| 15 import 'memory_compiler.dart'; | 15 import 'memory_compiler.dart'; |
| 16 import 'serialization_helper.dart'; | 16 import 'serialization_helper.dart'; |
| 17 | 17 import 'serialization_test_data.dart'; |
| 18 const List<Test> TESTS = const <Test>[ | |
| 19 const Test(const { | |
| 20 'main.dart': 'main() {}' | |
| 21 }), | |
| 22 | |
| 23 const Test(const { | |
| 24 'main.dart': 'main() => print("Hello World");' | |
| 25 }), | |
| 26 | |
| 27 const Test(const { | |
| 28 'main.dart': 'main() => print("Hello World", 0);' | |
| 29 }, | |
| 30 expectedWarningCount: 1, | |
| 31 expectedInfoCount: 1), | |
| 32 | |
| 33 const Test(const { | |
| 34 'main.dart': r''' | |
| 35 main() { | |
| 36 String text = "Hello World"; | |
| 37 print('$text'); | |
| 38 }''' | |
| 39 }), | |
| 40 | |
| 41 const Test(const { | |
| 42 'main.dart': r''' | |
| 43 main() { | |
| 44 String text = "Hello World"; | |
| 45 print('$text', text); | |
| 46 }''' | |
| 47 }, | |
| 48 expectedWarningCount: 1, | |
| 49 expectedInfoCount: 1), | |
| 50 | |
| 51 const Test(const { | |
| 52 'main.dart': r''' | |
| 53 main(List<String> arguments) { | |
| 54 print(arguments); | |
| 55 }''' | |
| 56 }), | |
| 57 | |
| 58 const Test(const { | |
| 59 'main.dart': r''' | |
| 60 main(List<String> arguments) { | |
| 61 for (int i = 0; i < arguments.length; i++) { | |
| 62 print(arguments[i]); | |
| 63 } | |
| 64 }''' | |
| 65 }), | |
| 66 | |
| 67 const Test(const { | |
| 68 'main.dart': r''' | |
| 69 main(List<String> arguments) { | |
| 70 for (String argument in arguments) { | |
| 71 print(argument); | |
| 72 } | |
| 73 }''' | |
| 74 }), | |
| 75 | |
| 76 const Test(const { | |
| 77 'main.dart': r''' | |
| 78 class Class {} | |
| 79 main() { | |
| 80 print(new Class()); | |
| 81 }''' | |
| 82 }), | |
| 83 | |
| 84 const Test(const { | |
| 85 'main.dart': r''' | |
| 86 class Class implements Function {} | |
| 87 main() { | |
| 88 print(new Class()); | |
| 89 }''' | |
| 90 }, | |
| 91 expectedWarningCount: 1), | |
| 92 | |
| 93 const Test(const { | |
| 94 'main.dart': r''' | |
| 95 class Class implements Function { | |
| 96 call() {} | |
| 97 } | |
| 98 main() { | |
| 99 print(new Class()()); | |
| 100 }''' | |
| 101 }), | |
| 102 | |
| 103 const Test(const { | |
| 104 'main.dart': r''' | |
| 105 class Class implements Comparable<Class> { | |
| 106 int compareTo(Class other) => 0; | |
| 107 } | |
| 108 main() { | |
| 109 print(new Class()); | |
| 110 }''' | |
| 111 }), | |
| 112 | |
| 113 const Test(const { | |
| 114 'main.dart': r''' | |
| 115 class Class implements Comparable<Class, Class> { | |
| 116 int compareTo(other) => 0; | |
| 117 } | |
| 118 main() { | |
| 119 print(new Class()); | |
| 120 }''' | |
| 121 }, | |
| 122 expectedWarningCount: 1), | |
| 123 | |
| 124 const Test(const { | |
| 125 'main.dart': r''' | |
| 126 class Class implements Comparable<Class> { | |
| 127 int compareTo(String other) => 0; | |
| 128 } | |
| 129 main() { | |
| 130 print(new Class().compareTo(null)); | |
| 131 }''' | |
| 132 }, | |
| 133 expectedWarningCount: 1, | |
| 134 expectedInfoCount: 1), | |
| 135 | |
| 136 const Test(const { | |
| 137 'main.dart': r''' | |
| 138 class Class implements Comparable { | |
| 139 bool compareTo(a, b) => true; | |
| 140 } | |
| 141 main() { | |
| 142 print(new Class().compareTo(null, null)); | |
| 143 }''' | |
| 144 }, | |
| 145 expectedWarningCount: 1, | |
| 146 expectedInfoCount: 1), | |
| 147 | |
| 148 const Test(const { | |
| 149 'main.dart': r''' | |
| 150 import 'dart:math'; | |
| 151 | |
| 152 class MyRandom implements Random { | |
| 153 int nextInt(int max) { | |
| 154 return max.length; | |
| 155 } | |
| 156 bool nextBool() => true; | |
| 157 double nextDouble() => 0.0; | |
| 158 } | |
| 159 main() { | |
| 160 new MyRandom().nextInt(0); | |
| 161 }''' | |
| 162 }, | |
| 163 expectedWarningCount: 1, | |
| 164 expectedInfoCount: 0), | |
| 165 | |
| 166 const Test(const { | |
| 167 'main.dart': r''' | |
| 168 import 'dart:math'; | |
| 169 | |
| 170 class MyRandom implements Random { | |
| 171 int nextInt(int max) { | |
| 172 return max.length; | |
| 173 } | |
| 174 bool nextBool() => true; | |
| 175 double nextDouble() => 0.0; | |
| 176 } | |
| 177 main() { | |
| 178 new MyRandom(); | |
| 179 }''' | |
| 180 }), | |
| 181 | |
| 182 const Test(const { | |
| 183 'main.dart': r''' | |
| 184 import 'dart:math'; | |
| 185 | |
| 186 class MyRandom implements Random { | |
| 187 int nextInt(int max) { | |
| 188 return max.length; | |
| 189 } | |
| 190 bool nextBool() => true; | |
| 191 double nextDouble() => 0.0; | |
| 192 } | |
| 193 main() { | |
| 194 // Invocation of `MyRandom.nextInt` is only detected knowing the actual | |
| 195 // implementation class for `List` and the world impact of its `shuffle` | |
| 196 // method. | |
| 197 [].shuffle(new MyRandom()); | |
| 198 }''' | |
| 199 }, | |
| 200 expectedWarningCount: 1, | |
| 201 expectedInfoCount: 0), | |
| 202 ]; | |
| 203 | 18 |
| 204 main(List<String> arguments) { | 19 main(List<String> arguments) { |
| 205 asyncTest(() async { | 20 asyncTest(() async { |
| 206 String serializedData = await serializeDartCore(); | 21 String serializedData = await serializeDartCore(); |
| 207 | 22 |
| 208 if (arguments.isNotEmpty) { | 23 if (arguments.isNotEmpty) { |
| 209 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last)); | 24 Uri entryPoint = Uri.base.resolve(nativeToUriPath(arguments.last)); |
| 210 await analyze(serializedData, entryPoint, null); | 25 await analyze(serializedData, entryPoint, null); |
| 211 } else { | 26 } else { |
| 212 Uri entryPoint = Uri.parse('memory:main.dart'); | 27 Uri entryPoint = Uri.parse('memory:main.dart'); |
| 213 for (Test test in TESTS) { | 28 for (Test test in TESTS) { |
| 214 await analyze(serializedData, entryPoint, test); | 29 await analyze(serializedData, entryPoint, test); |
| 215 } | 30 } |
| 216 } | 31 } |
| 217 }); | 32 }); |
| 218 } | 33 } |
| 219 | 34 |
| 220 class Test { | |
| 221 final Map sourceFiles; | |
| 222 final int expectedErrorCount; | |
| 223 final int expectedWarningCount; | |
| 224 final int expectedHintCount; | |
| 225 final int expectedInfoCount; | |
| 226 | |
| 227 const Test(this.sourceFiles, { | |
| 228 this.expectedErrorCount: 0, | |
| 229 this.expectedWarningCount: 0, | |
| 230 this.expectedHintCount: 0, | |
| 231 this.expectedInfoCount: 0}); | |
| 232 } | |
| 233 | |
| 234 Future analyze(String serializedData, Uri entryPoint, Test test) async { | 35 Future analyze(String serializedData, Uri entryPoint, Test test) async { |
| 235 DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); | 36 DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); |
| 236 await runCompiler( | 37 await runCompiler( |
| 237 entryPoint: entryPoint, | 38 entryPoint: entryPoint, |
| 238 memorySourceFiles: test != null ? test.sourceFiles : const {}, | 39 memorySourceFiles: test != null ? test.sourceFiles : const {}, |
| 239 options: [Flags.analyzeOnly], | 40 options: [Flags.analyzeOnly], |
| 240 diagnosticHandler: diagnosticCollector, | 41 diagnosticHandler: diagnosticCollector, |
| 241 beforeRun: (Compiler compiler) { | 42 beforeRun: (Compiler compiler) { |
| 242 deserialize(compiler, serializedData); | 43 deserialize(compiler, serializedData); |
| 243 }); | 44 }); |
| 244 if (test != null) { | 45 if (test != null) { |
| 245 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length, | 46 Expect.equals(test.expectedErrorCount, diagnosticCollector.errors.length, |
| 246 "Unexpected error count."); | 47 "Unexpected error count."); |
| 247 Expect.equals( | 48 Expect.equals( |
| 248 test.expectedWarningCount, | 49 test.expectedWarningCount, |
| 249 diagnosticCollector.warnings.length, | 50 diagnosticCollector.warnings.length, |
| 250 "Unexpected warning count."); | 51 "Unexpected warning count."); |
| 251 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length, | 52 Expect.equals(test.expectedHintCount, diagnosticCollector.hints.length, |
| 252 "Unexpected hint count."); | 53 "Unexpected hint count."); |
| 253 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length, | 54 Expect.equals(test.expectedInfoCount, diagnosticCollector.infos.length, |
| 254 "Unexpected info count."); | 55 "Unexpected info count."); |
| 255 } | 56 } |
| 256 } | 57 } |
| 257 | 58 |
| OLD | NEW |