| 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 |
| 7 import "package:async_helper/async_helper.dart"; |
| 8 import 'package:compiler/compiler.dart'; |
| 9 import "package:compiler/src/elements/elements.dart"; |
| 6 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| 7 import "package:async_helper/async_helper.dart"; | 11 |
| 8 import "package:compiler/src/elements/elements.dart"; | |
| 9 import "package:compiler/src/resolution/members.dart"; | |
| 10 import "package:compiler/src/diagnostics/diagnostic_listener.dart"; | |
| 11 import "mock_compiler.dart"; | 12 import "mock_compiler.dart"; |
| 12 import "diagnostic_reporter_helper.dart"; | |
| 13 | |
| 14 | |
| 15 class CallbackMockCompiler extends MockCompiler { | |
| 16 CallbackReporter reporter; | |
| 17 | |
| 18 CallbackMockCompiler() : super.internal() { | |
| 19 reporter = new CallbackReporter(super.reporter); | |
| 20 } | |
| 21 | |
| 22 } | |
| 23 | |
| 24 class CallbackReporter extends DiagnosticReporterWrapper { | |
| 25 final DiagnosticReporter reporter; | |
| 26 | |
| 27 CallbackReporter(this.reporter); | |
| 28 | |
| 29 var onError; | |
| 30 var onWarning; | |
| 31 | |
| 32 setOnError(var f) => onError = f; | |
| 33 setOnWarning(var f) => onWarning = f; | |
| 34 | |
| 35 void reportWarning( | |
| 36 DiagnosticMessage message, | |
| 37 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { | |
| 38 if (onWarning != null) { | |
| 39 onWarning(this, message.spannable, message.message); | |
| 40 } | |
| 41 super.reportWarning(message, infos); | |
| 42 } | |
| 43 | |
| 44 void reportError( | |
| 45 DiagnosticMessage message, | |
| 46 [List<DiagnosticMessage> infos = const <DiagnosticMessage>[]]) { | |
| 47 if (onError != null) { | |
| 48 onError(this, message.spannable, message.message); | |
| 49 } | |
| 50 super.reportError(message, infos); | |
| 51 } | |
| 52 } | |
| 53 | 13 |
| 54 Future testErrorHandling() { | 14 Future testErrorHandling() { |
| 55 // Test that compiler.currentElement is set correctly when | 15 // Test that compiler.currentElement is set correctly when |
| 56 // reporting errors/warnings. | 16 // reporting errors/warnings. |
| 57 CallbackMockCompiler compiler = new CallbackMockCompiler(); | 17 MockCompiler compiler = new MockCompiler.internal(); |
| 58 return compiler.init().then((_) { | 18 return compiler.init().then((_) { |
| 59 ResolverVisitor visitor = compiler.resolverVisitor(); | |
| 60 compiler.parseScript('NoSuchPrefix.NoSuchType foo() {}'); | 19 compiler.parseScript('NoSuchPrefix.NoSuchType foo() {}'); |
| 61 FunctionElement foo = compiler.mainApp.find('foo'); | 20 FunctionElement foo = compiler.mainApp.find('foo'); |
| 62 compiler.reporter.setOnWarning( | 21 compiler.diagnosticHandler = |
| 63 (c, n, m) => Expect.equals(foo, compiler.currentElement)); | 22 (Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 23 if (kind == Diagnostic.WARNING) { |
| 24 Expect.equals(foo, compiler.currentElement); |
| 25 } |
| 26 }; |
| 64 foo.computeType(compiler.resolution); | 27 foo.computeType(compiler.resolution); |
| 65 Expect.equals(1, compiler.diagnosticCollector.warnings.length); | 28 Expect.equals(1, compiler.diagnosticCollector.warnings.length); |
| 66 }); | 29 }); |
| 67 } | 30 } |
| 68 | 31 |
| 69 main() { | 32 main() { |
| 70 asyncTest(() => testErrorHandling()); | 33 asyncTest(() => testErrorHandling()); |
| 71 } | 34 } |
| OLD | NEW |