| 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.test.diagnostic_helper; | 5 library dart2js.test.diagnostic_helper; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:compiler/compiler_new.dart' show | 9 import 'package:compiler/compiler_new.dart' show |
| 10 CompilerDiagnostics, | 10 CompilerDiagnostics, |
| 11 Diagnostic; | 11 Diagnostic; |
| 12 import 'package:compiler/src/diagnostics/messages.dart' show | 12 import 'package:compiler/src/diagnostics/messages.dart' show |
| 13 Message, | 13 Message, |
| 14 MessageKind; | 14 MessageKind; |
| 15 import 'package:expect/expect.dart'; | 15 import 'package:expect/expect.dart'; |
| 16 | 16 |
| 17 class CollectedMessage { | 17 class CollectedMessage { |
| 18 final Message message; | 18 final Message message; |
| 19 final Uri uri; | 19 final Uri uri; |
| 20 final int begin; | 20 final int begin; |
| 21 final int end; | 21 final int end; |
| 22 final String text; | 22 final String text; |
| 23 final Diagnostic kind; | 23 final Diagnostic kind; |
| 24 | 24 |
| 25 CollectedMessage( | 25 CollectedMessage( |
| 26 this.message, this.uri, this.begin, this.end, this.text, this.kind); | 26 this.message, this.uri, this.begin, this.end, this.text, this.kind); |
| 27 | 27 |
| 28 MessageKind get messageKind => message.kind; | 28 MessageKind get messageKind => message.kind; |
| 29 | 29 |
| 30 String toString() => '${message.kind}:$uri:$begin:$end:$text:$kind'; | 30 String toString() { |
| 31 return '${message != null ? message.kind : ''}' |
| 32 ':$uri:$begin:$end:$text:$kind'; |
| 33 } |
| 31 } | 34 } |
| 32 | 35 |
| 33 class DiagnosticCollector implements CompilerDiagnostics { | 36 class DiagnosticCollector implements CompilerDiagnostics { |
| 34 List<CollectedMessage> messages = <CollectedMessage>[]; | 37 List<CollectedMessage> messages = <CollectedMessage>[]; |
| 35 | 38 |
| 36 void call(Uri uri, int begin, int end, String message, Diagnostic kind) { | 39 void call(Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 37 throw ''; | 40 throw ''; |
| 38 report(null, uri, begin, end, message, kind); | 41 report(null, uri, begin, end, message, kind); |
| 39 } | 42 } |
| 40 | 43 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 70 } | 73 } |
| 71 | 74 |
| 72 /// `true` if non-verbose messages has been collected. | 75 /// `true` if non-verbose messages has been collected. |
| 73 bool get hasRegularMessages { | 76 bool get hasRegularMessages { |
| 74 return messages.any((m) => m.kind != Diagnostic.VERBOSE_INFO); | 77 return messages.any((m) => m.kind != Diagnostic.VERBOSE_INFO); |
| 75 } | 78 } |
| 76 | 79 |
| 77 void clear() { | 80 void clear() { |
| 78 messages.clear(); | 81 messages.clear(); |
| 79 } | 82 } |
| 83 |
| 84 void checkMessages(List<Expected> expectedMessages) { |
| 85 int index = 0; |
| 86 Iterable<CollectedMessage> messages = |
| 87 filterMessagesByKinds( |
| 88 [Diagnostic.ERROR, |
| 89 Diagnostic.WARNING, |
| 90 Diagnostic.HINT, |
| 91 Diagnostic.INFO]); |
| 92 for (CollectedMessage message in messages) { |
| 93 if (index >= expectedMessages.length) { |
| 94 Expect.fail("Unexpected messages:\n " |
| 95 "${messages.skip(index).join('\n ')}"); |
| 96 } else { |
| 97 Expected expected = expectedMessages[index]; |
| 98 Expect.equals(expected.messageKind, message.messageKind, |
| 99 "Unexpected message kind in:\n ${messages.join('\n ')}"); |
| 100 Expect.equals(expected.diagnosticKind, message.kind, |
| 101 "Unexpected diagnostic kind in\n ${messages.join('\n ')}"); |
| 102 index++; |
| 103 } |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 class Expected { |
| 109 final MessageKind messageKind; |
| 110 final Diagnostic diagnosticKind; |
| 111 |
| 112 const Expected(this.messageKind, this.diagnosticKind); |
| 113 |
| 114 const Expected.error(MessageKind messageKind) |
| 115 : this(messageKind, Diagnostic.ERROR); |
| 116 |
| 117 const Expected.warning(MessageKind messageKind) |
| 118 : this(messageKind, Diagnostic.WARNING); |
| 119 |
| 120 const Expected.hint(MessageKind messageKind) |
| 121 : this(messageKind, Diagnostic.HINT); |
| 122 |
| 123 const Expected.info(MessageKind messageKind) |
| 124 : this(messageKind, Diagnostic.INFO); |
| 80 } | 125 } |
| 81 | 126 |
| 82 | 127 |
| 83 void compareWarningKinds(String text, | 128 void compareWarningKinds(String text, |
| 84 List expectedWarnings, | 129 List expectedWarnings, |
| 85 Iterable<CollectedMessage> foundWarnings) { | 130 Iterable<CollectedMessage> foundWarnings) { |
| 86 compareMessageKinds(text, expectedWarnings, foundWarnings, 'warning'); | 131 compareMessageKinds(text, expectedWarnings, foundWarnings, 'warning'); |
| 87 } | 132 } |
| 88 | 133 |
| 89 /// [expectedMessages] must be a list of either [MessageKind] or [CheckMessage]. | 134 /// [expectedMessages] must be a list of either [MessageKind] or [CheckMessage]. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 String expectedValue = '${arguments[key]}'; | 185 String expectedValue = '${arguments[key]}'; |
| 141 String foundValue = '${message.arguments[key]}'; | 186 String foundValue = '${message.arguments[key]}'; |
| 142 if (expectedValue != foundValue) { | 187 if (expectedValue != foundValue) { |
| 143 return 'Expected argument $key with value $expectedValue, ' | 188 return 'Expected argument $key with value $expectedValue, ' |
| 144 'found $foundValue.'; | 189 'found $foundValue.'; |
| 145 } | 190 } |
| 146 } | 191 } |
| 147 return null; | 192 return null; |
| 148 }; | 193 }; |
| 149 } | 194 } |
| OLD | NEW |