| 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.message_kind_helper; | 5 library dart2js.test.message_kind_helper; |
| 6 | 6 |
| 7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 import 'package:compiler/src/commandline_options.dart'; | 10 import 'package:compiler/src/commandline_options.dart'; |
| 11 import 'package:compiler/src/compiler.dart' show | 11 import 'package:compiler/src/compiler.dart' show |
| 12 Compiler; | 12 Compiler; |
| 13 import 'package:compiler/src/dart_backend/dart_backend.dart' show | 13 import 'package:compiler/src/dart_backend/dart_backend.dart' show |
| 14 DartBackend; | 14 DartBackend; |
| 15 import 'package:compiler/src/diagnostics/messages.dart' show | 15 import 'package:compiler/src/diagnostics/messages.dart' show |
| 16 MessageKind, | 16 MessageKind, |
| 17 MessageTemplate; | 17 MessageTemplate; |
| 18 import 'package:compiler/src/old_to_new_api.dart' show | 18 import 'package:compiler/compiler_new.dart' show |
| 19 LegacyCompilerDiagnostics; | 19 Diagnostic; |
| 20 | 20 |
| 21 import 'memory_compiler.dart'; | 21 import 'memory_compiler.dart'; |
| 22 | 22 |
| 23 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]'; | 23 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]'; |
| 24 | 24 |
| 25 /// Most examples generate a single diagnostic. | 25 /// Most examples generate a single diagnostic. |
| 26 /// Add an exception here if a single diagnostic cannot be produced. | 26 /// Add an exception here if a single diagnostic cannot be produced. |
| 27 /// However, consider that a single concise diagnostic is easier to understand, | 27 /// However, consider that a single concise diagnostic is easier to understand, |
| 28 /// so try to change error reporting logic before adding an exception. | 28 /// so try to change error reporting logic before adding an exception. |
| 29 final Set<MessageKind> kindsWithExtraMessages = new Set<MessageKind>.from([ | 29 final Set<MessageKind> kindsWithExtraMessages = new Set<MessageKind>.from([ |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 return Future.forEach(template.examples, (example) { | 62 return Future.forEach(template.examples, (example) { |
| 63 if (example is String) { | 63 if (example is String) { |
| 64 example = {'main.dart': example}; | 64 example = {'main.dart': example}; |
| 65 } else { | 65 } else { |
| 66 Expect.isTrue(example is Map, | 66 Expect.isTrue(example is Map, |
| 67 "Example must be either a String or a Map."); | 67 "Example must be either a String or a Map."); |
| 68 Expect.isTrue(example.containsKey('main.dart'), | 68 Expect.isTrue(example.containsKey('main.dart'), |
| 69 "Example map must contain a 'main.dart' entry."); | 69 "Example map must contain a 'main.dart' entry."); |
| 70 } | 70 } |
| 71 List<String> messages = <String>[]; | 71 DiagnosticCollector collector = new DiagnosticCollector(); |
| 72 void collect(Uri uri, int begin, int end, String message, kind) { | |
| 73 if (kind.name == 'verbose info' || kind.name == 'info') { | |
| 74 return; | |
| 75 } | |
| 76 messages.add(message); | |
| 77 } | |
| 78 | 72 |
| 79 bool oldBackendIsDart; | 73 bool oldBackendIsDart; |
| 80 if (cachedCompiler != null) { | 74 if (cachedCompiler != null) { |
| 81 oldBackendIsDart = cachedCompiler.backend is DartBackend; | 75 oldBackendIsDart = cachedCompiler.backend is DartBackend; |
| 82 } | 76 } |
| 83 bool newBackendIsDart = template.options.contains('--output-type=dart'); | 77 bool newBackendIsDart = template.options.contains('--output-type=dart'); |
| 84 | 78 |
| 85 Compiler compiler = compilerFor( | 79 Compiler compiler = compilerFor( |
| 86 memorySourceFiles: example, | 80 memorySourceFiles: example, |
| 87 diagnosticHandler: new LegacyCompilerDiagnostics(collect), | 81 diagnosticHandler: collector, |
| 88 options: [Flags.analyzeOnly, | 82 options: [Flags.analyzeOnly, |
| 89 Flags.enableExperimentalMirrors]..addAll(template.options), | 83 Flags.enableExperimentalMirrors]..addAll(template.options), |
| 90 cachedCompiler: | 84 cachedCompiler: |
| 91 // TODO(johnniwinther): Remove this restriction when constant | 85 // TODO(johnniwinther): Remove this restriction when constant |
| 92 // values can be computed directly from the expressions. | 86 // values can be computed directly from the expressions. |
| 93 oldBackendIsDart == newBackendIsDart ? cachedCompiler : null); | 87 oldBackendIsDart == newBackendIsDart ? cachedCompiler : null); |
| 94 | 88 |
| 95 return compiler.run(Uri.parse('memory:main.dart')).then((_) { | 89 return compiler.run(Uri.parse('memory:main.dart')).then((_) { |
| 90 Iterable<DiagnosticMessage> messages = collector.filterMessagesByKinds( |
| 91 [Diagnostic.ERROR, |
| 92 Diagnostic.WARNING, |
| 93 Diagnostic.HINT, |
| 94 Diagnostic.CRASH]); |
| 96 | 95 |
| 97 Expect.isFalse(messages.isEmpty, 'No messages in """$example"""'); | 96 Expect.isFalse(messages.isEmpty, 'No messages in """$example"""'); |
| 98 | 97 |
| 99 String expectedText = !template.hasHowToFix | 98 String expectedText = !template.hasHowToFix |
| 100 ? template.template : '${template.template}\n${template.howToFix}'; | 99 ? template.template : '${template.template}\n${template.howToFix}'; |
| 101 String pattern = expectedText.replaceAllMapped( | 100 String pattern = expectedText.replaceAllMapped( |
| 102 new RegExp(ESCAPE_REGEXP), (m) => '\\${m[0]}'); | 101 new RegExp(ESCAPE_REGEXP), (m) => '\\${m[0]}'); |
| 103 pattern = pattern.replaceAll(new RegExp(r'#\\\{[^}]*\\\}'), '.*'); | 102 pattern = pattern.replaceAll(new RegExp(r'#\\\{[^}]*\\\}'), '.*'); |
| 104 | 103 |
| 104 bool checkMessage(DiagnosticMessage message) { |
| 105 if (message.message.kind != MessageKind.GENERIC) { |
| 106 return message.message.kind == template.kind; |
| 107 } else { |
| 108 return new RegExp('^$pattern\$').hasMatch(message.text); |
| 109 } |
| 110 } |
| 111 |
| 105 // TODO(johnniwinther): Extend MessageKind to contain information on | 112 // TODO(johnniwinther): Extend MessageKind to contain information on |
| 106 // where info messages are expected. | 113 // where info messages are expected. |
| 107 bool messageFound = false; | 114 bool messageFound = false; |
| 108 List unexpectedMessages = []; | 115 List unexpectedMessages = []; |
| 109 for (String message in messages) { | 116 for (DiagnosticMessage message in messages) { |
| 110 if (!messageFound && new RegExp('^$pattern\$').hasMatch(message)) { | 117 if (!messageFound && checkMessage(message)) { |
| 111 messageFound = true; | 118 messageFound = true; |
| 112 } else { | 119 } else { |
| 113 unexpectedMessages.add(message); | 120 unexpectedMessages.add(message); |
| 114 } | 121 } |
| 115 } | 122 } |
| 116 Expect.isTrue(messageFound, '"$pattern" does not match any in $messages'); | 123 Expect.isTrue(messageFound, |
| 124 '${template.kind}} does not match any in\n ' |
| 125 '${messages.join('\n ')}'); |
| 117 Expect.isFalse(compiler.reporter.hasCrashed); | 126 Expect.isFalse(compiler.reporter.hasCrashed); |
| 118 if (!unexpectedMessages.isEmpty) { | 127 if (!unexpectedMessages.isEmpty) { |
| 119 for (String message in unexpectedMessages) { | 128 for (DiagnosticMessage message in unexpectedMessages) { |
| 120 print("Unexpected message: $message"); | 129 print("Unexpected message: $message"); |
| 121 } | 130 } |
| 122 if (!kindsWithExtraMessages.contains(template.kind)) { | 131 if (!kindsWithExtraMessages.contains(template.kind)) { |
| 123 // Try changing the error reporting logic before adding an exception | 132 // Try changing the error reporting logic before adding an exception |
| 124 // to [kindsWithExtraMessages]. | 133 // to [kindsWithExtraMessages]. |
| 125 throw 'Unexpected messages found.'; | 134 throw 'Unexpected messages found.'; |
| 126 } | 135 } |
| 127 } | 136 } |
| 128 | 137 |
| 129 bool pendingStuff = false; | 138 bool pendingStuff = false; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 143 kindsWithPendingClasses.contains(template)); | 152 kindsWithPendingClasses.contains(template)); |
| 144 | 153 |
| 145 if (!pendingStuff) { | 154 if (!pendingStuff) { |
| 146 // If there is pending stuff, or the compiler was cancelled, we | 155 // If there is pending stuff, or the compiler was cancelled, we |
| 147 // shouldn't reuse the compiler. | 156 // shouldn't reuse the compiler. |
| 148 cachedCompiler = compiler; | 157 cachedCompiler = compiler; |
| 149 } | 158 } |
| 150 }); | 159 }); |
| 151 }).then((_) => cachedCompiler); | 160 }).then((_) => cachedCompiler); |
| 152 } | 161 } |
| OLD | NEW |