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 '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' show | 10 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' show |
11 Compiler, | 11 Compiler, |
12 MessageKind; | 12 MessageKind; |
13 | 13 |
14 import 'memory_compiler.dart'; | 14 import 'memory_compiler.dart'; |
15 | 15 |
16 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]'; | 16 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]'; |
17 | 17 |
| 18 /// Most examples generate a single diagnostic. |
| 19 /// Add an exception here if a single diagnostic cannot be produced. |
| 20 /// However, consider that a single concise diagnostic is easier to understand, |
| 21 /// so try to change error reporting logic before adding an exception. |
| 22 final Set<MessageKind> kindsWithExtraMessages = new Set<MessageKind>.from([ |
| 23 // See http://dartbug.com/18361. |
| 24 MessageKind.CANNOT_EXTEND_MALFORMED, |
| 25 MessageKind.CANNOT_IMPLEMENT_MALFORMED, |
| 26 MessageKind.CANNOT_MIXIN, |
| 27 MessageKind.CANNOT_MIXIN_MALFORMED, |
| 28 MessageKind.CYCLIC_TYPEDEF_ONE, |
| 29 MessageKind.EQUAL_MAP_ENTRY_KEY, |
| 30 MessageKind.FINAL_FUNCTION_TYPE_PARAMETER, |
| 31 MessageKind.FORMAL_DECLARED_CONST, |
| 32 MessageKind.FORMAL_DECLARED_STATIC, |
| 33 MessageKind.HEX_DIGIT_EXPECTED, |
| 34 MessageKind.HIDDEN_IMPLICIT_IMPORT, |
| 35 MessageKind.HIDDEN_IMPORT, |
| 36 MessageKind.INHERIT_GETTER_AND_METHOD, |
| 37 MessageKind.UNIMPLEMENTED_METHOD, |
| 38 MessageKind.UNIMPLEMENTED_METHOD_ONE, |
| 39 MessageKind.UNTERMINATED_STRING, |
| 40 MessageKind.VAR_FUNCTION_TYPE_PARAMETER, |
| 41 MessageKind.VOID_NOT_ALLOWED, |
| 42 MessageKind.UNMATCHED_TOKEN, |
| 43 ]); |
| 44 |
18 Future<Compiler> check(MessageKind kind, Compiler cachedCompiler) { | 45 Future<Compiler> check(MessageKind kind, Compiler cachedCompiler) { |
19 Expect.isNotNull(kind.howToFix); | 46 Expect.isNotNull(kind.howToFix); |
20 Expect.isFalse(kind.examples.isEmpty); | 47 Expect.isFalse(kind.examples.isEmpty); |
21 | 48 |
22 return Future.forEach(kind.examples, (example) { | 49 return Future.forEach(kind.examples, (example) { |
23 if (example is String) { | 50 if (example is String) { |
24 example = {'main.dart': example}; | 51 example = {'main.dart': example}; |
25 } else { | 52 } else { |
26 Expect.isTrue(example is Map, | 53 Expect.isTrue(example is Map, |
27 "Example must be either a String or a Map."); | 54 "Example must be either a String or a Map."); |
28 Expect.isTrue(example.containsKey('main.dart'), | 55 Expect.isTrue(example.containsKey('main.dart'), |
29 "Example map must contain a 'main.dart' entry."); | 56 "Example map must contain a 'main.dart' entry."); |
30 } | 57 } |
31 List<String> messages = <String>[]; | 58 List<String> messages = <String>[]; |
32 void collect(Uri uri, int begin, int end, String message, kind) { | 59 void collect(Uri uri, int begin, int end, String message, kind) { |
33 if (kind.name == 'verbose info') { | 60 if (kind.name == 'verbose info' || kind.name == 'info') { |
34 return; | 61 return; |
35 } | 62 } |
36 messages.add(message); | 63 messages.add(message); |
37 } | 64 } |
38 | 65 |
39 Compiler compiler = compilerFor( | 66 Compiler compiler = compilerFor( |
40 example, | 67 example, |
41 diagnosticHandler: collect, | 68 diagnosticHandler: collect, |
42 options: ['--analyze-only'], | 69 options: ['--analyze-only'], |
43 cachedCompiler: cachedCompiler); | 70 cachedCompiler: cachedCompiler); |
44 | 71 |
45 return compiler.run(Uri.parse('memory:main.dart')).then((_) { | 72 return compiler.run(Uri.parse('memory:main.dart')).then((_) { |
46 | 73 |
47 Expect.isFalse(messages.isEmpty, 'No messages in """$example"""'); | 74 Expect.isFalse(messages.isEmpty, 'No messages in """$example"""'); |
48 | 75 |
49 String expectedText = !kind.hasHowToFix | 76 String expectedText = !kind.hasHowToFix |
50 ? kind.template : '${kind.template}\n${kind.howToFix}'; | 77 ? kind.template : '${kind.template}\n${kind.howToFix}'; |
51 String pattern = expectedText.replaceAllMapped( | 78 String pattern = expectedText.replaceAllMapped( |
52 new RegExp(ESCAPE_REGEXP), (m) => '\\${m[0]}'); | 79 new RegExp(ESCAPE_REGEXP), (m) => '\\${m[0]}'); |
53 pattern = pattern.replaceAll(new RegExp(r'#\\\{[^}]*\\\}'), '.*'); | 80 pattern = pattern.replaceAll(new RegExp(r'#\\\{[^}]*\\\}'), '.*'); |
54 | 81 |
55 // TODO(johnniwinther): Extend MessageKind to contain information on | 82 // TODO(johnniwinther): Extend MessageKind to contain information on |
56 // where info messages are expected. | 83 // where info messages are expected. |
57 bool messageFound = false; | 84 bool messageFound = false; |
| 85 List unexpectedMessages = []; |
58 for (String message in messages) { | 86 for (String message in messages) { |
59 if (new RegExp('^$pattern\$').hasMatch(message)) { | 87 if (!messageFound && new RegExp('^$pattern\$').hasMatch(message)) { |
60 messageFound = true; | 88 messageFound = true; |
| 89 } else { |
| 90 unexpectedMessages.add(message); |
61 } | 91 } |
62 } | 92 } |
63 Expect.isTrue(messageFound, '"$pattern" does not match any in $messages'); | 93 Expect.isTrue(messageFound, '"$pattern" does not match any in $messages'); |
64 Expect.isFalse(compiler.hasCrashed); | 94 Expect.isFalse(compiler.hasCrashed); |
| 95 if (!unexpectedMessages.isEmpty) { |
| 96 for (String message in unexpectedMessages) { |
| 97 print("Unexpected message: $message"); |
| 98 } |
| 99 if (!kindsWithExtraMessages.contains(kind)) { |
| 100 // Try changing the error reporting logic before adding an exception |
| 101 // to [kindsWithExtraMessages]. |
| 102 throw 'Unexpected messages found.'; |
| 103 } |
| 104 } |
65 cachedCompiler = compiler; | 105 cachedCompiler = compiler; |
66 }); | 106 }); |
67 }).then((_) => cachedCompiler); | 107 }).then((_) => cachedCompiler); |
68 } | 108 } |
OLD | NEW |