OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import "dart:async"; | 5 import "dart:async"; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import "package:async_helper/async_helper.dart"; | 7 import "package:async_helper/async_helper.dart"; |
8 import "package:compiler/src/diagnostics/messages.dart"; | 8 import "package:compiler/src/diagnostics/messages.dart"; |
9 import "package:compiler/src/elements/elements.dart"; | 9 import "package:compiler/src/elements/elements.dart"; |
10 import "package:compiler/src/resolution/members.dart"; | 10 import "package:compiler/src/resolution/members.dart"; |
11 import "package:compiler/src/diagnostics/spannable.dart"; | 11 import "package:compiler/src/diagnostics/diagnostic_listener.dart"; |
12 import "mock_compiler.dart"; | 12 import "mock_compiler.dart"; |
13 | 13 |
14 | 14 |
15 class CallbackMockCompiler extends MockCompiler { | 15 class CallbackMockCompiler extends MockCompiler { |
16 CallbackMockCompiler() : super.internal(); | 16 CallbackMockCompiler() : super.internal(); |
17 | 17 |
18 var onError; | 18 var onError; |
19 var onWarning; | 19 var onWarning; |
20 | 20 |
21 setOnError(var f) => onError = f; | 21 setOnError(var f) => onError = f; |
22 setOnWarning(var f) => onWarning = f; | 22 setOnWarning(var f) => onWarning = f; |
23 | 23 |
24 void reportWarning(Spannable node, | 24 void reportWarning( |
25 MessageKind messageKind, | 25 DiagnosticMessage message, |
26 [Map arguments = const {}]) { | 26 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { |
27 if (onWarning != null) { | 27 if (onWarning != null) { |
28 MessageTemplate template = MessageTemplate.TEMPLATES[messageKind]; | 28 onWarning(this, message.spannable, message.message); |
29 onWarning(this, node, template.message(arguments)); | |
30 } | 29 } |
31 super.reportWarning(node, messageKind, arguments); | 30 super.reportWarning(message, infos); |
32 } | 31 } |
33 | 32 |
34 void reportError(Spannable node, | 33 void reportError( |
35 MessageKind messageKind, | 34 DiagnosticMessage message, |
36 [Map arguments = const {}]) { | 35 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { |
37 if (onError != null) { | 36 if (onError != null) { |
38 MessageTemplate template = MessageTemplate.TEMPLATES[messageKind]; | 37 onError(this, message.spannable, message.message); |
39 onError(this, node, template.message(arguments)); | |
40 } | 38 } |
41 super.reportError(node, messageKind, arguments); | 39 super.reportError(message, infos); |
42 } | 40 } |
43 } | 41 } |
44 | 42 |
45 Future testErrorHandling() { | 43 Future testErrorHandling() { |
46 // Test that compiler.currentElement is set correctly when | 44 // Test that compiler.currentElement is set correctly when |
47 // reporting errors/warnings. | 45 // reporting errors/warnings. |
48 CallbackMockCompiler compiler = new CallbackMockCompiler(); | 46 CallbackMockCompiler compiler = new CallbackMockCompiler(); |
49 return compiler.init().then((_) { | 47 return compiler.init().then((_) { |
50 ResolverVisitor visitor = compiler.resolverVisitor(); | 48 ResolverVisitor visitor = compiler.resolverVisitor(); |
51 compiler.parseScript('NoSuchPrefix.NoSuchType foo() {}'); | 49 compiler.parseScript('NoSuchPrefix.NoSuchType foo() {}'); |
52 FunctionElement foo = compiler.mainApp.find('foo'); | 50 FunctionElement foo = compiler.mainApp.find('foo'); |
53 compiler.setOnWarning( | 51 compiler.setOnWarning( |
54 (c, n, m) => Expect.equals(foo, compiler.currentElement)); | 52 (c, n, m) => Expect.equals(foo, compiler.currentElement)); |
55 foo.computeType(compiler); | 53 foo.computeType(compiler); |
56 Expect.equals(1, compiler.warnings.length); | 54 Expect.equals(1, compiler.warnings.length); |
57 }); | 55 }); |
58 } | 56 } |
59 | 57 |
60 main() { | 58 main() { |
61 asyncTest(() => testErrorHandling()); | 59 asyncTest(() => testErrorHandling()); |
62 } | 60 } |
OLD | NEW |